summaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2018-03-12 12:40:44 +0100
committerJoram Wilander <jwawilander@gmail.com>2018-03-12 07:40:44 -0400
commit81acb1a14b404941dcd79c3088f09ee310896690 (patch)
treea58c439a7f3bb81ae78a16a19b4d5e58da4b4188 /cmd
parentcb1b8fc3337f2e7611b342d077573312a62d5619 (diff)
downloadchat-81acb1a14b404941dcd79c3088f09ee310896690.tar.gz
chat-81acb1a14b404941dcd79c3088f09ee310896690.tar.bz2
chat-81acb1a14b404941dcd79c3088f09ee310896690.zip
[MM-9720] Platform command to change user email address (#8422)
Diffstat (limited to 'cmd')
-rw-r--r--cmd/commands/user.go40
-rw-r--r--cmd/commands/user_test.go28
2 files changed, 68 insertions, 0 deletions
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 {
diff --git a/cmd/commands/user_test.go b/cmd/commands/user_test.go
index 960ac3878..a1081c5d3 100644
--- a/cmd/commands/user_test.go
+++ b/cmd/commands/user_test.go
@@ -9,6 +9,7 @@ import (
"github.com/mattermost/mattermost-server/api"
"github.com/mattermost/mattermost-server/cmd"
"github.com/mattermost/mattermost-server/model"
+ "github.com/stretchr/testify/require"
)
func TestCreateUserWithTeam(t *testing.T) {
@@ -80,3 +81,30 @@ func TestMakeUserActiveAndInactive(t *testing.T) {
// activate the inactive user
cmd.CheckCommand(t, "user", "activate", th.BasicUser.Email)
}
+
+func TestChangeUserEmail(t *testing.T) {
+ th := api.Setup().InitBasic()
+ defer th.TearDown()
+
+ newEmail := model.NewId() + "@mattermost-test.com"
+
+ cmd.CheckCommand(t, "user", "email", th.BasicUser.Username, newEmail)
+ if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err == nil {
+ t.Fatal("should've updated to the new email")
+ }
+ if result := <-th.App.Srv.Store.User().GetByEmail(newEmail); result.Err != nil {
+ t.Fatal()
+ } else {
+ user := result.Data.(*model.User)
+ if user.Email != newEmail {
+ t.Fatal("should've updated to the new email")
+ }
+ }
+
+ // should fail because using an invalid email
+ require.Error(t, cmd.RunCommand(t, "user", "email", th.BasicUser.Username, "wrong$email.com"))
+
+ // should fail because user not found
+ require.Error(t, cmd.RunCommand(t, "user", "email", "invalidUser", newEmail))
+
+}