Exemplo n.º 1
0
func cmdCreateUser() {
	if flagCmdCreateUser {
		if len(flagEmail) == 0 {
			fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
			os.Exit(1)
		}

		if len(flagPassword) == 0 {
			fmt.Fprintln(os.Stderr, "flag needs an argument: -password")
			os.Exit(1)
		}

		var team *model.Team
		user := &model.User{}
		user.Email = flagEmail
		user.Password = flagPassword

		if len(flagUsername) == 0 {
			splits := strings.Split(strings.Replace(flagEmail, "@", " ", -1), " ")
			user.Username = splits[0]
		} else {
			user.Username = flagUsername
		}

		if len(flagTeamName) > 0 {
			if result := <-app.Srv.Store.Team().GetByName(flagTeamName); result.Err != nil {
				l4g.Error("%v", result.Err)
				flushLogAndExit(1)
			} else {
				team = result.Data.(*model.Team)
			}
		}

		ruser, err := app.CreateUser(user)
		if err != nil {
			if err.Id != "store.sql_user.save.email_exists.app_error" {
				l4g.Error("%v", err)
				flushLogAndExit(1)
			}
		}

		if team != nil {
			err = app.JoinUserToTeam(team, ruser)
			if err != nil {
				l4g.Error("%v", err)
				flushLogAndExit(1)
			}
		}

		os.Exit(0)
	}
}
Exemplo n.º 2
0
func userCreateCmdF(cmd *cobra.Command, args []string) error {
	initDBCommandContextCobra(cmd)
	username, erru := cmd.Flags().GetString("username")
	if erru != nil || username == "" {
		return errors.New("Username is required")
	}
	email, erre := cmd.Flags().GetString("email")
	if erre != nil || email == "" {
		return errors.New("Email is required")
	}
	password, errp := cmd.Flags().GetString("password")
	if errp != nil || password == "" {
		return errors.New("Password is required")
	}
	nickname, _ := cmd.Flags().GetString("nickname")
	firstname, _ := cmd.Flags().GetString("firstname")
	lastname, _ := cmd.Flags().GetString("lastname")
	locale, _ := cmd.Flags().GetString("locale")
	system_admin, _ := cmd.Flags().GetBool("system_admin")

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

	ruser, err := app.CreateUser(user)
	if err != nil {
		return errors.New("Unable to create user. Error: " + err.Error())
	}

	if system_admin {
		api.UpdateUserRoles(ruser, "system_user system_admin")
	}

	CommandPrettyPrintln("Created User")

	return nil
}