|
|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
package login |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"errors" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus" |
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
|
"github.com/grafana/grafana/pkg/models" |
|
|
|
|
@ -180,8 +182,11 @@ func updateUserAuth(user *models.User, extUser *models.ExternalUserInfo) error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func syncOrgRoles(user *models.User, extUser *models.ExternalUserInfo) error { |
|
|
|
|
// don't sync org roles if none are specified
|
|
|
|
|
logger.Debug("Syncing organization roles", "id", user.Id, "extOrgRoles", extUser.OrgRoles) |
|
|
|
|
|
|
|
|
|
// don't sync org roles if none is specified
|
|
|
|
|
if len(extUser.OrgRoles) == 0 { |
|
|
|
|
logger.Debug("Not syncing organization roles since external user doesn't have any") |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -197,11 +202,12 @@ func syncOrgRoles(user *models.User, extUser *models.ExternalUserInfo) error { |
|
|
|
|
for _, org := range orgsQuery.Result { |
|
|
|
|
handledOrgIds[org.OrgId] = true |
|
|
|
|
|
|
|
|
|
if extUser.OrgRoles[org.OrgId] == "" { |
|
|
|
|
extRole := extUser.OrgRoles[org.OrgId] |
|
|
|
|
if extRole == "" { |
|
|
|
|
deleteOrgIds = append(deleteOrgIds, org.OrgId) |
|
|
|
|
} else if extUser.OrgRoles[org.OrgId] != org.Role { |
|
|
|
|
} else if extRole != org.Role { |
|
|
|
|
// update role
|
|
|
|
|
cmd := &models.UpdateOrgUserCommand{OrgId: org.OrgId, UserId: user.Id, Role: extUser.OrgRoles[org.OrgId]} |
|
|
|
|
cmd := &models.UpdateOrgUserCommand{OrgId: org.OrgId, UserId: user.Id, Role: extRole} |
|
|
|
|
if err := bus.Dispatch(cmd); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
@ -224,13 +230,15 @@ func syncOrgRoles(user *models.User, extUser *models.ExternalUserInfo) error { |
|
|
|
|
|
|
|
|
|
// delete any removed org roles
|
|
|
|
|
for _, orgId := range deleteOrgIds { |
|
|
|
|
logger.Debug("Removing user's organization membership as part of syncing with OAuth login", |
|
|
|
|
"userId", user.Id, "orgId", orgId) |
|
|
|
|
cmd := &models.RemoveOrgUserCommand{OrgId: orgId, UserId: user.Id} |
|
|
|
|
err := bus.Dispatch(cmd) |
|
|
|
|
if err == models.ErrLastOrgAdmin { |
|
|
|
|
logger.Error(err.Error(), "userId", cmd.UserId, "orgId", cmd.OrgId) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
if err != nil { |
|
|
|
|
if err := bus.Dispatch(cmd); err != nil { |
|
|
|
|
if errors.Is(err, models.ErrLastOrgAdmin) { |
|
|
|
|
logger.Error(err.Error(), "userId", cmd.UserId, "orgId", cmd.OrgId) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|