Goit

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

Goit/src/goit/user.go (137 lines, 2.9 KiB) -rw-r--r-- file download

ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
0
// Copyright (C) 2023, Jakob Wakeling
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
1
// All rights reserved.
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
2
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
3
package goit
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
4
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
5
import (
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
6
	"database/sql"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
7
	"errors"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
8
	"fmt"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
9
	"net/http"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
10
	"strings"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
11
)
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
12
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
13
type User struct {
08dff9a Jakob Wakeling 2023-11-07 21:03:04
14
	Id       int64  `json:"id"`
08dff9a Jakob Wakeling 2023-11-07 21:03:04
15
	Name     string `json:"name"`
08dff9a Jakob Wakeling 2023-11-07 21:03:04
16
	FullName string `json:"name_full"`
08dff9a Jakob Wakeling 2023-11-07 21:03:04
17
	Pass     []byte `json:"pass"`
08dff9a Jakob Wakeling 2023-11-07 21:03:04
18
	PassAlgo string `json:"pass_algo"`
08dff9a Jakob Wakeling 2023-11-07 21:03:04
19
	Salt     []byte `json:"salt"`
08dff9a Jakob Wakeling 2023-11-07 21:03:04
20
	IsAdmin  bool   `json:"is_admin"`
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
21
}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
22
1828e5f Jakob Wakeling 2023-07-19 19:53:59
23
func HandleUserLogout(w http.ResponseWriter, r *http.Request) {
0893c1e Jakob Wakeling 2023-07-21 17:11:15
24
	id, s := GetSessionCookie(r)
0893c1e Jakob Wakeling 2023-07-21 17:11:15
25
	EndSession(id, s.Token)
0893c1e Jakob Wakeling 2023-07-21 17:11:15
26
	EndSessionCookie(w)
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
27
	http.Redirect(w, r, "/", http.StatusFound)
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
28
}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
29
463cf58 Jakob Wakeling 2023-11-22 23:25:37
30
func GetUsers() ([]User, error) {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
31
	users := []User{}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
32
463cf58 Jakob Wakeling 2023-11-22 23:25:37
33
	rows, err := db.Query("SELECT id, name, name_full, pass, pass_algo, salt, is_admin FROM users")
463cf58 Jakob Wakeling 2023-11-22 23:25:37
34
	if err != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
35
		return nil, err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
36
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
37
463cf58 Jakob Wakeling 2023-11-22 23:25:37
38
	defer rows.Close()
463cf58 Jakob Wakeling 2023-11-22 23:25:37
39
463cf58 Jakob Wakeling 2023-11-22 23:25:37
40
	for rows.Next() {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
41
		u := User{}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
42
		if err := rows.Scan(&u.Id, &u.Name, &u.FullName, &u.Pass, &u.PassAlgo, &u.Salt, &u.IsAdmin); err != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
43
			return nil, err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
44
		}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
45
463cf58 Jakob Wakeling 2023-11-22 23:25:37
46
		users = append(users, u)
463cf58 Jakob Wakeling 2023-11-22 23:25:37
47
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
48
463cf58 Jakob Wakeling 2023-11-22 23:25:37
49
	if rows.Err() != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
50
		return nil, err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
51
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
52
463cf58 Jakob Wakeling 2023-11-22 23:25:37
53
	return users, nil
463cf58 Jakob Wakeling 2023-11-22 23:25:37
54
}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
55
0893c1e Jakob Wakeling 2023-07-21 17:11:15
56
func GetUser(id int64) (*User, error) {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
57
	u := User{}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
58
1828e5f Jakob Wakeling 2023-07-19 19:53:59
59
	if err := db.QueryRow(
68784b6 Jakob Wakeling 2023-09-10 17:23:55
60
		"SELECT id, name, name_full, pass, pass_algo, salt, is_admin FROM users WHERE id = ?", id,
68784b6 Jakob Wakeling 2023-09-10 17:23:55
61
	).Scan(&u.Id, &u.Name, &u.FullName, &u.Pass, &u.PassAlgo, &u.Salt, &u.IsAdmin); err != nil {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
62
		if !errors.Is(err, sql.ErrNoRows) {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
63
			return nil, fmt.Errorf("[SELECT:user] %w", err)
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
64
		} else {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
65
			return nil, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
66
		}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
67
	} else {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
68
		return &u, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
69
	}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
70
}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
71
6727af8 Jakob Wakeling 2023-07-19 23:43:37
72
func GetUserByName(name string) (*User, error) {
6727af8 Jakob Wakeling 2023-07-19 23:43:37
73
	u := &User{}
6727af8 Jakob Wakeling 2023-07-19 23:43:37
74
6727af8 Jakob Wakeling 2023-07-19 23:43:37
75
	err := db.QueryRow(
6727af8 Jakob Wakeling 2023-07-19 23:43:37
76
		"SELECT id, name, name_full, pass, pass_algo, salt, is_admin FROM users WHERE name = ?", strings.ToLower(name),
d631c5e Jakob Wakeling 2023-07-20 23:13:39
77
	).Scan(&u.Id, &u.Name, &u.FullName, &u.Pass, &u.PassAlgo, &u.Salt, &u.IsAdmin)
6727af8 Jakob Wakeling 2023-07-19 23:43:37
78
	if errors.Is(err, sql.ErrNoRows) {
6727af8 Jakob Wakeling 2023-07-19 23:43:37
79
		return nil, nil
6727af8 Jakob Wakeling 2023-07-19 23:43:37
80
	} else if err != nil {
6727af8 Jakob Wakeling 2023-07-19 23:43:37
81
		return nil, err
6727af8 Jakob Wakeling 2023-07-19 23:43:37
82
	}
6727af8 Jakob Wakeling 2023-07-19 23:43:37
83
6727af8 Jakob Wakeling 2023-07-19 23:43:37
84
	return u, nil
6727af8 Jakob Wakeling 2023-07-19 23:43:37
85
}
6727af8 Jakob Wakeling 2023-07-19 23:43:37
86
1828e5f Jakob Wakeling 2023-07-19 19:53:59
87
func UserExists(name string) (bool, error) {
1828e5f Jakob Wakeling 2023-07-19 19:53:59
88
	if err := db.QueryRow("SELECT name FROM users WHERE name = ?", strings.ToLower(name)).Scan(&name); err != nil {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
89
		if !errors.Is(err, sql.ErrNoRows) {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
90
			return false, err
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
91
		} else {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
92
			return false, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
93
		}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
94
	} else {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
95
		return true, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
96
	}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
97
}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
98
463cf58 Jakob Wakeling 2023-11-22 23:25:37
99
func CreateUser(user User) error {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
100
	if _, err := db.Exec(
463cf58 Jakob Wakeling 2023-11-22 23:25:37
101
		"INSERT INTO users (name, name_full, pass, pass_algo, salt, is_admin) VALUES (?, ?, ?, ?, ?, ?)",
463cf58 Jakob Wakeling 2023-11-22 23:25:37
102
		user.Name, user.FullName, user.Pass, user.PassAlgo, user.Salt, user.IsAdmin,
463cf58 Jakob Wakeling 2023-11-22 23:25:37
103
	); err != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
104
		return err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
105
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
106
463cf58 Jakob Wakeling 2023-11-22 23:25:37
107
	return nil
463cf58 Jakob Wakeling 2023-11-22 23:25:37
108
}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
109
d0994fe Jakob Wakeling 2023-09-07 22:53:16
110
func UpdateUser(uid int64, user User) error {
d0994fe Jakob Wakeling 2023-09-07 22:53:16
111
	if _, err := db.Exec(
463cf58 Jakob Wakeling 2023-11-22 23:25:37
112
		"UPDATE users SET name = ?, name_full = ?, is_admin = ? WHERE id = ?",
463cf58 Jakob Wakeling 2023-11-22 23:25:37
113
		user.Name, user.FullName, user.IsAdmin, uid,
d0994fe Jakob Wakeling 2023-09-07 22:53:16
114
	); err != nil {
d0994fe Jakob Wakeling 2023-09-07 22:53:16
115
		return err
d0994fe Jakob Wakeling 2023-09-07 22:53:16
116
	}
d0994fe Jakob Wakeling 2023-09-07 22:53:16
117
d0994fe Jakob Wakeling 2023-09-07 22:53:16
118
	return nil
d0994fe Jakob Wakeling 2023-09-07 22:53:16
119
}
68784b6 Jakob Wakeling 2023-09-10 17:23:55
120
68784b6 Jakob Wakeling 2023-09-10 17:23:55
121
func UpdatePassword(uid int64, password string) error {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
122
	salt, err := Salt()
68784b6 Jakob Wakeling 2023-09-10 17:23:55
123
	if err != nil {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
124
		return err
68784b6 Jakob Wakeling 2023-09-10 17:23:55
125
	}
68784b6 Jakob Wakeling 2023-09-10 17:23:55
126
68784b6 Jakob Wakeling 2023-09-10 17:23:55
127
	if _, err := db.Exec(
68784b6 Jakob Wakeling 2023-09-10 17:23:55
128
		"UPDATE users SET pass = ?, pass_algo = ?, salt = ? WHERE id = ?",
68784b6 Jakob Wakeling 2023-09-10 17:23:55
129
		Hash(password, salt), "argon2", salt, uid,
68784b6 Jakob Wakeling 2023-09-10 17:23:55
130
	); err != nil {
68784b6 Jakob Wakeling 2023-09-10 17:23:55
131
		return err
68784b6 Jakob Wakeling 2023-09-10 17:23:55
132
	}
68784b6 Jakob Wakeling 2023-09-10 17:23:55
133
68784b6 Jakob Wakeling 2023-09-10 17:23:55
134
	return nil
68784b6 Jakob Wakeling 2023-09-10 17:23:55
135
}
136