|
|
|
@ -16,14 +16,14 @@ func init() { |
|
|
|
|
func UpsertUser(cmd *m.UpsertUserCommand) error { |
|
|
|
|
extUser := cmd.ExternalUser |
|
|
|
|
|
|
|
|
|
userQuery := m.GetUserByAuthInfoQuery{ |
|
|
|
|
userQuery := &m.GetUserByAuthInfoQuery{ |
|
|
|
|
AuthModule: extUser.AuthModule, |
|
|
|
|
AuthId: extUser.AuthId, |
|
|
|
|
UserId: extUser.UserId, |
|
|
|
|
Email: extUser.Email, |
|
|
|
|
Login: extUser.Login, |
|
|
|
|
} |
|
|
|
|
err := bus.Dispatch(&userQuery) |
|
|
|
|
err := bus.Dispatch(userQuery) |
|
|
|
|
if err != nil { |
|
|
|
|
if err != m.ErrUserNotFound { |
|
|
|
|
return err |
|
|
|
@ -47,8 +47,19 @@ func UpsertUser(cmd *m.UpsertUserCommand) error { |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if extUser.AuthModule != "" && extUser.AuthId != "" { |
|
|
|
|
cmd2 := &m.SetAuthInfoCommand{ |
|
|
|
|
UserId: cmd.Result.Id, |
|
|
|
|
AuthModule: extUser.AuthModule, |
|
|
|
|
AuthId: extUser.AuthId, |
|
|
|
|
} |
|
|
|
|
if err := bus.Dispatch(cmd2); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
cmd.Result = userQuery.User |
|
|
|
|
cmd.Result = userQuery.Result |
|
|
|
|
|
|
|
|
|
// sync user info
|
|
|
|
|
err = updateUser(cmd.Result, extUser) |
|
|
|
@ -57,17 +68,6 @@ func UpsertUser(cmd *m.UpsertUserCommand) error { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if userQuery.UserAuth == nil && extUser.AuthModule != "" && extUser.AuthId != "" { |
|
|
|
|
cmd2 := m.SetAuthInfoCommand{ |
|
|
|
|
UserId: cmd.Result.Id, |
|
|
|
|
AuthModule: extUser.AuthModule, |
|
|
|
|
AuthId: extUser.AuthId, |
|
|
|
|
} |
|
|
|
|
if err := bus.Dispatch(&cmd2); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = syncOrgRoles(cmd.Result, extUser) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
@ -77,12 +77,12 @@ func UpsertUser(cmd *m.UpsertUserCommand) error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func createUser(extUser *m.ExternalUserInfo) (*m.User, error) { |
|
|
|
|
cmd := m.CreateUserCommand{ |
|
|
|
|
cmd := &m.CreateUserCommand{ |
|
|
|
|
Login: extUser.Login, |
|
|
|
|
Email: extUser.Email, |
|
|
|
|
Name: extUser.Name, |
|
|
|
|
} |
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil { |
|
|
|
|
if err := bus.Dispatch(cmd); err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -91,7 +91,7 @@ func createUser(extUser *m.ExternalUserInfo) (*m.User, error) { |
|
|
|
|
|
|
|
|
|
func updateUser(user *m.User, extUser *m.ExternalUserInfo) error { |
|
|
|
|
// sync user info
|
|
|
|
|
updateCmd := m.UpdateUserCommand{ |
|
|
|
|
updateCmd := &m.UpdateUserCommand{ |
|
|
|
|
UserId: user.Id, |
|
|
|
|
} |
|
|
|
|
needsUpdate := false |
|
|
|
@ -111,7 +111,7 @@ func updateUser(user *m.User, extUser *m.ExternalUserInfo) error { |
|
|
|
|
|
|
|
|
|
if needsUpdate { |
|
|
|
|
log.Debug("Syncing user info", "id", user.Id, "update", updateCmd) |
|
|
|
|
err := bus.Dispatch(&updateCmd) |
|
|
|
|
err := bus.Dispatch(updateCmd) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -126,8 +126,8 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
orgsQuery := m.GetUserOrgListQuery{UserId: user.Id} |
|
|
|
|
if err := bus.Dispatch(&orgsQuery); err != nil { |
|
|
|
|
orgsQuery := &m.GetUserOrgListQuery{UserId: user.Id} |
|
|
|
|
if err := bus.Dispatch(orgsQuery); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -142,8 +142,8 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error { |
|
|
|
|
deleteOrgIds = append(deleteOrgIds, org.OrgId) |
|
|
|
|
} else if extUser.OrgRoles[org.OrgId] != org.Role { |
|
|
|
|
// update role
|
|
|
|
|
cmd := m.UpdateOrgUserCommand{OrgId: org.OrgId, UserId: user.Id, Role: extUser.OrgRoles[org.OrgId]} |
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil { |
|
|
|
|
cmd := &m.UpdateOrgUserCommand{OrgId: org.OrgId, UserId: user.Id, Role: extUser.OrgRoles[org.OrgId]} |
|
|
|
|
if err := bus.Dispatch(cmd); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -156,8 +156,8 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// add role
|
|
|
|
|
cmd := m.AddOrgUserCommand{UserId: user.Id, Role: orgRole, OrgId: orgId} |
|
|
|
|
err := bus.Dispatch(&cmd) |
|
|
|
|
cmd := &m.AddOrgUserCommand{UserId: user.Id, Role: orgRole, OrgId: orgId} |
|
|
|
|
err := bus.Dispatch(cmd) |
|
|
|
|
if err != nil && err != m.ErrOrgNotFound { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -165,8 +165,8 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error { |
|
|
|
|
|
|
|
|
|
// delete any removed org roles
|
|
|
|
|
for _, orgId := range deleteOrgIds { |
|
|
|
|
cmd := m.RemoveOrgUserCommand{OrgId: orgId, UserId: user.Id} |
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil { |
|
|
|
|
cmd := &m.RemoveOrgUserCommand{OrgId: orgId, UserId: user.Id} |
|
|
|
|
if err := bus.Dispatch(cmd); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|