Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
[MM-29523] New flag for user creation command (#257)
Browse files Browse the repository at this point in the history
* New flag for user creation command

This new flag will mark the user email as verified without having
too do it after the creation
  • Loading branch information
ethervoid authored Oct 27, 2020
1 parent d846056 commit 60b3a44
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 18 deletions.
37 changes: 25 additions & 12 deletions commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ var UserDeactivateCmd = &cobra.Command{
}

var UserCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a user",
Long: "Create a user",
Example: ` user create --email [email protected] --username userexample --password Password1`,
RunE: withClient(userCreateCmdF),
Use: "create",
Short: "Create a user",
Long: "Create a user",
Example: ` # You can create a user
$ mmctl user create --email [email protected] --username userexample --password Password1
# You can define optional fields like first name, last name and nick name too
$ mmctl user create --email [email protected] --username userexample --password Password1 --firstname User --lastname Example --nickname userex
# Also you can create the user as system administrator
$ mmctl user create --email [email protected] --username userexample --password Password1 --system-admin
# Finally you can verify user on creation if you have enough permissions
$ mmctl user create --email [email protected] --username userexample --password Password1 --system-admin --email-verified`,
RunE: withClient(userCreateCmdF),
}

var UserInviteCmd = &cobra.Command{
Expand Down Expand Up @@ -221,6 +231,7 @@ func init() {
UserCreateCmd.Flags().String("lastname", "", "Optional. The last name for the new user account")
UserCreateCmd.Flags().String("locale", "", "Optional. The locale (ex: en, fr) for the new user account")
UserCreateCmd.Flags().Bool("system_admin", false, "Optional. If supplied, the new user will be a system administrator. Defaults to false")
UserCreateCmd.Flags().Bool("email_verified", false, "Optional. If supplied, the new user will have the email verified. Defaults to false")

DeleteUsersCmd.Flags().Bool("confirm", false, "Confirm you really want to delete the user and a DB backup has been performed")
DeleteAllUsersCmd.Flags().Bool("confirm", false, "Confirm you really want to delete the user and a DB backup has been performed")
Expand Down Expand Up @@ -364,15 +375,17 @@ func userCreateCmdF(c client.Client, cmd *cobra.Command, args []string) error {
lastname, _ := cmd.Flags().GetString("lastname")
locale, _ := cmd.Flags().GetString("locale")
systemAdmin, _ := cmd.Flags().GetBool("system_admin")
emailVerified, _ := cmd.Flags().GetBool("email_verified")

user := &model.User{
Username: username,
Email: email,
Password: password,
Nickname: nickname,
FirstName: firstname,
LastName: lastname,
Locale: locale,
Username: username,
Email: email,
Password: password,
Nickname: nickname,
FirstName: firstname,
LastName: lastname,
Locale: locale,
EmailVerified: emailVerified,
}

ruser, response := c.CreateUser(user)
Expand Down
20 changes: 20 additions & 0 deletions commands/user_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,26 @@ func (s *MmctlE2ETestSuite) TestCreateUserCmd() {
s.Equal(username, user.Username)
s.Equal(false, user.IsSystemAdmin())
})

s.RunForSystemAdminAndLocal("Should create new user with the email already verified only for admin or local mode", func(c client.Client) {
printer.Clean()
email := s.th.GenerateTestEmail()
username := model.NewId()
cmd := &cobra.Command{}
cmd.Flags().String("username", username, "")
cmd.Flags().String("email", email, "")
cmd.Flags().String("password", "somepass", "")
cmd.Flags().Bool("email_verified", true, "")

err := userCreateCmdF(c, cmd, []string{})
s.Require().Nil(err)
s.Len(printer.GetLines(), 1)
user, err := s.th.App.GetUserByEmail(email)
s.Require().Nil(err)
s.Equal(username, user.Username)
s.Equal(false, user.IsSystemAdmin())
s.Equal(true, user.EmailVerified)
})
}

func (s *MmctlE2ETestSuite) TestUpdateUserEmailCmd() {
Expand Down
13 changes: 12 additions & 1 deletion docs/mmctl_user_create.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ Examples

::

user create --email [email protected] --username userexample --password Password1
# You can create a user
$ mmctl user create --email [email protected] --username userexample --password Password1

# You can define optional fields like first name, last name and nick name too
$ mmctl user create --email [email protected] --username userexample --password Password1 --firstname User --lastname Example --nickname userex

# Also you can create the user as system administrator
$ mmctl user create --email [email protected] --username userexample --password Password1 --system-admin

# Finally you can verify user on creation if you have enough permissions
$ mmctl user create --email [email protected] --username userexample --password Password1 --system-admin --email-verified

Options
~~~~~~~

::

--email string Required. The email address for the new user account
--email_verified Optional. If supplied, the new user will have the email verified. Defaults to false
--firstname string Optional. The first name for the new user account
-h, --help help for create
--lastname string Optional. The last name for the new user account
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/golang/mock v1.3.1
github.com/magefile/mage v1.9.0
github.com/mattermost/gorp v2.0.1-0.20200527092429-d62b7b9cadfc+incompatible // indirect
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20201026144558-8f895908fcb2
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20201027094120-f5531a5a437f
github.com/pkg/errors v0.9.1
github.com/poy/onpar v1.0.0 // indirect
github.com/spf13/cobra v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d h1:2DV7VIlEv6J5R5o
github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d/go.mod h1:HLbgMEI5K131jpxGazJ97AxfPDt31osq36YS1oxFQPQ=
github.com/mattermost/logr v1.0.13 h1:6F/fM3csvH6Oy5sUpJuW7YyZSzZZAhJm5VcgKMxA2P8=
github.com/mattermost/logr v1.0.13/go.mod h1:Mt4DPu1NXMe6JxPdwCC0XBoxXmN9eXOIRPoZarU2PXs=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20201026144558-8f895908fcb2 h1:iAL6uBbWo+paFACcUSEqRThoqUwS53yUgltTkSu+zwk=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20201026144558-8f895908fcb2/go.mod h1:qqnleL7miOBUEpTtwLpzc51OkPf24KebilSsmTRrmxg=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20201027094120-f5531a5a437f h1:91FDcj5haakBgSf5TuEVUvZfXKgh0qDRF+451xnK9es=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20201027094120-f5531a5a437f/go.mod h1:qqnleL7miOBUEpTtwLpzc51OkPf24KebilSsmTRrmxg=
github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0 h1:G9tL6JXRBMzjuD1kkBtcnd42kUiT6QDwxfFYu7adM6o=
github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0/go.mod h1:nV5bfVpT//+B1RPD2JvRnxbkLmJEYXmRaaVl15fsXjs=
github.com/mattermost/viper v1.0.4 h1:cMYOz4PhguscGSPxrSokUtib5HrG4gCpiUh27wyA3d0=
Expand Down
71 changes: 71 additions & 0 deletions vendor/github.com/mattermost/mattermost-server/v5/api4/cloud.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/mattermost/mattermost-server/v5/api4/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions vendor/github.com/mattermost/mattermost-server/v5/app/cloud.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions vendor/github.com/mattermost/mattermost-server/v5/app/email.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 60b3a44

Please sign in to comment.