From 81acb1a14b404941dcd79c3088f09ee310896690 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Mon, 12 Mar 2018 12:40:44 +0100 Subject: [MM-9720] Platform command to change user email address (#8422) --- cmd/commands/user.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'cmd/commands/user.go') diff --git a/cmd/commands/user.go b/cmd/commands/user.go index dda1c5bfe..a8b7341b2 100644 --- a/cmd/commands/user.go +++ b/cmd/commands/user.go @@ -66,6 +66,15 @@ var ResetUserPasswordCmd = &cobra.Command{ RunE: resetUserPasswordCmdF, } +var updateUserEmailCmd = &cobra.Command{ + Use: "email [user] [new email]", + Short: "Change email of the user", + Long: "Change email of the user.", + Example: ` user email test user@example.com + user activate username`, + RunE: updateUserEmailCmdF, +} + var ResetUserMfaCmd = &cobra.Command{ Use: "resetmfa [users]", Short: "Turn off MFA", @@ -229,6 +238,7 @@ Global Flags: UserCreateCmd, UserInviteCmd, ResetUserPasswordCmd, + updateUserEmailCmd, ResetUserMfaCmd, DeleteUserCmd, DeleteAllUsersCmd, @@ -399,6 +409,36 @@ func resetUserPasswordCmdF(command *cobra.Command, args []string) error { return nil } +func updateUserEmailCmdF(command *cobra.Command, args []string) error { + a, err := cmd.InitDBCommandContextCobra(command) + if err != nil { + return err + } + + newEmail := args[1] + + if !model.IsValidEmail(newEmail) { + return errors.New("Invalid email: '" + newEmail + "'") + } + + if len(args) != 2 { + return errors.New("Expected two arguments. See help text for details.") + } + + user := getUserFromUserArg(a, args[0]) + if user == nil { + return errors.New("Unable to find user '" + args[0] + "'") + } + + user.Email = newEmail + _, errUpdate := a.UpdateUser(user, true) + if err != nil { + return errUpdate + } + + return nil +} + func resetUserMfaCmdF(command *cobra.Command, args []string) error { a, err := cmd.InitDBCommandContextCobra(command) if err != nil { -- cgit v1.2.3-1-g7c22