-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_user.go
306 lines (252 loc) · 7.34 KB
/
rest_user.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
301
302
303
304
305
306
package main
import (
"github.com/labstack/echo"
// "gopkg.in/mgutz/dat.v1"
"fmt"
"log"
"net/http"
)
/////////////////////////////////////////////////////////////////////////////////////////////////
// User Functions
/*
e.Get("/users", queryUsers)
e.Get("/users/:id", getUser)
e.Post("/users/:id", newUser)
e.Patch("/users/:id", saveUser)
e.Delete("/users/:id", deleteUser)
*/
type DBusers struct {
ID int `db:"id",json:"number"`
Username string `db:"username"`
Passwd string `db:"passwd"`
Name string `db:"name"`
Email string `db:"email"`
Address string `db:"address"`
SMS string `db:"sms"`
SiteId int `db:"site_id"`
SiteName *string `db."sitename"`
Role string `db:"role"`
Sites []*DBsite `db:"sites"`
Skills []*DBskill `db:"skills"`
Notes string `db:"notes"`
}
type DBuserlog struct {
Type string `db:"type"`
RefID int `db:"ref_id"`
Username *string `db:"username"`
Logdate string `db:"logdate"`
IP string `db:"ip"`
Descr *string `db:"descr"`
}
type DBuserSite struct {
UserID int `db:"user_id"`
SiteID int `db:"site_id"`
Role string `db:"role"`
}
type DBuserSkill struct {
UserID int `db:"user_id"`
SkillID int `db:"skill_id"`
}
func queryUsers(c echo.Context) error {
_, err := securityCheck(c, "readUser")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
var users []*DBusers
// SQL(`select *,array(select concat(logdate,ip,descr) from user_log where user_id=users.id order by logdate desc) as logs from users`).
err = DB.SQL(`select
users.*,site.name as sitename
from users
left join site on site.id=users.site_id
order by lower(username)`).
QueryStructs(&users)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, users)
}
func queryUsersWithSkill(c echo.Context) error {
_, err := securityCheck(c, "readUser")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
var users []*DBusers
// SQL(`select *,array(select concat(logdate,ip,descr) from user_log where user_id=users.id order by logdate desc) as logs from users`).
skillID := getID(c)
err = DB.SQL(`select
u.*,t.name as sitename from user_skill s
left join users u on u.id = s.user_id
left join site t on t.id = u.site_id
where s.skill_id=$1
order by lower(username)`, skillID).
QueryStructs(&users)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, users)
}
func getUser(c echo.Context) error {
_, err := securityCheck(c, "readUser")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
var user DBusers
err = DB.SQL(`select * from users where id=$1`, id).QueryStruct(&user)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
// Fill in the sites for the user
DB.SQL(`select
s.*
from user_site u
left join site s on s.id=u.site_id
where u.user_id=$1`, id).
QueryStructs(&user.Sites)
// Fill in the skills for the user
DB.SQL(`select
s.*
from user_skill u
left join skill s on s.id=u.skill_id
where u.user_id=$1`, id).
QueryStructs(&user.Skills)
// Fill in the site name
DB.SQL(`select
name as sitename from site
where id=$1`, user.SiteId).
QueryScalar(&user.SiteName)
return c.JSON(http.StatusOK, user)
}
func newUser(c echo.Context) error {
claim, err := securityCheck(c, "writeUser")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
record := &DBusers{}
if err := c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
if len(record.Sites) > 0 {
record.SiteId = record.Sites[0].ID
} else {
record.SiteId = 0
}
err = DB.InsertInto("users").
Whitelist("username", "passwd", "address", "name", "email", "role", "sms", "site_id").
Record(record).
Returning("id").
QueryScalar(&record.ID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Now create the user_site records
for _, addSite := range record.Sites {
userSite := &DBuserSite{
UserID: record.ID,
SiteID: addSite.ID,
Role: record.Role,
}
_, xe := DB.InsertInto("user_site").
Whitelist("user_id", "site_id", "role").
Record(userSite).
Exec()
if xe != nil {
log.Println("ERROR inserting user_site", xe.Error())
}
}
// Now create the user_skill recordsd
for _, addSkill := range record.Skills {
userSkill := &DBuserSkill{
UserID: record.ID,
SkillID: addSkill.ID,
}
_, xe := DB.InsertInto("user_skill").Whitelist("user_id", "skill_id").Record(userSkill).Exec()
if xe != nil {
log.Println("ERROR inserting user_skill", xe.Error())
}
}
// Now log the creation of the new user
sysLog(1, "Users", "U", record.ID, fmt.Sprintf("Account Created - %s", record.Username), c, claim)
// insert into DB, fill in the ID of the new user
return c.JSON(http.StatusCreated, record)
}
func saveUser(c echo.Context) error {
claim, err := securityCheck(c, "writeUser")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
userID := getID(c)
preRecord := &DBusers{}
DB.Select("id", "username", "name", "passwd", "email", "address", "sms", "role", "site_id", "notes").
From("users").
Where("id=$1", userID).
QueryStruct(preRecord)
record := &DBusers{}
if err = c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Get the first site from the list of sites added
if len(record.Sites) > 0 {
record.SiteId = record.Sites[0].ID
} else {
record.SiteId = 0
}
_, err = DB.Update("users").
Set("username", record.Username).
Set("name", record.Name).
Set("passwd", record.Passwd).
Set("email", record.Email).
Set("address", record.Address).
Set("sms", record.SMS).
Set("role", record.Role).
Set("site_id", record.SiteId).
Set("notes", record.Notes).
Where("id = $1", userID).
Exec()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Now create the user_site records
DB.DeleteFrom("user_site").Where("user_id=$1", userID).Exec()
for _, addSite := range record.Sites {
userSite := &DBuserSite{
UserID: userID,
SiteID: addSite.ID,
Role: record.Role,
}
DB.InsertInto("user_site").
Whitelist("user_id", "site_id", "role").
Record(userSite).
Exec()
}
// Now create the user_skill recordsd
DB.DeleteFrom("user_skill").Where("user_id=$1", userID).Exec()
for _, addSkill := range record.Skills {
userSkill := &DBuserSkill{
UserID: userID,
SkillID: addSkill.ID,
}
DB.InsertInto("user_skill").Whitelist("user_id", "skill_id").Record(userSkill).Exec()
}
sysLogUpdate(1, "Users", "U", userID, "User Updated", c, claim, *preRecord, *record)
return c.JSON(http.StatusOK, record)
}
func deleteUser(c echo.Context) error {
claim, err := securityCheck(c, "writeUser")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
_, err = DB.
DeleteFrom("users").
Where("id = $1", id).
Exec()
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Now delete the user_site and user_skill references
DB.DeleteFrom("user_site").Where("user_id=$1", id).Exec()
DB.DeleteFrom("user_skill").Where("user_id=$1", id).Exec()
sysLog(3, "Users", "U", id, "User Deleted", c, claim)
return c.String(http.StatusOK, "User Deleted")
}