Skip to content

Commit

Permalink
Merge pull request #545 from RodrigoAS28/oauthAdminUser
Browse files Browse the repository at this point in the history
Oauth admin user
  • Loading branch information
adubovikov authored Feb 19, 2024
2 parents 96cdc1d + 9bb9923 commit 5327f33
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions data/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ func (us *UserService) GetUser(UserName string, isAdmin bool) ([]*model.TableUse
return user, len(user), nil
}

// this method checks if a user is admin using the email address
func (us *UserService) IsAdmin(email string) (bool, error) {

var user []*model.TableUser
var sqlWhere = make(map[string]interface{})

sqlWhere = map[string]interface{}{"email": email}

if err := us.Session.Debug().Table("users").Where(sqlWhere).Find(&user).Error; err != nil {
return false, err
}

if len(user) != 1 {
return false, nil
}

return user[0].UserGroup != "" && strings.Contains(strings.ToLower(user[0].UserGroup), "admin"), nil
}

// this method gets all users from database
func (us *UserService) GetUserByUUID(GUID, UserName string) ([]*model.TableUser, int, error) {

Expand Down Expand Up @@ -369,10 +388,17 @@ func (us *UserService) LoginUserUsingOauthToken(oAuth2Object model.OAuth2MapToke

logger.Debug("LoginUserUsingOauthToken json profile: ", string(oAuth2Object.ProfileJson))

userData.UserGroup = "user"
userData.IsAdmin = false
if userJsonData.Exists("email") {
userData.Email = userJsonData.S("email").Data().(string)
userData.UserName = userData.Email
userData.Id = int(hashString(userData.UserName))
isAdmin, _ := us.IsAdmin(userData.Email)
if isAdmin {
userData.UserGroup = "admin"
userData.IsAdmin = true
}
}

if userJsonData.Exists("family_name") {
Expand Down Expand Up @@ -407,8 +433,6 @@ func (us *UserService) LoginUserUsingOauthToken(oAuth2Object model.OAuth2MapToke
hash := md5.Sum([]byte(userData.UserName))
userData.GUID = hex.EncodeToString(hash[:])
userData.ExternalAuth = true
userData.UserGroup = "user"
userData.IsAdmin = false

token, err := auth.Token(userData)
return token, userData, err
Expand Down

0 comments on commit 5327f33

Please sign in to comment.