Skip to content

Commit

Permalink
Checking OAUTH users for admin rights
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoAS28 committed Feb 15, 2024
1 parent 8361b3b commit 12c243c
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].IsAdmin, 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 12c243c

Please sign in to comment.