-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee-signing.go
300 lines (262 loc) · 9.35 KB
/
employee-signing.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package controllers
import (
"fmt"
"strings"
"github.com/opensourceways/app-cla-server/config"
"github.com/opensourceways/app-cla-server/dbmodels"
"github.com/opensourceways/app-cla-server/email"
"github.com/opensourceways/app-cla-server/models"
)
type EmployeeSigningController struct {
baseController
}
func (this *EmployeeSigningController) Prepare() {
if this.isPostRequest() {
// sign as employee
this.apiPrepare(PermissionIndividualSigner)
} else {
if strings.HasSuffix(this.routerPattern(), "/:link_id/:email") {
this.apiPrepare(PermissionOwnerOfOrg)
} else {
// get, update and delete employee
this.apiPrepare(PermissionEmployeeManager)
}
}
}
// @Title Post
// @Description sign employee cla
// @Param :link_id path string true "link id"
// @Param :cla_lang path string true "cla language"
// @Param :cla_hash path string true "the hash of cla content"
// @Param body body models.EmployeeSigning true "body for individual signing"
// @Success 201 {string} "sign successfully"
// @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 error_parsing_api_body: parse payload of request failed
// @Failure 406 unmatched_email: the email is not same as the one which signer sets on the code platform
// @Failure 407 unmatched_user_id: the user id is not same as the one which was fetched from code platform
// @Failure 408 expired_verification_code: the verification code is expired
// @Failure 409 wrong_verification_code: the verification code is wrong
// @Failure 410 no_link: the link id is not exists
// @Failure 411 no_employee_manager: there is not any employee managers for the corresponding corp
// @Failure 412 unmatched_cla: the cla hash is not equal to the one of backend server
// @Failure 413 resigned: the signer has signed the cla
// @Failure 500 system_error: system error
// @router /:link_id/:cla_lang/:cla_hash [post]
func (this *EmployeeSigningController) Post() {
action := "sign employeee cla"
linkID := this.GetString(":link_id")
claLang := this.GetString(":cla_lang")
pl, fr := this.tokenPayloadBasedOnCodePlatform()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
var info models.EmployeeSigning
if fr := this.fetchInputPayload(&info); fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
info.CLALanguage = claLang
if err := (&info).Validate(linkID, pl.User, pl.Email); err != nil {
this.sendModelErrorAsResp(err, action)
return
}
orgInfo, merr := models.GetOrgOfLink(linkID)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
managers, merr := models.ListCorporationManagers(linkID, info.Email, dbmodels.RoleManager)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
if len(managers) <= 0 {
this.sendFailedResponse(400, errNoCorpEmployeeManager, fmt.Errorf("no managers"), action)
return
}
fr = signHelper(
linkID, claLang, dbmodels.ApplyToIndividual,
func(claInfo *models.CLAInfo) *failedApiResult {
if claInfo.CLAHash != this.GetString(":cla_hash") {
return newFailedApiResult(400, errUnmatchedCLA, fmt.Errorf("invalid cla"))
}
info.Info = getSingingInfo(info.Info, claInfo.Fields)
if err := (&info).Create(linkID, false); err != nil {
if err.IsErrorOf(models.ErrNoLinkOrResigned) {
return newFailedApiResult(400, errResigned, err)
}
return parseModelError(err)
}
return nil
},
)
if fr != nil {
this.sendFailedResultAsResp(fr, action)
} else {
this.sendSuccessResp("sign successfully")
this.notifyManagers(linkID, managers, &info, orgInfo)
}
}
// @Title GetAll
// @Description get all the employees
// @Success 200 {object} dbmodels.IndividualSigningBasicInfo
// @Failure 400 missing_token: token is missing
// @Failure 401 unknown_token: token is unknown
// @Failure 402 expired_token: token is expired
// @Failure 403 unauthorized_token: the permission of token is unmatched
// @Failure 500 system_error: system error
// @router / [get]
func (this *EmployeeSigningController) GetAll() {
action := "list employees"
pl, fr := this.tokenPayloadBasedOnCorpManager()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
r, merr := models.ListIndividualSigning(pl.LinkID, pl.Email, this.GetString("cla_language"))
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
this.sendSuccessResp(r)
}
// @Title List
// @Description get all the employees by community manager
// @Param :link_id path string true "link id"
// @Param :email path string true "the email of corp"
// @Success 200 {object} dbmodels.IndividualSigningBasicInfo
// @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 unknown_link: unkown link id
// @Failure 406 not_yours_org: the link doesn't belong to your community
// @Failure 500 system_error: system error
// @router /:link_id/:email [get]
func (this *EmployeeSigningController) List() {
action := "list employees"
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
}
r, merr := models.ListIndividualSigning(linkID, corpEmail, "")
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
this.sendSuccessResp(r)
}
// @Title Update
// @Description enable/unable employee signing
// @Param :email path string true "email"
// @Success 202 {int} map
// @router /:email [put]
func (this *EmployeeSigningController) Update() {
action := "enable/unable employee signing"
sendResp := this.newFuncForSendingFailedResp(action)
employeeEmail := this.GetString(":email")
pl, fr := this.tokenPayloadBasedOnCorpManager()
if fr != nil {
sendResp(fr)
return
}
if !pl.hasEmployee(employeeEmail) {
this.sendFailedResponse(400, errNotSameCorp, fmt.Errorf("not same corp"), action)
return
}
var info models.EmployeeSigningUdateInfo
if fr := this.fetchInputPayload(&info); fr != nil {
sendResp(fr)
return
}
if err := (&info).Update(pl.LinkID, employeeEmail); err != nil {
if err.IsErrorOf(models.ErrNoLinkOrUnsigned) {
this.sendFailedResponse(400, errUnsigned, err, action)
} else {
this.sendModelErrorAsResp(err, action)
}
return
}
this.sendSuccessResp("enabled employee successfully")
msg := this.newEmployeeNotification(pl, employeeEmail)
if info.Enabled {
msg.Active = true
sendEmailToIndividual(pl.LinkID, employeeEmail, "Activate CLA signing", msg)
} else {
msg.Inactive = true
sendEmailToIndividual(pl.LinkID, employeeEmail, "Inactivate CLA signing", msg)
}
}
// @Title Delete
// @Description delete employee signing
// @Param :email path string true "email"
// @Success 204 {string} delete success!
// @router /:email [delete]
func (this *EmployeeSigningController) Delete() {
action := "delete employee signing"
employeeEmail := this.GetString(":email")
pl, fr := this.tokenPayloadBasedOnCorpManager()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if !pl.hasEmployee(employeeEmail) {
this.sendFailedResponse(400, errNotSameCorp, fmt.Errorf("not same corp"), action)
return
}
if err := models.DeleteEmployeeSigning(pl.LinkID, employeeEmail); err != nil {
this.sendModelErrorAsResp(err, action)
return
}
this.sendSuccessResp("delete employee successfully")
msg := this.newEmployeeNotification(pl, employeeEmail)
msg.Removing = true
sendEmailToIndividual(pl.LinkID, employeeEmail, "Remove employee", msg)
}
func (this *EmployeeSigningController) notifyManagers(linkID string, managers []dbmodels.CorporationManagerListResult, info *models.EmployeeSigning, orgInfo *models.OrgInfo) {
ms := make([]string, 0, len(managers))
to := make([]string, 0, len(managers))
for _, item := range managers {
to = append(to, item.Email)
ms = append(ms, fmt.Sprintf("%s: %s", item.Name, item.Email))
}
msg := email.EmployeeSigning{
Name: info.Name,
Org: orgInfo.OrgAlias,
ProjectURL: orgInfo.ProjectURL(),
Managers: " " + strings.Join(ms, "\n "),
}
sendEmailToIndividual(
linkID, info.Email,
fmt.Sprintf("Signing CLA on project of \"%s\"", msg.Org),
msg,
)
msg1 := email.NotifyingManager{
Org: orgInfo.OrgAlias,
EmployeeEmail: info.Email,
ProjectURL: orgInfo.ProjectURL(),
URLOfCLAPlatform: config.AppConfig.CLAPlatformURL,
}
sendEmail(linkID, to, "An employee has signed CLA", msg1)
}
func (this *EmployeeSigningController) newEmployeeNotification(pl *acForCorpManagerPayload, employeeName string) *email.EmployeeNotification {
return &email.EmployeeNotification{
Name: employeeName,
Manager: pl.Email,
Org: pl.OrgAlias,
ProjectURL: pl.ProjectURL(),
}
}