From afa199b79b9a61ab15b4b0a4b8d3d6252a2302f0 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Sun, 25 Aug 2024 06:58:32 +0530 Subject: [PATCH] fix user update cmd --- cli/cmd/user/create.go | 2 +- cli/cmd/user/update.go | 9 ++++++--- cli/functions/user.go | 2 +- logic/auth.go | 6 ++++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cli/cmd/user/create.go b/cli/cmd/user/create.go index 0413213f..16af503d 100644 --- a/cli/cmd/user/create.go +++ b/cli/cmd/user/create.go @@ -42,7 +42,7 @@ func init() { userCreateCmd.Flags().StringVar(&username, "name", "", "Name of the user") userCreateCmd.Flags().StringVar(&password, "password", "", "Password of the user") - userCreateCmd.Flags().StringVarP(&platformID, "platform-id", "r", models.ServiceUser.String(), + userCreateCmd.Flags().StringVarP(&platformID, "platform-role", "r", models.ServiceUser.String(), "Platform Role of the user; run `nmctl roles list` to see available user roles") userCreateCmd.MarkFlagRequired("name") userCreateCmd.MarkFlagRequired("password") diff --git a/cli/cmd/user/update.go b/cli/cmd/user/update.go index f8030761..31e9ccee 100644 --- a/cli/cmd/user/update.go +++ b/cli/cmd/user/update.go @@ -41,10 +41,13 @@ var userUpdateCmd = &cobra.Command{ } func init() { - userUpdateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ?") - userUpdateCmd.Flags().StringVar(&networks, "networks", "", "List of networks the user will access to (comma separated)") - userUpdateCmd.Flags().StringVarP(&platformID, "platform-id", "r", "", + + userUpdateCmd.Flags().StringVar(&password, "password", "", "Password of the user") + userUpdateCmd.Flags().StringVarP(&platformID, "platform-role", "r", "", "Platform Role of the user; run `nmctl roles list` to see available user roles") + userUpdateCmd.PersistentFlags().StringToStringVarP(&networkRoles, "network-roles", "n", nil, + "Mapping of networkID and list of roles user will be part of (comma separated)") + userUpdateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ? (deprecated v0.25.0 onwards)") userUpdateCmd.Flags().StringArrayVarP(&groups, "groups", "g", nil, "List of user groups the user will be part of (comma separated)") rootCmd.AddCommand(userUpdateCmd) } diff --git a/cli/functions/user.go b/cli/functions/user.go index 6f435ea4..e5572bae 100644 --- a/cli/functions/user.go +++ b/cli/functions/user.go @@ -20,7 +20,7 @@ func CreateUser(payload *models.User) *models.User { // UpdateUser - update a user func UpdateUser(payload *models.User) *models.User { - return request[models.User](http.MethodPut, "/api/users/networks/"+payload.UserName, payload) + return request[models.User](http.MethodPut, "/api/users/"+payload.UserName, payload) } // DeleteUser - delete a user diff --git a/logic/auth.go b/logic/auth.go index 3e2623ff..7486d310 100644 --- a/logic/auth.go +++ b/logic/auth.go @@ -297,7 +297,9 @@ func UpdateUser(userchange, user *models.User) (*models.User, error) { } // Reset Gw Access for service users go UpdateUserGwAccess(*user, *userchange) - user.PlatformRoleID = userchange.PlatformRoleID + if userchange.PlatformRoleID != "" { + user.PlatformRoleID = userchange.PlatformRoleID + } user.UserGroups = userchange.UserGroups user.NetworkRoles = userchange.NetworkRoles err := ValidateUser(user) @@ -325,7 +327,7 @@ func ValidateUser(user *models.User) error { // check if role is valid _, err := GetRole(user.PlatformRoleID) if err != nil { - return err + return errors.New("failed to fetch platform role " + user.PlatformRoleID.String()) } v := validator.New() _ = v.RegisterValidation("in_charset", func(fl validator.FieldLevel) bool {