Commit dbf61355 by bergquist

style: code simplifications

parent c22495b6
package login package login
import ( import (
"fmt"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
...@@ -24,13 +22,13 @@ func UpsertUser(cmd *m.UpsertUserCommand) error { ...@@ -24,13 +22,13 @@ func UpsertUser(cmd *m.UpsertUserCommand) error {
Login: extUser.Login, Login: extUser.Login,
} }
err := bus.Dispatch(userQuery) err := bus.Dispatch(userQuery)
if err != nil { if err != m.ErrUserNotFound && err != nil {
if err != m.ErrUserNotFound {
return err return err
} }
if err != nil {
if !cmd.SignupAllowed { if !cmd.SignupAllowed {
log.Warn(fmt.Sprintf("Not allowing %s login, user not found in internal user database and allow signup = false", extUser.AuthModule)) log.Warn("Not allowing %s login, user not found in internal user database and allow signup = false", extUser.AuthModule)
return ErrInvalidCredentials return ErrInvalidCredentials
} }
...@@ -58,10 +56,10 @@ func UpsertUser(cmd *m.UpsertUserCommand) error { ...@@ -58,10 +56,10 @@ func UpsertUser(cmd *m.UpsertUserCommand) error {
return err return err
} }
} }
} else { } else {
cmd.Result = userQuery.Result cmd.Result = userQuery.Result
// sync user info
err = updateUser(cmd.Result, extUser) err = updateUser(cmd.Result, extUser)
if err != nil { if err != nil {
return err return err
...@@ -90,33 +88,32 @@ func updateUser(user *m.User, extUser *m.ExternalUserInfo) error { ...@@ -90,33 +88,32 @@ func updateUser(user *m.User, extUser *m.ExternalUserInfo) error {
updateCmd := &m.UpdateUserCommand{ updateCmd := &m.UpdateUserCommand{
UserId: user.Id, UserId: user.Id,
} }
needsUpdate := false
needsUpdate := false
if extUser.Login != "" && extUser.Login != user.Login { if extUser.Login != "" && extUser.Login != user.Login {
updateCmd.Login = extUser.Login updateCmd.Login = extUser.Login
user.Login = extUser.Login user.Login = extUser.Login
needsUpdate = true needsUpdate = true
} }
if extUser.Email != "" && extUser.Email != user.Email { if extUser.Email != "" && extUser.Email != user.Email {
updateCmd.Email = extUser.Email updateCmd.Email = extUser.Email
user.Email = extUser.Email user.Email = extUser.Email
needsUpdate = true needsUpdate = true
} }
if extUser.Name != "" && extUser.Name != user.Name { if extUser.Name != "" && extUser.Name != user.Name {
updateCmd.Name = extUser.Name updateCmd.Name = extUser.Name
user.Name = extUser.Name user.Name = extUser.Name
needsUpdate = true needsUpdate = true
} }
if needsUpdate { if !needsUpdate {
log.Debug("Syncing user info", "id", user.Id, "update", updateCmd) return nil
err := bus.Dispatch(updateCmd)
if err != nil {
return err
}
} }
return nil log.Debug("Syncing user info", "id", user.Id, "update", updateCmd)
return bus.Dispatch(updateCmd)
} }
func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error { func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error {
...@@ -176,13 +173,11 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error { ...@@ -176,13 +173,11 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error {
user.OrgId = orgId user.OrgId = orgId
break break
} }
err := bus.Dispatch(&m.SetUsingOrgCommand{
return bus.Dispatch(&m.SetUsingOrgCommand{
UserId: user.Id, UserId: user.Id,
OrgId: user.OrgId, OrgId: user.OrgId,
}) })
if err != nil {
return err
}
} }
return nil return nil
......
...@@ -125,7 +125,7 @@ func TestLdapAuther(t *testing.T) { ...@@ -125,7 +125,7 @@ func TestLdapAuther(t *testing.T) {
ldapAutherScenario("given current org role is removed in ldap", func(sc *scenarioContext) { ldapAutherScenario("given current org role is removed in ldap", func(sc *scenarioContext) {
ldapAuther := NewLdapAuthenticator(&LdapServerConf{ ldapAuther := NewLdapAuthenticator(&LdapServerConf{
LdapGroups: []*LdapGroupToOrgRole{ LdapGroups: []*LdapGroupToOrgRole{
{GroupDN: "cn=users", OrgId: 1, OrgRole: "Admin"}, {GroupDN: "cn=users", OrgId: 2, OrgRole: "Admin"},
}, },
}) })
...@@ -140,7 +140,7 @@ func TestLdapAuther(t *testing.T) { ...@@ -140,7 +140,7 @@ func TestLdapAuther(t *testing.T) {
Convey("Should remove org role", func() { Convey("Should remove org role", func() {
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(sc.removeOrgUserCmd, ShouldNotBeNil) So(sc.removeOrgUserCmd, ShouldNotBeNil)
So(sc.setUsingOrgCmd.OrgId, ShouldEqual, 1) So(sc.setUsingOrgCmd.OrgId, ShouldEqual, 2)
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment