// https://github.com/gigforks/go-gogs-client/wiki/Administration-Users#create-a-new-user func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { u := &models.User{ Name: form.Username, Email: form.Email, Passwd: form.Password, IsActive: true, LoginType: models.LOGIN_PLAIN, } parseLoginSource(ctx, u, form.SourceID, form.LoginName) if ctx.Written() { return } if err := models.CreateUser(u); err != nil { if models.IsErrUserAlreadyExist(err) || models.IsErrEmailAlreadyUsed(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "CreateUser", err) } return } log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) // Send e-mail notification. if form.SendNotify && setting.MailService != nil { mailer.SendRegisterNotifyMail(ctx.Context.Context, u) } ctx.JSON(201, convert.ToUser(u)) }
func NewUserPost(ctx *context.Context, form auth.AdminCrateUserForm) { ctx.Data["Title"] = ctx.Tr("admin.users.new_account") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminUsers"] = true sources, err := models.LoginSources() if err != nil { ctx.Handle(500, "LoginSources", err) return } ctx.Data["Sources"] = sources ctx.Data["CanSendEmail"] = setting.MailService != nil if ctx.HasError() { ctx.HTML(200, USER_NEW) return } u := &models.User{ Name: form.UserName, Email: form.Email, Passwd: form.Password, IsActive: true, LoginType: models.LOGIN_PLAIN, } if len(form.LoginType) > 0 { fields := strings.Split(form.LoginType, "-") if len(fields) == 2 { u.LoginType = models.LoginType(com.StrTo(fields[0]).MustInt()) u.LoginSource = com.StrTo(fields[1]).MustInt64() u.LoginName = form.LoginName } } if err := models.CreateUser(u); err != nil { switch { case models.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form) case models.IsErrEmailAlreadyUsed(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form) case models.IsErrNameReserved(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &form) case models.IsErrNamePatternNotAllowed(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &form) default: ctx.Handle(500, "CreateUser", err) } return } log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) // Send e-mail notification. if form.SendNotify && setting.MailService != nil { mailer.SendRegisterNotifyMail(ctx.Context, u) } ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name)) ctx.Redirect(setting.AppSubUrl + "/admin/users/" + com.ToStr(u.Id)) }