Goit

Simple and lightweight Git web server
git clone https://git.omkov.net/Goit
git clone [email protected]:Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2025-01-04 23:13:02
Commit37a8bcfb6e00c3dc44d0ed18a95349500b35fc00
Parenta0fd9fb9f71026e34ecf9afc3c48236d13a8a370

Implement user default visibility functionality

Diffstat

M res/admin/user_edit.html | 20 ++++++++++++++++++--
M res/repo/edit.html | 12 +++++++++---
M res/user/edit.html | 18 +++++++++++++++++-
M src/admin/users.go | 11 ++++++++---
M src/repo/create.go | 6 +++---
M src/user/edit.go | 9 +++++++--

6 files changed, 62 insertions, 14 deletions

diff --git a/res/admin/user_edit.html b/res/admin/user_edit.html
index 3f83036..0d7631c 100644
--- a/res/admin/user_edit.html
+++ b/res/admin/user_edit.html
@@ -5,11 +5,11 @@
 		<header>{{template "admin/header" .}}</header><hr>
 		<main>
 			<h1>{{.Title}}</h1><hr>
-			<form action="/admin/user/edit?user={{.Form.Id}}" method="post">
+			<form action="/admin/user/edit?user={{.Form.ID}}" method="post">
 				{{.CsrfField}}
 				<table>
 					<tr><td><label for="id">ID</label></td></tr>
-					<tr><td><span>{{.Form.Id}}</span></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>
@@ -18,6 +18,22 @@
 					<tr><td><input type="password" name="password" placeholder="unchanged"></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><label for="visibility">Default Visibility</label></td></tr>
+					<tr>
+						<td>
+							<select name="visibility">
+								<option value="public" {{if eq .Form.DefaultVisibility "public"}}selected{{end}}>
+									Public
+								</option>
+								<option value="private" {{if eq .Form.DefaultVisibility "private"}}selected{{end}}>
+									Private
+								</option>
+								<option value="limited" {{if eq .Form.DefaultVisibility "limited"}}selected{{end}}>
+									Limited
+								</option>
+							</select>
+						</td>
+					</tr>
 					<tr><td>
 						<input type="submit" name="submit" value="Update">
 						<a href="/admin/users" style="color: inherit;">Cancel</a>
diff --git a/res/repo/edit.html b/res/repo/edit.html
index fd43599..36efc3d 100644
--- a/res/repo/edit.html
+++ b/res/repo/edit.html
@@ -33,9 +33,15 @@
 						<td style="text-align: right;"><label for="visibility">Visibility</label></td>
 						<td>
 							<select name="visibility">
-								<option value="public" {{if eq .Edit.Visibility "public"}}selected{{end}}>Public</option>
-								<option value="private" {{if eq .Edit.Visibility "private"}}selected{{end}}>Private</option>
-								<option value="limited" {{if eq .Edit.Visibility "limited"}}selected{{end}}>Limited</option>
+								<option value="public" {{if eq .Edit.Visibility "public"}}selected{{end}}>
+									Public
+								</option>
+								<option value="private" {{if eq .Edit.Visibility "private"}}selected{{end}}>
+									Private
+								</option>
+								<option value="limited" {{if eq .Edit.Visibility "limited"}}selected{{end}}>
+									Limited
+								</option>
 							</select>
 						</td>
 					</tr>
diff --git a/res/user/edit.html b/res/user/edit.html
index 69759ec..c26abbf 100644
--- a/res/user/edit.html
+++ b/res/user/edit.html
@@ -12,6 +12,22 @@
 					<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="visibility">Default Visibility</label></td></tr>
+					<tr>
+						<td>
+							<select name="visibility">
+								<option value="public" {{if eq .Form.DefaultVisibility "public"}}selected{{end}}>
+									Public
+								</option>
+								<option value="private" {{if eq .Form.DefaultVisibility "private"}}selected{{end}}>
+									Private
+								</option>
+								<option value="limited" {{if eq .Form.DefaultVisibility "limited"}}selected{{end}}>
+									Limited
+								</option>
+							</select>
+						</td>
+					</tr>
 					<tr>
 						<td>
 							<input type="submit" name="submit" value="Update">
@@ -42,7 +58,7 @@
 				</table>
 			</form><hr>
 			<table>
-				<tr><td style="text-align: right;"><span>ID:</span></td><td><span>{{.Form.Id}}</span></td></tr>
+				<tr><td style="text-align: right;"><span>ID:</span></td><td><span>{{.Form.ID}}</span></td></tr>
 			</table>
 		</main>
 	</body>
