Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/user/edit.go (110 lines, 3.1 KiB) -rw-r--r-- file download

c2c5250 Jakob Wakeling 2023-12-12 09:13:37
0
// Copyright (C) 2023, Jakob Wakeling
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
1
// All rights reserved.
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
2
d0994fe Jakob Wakeling 2023-09-07 22:53:16
3
package user
d0994fe Jakob Wakeling 2023-09-07 22:53:16
4
d0994fe Jakob Wakeling 2023-09-07 22:53:16
5
import (
68784b6 Jakob Wakeling 2023-09-10 17:23:55
6
	"bytes"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
7
	"fmt"
b804701 Jakob Wakeling 2023-11-27 23:52:28
8
	"html/template"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
9
	"log"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
10
	"net/http"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
11
	"slices"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
12
945fd71 Jakob Wakeling 2023-11-05 15:39:31
13
	"github.com/Jamozed/Goit/src/goit"
b804701 Jakob Wakeling 2023-11-27 23:52:28
14
	"github.com/gorilla/csrf"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
15
)
d0994fe Jakob Wakeling 2023-09-07 22:53:16
16
d0994fe Jakob Wakeling 2023-09-07 22:53:16
17
func HandleEdit(w http.ResponseWriter, r *http.Request) {
473d02c Jakob Wakeling 2023-11-28 00:01:06
18
	auth, user, err := goit.Auth(w, r, true)
d0994fe Jakob Wakeling 2023-09-07 22:53:16
19
	if err != nil {
473d02c Jakob Wakeling 2023-11-28 00:01:06
20
		log.Println("[admin]", err.Error())
d0994fe Jakob Wakeling 2023-09-07 22:53:16
21
		goit.HttpError(w, http.StatusInternalServerError)
473d02c Jakob Wakeling 2023-11-28 00:01:06
22
	}
473d02c Jakob Wakeling 2023-11-28 00:01:06
23
473d02c Jakob Wakeling 2023-11-28 00:01:06
24
	if !auth {
473d02c Jakob Wakeling 2023-11-28 00:01:06
25
		goit.HttpError(w, http.StatusUnauthorized)
d0994fe Jakob Wakeling 2023-09-07 22:53:16
26
		return
d0994fe Jakob Wakeling 2023-09-07 22:53:16
27
	}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
28
d0994fe Jakob Wakeling 2023-09-07 22:53:16
29
	data := struct {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
30
		Title, MessageA, MessageB string
d0994fe Jakob Wakeling 2023-09-07 22:53:16
31
d0994fe Jakob Wakeling 2023-09-07 22:53:16
32
		Form struct{ Id, Name, FullName string }
b804701 Jakob Wakeling 2023-11-27 23:52:28
33
b804701 Jakob Wakeling 2023-11-27 23:52:28
34
		CsrfField template.HTML
d0994fe Jakob Wakeling 2023-09-07 22:53:16
35
	}{
d0994fe Jakob Wakeling 2023-09-07 22:53:16
36
		Title: "User - Edit",
b804701 Jakob Wakeling 2023-11-27 23:52:28
37
b804701 Jakob Wakeling 2023-11-27 23:52:28
38
		CsrfField: csrf.TemplateField(r),
d0994fe Jakob Wakeling 2023-09-07 22:53:16
39
	}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
40
d0994fe Jakob Wakeling 2023-09-07 22:53:16
41
	data.Form.Id = fmt.Sprint(user.Id)
d0994fe Jakob Wakeling 2023-09-07 22:53:16
42
	data.Form.Name = user.Name
d0994fe Jakob Wakeling 2023-09-07 22:53:16
43
	data.Form.FullName = user.FullName
d0994fe Jakob Wakeling 2023-09-07 22:53:16
44
d0994fe Jakob Wakeling 2023-09-07 22:53:16
45
	if r.Method == http.MethodPost {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
46
		if r.FormValue("submit") == "Update" {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
47
			data.Form.Name = r.FormValue("username")
68784b6 Jakob Wakeling 2023-09-10 17:23:55
48
			data.Form.FullName = r.FormValue("fullname")
68784b6 Jakob Wakeling 2023-09-10 17:23:55
49
68784b6 Jakob Wakeling 2023-09-10 17:23:55
50
			if data.Form.Name == "" {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
51
				data.MessageA = "Username cannot be empty"
efb68c3 Jakob Wakeling 2023-12-06 20:37:06
52
			} else if slices.Contains(goit.Reserved, data.Form.Name) && user.Id != 0 || !goit.IsLegal(data.Form.Name) {
efb68c3 Jakob Wakeling 2023-12-06 20:37:06
53
				data.MessageA = "Username \"" + data.Form.Name + "\" is illegal"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
54
			} else if exists, err := goit.UserExists(data.Form.Name); err != nil {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
55
				log.Println("[/user/edit]", err.Error())
68784b6 Jakob Wakeling 2023-09-10 17:23:55
56
				goit.HttpError(w, http.StatusInternalServerError)
68784b6 Jakob Wakeling 2023-09-10 17:23:55
57
				return
68784b6 Jakob Wakeling 2023-09-10 17:23:55
58
			} else if exists && data.Form.Name != user.Name {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
59
				data.MessageA = "Username \"" + data.Form.Name + "\" is taken"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
60
			} else if err := goit.UpdateUser(user.Id, goit.User{
463cf58 Jakob Wakeling 2023-11-22 23:25:37
61
				Name: data.Form.Name, FullName: data.Form.FullName, IsAdmin: user.IsAdmin,
68784b6 Jakob Wakeling 2023-09-10 17:23:55
62
			}); err != nil {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
63
				log.Println("[/user/edit]", err.Error())
68784b6 Jakob Wakeling 2023-09-10 17:23:55
64
				goit.HttpError(w, http.StatusInternalServerError)
68784b6 Jakob Wakeling 2023-09-10 17:23:55
65
				return
68784b6 Jakob Wakeling 2023-09-10 17:23:55
66
			} else {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
67
				http.Redirect(w, r, "/user/edit?m=a", http.StatusFound)
68784b6 Jakob Wakeling 2023-09-10 17:23:55
68
				return
68784b6 Jakob Wakeling 2023-09-10 17:23:55
69
			}
68784b6 Jakob Wakeling 2023-09-10 17:23:55
70
		} else if r.FormValue("submit") == "Update Password" {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
71
			password := r.FormValue("password")
68784b6 Jakob Wakeling 2023-09-10 17:23:55
72
			newPassword := r.FormValue("new_password")
68784b6 Jakob Wakeling 2023-09-10 17:23:55
73
			confirmPassword := r.FormValue("confirm_password")
d0994fe Jakob Wakeling 2023-09-07 22:53:16
74
68784b6 Jakob Wakeling 2023-09-10 17:23:55
75
			if password == "" {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
76
				data.MessageB = "Current Password cannot be empty"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
77
			} else if newPassword == "" {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
78
				data.MessageB = "New Password cannot be empty"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
79
			} else if confirmPassword == "" {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
80
				data.MessageB = "Confirm New Password cannot be empty"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
81
			} else if newPassword != confirmPassword {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
82
				data.MessageB = "New Password and Confirm Password do not match"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
83
			} else if !bytes.Equal(goit.Hash(password, user.Salt), user.Pass) {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
84
				data.MessageB = "Password incorrect"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
85
			} else if err := goit.UpdatePassword(user.Id, newPassword); err != nil {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
86
				log.Println("[/user/edit]", err.Error())
68784b6 Jakob Wakeling 2023-09-10 17:23:55
87
				goit.HttpError(w, http.StatusInternalServerError)
68784b6 Jakob Wakeling 2023-09-10 17:23:55
88
				return
68784b6 Jakob Wakeling 2023-09-10 17:23:55
89
			} else {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
90
				http.Redirect(w, r, "/user/edit?m=b", http.StatusFound)
68784b6 Jakob Wakeling 2023-09-10 17:23:55
91
				return
68784b6 Jakob Wakeling 2023-09-10 17:23:55
92
			}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
93
		} else {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
94
			data.MessageA = "Invalid submit value"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
95
		}
68784b6 Jakob Wakeling 2023-09-10 17:23:55
96
	}
68784b6 Jakob Wakeling 2023-09-10 17:23:55
97
68784b6 Jakob Wakeling 2023-09-10 17:23:55
98
	switch r.FormValue("m") {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
99
	case "a":
68784b6 Jakob Wakeling 2023-09-10 17:23:55
100
		data.MessageA = "User updated successfully"
68784b6 Jakob Wakeling 2023-09-10 17:23:55
101
	case "b":
68784b6 Jakob Wakeling 2023-09-10 17:23:55
102
		data.MessageB = "Password updated successfully"
d0994fe Jakob Wakeling 2023-09-07 22:53:16
103
	}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
104
d0994fe Jakob Wakeling 2023-09-07 22:53:16
105
	if err := goit.Tmpl.ExecuteTemplate(w, "user/edit", data); err != nil {
d0994fe Jakob Wakeling 2023-09-07 22:53:16
106
		log.Println("[/user/edit]", err.Error())
d0994fe Jakob Wakeling 2023-09-07 22:53:16
107
	}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
108
}
109