-
Notifications
You must be signed in to change notification settings - Fork 181
/
userModels.go
300 lines (255 loc) · 7.44 KB
/
userModels.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 models
import (
"errors"
"net/url"
"strings"
"github.com/hzwy23/dbobj"
"github.com/hzwy23/hauth/utils"
"github.com/hzwy23/hauth/utils/crypto/haes"
"github.com/hzwy23/hauth/utils/logs"
"github.com/hzwy23/hauth/utils/validator"
)
type UserModel struct {
morg OrgModel
}
type UserInfo struct {
User_id string `json:"user_id"`
User_name string `json:"user_name"`
User_status_desc string `json:"status_desc"`
User_create_date string `json:"create_date"`
User_owner string `json:"create_user"`
User_email string `json:"user_email"`
User_phone string `json:"user_phone"`
Org_unit_id string `json:"org_unit_id"`
Org_unit_desc string `json:"org_unit_desc"`
Domain_id string `json:"domain_id"`
Domain_name string `json:"domain_name"`
User_maintance_date string `json:"modify_date"`
User_maintance_user string `json:"modify_user"`
User_status_id string `json:"status_cd"`
}
// 查询用户自己的详细信息
func (UserModel) GetOwnerDetails(user_id string) ([]UserInfo, error) {
var rst []UserInfo
row, err := dbobj.Query(sys_rdbms_023, user_id)
defer row.Close()
if err != nil {
logs.Error(err)
return nil, err
}
err = dbobj.Scan(row, &rst)
return rst, err
}
// 查询域中所有的用户信息
func (UserModel) GetDefault(domain_id string) ([]UserInfo, error) {
row, err := dbobj.Query(sys_rdbms_017, domain_id)
defer row.Close()
if err != nil {
logs.Error(err)
return nil, err
}
var rst []UserInfo
err = dbobj.Scan(row, &rst)
return rst, err
}
// 新增用户信息
func (UserModel) Post(data url.Values, user_id string) (string, error) {
userId := data.Get("userId")
userDesc := data.Get("userDesc")
password := data.Get("userPasswd")
surepassword := data.Get("userPasswdConfirm")
userStatus := data.Get("userStatus")
userEmail := data.Get("userEmail")
userPhone := data.Get("userPhone")
userOrgUnitId := data.Get("userOrgUnitId")
domain_id := data.Get("domainId")
if !validator.IsWord(userId) {
return "error_user_id_check", errors.New("error_user_id_check")
}
//
if validator.IsEmpty(userDesc) {
return "error_user_name_check", errors.New("error_user_name_check")
}
if validator.IsEmpty(password) {
return "error_user_passwd_check", errors.New("error_user_passwd_check")
}
if validator.IsEmpty(surepassword) {
return "error_passwd_empty", errors.New("error_passwd_empty")
}
if password != surepassword {
return "error_passwd_confirm_failed", errors.New("error_passwd_confirm_failed")
}
if len(strings.TrimSpace(password)) < 6 {
return "error_passwd_short", errors.New("error_passwd_short")
}
userPasswd, err := haes.Encrypt(password)
if err != nil {
logs.Error(err)
return "error_user_passwd_encrypt", errors.New("error_user_passwd_encrypt")
}
//
if !validator.IsEmail(userEmail) {
return "error_user_email_check", errors.New("error_user_email_check")
}
if !validator.IsWord(userOrgUnitId) {
return "error_user_role_org", errors.New("error_user_role_org")
}
//
if !validator.IsMobilePhone(userPhone) {
return "error_user_phone_check", errors.New("error_user_phone_check")
}
org_domain_id, _ := utils.SplitDomain(userOrgUnitId)
if domain_id != org_domain_id {
return "error_user_org_format", errors.New("error_user_org_format")
}
tx, err := dbobj.Begin()
// insert user details
//
_, err = tx.Exec(sys_rdbms_018, userId, userDesc, user_id, userEmail, userPhone, userOrgUnitId, user_id)
if err != nil {
tx.Rollback()
logs.Error(err)
return "error_user_post", err
}
// insert user passwd
_, err = tx.Exec(sys_rdbms_019, userId, userPasswd, userStatus)
if err != nil {
tx.Rollback()
logs.Error(err)
return "error_user_post", err
}
// insert theme info
_, err = tx.Exec(sys_rdbms_045, userId, "1001")
if err != nil {
tx.Rollback()
logs.Error(err.Error())
return "error_user_post", err
}
err = tx.Commit()
if err != nil {
logs.Error(err)
return "error_user_post", err
}
return "success", nil
}
// 删除用户信息
func (UserModel) Delete(data []UserInfo) (string, error) {
tx, err := dbobj.Begin()
if err != nil {
return "error_sql_begin", err
}
for _, val := range data {
_, err = tx.Exec(sys_rdbms_007, val.User_id, val.Org_unit_id)
if err != nil {
tx.Rollback()
logs.Error(err)
return "error_user_exec", err
}
}
err = tx.Commit()
if err != nil {
logs.Error(err)
return "error_user_commit", err
}
return "success", nil
}
// 搜索用户信息
func (this UserModel) Search(org_id string, status_id string, domain_id string) ([]UserInfo, error) {
var rst []UserInfo
ret, err := this.GetDefault(domain_id)
if err != nil {
logs.Error(err)
return nil, err
}
if !validator.IsEmpty(org_id) {
orglist, err := this.morg.GetSubOrgInfo(domain_id, org_id)
if err != nil {
logs.Error(err)
return nil, err
}
var orgmap map[string]string = make(map[string]string)
for _, val := range orglist {
orgmap[val.Org_unit_id] = ""
}
for _, val := range ret {
if _, ok := orgmap[val.Org_unit_id]; ok {
if !validator.IsEmpty(status_id) {
if val.User_status_id == status_id {
rst = append(rst, val)
}
} else {
rst = append(rst, val)
}
}
}
} else {
for _, val := range ret {
if !validator.IsEmpty(status_id) {
if val.User_status_id == status_id {
rst = append(rst, val)
}
} else {
rst = append(rst, val)
}
}
}
return rst, nil
}
func (this UserModel) ModifyStatus(status_id, user_id string) (string, error) {
if !validator.IsIn(status_id, "0", "1") {
return "error_user_status_empty", errors.New("error_user_status_empty")
}
_, err := dbobj.Exec(sys_rdbms_016, status_id, user_id)
return "error_user_modify_status", err
}
func (this UserModel) ModifyPasswd(data url.Values) (string, error) {
user_id := data.Get("userid")
user_password := data.Get("newpasswd")
confirm_password := data.Get("surepasswd")
if user_password != confirm_password {
return "error_passwd_confirm_failed", errors.New("error_passwd_confirm_failed")
}
if len(strings.TrimSpace(confirm_password)) < 6 || len(strings.TrimSpace(confirm_password)) > 30 {
return "error_passwd_short", errors.New("error_passwd_short")
}
encry_passwd, err := haes.Encrypt(user_password)
if err != nil {
logs.Error(err)
return "error_password_encrpty", errors.New("error_password_encrpty")
}
_, err = dbobj.Exec(sys_rdbms_020, encry_passwd, user_id)
if err != nil {
logs.Error(err)
return "error_user_modify_passwd", err
}
return "success", nil
}
// 修改用户信息
func (this UserModel) Put(data url.Values, modify_user string) (string, error) {
user_name := data.Get("userDesc")
org_id := data.Get("orgId")
phone := data.Get("userPhone")
email := data.Get("userEmail")
user_id := data.Get("userId")
if !validator.IsWord(user_id) {
return "error_user_id_empty", errors.New("error_user_id_empty")
}
if validator.IsEmpty(user_name) {
return "error_user_desc_empty", errors.New("error_user_desc_empty")
}
if !validator.IsEmail(email) {
return "error_user_email_format", errors.New("error_user_email_format")
}
if !validator.IsWord(org_id) {
return "error_org_id_format", errors.New("error_org_id_format")
}
if !validator.IsMobilePhone(phone) {
return "error_user_phone_format", errors.New("error_user_phone_format")
}
_, err := dbobj.Exec(sys_rdbms_021, user_name, phone, email, modify_user, org_id, user_id)
if err != nil {
logs.Error(err)
return "error_user_modify_info", err
}
return "success", nil
}