forked from shunfei/cronsun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadministrator.go
250 lines (210 loc) · 6.12 KB
/
administrator.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
package web
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/log"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Administrator struct{}
func adminAuthHandler(ctx *Context) (abort bool) {
return authHandler(true, cronsun.Administrator)(ctx)
}
func NewAdminAuthHandler(f func(ctx *Context)) BaseHandler {
return BaseHandler{
BeforeHandle: adminAuthHandler,
Handle: f,
}
}
type Account struct {
Role cronsun.Role `json:"role"`
Email string `json:"email"`
Status cronsun.UserStatus `json:"status"`
Session bool `json:"session"`
CreateTime time.Time `json:"createTime"`
}
func (this *Administrator) GetAccountList(ctx *Context) {
list, err := cronsun.GetAccounts(nil)
if err != nil {
outJSONWithCode(ctx.W, http.StatusInternalServerError, err.Error())
return
}
var alist = make([]Account, len(list))
for i := range list {
alist[i] = Account{
Role: list[i].Role,
Email: list[i].Email,
Status: list[i].Status,
Session: list[i].Session != "",
CreateTime: list[i].CreateTime,
}
}
outJSONWithCode(ctx.W, http.StatusOK, alist)
}
func (this *Administrator) GetAccount(ctx *Context) {
vars := mux.Vars(ctx.R)
email := strings.TrimSpace(vars["email"])
if email == "" {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Email required.")
return
}
u, err := cronsun.GetAccountByEmail(email)
if err != nil {
if err == mgo.ErrNotFound {
outJSONWithCode(ctx.W, http.StatusNotFound, fmt.Sprintf("Email [%s] not found.", email))
} else {
outJSONWithCode(ctx.W, http.StatusInternalServerError, err.Error())
}
return
}
outJSONWithCode(ctx.W, http.StatusOK, &Account{
Role: u.Role,
Email: u.Email,
Status: u.Status,
Session: u.Session != "",
CreateTime: u.CreateTime,
})
}
func (this *Administrator) AddAccount(ctx *Context) {
account := struct {
Role cronsun.Role `json:"role"`
Email string `json:"email"`
Password string `json:"password"`
}{}
decoder := json.NewDecoder(ctx.R.Body)
err := decoder.Decode(&account)
if err != nil {
outJSONWithCode(ctx.W, http.StatusBadRequest, err.Error())
return
}
ctx.R.Body.Close()
if !account.Role.Defined() {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account role undefined.")
return
}
account.Email = strings.TrimSpace(account.Email)
if len(account.Email) == 0 {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account email is required.")
return
}
account.Password = strings.TrimSpace(account.Password)
if len(account.Password) == 0 {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account password is required.")
return
}
u, err := cronsun.GetAccountByEmail(account.Email)
if err == nil || u != nil {
outJSONWithCode(ctx.W, http.StatusConflict, fmt.Sprintf("Email [%s] has been used.", account.Email))
return
}
salt := genSalt()
err = cronsun.CreateAccount(&cronsun.Account{
Role: account.Role,
Email: account.Email,
Salt: salt,
Status: cronsun.UserActived,
Password: encryptPassword(account.Password, salt),
})
if err != nil {
outJSONWithCode(ctx.W, http.StatusBadRequest, fmt.Sprintf("Failed to create user: %s.", err.Error()))
return
}
outJSONWithCode(ctx.W, http.StatusNoContent, nil)
}
func (this *Administrator) UpdateAccount(ctx *Context) {
account := struct {
Role cronsun.Role `json:"role"`
OriginEmail string `json:"originEmail"`
Email string `json:"email"`
Password string `json:"password"`
Status cronsun.UserStatus `json:"status"`
}{}
decoder := json.NewDecoder(ctx.R.Body)
err := decoder.Decode(&account)
if err != nil {
outJSONWithCode(ctx.W, http.StatusBadRequest, err.Error())
return
}
ctx.R.Body.Close()
account.OriginEmail = strings.TrimSpace(account.OriginEmail)
if account.OriginEmail == "" {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account origin email is required.")
return
}
if !account.Role.Defined() {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account role undefined.")
return
}
if !account.Status.Defined() {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account status undefined.")
return
}
account.Email = strings.TrimSpace(account.Email)
if len(account.Email) == 0 {
outJSONWithCode(ctx.W, http.StatusBadRequest, "Account email is required.")
return
}
originAccount, err := cronsun.GetAccountByEmail(account.OriginEmail)
if err != nil {
if err == mgo.ErrNotFound {
outJSONWithCode(ctx.W, http.StatusNotFound, "Email not found.")
} else {
outJSONWithCode(ctx.W, http.StatusInternalServerError, err.Error())
}
return
}
if originAccount.Unchangeable && originAccount.Email != ctx.Session.Email {
outJSONWithCode(ctx.W, http.StatusForbidden, "You can not change this account.")
return
}
var update = bson.M{}
if !originAccount.Unchangeable {
update = bson.M{
"status": account.Status,
"role": account.Role,
}
}
if account.Email != account.OriginEmail {
update["email"] = account.Email
}
account.Password = strings.TrimSpace(account.Password)
if len(account.Password) != 0 {
salt := genSalt()
update["salt"] = salt
update["password"] = encryptPassword(account.Password, salt)
}
if len(update) == 0 {
outJSONWithCode(ctx.W, http.StatusOK, nil)
return
}
err = cronsun.UpdateAccount(bson.M{"email": account.OriginEmail}, update)
if err != nil {
outJSONWithCode(ctx.W, http.StatusBadRequest, fmt.Sprintf("Failed to update user: %s.", err.Error()))
return
}
this.removeSession(account.Email)
if ctx.Session.Email == originAccount.Email {
ctx.Session.Email = ""
delete(ctx.Session.Data, "role")
ctx.Session.Store()
outJSONWithCode(ctx.W, http.StatusUnauthorized, nil)
return
}
outJSONWithCode(ctx.W, http.StatusOK, nil)
}
func (this *Administrator) removeSession(email string) {
u, err := cronsun.GetAccountByEmail(email)
if err != nil {
log.Errorf("Failed to remove user [%s] session: %s", email, err.Error())
return
}
if u.Session == "" {
return
}
sessManager.CleanSeesionData(u.Session)
}