added validation check for email and account name

alpha
alex 2023-02-24 09:02:35 +01:00
parent 5c07f9c4de
commit 4ffed40ef8
2 changed files with 27 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package utils
import (
"encoding/json"
"time"
"clickandjoin.app/managementsystem/modules/structs"
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
@ -32,6 +33,10 @@ func UnmarshalReceivedMessage(body []byte, message any) error {
return nil
}
func GetCurrentTimestamp() int64 {
return time.Now().Unix()
}
func ValidatorInit() {
cnjvalidator.Validate.RegisterStructValidationMapRules(cnjglobals.GeneralRules,
structs.UpdateUserRequest{},

View File

@ -5,6 +5,7 @@ import (
"clickandjoin.app/managementsystem/modules/scylladb"
"clickandjoin.app/managementsystem/modules/structs"
"clickandjoin.app/managementsystem/modules/utils"
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
"git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/cnjvalidator"
"git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/dbstructs"
@ -65,32 +66,41 @@ func UpdateUser(c *fiber.Ctx) error {
}
// check whether the email in the requested has been changed in relation to the user
if foundUser.Email != body.Email {
if body.Email != foundUser.Email {
if status, err := cnjvalidator.HandleEmailValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.Email); err != nil {
return c.SendStatus(status)
}
}
accountNameLc := strings.ToLower(body.AccountName)
lastAccountNameLc := foundUser.LastAccountNameLc
// check whether the account name in the requested has been changed in relation to the user
if foundUser.AccountNameLc != accountNameLc {
if status, err := cnjvalidator.HandleAccountNameValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.AccountName); err != nil {
return c.SendStatus(status)
if accountNameLc != foundUser.AccountNameLc {
lastAccountNameLc = foundUser.AccountNameLc
if accountNameLc != foundUser.LastAccountNameLc {
if status, err := cnjvalidator.HandleAccountNameValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.AccountName); err != nil {
return c.SendStatus(status)
}
}
}
gocnjhelper.LogDebugf("lastAc %s", lastAccountNameLc, accountNameLc != foundUser.AccountNameLc)
updatedUser := dbstructs.User{
Id: params.UserId,
Username: body.Username,
AccountName: body.AccountName,
AccountNameLc: strings.ToLower(body.AccountName),
Email: body.Email,
Description: body.Description,
AccountStatus: body.AccountStatus,
Id: params.UserId,
Username: body.Username,
AccountName: body.AccountName,
AccountNameLc: accountNameLc,
LastAccountNameLc: lastAccountNameLc,
AccountNameUpdatedAt: utils.GetCurrentTimestamp(),
Email: body.Email,
Description: body.Description,
AccountStatus: body.AccountStatus,
}
if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Update("username", "account_name", "account_name_lc", "email", "description", "account_status")).BindStruct(updatedUser).ExecRelease(); err != nil {
if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Update("username", "account_name", "account_name_lc", "last_account_name_lc", "account_name_updated_at", "email", "description", "account_status")).BindStruct(updatedUser).ExecRelease(); err != nil {
gocnjhelper.LogErrorf("Failed to update user, err: %s", err.Error())
return c.SendStatus(fiber.StatusInternalServerError)
}