소스 검색

feat(go): add display name field for user;

Vishal Dalwadi 4 달 전
부모
커밋
0284fd9705
6개의 변경된 파일16개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 0
      logic/auth.go
  2. 1 0
      models/user_mgmt.go
  3. 2 0
      pro/auth/sync.go
  4. 3 1
      pro/idp/azure/azure.go
  5. 2 1
      pro/idp/google/google.go
  6. 1 0
      pro/idp/idp.go

+ 7 - 0
logic/auth.go

@@ -304,6 +304,13 @@ func UpdateUser(userchange, user *models.User) (*models.User, error) {
 	if err := IsNetworkRolesValid(userchange.NetworkRoles); err != nil {
 		return userchange, errors.New("invalid network roles: " + err.Error())
 	}
+
+	if user.ExternalIdentityProviderID != "" &&
+		user.DisplayName != userchange.DisplayName {
+		return userchange, errors.New("display name cannot be updated for external user")
+	}
+	user.DisplayName = userchange.DisplayName
+
 	// Reset Gw Access for service users
 	go UpdateUserGwAccess(*user, *userchange)
 	if userchange.PlatformRoleID != "" {

+ 1 - 0
models/user_mgmt.go

@@ -156,6 +156,7 @@ type UserGroup struct {
 type User struct {
 	UserName                   string                                `json:"username" bson:"username" validate:"min=3,in_charset|email"`
 	ExternalIdentityProviderID string                                `json:"external_identity_provider_id"`
+	DisplayName                string                                `json:"display_name"`
 	AccountDisabled            bool                                  `json:"account_disabled"`
 	Password                   string                                `json:"password" bson:"password" validate:"required,min=5"`
 	IsAdmin                    bool                                  `json:"isadmin" bson:"isadmin"` // deprecated

+ 2 - 0
pro/auth/sync.go

@@ -114,6 +114,7 @@ func syncUsers(idpUsers []idp.User) error {
 			err = logic.CreateUser(&models.User{
 				UserName:                   user.Username,
 				ExternalIdentityProviderID: user.ID,
+				DisplayName:                user.DisplayName,
 				AccountDisabled:            user.AccountDisabled,
 				Password:                   password,
 				AuthType:                   models.OAuth,
@@ -124,6 +125,7 @@ func syncUsers(idpUsers []idp.User) error {
 			}
 		} else {
 			if dbUser.AccountDisabled != user.AccountDisabled {
+				dbUser.DisplayName = user.DisplayName
 				dbUser.AccountDisabled = user.AccountDisabled
 				err = logic.UpsertUser(dbUser)
 				if err != nil {

+ 3 - 1
pro/idp/azure/azure.go

@@ -33,7 +33,7 @@ func (a *Client) GetUsers() ([]idp.User, error) {
 	}
 
 	client := &http.Client{}
-	req, err := http.NewRequest("GET", "https://graph.microsoft.com/v1.0/users?$select=id,userPrincipalName,accountEnabled", nil)
+	req, err := http.NewRequest("GET", "https://graph.microsoft.com/v1.0/users?$select=id,userPrincipalName,displayName,accountEnabled", nil)
 	if err != nil {
 		return nil, err
 	}
@@ -60,6 +60,7 @@ func (a *Client) GetUsers() ([]idp.User, error) {
 		retval[i] = idp.User{
 			ID:              user.Id,
 			Username:        user.UserPrincipalName,
+			DisplayName:     user.DisplayName,
 			AccountDisabled: user.AccountEnabled,
 		}
 	}
@@ -148,6 +149,7 @@ type getUsersResponse struct {
 	Value        []struct {
 		Id                string `json:"id"`
 		UserPrincipalName string `json:"userPrincipalName"`
+		DisplayName       string `json:"displayName"`
 		AccountEnabled    bool   `json:"accountEnabled"`
 	} `json:"value"`
 }

+ 2 - 1
pro/idp/google/google.go

@@ -63,12 +63,13 @@ func (g *Client) GetUsers() ([]idp.User, error) {
 	var retval []idp.User
 	err := g.service.Users.List().
 		Customer("my_customer").
-		Fields("users(id,primaryEmail,suspended)", "nextPageToken").
+		Fields("users(id,primaryEmail,name,suspended)", "nextPageToken").
 		Pages(context.TODO(), func(users *admindir.Users) error {
 			for _, user := range users.Users {
 				retval = append(retval, idp.User{
 					ID:              user.Id,
 					Username:        user.PrimaryEmail,
+					DisplayName:     user.Name.FullName,
 					AccountDisabled: user.Suspended,
 				})
 			}

+ 1 - 0
pro/idp/idp.go

@@ -8,6 +8,7 @@ type Client interface {
 type User struct {
 	ID              string
 	Username        string
+	DisplayName     string
 	AccountDisabled bool
 }