-
Notifications
You must be signed in to change notification settings - Fork 17
/
god.signup.go
53 lines (42 loc) · 1.62 KB
/
god.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
package auth
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/spo-iitk/ras-backend/constants"
"github.com/spo-iitk/ras-backend/mail"
"github.com/spo-iitk/ras-backend/middleware"
)
func godSignUpHandler(mail_channel chan mail.Mail) gin.HandlerFunc {
return func(ctx *gin.Context) {
middleware.Authenticator()(ctx)
if middleware.GetRoleID(ctx) != constants.GOD {
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Only God can sign up for GOD"})
return
}
var req User
err := ctx.ShouldBindJSON(&req)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.UserID == "" || req.Password == "" || req.Name == "" || req.RoleID == 0 {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing fields"})
return
}
if req.RoleID == constants.STUDENT || req.RoleID == constants.COMPANY || req.RoleID == constants.GOD {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid role"})
return
}
pass := req.Password
req.Password = hashAndSalt(req.Password)
id, err := firstOrCreateUser(ctx, &req)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
mail_channel <- mail.GenerateMail(req.UserID, "Registered on RAS", "Dear "+req.Name+",\n\nYou have been registered as an Admin.\n"+"Your new credentials are: \n\nUser ID: "+req.UserID+"\nPassword: "+pass)
logrus.Info("Admin registered: ", req.UserID, " by ", middleware.GetUserID(ctx), " with id ", id)
ctx.JSON(http.StatusOK, gin.H{"id": id})
}
}