(c *context.Context, f form.AdminCrateUser)
| 55 | } |
| 56 | |
| 57 | func NewUserPost(c *context.Context, f form.AdminCrateUser) { |
| 58 | c.Data["Title"] = c.Tr("admin.users.new_account") |
| 59 | c.Data["PageIsAdmin"] = true |
| 60 | c.Data["PageIsAdminUsers"] = true |
| 61 | |
| 62 | sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{}) |
| 63 | if err != nil { |
| 64 | c.Error(err, "list login sources") |
| 65 | return |
| 66 | } |
| 67 | c.Data["Sources"] = sources |
| 68 | |
| 69 | c.Data["CanSendEmail"] = conf.Email.Enabled |
| 70 | |
| 71 | if c.HasError() { |
| 72 | c.HTML(http.StatusBadRequest, tmplAdminUserNew) |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | createUserOpts := database.CreateUserOptions{ |
| 77 | Password: f.Password, |
| 78 | Activated: true, |
| 79 | } |
| 80 | if len(f.LoginType) > 0 { |
| 81 | fields := strings.Split(f.LoginType, "-") |
| 82 | if len(fields) == 2 { |
| 83 | createUserOpts.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64) |
| 84 | createUserOpts.LoginName = f.LoginName |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | user, err := database.Handle.Users().Create(c.Req.Context(), f.UserName, f.Email, createUserOpts) |
| 89 | if err != nil { |
| 90 | switch { |
| 91 | case database.IsErrUserAlreadyExist(err): |
| 92 | c.Data["Err_UserName"] = true |
| 93 | c.RenderWithErr(c.Tr("form.username_been_taken"), http.StatusUnprocessableEntity, tmplAdminUserNew, &f) |
| 94 | case database.IsErrEmailAlreadyUsed(err): |
| 95 | c.Data["Err_Email"] = true |
| 96 | c.RenderWithErr(c.Tr("form.email_been_used"), http.StatusUnprocessableEntity, tmplAdminUserNew, &f) |
| 97 | case database.IsErrNameNotAllowed(err): |
| 98 | c.Data["Err_UserName"] = true |
| 99 | c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, tmplAdminUserNew, &f) |
| 100 | default: |
| 101 | c.Error(err, "create user") |
| 102 | } |
| 103 | return |
| 104 | } |
| 105 | log.Trace("Account %q created by admin %q", user.Name, c.User.Name) |
| 106 | |
| 107 | // Send email notification. |
| 108 | if f.SendNotify && conf.Email.Enabled { |
| 109 | email.SendRegisterNotifyMail(c.Context, database.NewMailerUser(user)) |
| 110 | } |
| 111 | |
| 112 | c.Flash.Success(c.Tr("admin.users.new_success", user.Name)) |
| 113 | c.Redirect(conf.Server.Subpath + "/admin/users/" + strconv.FormatInt(user.ID, 10)) |
| 114 | } |
nothing calls this directly
no test coverage detected