forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_api_signup.go
85 lines (74 loc) · 1.83 KB
/
d_api_signup.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
package uadmin
import "net/http"
func dAPISignupHandler(w http.ResponseWriter, r *http.Request, s *Session) {
// Check if signup API is allowed
if !AllowDAPISignup {
w.WriteHeader(http.StatusForbidden)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Signup API is disabled",
})
return
}
// get variables from request
username := r.FormValue("username")
email := r.FormValue("email")
firstName := r.FormValue("first_name")
lastName := r.FormValue("last_name")
password := r.FormValue("password")
// set the username to email if there is no username
if username == "" && email != "" {
username = email
}
// check if password is empty
if password == "" {
w.WriteHeader(http.StatusBadRequest)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "password is empty",
})
return
}
// create user object
user := User{
Username: username,
FirstName: firstName,
LastName: lastName,
Password: password,
Email: email,
Active: DAPISignupActive,
Admin: false,
RemoteAccess: DAPISignupAllowRemote,
UserGroupID: uint(DAPISignupGroupID),
}
// run custom validation
if SignupValidationHandler != nil {
err := SignupValidationHandler(&user)
w.WriteHeader(http.StatusBadRequest)
if err != nil {
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": err.Error(),
})
return
}
}
// Save user record
user.Save()
// Check if the record was not saved, that means the username is taken
if user.ID == 0 {
w.WriteHeader(400)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "username taken",
})
}
// if the user is active, then login in
if user.Active {
dAPILoginHandler(w, r, s)
return
}
ReturnJSON(w, r, map[string]interface{}{
"status": "ok",
})
}