Skip to content

Commit

Permalink
Delete corp signing (#83)
Browse files Browse the repository at this point in the history
* delete corp signing

* add lock before deleting signing

* add lock
  • Loading branch information
zengchen1024 authored Mar 15, 2021
1 parent 8eb41ed commit 46adcd0
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 16 deletions.
16 changes: 6 additions & 10 deletions controllers/cla.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ func (this *CLAController) Add() {
return
}

orgInfo := pl.orgInfo(linkID)
filePath := genOrgFileLockPath(orgInfo.Platform, orgInfo.OrgID, orgInfo.RepoID)
unlock, err := util.Lock(filePath)
if err != nil {
this.sendFailedResponse(500, errSystemError, err, action)
unlock, fr := lockOnRepo(pl.orgInfo(linkID))
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
defer unlock()
Expand Down Expand Up @@ -101,11 +99,9 @@ func (this *CLAController) Delete() {
return
}

orgInfo := pl.orgInfo(linkID)
filePath := genOrgFileLockPath(orgInfo.Platform, orgInfo.OrgID, orgInfo.RepoID)
unlock, err := util.Lock(filePath)
if err != nil {
this.sendFailedResponse(500, errSystemError, err, action)
unlock, fr := lockOnRepo(pl.orgInfo(linkID))
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
defer unlock()
Expand Down
9 changes: 6 additions & 3 deletions controllers/corporation-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ func (this *CorporationManagerController) Put() {
this.sendFailedResultAsResp(fr, action)
return
}
orgInfo := pl.orgInfo(linkID)

orgInfo, merr := models.GetOrgOfLink(linkID)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
// lock to avoid the conflict with the deleting corp signing
unlock, fr := lockOnRepo(orgInfo)
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
defer unlock()

// call models.GetCorpSigningBasicInfo before models.IsCorpSigningPDFUploaded
// to check wheather corp has signed
Expand Down
8 changes: 8 additions & 0 deletions controllers/corporation-pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func (this *CorporationPDFController) Upload() {
return
}

// lock to avoid conflict with deleting corp signing
unlock, fr := lockOnRepo(pl.orgInfo(linkID))
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
defer unlock()

b, merr := models.IsCorpSigned(linkID, corpEmail)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
Expand Down
57 changes: 57 additions & 0 deletions controllers/corporation-signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,63 @@ func (this *CorporationSigningController) checkCLAForSigning(claFile, orgSignatu
return nil
}

// @Title Delete
// @Description delete corp signing
// @Param :link_id path string true "link id"
// @Param :email path string true "corp email"
// @Success 204 {string} delete success!
// @Failure 400 missing_url_path_parameter: missing url path parameter
// @Failure 401 missing_token: token is missing
// @Failure 402 unknown_token: token is unknown
// @Failure 403 expired_token: token is expired
// @Failure 404 unauthorized_token: the permission of token is unmatched
// @Failure 405 not_yours_org: the link doesn't belong to your community
// @Failure 406 unknown_link: unkown link id
// @Failure 407 no_link: the link id is not exists
// @Failure 500 system_error: system error
// @router /:link_id/:email [delete]
func (this *CorporationSigningController) Delete() {
action := "delete corp signing"
linkID := this.GetString(":link_id")
corpEmail := this.GetString(":email")

pl, fr := this.tokenPayloadBasedOnCodePlatform()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if fr := pl.isOwnerOfLink(linkID); fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}

unlock, fr := lockOnRepo(pl.orgInfo(linkID))
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
defer unlock()

managers, merr := models.ListCorporationManagers(linkID, corpEmail, dbmodels.RoleAdmin)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
if len(managers) > 0 {
this.sendFailedResponse(
400, errCorpManagerExists,
fmt.Errorf("can't delete corp signing info, because admin manager exists"), action)
return
}

if err := models.DeleteCorpSigning(linkID, corpEmail); err != nil {
this.sendModelErrorAsResp(err, action)
return
}

this.sendSuccessResp("delete corp signing successfully")
}

// @Title ResendCorpSigningEmail
// @Description resend corp signing email
// @Param :org_id path string true "org cla id"
Expand Down
14 changes: 11 additions & 3 deletions controllers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func signHelper(linkID, claLang, applyTo string, doSign func(*models.CLAInfo) *f

// no contributor signed for this language. lock to avoid the cla to be changed
// before writing to the db.
unlock, err := util.Lock(genOrgFileLockPath(orgInfo.Platform, orgInfo.OrgID, orgInfo.RepoID))
if err != nil {
return newFailedApiResult(500, errSystemError, err)
unlock, fr := lockOnRepo(orgInfo)
if fr != nil {
return fr
}
defer unlock()

Expand Down Expand Up @@ -193,3 +193,11 @@ func saveCorpCLAAtLocal(cla *models.CLACreateOpt, linkID string) *failedApiResul

return nil
}

func lockOnRepo(orgInfo *dbmodels.OrgInfo) (func(), *failedApiResult) {
unlock, err := util.Lock(genOrgFileLockPath(orgInfo.Platform, orgInfo.OrgID, orgInfo.RepoID))
if err != nil {
return nil, newFailedApiResult(500, errSystemError, err)
}
return unlock, nil
}
1 change: 1 addition & 0 deletions dbmodels/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type IDB interface {
type ICorporationSigning interface {
InitializeCorpSigning(linkID string, info *OrgInfo, cla *CLAInfo) IDBError
SignCorpCLA(orgCLAID string, info *CorpSigningCreateOpt) IDBError
DeleteCorpSigning(linkID, email string) IDBError
IsCorpSigned(linkID, email string) (bool, IDBError)
ListCorpSignings(linkID, language string) ([]CorporationSigningSummary, IDBError)
GetCorpSigningDetail(linkID, email string) ([]Field, *CorpSigningCreateOpt, IDBError)
Expand Down
12 changes: 12 additions & 0 deletions models/corporation-signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ func GetCorpSigningDetail(linkID, email string) ([]dbmodels.Field, *dbmodels.Cor

return f, s, parseDBError(err)
}

func DeleteCorpSigning(linkID, email string) IModelError {
err := dbmodels.GetDB().DeleteCorpSigning(linkID, email)
if err == nil {
return nil
}

if err.IsErrorOf(dbmodels.ErrNoDBRecord) {
return newModelError(ErrNoLink, err)
}
return parseDBError(err)
}
58 changes: 58 additions & 0 deletions mongodb/corporation-signing-deleted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package mongodb

import (
"context"

"github.com/opensourceways/app-cla-server/dbmodels"
)

func (this *client) getCorpSigning(linkID, email string) (*dCorpSigning, dbmodels.IDBError) {
var v []cCorpSigning

f := func(ctx context.Context) error {
return this.getArrayElem(
ctx, this.corpSigningCollection, fieldSignings,
docFilterOfSigning(linkID), elemFilterOfCorpSigning(email),
nil, &v,
)
}

if err := withContext(f); err != nil {
return nil, newSystemError(err)
}

if len(v) == 0 {
return nil, errNoDBRecord
}

signings := v[0].Signings
if len(signings) == 0 {
return nil, nil
}

return &signings[0], nil
}

func (this *client) DeleteCorpSigning(linkID, email string) dbmodels.IDBError {
data, err := this.getCorpSigning(linkID, email)
if err != nil {
return err
}
if data == nil {
return nil
}

doc, err := structToMap(data)
if err != nil {
return err
}

f := func(ctx context.Context) dbmodels.IDBError {
return this.moveArrayElem(
ctx, this.corpSigningCollection, fieldSignings, fieldDeleted,
docFilterOfSigning(linkID), elemFilterOfCorpSigning(email), doc,
)
}

return withContext1(f)
}
20 changes: 20 additions & 0 deletions mongodb/db-ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ func (this *client) pullAndReturnArrayElem(ctx context.Context, collection, arra
return nil
}

func (this *client) moveArrayElem(ctx context.Context, collection, from, to string, filterOfDoc, filterOfArray, value bson.M) dbmodels.IDBError {
col := this.collection(collection)

r, err := col.UpdateOne(
ctx, filterOfDoc,
bson.M{
"$pull": bson.M{from: filterOfArray},
"$push": bson.M{to: value},
},
)
if err != nil {
return newSystemError(err)
}

if r.MatchedCount == 0 {
return errNoDBRecord
}
return nil
}

func (this *client) getDoc(ctx context.Context, collection string, filterOfDoc, project bson.M, result interface{}) dbmodels.IDBError {
col := this.collection(collection)

Expand Down
1 change: 1 addition & 0 deletions mongodb/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
fieldRepo = "repo"
fieldCorpID = "corp_id"
fieldSignings = "signings"
fieldDeleted = "deleted"
fieldLang = "lang"
fieldOrgEmail = "org_email"
fieldOrgAlias = "org_alias"
Expand Down
9 changes: 9 additions & 0 deletions routers/commentsRouter_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ func init() {
Filters: nil,
Params: nil})

beego.GlobalControllerRouter["github.com/opensourceways/app-cla-server/controllers:CorporationSigningController"] = append(beego.GlobalControllerRouter["github.com/opensourceways/app-cla-server/controllers:CorporationSigningController"],
beego.ControllerComments{
Method: "Delete",
Router: "/:link_id/:email",
AllowHTTPMethods: []string{"delete"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})

beego.GlobalControllerRouter["github.com/opensourceways/app-cla-server/controllers:CorporationSigningController"] = append(beego.GlobalControllerRouter["github.com/opensourceways/app-cla-server/controllers:CorporationSigningController"],
beego.ControllerComments{
Method: "ResendCorpSigningEmail",
Expand Down

0 comments on commit 46adcd0

Please sign in to comment.