diff --git a/src/admin/users.go b/src/admin/users.go
index a427c31..d09f18c 100644
--- a/src/admin/users.go
+++ b/src/admin/users.go
@@ -154,8 +154,8 @@ func HandleUserEdit(w http.ResponseWriter, r *http.Request) {
 		Title, Message string
 
 		Form struct {
-			Id, Name, FullName string
-			IsAdmin            bool
+			ID, Name, FullName, DefaultVisibility string
+			IsAdmin                               bool
 		}
 
 		CsrfField template.HTML
@@ -165,16 +165,18 @@ func HandleUserEdit(w http.ResponseWriter, r *http.Request) {
 		CsrfField: csrf.TemplateField(r),
 	}
 
-	data.Form.Id = fmt.Sprint(u.Id)
+	data.Form.ID = fmt.Sprint(u.Id)
 	data.Form.Name = u.Name
 	data.Form.FullName = u.FullName
 	data.Form.IsAdmin = u.IsAdmin
+	data.Form.DefaultVisibility = u.DefaultVisibility.String()
 
 	if r.Method == http.MethodPost {
 		data.Form.Name = strings.ToLower(r.FormValue("username"))
 		data.Form.FullName = r.FormValue("fullname")
 		password := r.FormValue("password")
 		data.Form.IsAdmin = r.FormValue("admin") == "true"
+		data.Form.DefaultVisibility = r.FormValue("visibility")
 
 		if data.Form.Name == "" {
 			data.Message = "Username cannot be empty"
@@ -186,9 +188,12 @@ func HandleUserEdit(w http.ResponseWriter, r *http.Request) {
 			return
 		} else if exists && data.Form.Name != u.Name {
 			data.Message = "Username \"" + data.Form.Name + "\" is taken"
+		} else if visibility := goit.VisibilityFromString(data.Form.DefaultVisibility); visibility == -1 {
+			data.Message = "Visibility \"" + data.Form.DefaultVisibility + "\" is invalid"
 		} else {
 			if err := goit.UpdateUser(u.Id, goit.User{
 				Name: data.Form.Name, FullName: data.Form.FullName, IsAdmin: data.Form.IsAdmin,
+				DefaultVisibility: visibility,
 			}); err != nil {
 				log.Println("[/admin/user/edit]", err.Error())
 				goit.HttpError(w, http.StatusInternalServerError)
diff --git a/src/repo/create.go b/src/repo/create.go
index 1ba8cf5..55d88dc 100644
--- a/src/repo/create.go
+++ b/src/repo/create.go
@@ -37,9 +37,9 @@ func HandleCreate(w http.ResponseWriter, r *http.Request) {
 
 		CsrfField template.HTML
 	}{
-		Title: "Repository - Create",
-
-		CsrfField: csrf.TemplateField(r),
+		Title:      "Repository - Create",
+		Visibility: user.DefaultVisibility.String(),
+		CsrfField:  csrf.TemplateField(r),
 	}
 
 	if r.Method == http.MethodPost {
diff --git a/src/user/edit.go b/src/user/edit.go
index 6d2f347..97a0fd0 100644
--- a/src/user/edit.go
+++ b/src/user/edit.go
@@ -31,7 +31,7 @@ func HandleEdit(w http.ResponseWriter, r *http.Request) {
 	data := struct {
 		Title, MessageA, MessageB string
 
-		Form struct{ Id, Name, FullName string }
+		Form struct{ ID, Name, FullName, DefaultVisibility string }
 
 		CsrfField template.HTML
 	}{
@@ -40,14 +40,16 @@ func HandleEdit(w http.ResponseWriter, r *http.Request) {
 		CsrfField: csrf.TemplateField(r),
 	}
 
-	data.Form.Id = fmt.Sprint(user.Id)
+	data.Form.ID = fmt.Sprint(user.Id)
 	data.Form.Name = user.Name
 	data.Form.FullName = user.FullName
+	data.Form.DefaultVisibility = user.DefaultVisibility.String()
 
 	if r.Method == http.MethodPost {
 		if r.FormValue("submit") == "Update" {
 			data.Form.Name = r.FormValue("username")
 			data.Form.FullName = r.FormValue("fullname")
+			data.Form.DefaultVisibility = r.FormValue("visibility")
 
 			if data.Form.Name == "" {
 				data.MessageA = "Username cannot be empty"
@@ -59,8 +61,11 @@ func HandleEdit(w http.ResponseWriter, r *http.Request) {
 				return
 			} else if exists && data.Form.Name != user.Name {
 				data.MessageA = "Username \"" + data.Form.Name + "\" is taken"
+			} else if visibility := goit.VisibilityFromString(data.Form.DefaultVisibility); visibility == -1 {
+				data.MessageA = "Visibility \"" + data.Form.DefaultVisibility + "\" is invalid"
 			} else if err := goit.UpdateUser(user.Id, goit.User{
 				Name: data.Form.Name, FullName: data.Form.FullName, IsAdmin: user.IsAdmin,
+				DefaultVisibility: visibility,
 			}); err != nil {
 				log.Println("[/user/edit]", err.Error())
 				goit.HttpError(w, http.StatusInternalServerError)