Author | Jakob Wakeling <[email protected]> |
Date | 2023-11-22 10:38:17 |
Commit | e3bf6a71be5c3e7af5d6f5fa12c3f89fa55324cd |
Parent | 463cf58b6afaa428962644e198ac283f0d8ddd9c |
Update admin create user page
Diffstat
M | res/admin/index.html | | | 1 | + |
M | res/admin/repo_edit.html | | | 1 | + |
M | res/admin/repos.html | | | 1 | + |
M | res/admin/user_create.html | | | 42 | +++++++++++++++++++++++++++++++----------- |
M | res/admin/user_edit.html | | | 5 | +++-- |
M | res/admin/users.html | | | 1 | + |
M | src/admin/users.go | | | 35 | +++++++++++++++++++++++------------ |
7 files changed, 61 insertions, 25 deletions
diff --git a/res/admin/index.html b/res/admin/index.html index f42f931..31ec05c 100644 --- a/res/admin/index.html +++ b/res/admin/index.html @@ -11,6 +11,7 @@ <tr><td> <a href="/admin/users">Users</a> | <a href="/admin/repos">Repositories</a> + | <a href="/admin/user/create">Create User</a> </td></tr> </table> </body> diff --git a/res/admin/repo_edit.html b/res/admin/repo_edit.html index 76ddf3e..0a9a132 100644 --- a/res/admin/repo_edit.html +++ b/res/admin/repo_edit.html @@ -12,6 +12,7 @@ <tr><td> <a href="/admin/users">Users</a> | <a href="/admin/repos">Repositories</a> + | <a href="/admin/user/create">Create User</a> </td></tr> </table> </header><hr> diff --git a/res/admin/repos.html b/res/admin/repos.html index f9be559..b8f66eb 100644 --- a/res/admin/repos.html +++ b/res/admin/repos.html @@ -12,6 +12,7 @@ <tr><td> <a href="/admin/users">Users</a> | <a href="/admin/repos">Repositories</a> + | <a href="/admin/user/create">Create User</a> </td></tr> </table> </header><hr> diff --git a/res/admin/user_create.html b/res/admin/user_create.html index 7edeb6e..7909270 100644 --- a/res/admin/user_create.html +++ b/res/admin/user_create.html @@ -1,19 +1,39 @@ <!DOCTYPE html> <head lang="en-GB">{{template "base/head" .}}</head> <body> + <header> + <table> + <tr> + <td rowspan="2"> + <a href="/"><img src="/static/favicon.png" style="max-height: 24px"></a> + </td> + <td><h1>{{.Title}}</h1></td> + </tr> + <tr><td> + <a href="/admin/users">Users</a> + | <a href="/admin/repos">Repositories</a> + | <a href="/admin/user/create">Create User</a> + </td></tr> + </table> + </header><hr> <main> - <h1>{{.Title}}</h1> + <h1>{{.Title}}</h1><hr> <form action="/admin/user/create" method="post"> - <label for="username">Username:</label> - <input type="text" name="username"><br> - <label for="fullname">Full Name:</label> - <input type="text" name="fullname"><br> - <label for="password">Password:</label> - <input type="password" name="password"><br> - <label for="admin">Admin:</label> - <input type="checkbox" name="admin" value="true"><br> - <input type="submit" value="Create"> + <table> + <tr><td><label for="username">Username</label></td></tr> + <tr><td><input type="text" name="username" value="{{.Form.Name}}" spellcheck="false"></td></tr> + <tr><td><label for="fullname">Full Name</label></td></tr> + <tr><td><input type="text" name="fullname" value="{{.Form.FullName}}" spellcheck="false"></td></tr> + <tr><td><label for="password">Password</label></td></tr> + <tr><td><input type="password" name="password"></td></tr> + <tr><td><label for="admin">Admin</label></td></tr> + <tr><td><input type="checkbox" name="admin" value="true" {{if .Form.IsAdmin}}checked{{end}}></td></tr> + <tr><td> + <input type="submit" name="submit" value="Create"> + <a href="/admin/users" style="color: inherit;">Cancel</a> + </td></tr> + <tr><td><span style="color: #AA0000">{{.Message}}</span></td></tr> + </table> </form> - <span>{{.Message}}</span> </main> </body> diff --git a/res/admin/user_edit.html b/res/admin/user_edit.html index 3424289..4f4f73f 100644 --- a/res/admin/user_edit.html +++ b/res/admin/user_edit.html @@ -12,6 +12,7 @@ <tr><td> <a href="/admin/users">Users</a> | <a href="/admin/repos">Repositories</a> + | <a href="/admin/user/create">Create User</a> </td></tr> </table> </header><hr> @@ -20,7 +21,7 @@ <form action="/admin/user/edit?user={{.Form.Id}}" method="post"> <table> <tr><td><label for="id">ID</label></td></tr> - <tr><td><input type="text" name="id" value="{{.Form.Id}}" spellcheck="false" disabled></td></tr> + <tr><td><span>{{.Form.Id}}</span></td></tr> <tr><td><label for="username">Username</label></td></tr> <tr><td><input type="text" name="username" value="{{.Form.Name}}" spellcheck="false"></td></tr> <tr><td><label for="fullname">Full Name</label></td></tr> @@ -32,8 +33,8 @@ <tr><td> <input type="submit" name="submit" value="Update"> <a href="/admin/users" style="color: inherit;">Cancel</a> - <span style="color: #AA0000">{{.Message}}</span> </td></tr> + <tr><td><span style="color: #AA0000">{{.Message}}</span></td></tr> </table> </form> </main> diff --git a/res/admin/users.html b/res/admin/users.html index 7839d2e..29ae963 100644 --- a/res/admin/users.html +++ b/res/admin/users.html @@ -12,6 +12,7 @@ <tr><td> <a href="/admin/users">Users</a> | <a href="/admin/repos">Repositories</a> + | <a href="/admin/user/create">Create User</a> </td></tr> </table> </header><hr> diff --git a/src/admin/users.go b/src/admin/users.go index eddad46..edf1edd 100644 --- a/src/admin/users.go +++ b/src/admin/users.go @@ -62,37 +62,48 @@ func HandleUserCreate(w http.ResponseWriter, r *http.Request) { return } - data := struct{ Title, Message string }{"Admin - Create User", ""} + data := struct { + Title, Message string + + Form struct { + Name, FullName string + IsAdmin bool + } + }{ + Title: "Admin - Create User", + } if r.Method == http.MethodPost { - username := strings.ToLower(r.FormValue("username")) - fullname := r.FormValue("fullname") + data.Form.Name = strings.ToLower(r.FormValue("username")) + data.Form.FullName = r.FormValue("fullname") password := r.FormValue("password") - isAdmin := r.FormValue("admin") == "true" + data.Form.IsAdmin = r.FormValue("admin") == "true" - if username == "" { + if data.Form.Name == "" { data.Message = "Username cannot be empty" - } else if slices.Contains(goit.Reserved, username) { - data.Message = "Username \"" + username + "\" is reserved" - } else if exists, err := goit.UserExists(username); err != nil { + } else if slices.Contains(goit.Reserved, data.Form.Name) { + data.Message = "Username \"" + data.Form.Name + "\" is reserved" + } else if exists, err := goit.UserExists(data.Form.Name); err != nil { log.Println("[/admin/user/create]", err.Error()) goit.HttpError(w, http.StatusInternalServerError) return } else if exists { - data.Message = "Username \"" + username + "\" is taken" + data.Message = "Username \"" + data.Form.Name + "\" is taken" } else if salt, err := goit.Salt(); err != nil { log.Println("[/admin/user/create]", err.Error()) goit.HttpError(w, http.StatusInternalServerError) return } else if err := goit.CreateUser(goit.User{ - Name: username, FullName: fullname, Pass: goit.Hash(password, salt), PassAlgo: "argon2", Salt: salt, - IsAdmin: isAdmin, + Name: data.Form.Name, FullName: data.Form.FullName, Pass: goit.Hash(password, salt), PassAlgo: "argon2", + Salt: salt, IsAdmin: data.Form.IsAdmin, }); err != nil { log.Println("[/admin/user/create]", err.Error()) goit.HttpError(w, http.StatusInternalServerError) return } else { - data.Message = "User \"" + username + "\" created successfully" + // data.Message = "User \"" + data.Form.Name + "\" created successfully" + http.Redirect(w, r, "/admin/users", http.StatusFound) + return } }