mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-11-10 00:47:26 +01:00
feat: SyncUserOnLogin now updates name of token logged user
This commit is contained in:
parent
5fa6c9db35
commit
1b5f4bff2c
@ -145,14 +145,18 @@ func GetAuthInstance() *Authentication {
|
|||||||
|
|
||||||
func persistUser(user *schema.User) {
|
func persistUser(user *schema.User) {
|
||||||
r := repository.GetUserRepository()
|
r := repository.GetUserRepository()
|
||||||
_, err := r.GetUser(user.Username)
|
dbUser, err := r.GetUser(user.Username)
|
||||||
|
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
log.Errorf("Error while loading user '%s': %v", user.Username, err)
|
log.Errorf("Error while loading user '%s': %v", user.Username, err)
|
||||||
} else if err == sql.ErrNoRows {
|
} else if err == sql.ErrNoRows { // Adds New User
|
||||||
if err := r.AddUser(user); err != nil {
|
if err := r.AddUser(user); err != nil {
|
||||||
log.Errorf("Error while adding user '%s' to DB: %v", user.Username, err)
|
log.Errorf("Error while adding user '%s' to DB: %v", user.Username, err)
|
||||||
}
|
}
|
||||||
|
} else { // Update Existing
|
||||||
|
if err := r.UpdateUser(dbUser, user); err != nil {
|
||||||
|
log.Errorf("Error while updating user '%s' to DB: %v", user.Username, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,6 +130,27 @@ func (r *UserRepository) AddUser(user *schema.User) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *UserRepository) UpdateUser(dbUser *schema.User, user *schema.User) error {
|
||||||
|
// user contains updated info, apply to dbuser
|
||||||
|
// TODO: Discuss updatable fields
|
||||||
|
if dbUser.Name != user.Name {
|
||||||
|
if _, err := sq.Update("user").Set("name", user.Name).Where("user.username = ?", dbUser.Username).RunWith(r.DB).Exec(); err != nil {
|
||||||
|
log.Errorf("error while updating name of user '%s'", user.Username)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toggled until greenlit
|
||||||
|
// if dbUser.HasRole(schema.RoleManager) && !reflect.DeepEqual(dbUser.Projects, user.Projects) {
|
||||||
|
// projects, _ := json.Marshal(user.Projects)
|
||||||
|
// if _, err := sq.Update("user").Set("projects", projects).Where("user.username = ?", dbUser.Username).RunWith(r.DB).Exec(); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *UserRepository) DelUser(username string) error {
|
func (r *UserRepository) DelUser(username string) error {
|
||||||
_, err := r.DB.Exec(`DELETE FROM user WHERE user.username = ?`, username)
|
_, err := r.DB.Exec(`DELETE FROM user WHERE user.username = ?`, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user