Add user status change command
continuous-integration/drone/pr Build is failing
Details
continuous-integration/drone/pr Build is failing
Details
This commit is contained in:
parent
594baa921b
commit
31c61d7feb
|
@ -38,6 +38,8 @@ var (
|
|||
userFlagPassword string
|
||||
userFlagAvatar = "default"
|
||||
userFlagResetPasswordDirectly bool
|
||||
userFlagEnableUser bool
|
||||
userFlagDisableUser bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -58,6 +60,10 @@ func init() {
|
|||
userResetPasswordCmd.Flags().BoolVarP(&userFlagResetPasswordDirectly, "direct", "d", false, "If provided, reset the password directly instead of sending the user a reset mail.")
|
||||
userResetPasswordCmd.Flags().StringVarP(&userFlagPassword, "password", "p", "", "The new password of the user. Only used in combination with --direct. You will be asked to enter it if not provided through the flag.")
|
||||
|
||||
// Change status flags
|
||||
userChangeEnabledCmd.Flags().BoolVarP(&userFlagDisableUser, "disable", "d", false, "Disable the user.")
|
||||
userChangeEnabledCmd.Flags().BoolVarP(&userFlagEnableUser, "enable", "e", false, "Enable the user.")
|
||||
|
||||
userCmd.AddCommand(userListCmd, userCreateCmd, userUpdateCmd, userResetPasswordCmd, userChangeEnabledCmd)
|
||||
rootCmd.AddCommand(userCmd)
|
||||
}
|
||||
|
@ -217,12 +223,27 @@ var userResetPasswordCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
var userChangeEnabledCmd = &cobra.Command{
|
||||
Use: "toggle-status",
|
||||
Short: "Enable or disable a user.",
|
||||
Use: "change-status [user id]",
|
||||
Short: "Enable or disable a user. Will toggle the current status if no flag (--enable or --disable) is provided.",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
initialize.FullInit()
|
||||
},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Flag to either enable or disable
|
||||
u := getUserFromArg(args[0])
|
||||
|
||||
if userFlagEnableUser {
|
||||
u.IsActive = true
|
||||
} else if userFlagDisableUser {
|
||||
u.IsActive = false
|
||||
} else {
|
||||
u.IsActive = !u.IsActive
|
||||
}
|
||||
_, err := user.UpdateUser(u)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not enable the user")
|
||||
}
|
||||
|
||||
fmt.Printf("User status successfully changed, user is now active: %t.\n", u.IsActive)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -315,7 +315,7 @@ func hashPassword(password string) (string, error) {
|
|||
func UpdateUser(user *User) (updatedUser *User, err error) {
|
||||
|
||||
// Check if it exists
|
||||
theUser, err := GetUserByID(user.ID)
|
||||
theUser, err := GetUserWithEmail(&User{ID: user.ID})
|
||||
if err != nil {
|
||||
return &User{}, err
|
||||
}
|
||||
|
@ -348,8 +348,6 @@ func UpdateUser(user *User) (updatedUser *User, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
user.Password = theUser.Password // set the password to the one in the database to not accedently resetting it
|
||||
|
||||
// Validate the avatar type
|
||||
if user.AvatarProvider != "" {
|
||||
if user.AvatarProvider != "default" &&
|
||||
|
@ -361,7 +359,10 @@ func UpdateUser(user *User) (updatedUser *User, err error) {
|
|||
}
|
||||
|
||||
// Update it
|
||||
_, err = x.ID(user.ID).Update(user)
|
||||
_, err = x.
|
||||
ID(user.ID).
|
||||
Cols("username", "email", "avatar_provider", "is_active").
|
||||
Update(user)
|
||||
if err != nil {
|
||||
return &User{}, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue