Преглед на файлове

make sure user's default org is kept up to date

Dan Cech преди 7 години
родител
ревизия
33760b5c3b
променени са 3 файла, в които са добавени 34 реда и са изтрити 4 реда
  1. 18 0
      pkg/login/ext_user.go
  2. 11 0
      pkg/login/ldap_test.go
  3. 5 4
      pkg/services/sqlstore/user.go

+ 18 - 0
pkg/login/ext_user.go

@@ -99,14 +99,17 @@ func updateUser(user *m.User, extUser *m.ExternalUserInfo) error {
 
 
 	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
 		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
 		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
 		needsUpdate = true
 		needsUpdate = true
 	}
 	}
 
 
@@ -172,5 +175,20 @@ func syncOrgRoles(user *m.User, extUser *m.ExternalUserInfo) error {
 		}
 		}
 	}
 	}
 
 
+	// update user's default org if needed
+	if _, ok := extUser.OrgRoles[user.OrgId]; !ok {
+		for orgId := range extUser.OrgRoles {
+			user.OrgId = orgId
+			break
+		}
+		err := bus.Dispatch(&m.SetUsingOrgCommand{
+			UserId: user.Id,
+			OrgId:  user.OrgId,
+		})
+		if err != nil {
+			return err
+		}
+	}
+
 	return nil
 	return nil
 }
 }

+ 11 - 0
pkg/login/ldap_test.go

@@ -118,6 +118,7 @@ func TestLdapAuther(t *testing.T) {
 				So(err, ShouldBeNil)
 				So(err, ShouldBeNil)
 				So(sc.updateOrgUserCmd, ShouldNotBeNil)
 				So(sc.updateOrgUserCmd, ShouldNotBeNil)
 				So(sc.updateOrgUserCmd.Role, ShouldEqual, m.ROLE_ADMIN)
 				So(sc.updateOrgUserCmd.Role, ShouldEqual, m.ROLE_ADMIN)
+				So(sc.setUsingOrgCmd.OrgId, ShouldEqual, 1)
 			})
 			})
 		})
 		})
 
 
@@ -139,6 +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)
 			})
 			})
 		})
 		})
 
 
@@ -159,6 +161,7 @@ func TestLdapAuther(t *testing.T) {
 				So(err, ShouldBeNil)
 				So(err, ShouldBeNil)
 				So(sc.removeOrgUserCmd, ShouldBeNil)
 				So(sc.removeOrgUserCmd, ShouldBeNil)
 				So(sc.updateOrgUserCmd, ShouldNotBeNil)
 				So(sc.updateOrgUserCmd, ShouldNotBeNil)
+				So(sc.setUsingOrgCmd.OrgId, ShouldEqual, 1)
 			})
 			})
 		})
 		})
 
 
@@ -178,6 +181,7 @@ func TestLdapAuther(t *testing.T) {
 			Convey("Should take first match, and ignore subsequent matches", func() {
 			Convey("Should take first match, and ignore subsequent matches", func() {
 				So(err, ShouldBeNil)
 				So(err, ShouldBeNil)
 				So(sc.updateOrgUserCmd, ShouldBeNil)
 				So(sc.updateOrgUserCmd, ShouldBeNil)
+				So(sc.setUsingOrgCmd.OrgId, ShouldEqual, 1)
 			})
 			})
 		})
 		})
 
 
@@ -197,6 +201,7 @@ func TestLdapAuther(t *testing.T) {
 			Convey("Should take first match, and ignore subsequent matches", func() {
 			Convey("Should take first match, and ignore subsequent matches", func() {
 				So(err, ShouldBeNil)
 				So(err, ShouldBeNil)
 				So(sc.addOrgUserCmd.Role, ShouldEqual, m.ROLE_ADMIN)
 				So(sc.addOrgUserCmd.Role, ShouldEqual, m.ROLE_ADMIN)
+				So(sc.setUsingOrgCmd.OrgId, ShouldEqual, 1)
 			})
 			})
 		})
 		})
 
 
@@ -340,6 +345,11 @@ func ldapAutherScenario(desc string, fn scenarioFunc) {
 			return nil
 			return nil
 		})
 		})
 
 
+		bus.AddHandler("test", func(cmd *m.SetUsingOrgCommand) error {
+			sc.setUsingOrgCmd = cmd
+			return nil
+		})
+
 		fn(sc)
 		fn(sc)
 	})
 	})
 }
 }
@@ -352,6 +362,7 @@ type scenarioContext struct {
 	updateOrgUserCmd       *m.UpdateOrgUserCommand
 	updateOrgUserCmd       *m.UpdateOrgUserCommand
 	removeOrgUserCmd       *m.RemoveOrgUserCommand
 	removeOrgUserCmd       *m.RemoveOrgUserCommand
 	updateUserCmd          *m.UpdateUserCommand
 	updateUserCmd          *m.UpdateUserCommand
+	setUsingOrgCmd         *m.SetUsingOrgCommand
 }
 }
 
 
 func (sc *scenarioContext) userQueryReturns(user *m.User) {
 func (sc *scenarioContext) userQueryReturns(user *m.User) {

+ 5 - 4
pkg/services/sqlstore/user.go

@@ -295,11 +295,12 @@ func SetUsingOrg(cmd *m.SetUsingOrgCommand) error {
 	}
 	}
 
 
 	return inTransaction(func(sess *DBSession) error {
 	return inTransaction(func(sess *DBSession) error {
-		user := m.User{}
-		sess.Id(cmd.UserId).Get(&user)
+		user := m.User{
+			Id:    cmd.UserId,
+			OrgId: cmd.OrgId,
+		}
 
 
-		user.OrgId = cmd.OrgId
-		_, err := sess.Id(user.Id).Update(&user)
+		_, err := sess.Id(cmd.UserId).Update(&user)
 		return err
 		return err
 	})
 	})
 }
 }