Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oauth admin user #545

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading