2023-02-12 23:21:07 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
// "io"
|
|
|
|
// "os"
|
|
|
|
"encoding/json"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"time"
|
|
|
|
// "golang.org/x/crypto/bcrypt"
|
|
|
|
|
2023-03-25 20:15:00 +00:00
|
|
|
"github.com/azukaar/cosmos-server/src/utils"
|
2023-02-12 23:21:07 +00:00
|
|
|
)
|
|
|
|
|
2023-03-10 20:59:56 +00:00
|
|
|
type CreateRequestJSON struct {
|
|
|
|
Nickname string `validate:"required,min=3,max=32,alphanum"`
|
2023-04-01 12:56:25 +00:00
|
|
|
Email string `validate:"omitempty,email"`
|
2023-03-10 20:59:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 23:21:07 +00:00
|
|
|
func UserCreate(w http.ResponseWriter, req *http.Request) {
|
2023-03-16 18:56:36 +00:00
|
|
|
if utils.AdminOnly(w, req) != nil {
|
2023-03-10 20:59:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-12 23:21:07 +00:00
|
|
|
|
|
|
|
if(req.Method == "POST") {
|
2023-03-10 20:59:56 +00:00
|
|
|
var request CreateRequestJSON
|
|
|
|
err1 := json.NewDecoder(req.Body).Decode(&request)
|
|
|
|
if err1 != nil {
|
|
|
|
utils.Error("UserCreation: Invalid User Request", err1)
|
|
|
|
utils.HTTPError(w, "User Creation Error",
|
|
|
|
http.StatusInternalServerError, "UC001")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errV := utils.Validate.Struct(request)
|
|
|
|
if errV != nil {
|
|
|
|
utils.Error("UserCreation: Invalid User Request", errV)
|
|
|
|
utils.HTTPError(w, "User Creation Error: " + errV.Error(),
|
|
|
|
http.StatusInternalServerError, "UC003")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nickname := utils.Sanitize(request.Nickname)
|
2023-03-13 21:06:19 +00:00
|
|
|
email := utils.Sanitize(request.Email)
|
2023-02-12 23:21:07 +00:00
|
|
|
|
2023-03-29 20:38:50 +00:00
|
|
|
c, errCo := utils.GetCollection(utils.GetRootAppId(), "users")
|
|
|
|
if errCo != nil {
|
|
|
|
utils.Error("Database Connect", errCo)
|
|
|
|
utils.HTTPError(w, "Database", http.StatusInternalServerError, "DB001")
|
|
|
|
return
|
|
|
|
}
|
2023-02-12 23:21:07 +00:00
|
|
|
|
|
|
|
user := utils.User{}
|
|
|
|
|
2023-03-10 20:59:56 +00:00
|
|
|
utils.Debug("UserCreation: Creating user " + nickname)
|
|
|
|
|
|
|
|
err2 := c.FindOne(nil, map[string]interface{}{
|
2023-02-12 23:21:07 +00:00
|
|
|
"Nickname": nickname,
|
|
|
|
}).Decode(&user)
|
|
|
|
|
2023-03-10 20:59:56 +00:00
|
|
|
if err2 == mongo.ErrNoDocuments {
|
2023-02-12 23:21:07 +00:00
|
|
|
RegisterKey := utils.GenerateRandomString(24)
|
|
|
|
RegisterKeyExp := time.Now().Add(time.Hour * 24 * 7)
|
|
|
|
|
2023-03-10 20:59:56 +00:00
|
|
|
_, err3 := c.InsertOne(nil, map[string]interface{}{
|
2023-02-12 23:21:07 +00:00
|
|
|
"Nickname": nickname,
|
2023-03-13 21:06:19 +00:00
|
|
|
"Email": email,
|
2023-02-12 23:21:07 +00:00
|
|
|
"Password": "",
|
|
|
|
"RegisterKey": RegisterKey,
|
|
|
|
"RegisterKeyExp": RegisterKeyExp,
|
|
|
|
"Role": utils.USER,
|
2023-03-10 20:59:56 +00:00
|
|
|
"PasswordCycle": 0,
|
|
|
|
"CreatedAt": time.Now(),
|
2023-02-12 23:21:07 +00:00
|
|
|
})
|
|
|
|
|
2023-03-10 20:59:56 +00:00
|
|
|
if err3 != nil {
|
|
|
|
utils.Error("UserCreation: Error while creating user", err3)
|
|
|
|
utils.HTTPError(w, "User Creation Error",
|
|
|
|
http.StatusInternalServerError, "UC001")
|
|
|
|
return
|
2023-02-12 23:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
2023-03-10 20:59:56 +00:00
|
|
|
"status": "OK",
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"registerKey": RegisterKey,
|
|
|
|
"registerKeyExp": RegisterKeyExp,
|
|
|
|
},
|
2023-02-12 23:21:07 +00:00
|
|
|
})
|
2023-03-10 20:59:56 +00:00
|
|
|
} else if err2 == nil {
|
|
|
|
utils.Error("UserCreation: User already exists", nil)
|
|
|
|
utils.HTTPError(w, "User already exists", http.StatusConflict, "UC002")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
utils.Error("UserCreation: Error while finding user", err2)
|
|
|
|
utils.HTTPError(w, "User Creation Error", http.StatusInternalServerError, "UC001")
|
|
|
|
return
|
2023-02-12 23:21:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-10 20:59:56 +00:00
|
|
|
utils.Error("UserCreation: Method not allowed" + req.Method, nil)
|
|
|
|
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
|
|
|
|
return
|
2023-02-12 23:21:07 +00:00
|
|
|
}
|
|
|
|
}
|