-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorporation-manager.go
144 lines (125 loc) · 4.21 KB
/
corporation-manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package controllers
import (
"fmt"
"net/http"
"github.com/opensourceways/app-cla-server/models"
)
type CorporationManagerController struct {
baseController
}
func (this *CorporationManagerController) Prepare() {
switch this.apiRequestMethod() {
case http.MethodPut:
// add administrator
this.apiPrepare(PermissionOwnerOfOrg)
case http.MethodPatch:
// reset password of manager
this.apiPrepareWithAC(
&accessController{Payload: &acForCorpManagerPayload{}},
[]string{PermissionCorpAdmin, PermissionEmployeeManager},
)
}
}
// @Title Put
// @Description add corporation administrator
// @Param :org_cla_id path string true "org cla id"
// @Param :email path string true "email of corp"
// @Success 202 {int} map
// @Failure util.ErrPDFHasNotUploaded
// @Failure util.ErrNumOfCorpManagersExceeded
// @router /:link_id/:email [put]
func (this *CorporationManagerController) Put() {
action := "add corp administrator"
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
}
orgInfo := pl.orgInfo(linkID)
// 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
corpSigning, merr := models.GetCorpSigningBasicInfo(linkID, corpEmail)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
uploaded, err := models.IsCorpSigningPDFUploaded(linkID, corpEmail)
if err != nil {
this.sendModelErrorAsResp(err, action)
return
}
if !uploaded {
this.sendFailedResponse(
400, errUnuploaded,
fmt.Errorf("pdf corporation signed has not been uploaded"), action)
return
}
added, merr := models.CreateCorporationAdministrator(linkID, corpSigning.AdminName, corpEmail)
if merr != nil {
if merr.IsErrorOf(models.ErrNoLinkOrManagerExists) {
this.sendFailedResponse(400, errCorpManagerExists, merr, action)
} else {
this.sendModelErrorAsResp(merr, action)
}
return
}
this.sendSuccessResp(action + " successfully")
notifyCorpAdmin(linkID, orgInfo, added)
}
// @Title Patch
// @Description reset password of corporation manager
// @Param body body dbmodels.CorporationManagerResetPassword true "body for resetting password"
// @Success 204 {string} "reset password successfully"
// @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 error_parsing_api_body: parse payload of request failed
// @Failure 406 same_password: the old and new passwords are same
// @Failure 407 too_short_or_long_password: the length of new password is too short or long
// @Failure 408 invalid_password: the format of new password is invalid
// @Failure 409 corp_manager_does_not_exist: manager may be removed
// @Failure 410 wrong_old_password: the old password is not correct
// @Failure 411 frequent_operation: don't operate frequently
// @Failure 500 system_error: system error
// @router / [patch]
func (this *CorporationManagerController) Patch() {
action := "reset password of corp's manager"
sendResp := this.newFuncForSendingFailedResp(action)
pl, fr := this.tokenPayloadBasedOnCorpManager()
if fr != nil {
sendResp(fr)
return
}
var info models.CorporationManagerResetPassword
if fr := this.fetchInputPayload(&info); fr != nil {
sendResp(fr)
return
}
if err := info.Validate(); err != nil {
sendResp(parseModelError(err))
return
}
if err := (&info).Reset(pl.LinkID, pl.Email); err != nil {
if err.IsErrorOf(models.ErrNoLinkOrNoManagerOrFO) {
this.sendFailedResponse(400, errFrequentOperation, err, action)
} else {
this.sendModelErrorAsResp(err, action)
}
return
}
this.sendSuccessResp("reset password successfully")
}