mirror of
https://github.com/gmemstr/pogo.git
synced 2024-09-19 17:21:10 +01:00
Admin: Added user editing
Added user editing route both backend and frontend, fully functional. Currently working on new user frontend route, then setup process. Also changed to use gorilla/feeds now that code has been upstreamed, so please update your dependencies.
This commit is contained in:
parent
1cf9e5c9dc
commit
4ef5d671f3
|
@ -20,7 +20,7 @@ To produce a product that is easy to deploy and easier to use when hosting a pod
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
[github.com/gmemstr/feeds](https://github.com/gmemstr/feeds) _this branch contains some fixes for "podcast specific" tags_
|
[github.com/gorilla/feeds](https://github.com/gorilla/feeds)
|
||||||
|
|
||||||
[github.com/fsnotify/fsnotify](https://github.com/fsnotify/fsnotify)
|
[github.com/fsnotify/fsnotify](https://github.com/fsnotify/fsnotify)
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,95 @@ func AddUser() common.Handler {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EditUser() common.Handler {
|
||||||
|
|
||||||
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
||||||
|
db, err := sql.Open("sqlite3", "assets/config/users.db")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: fmt.Sprintf("error in reading user database: %v", err),
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = r.ParseMultipartForm(32 << 20)
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: err.Error(),
|
||||||
|
StatusCode: http.StatusBadRequest,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
id := strings.Join(r.Form["id"], "")
|
||||||
|
username := strings.Join(r.Form["username"], "")
|
||||||
|
password := strings.Join(r.Form["oldpw"], "")
|
||||||
|
newpassword := strings.Join(r.Form["newpw1"], "")
|
||||||
|
realname := strings.Join(r.Form["realname"], "")
|
||||||
|
email := strings.Join(r.Form["email"], "")
|
||||||
|
pwhash, err := bcrypt.GenerateFromPassword([]byte(password), 4)
|
||||||
|
|
||||||
|
statement, err := db.Prepare("UPDATE users SET username=?, hash=?, realname=?, email=? WHERE id=?")
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: fmt.Sprintf("error preparing sqlite3 statement: %v", err),
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pwstatement, err := db.Prepare("SELECT hash FROM users WHERE id=?")
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: fmt.Sprintf("error preparing sqlite3 statement: %v", err),
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp, err := pwstatement.Query(id)
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: fmt.Sprintf("error executing sqlite3 statement: %v", err),
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var hash []byte
|
||||||
|
|
||||||
|
for tmp.Next() {
|
||||||
|
err = tmp.Scan(&hash)
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: fmt.Sprintf("error executing sqlite3 statement: %v", err),
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(hash)
|
||||||
|
if bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) != nil {
|
||||||
|
fmt.Println("Passwords do not match")
|
||||||
|
w.Write([]byte("<script>window.location = '/admin#/users/editerror';</script>"))
|
||||||
|
db.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if newpassword != "" {
|
||||||
|
pwhash, err = bcrypt.GenerateFromPassword([]byte(newpassword), 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = statement.Exec(username,pwhash,realname,email,id)
|
||||||
|
if err != nil {
|
||||||
|
return &common.HTTPError{
|
||||||
|
Message: fmt.Sprintf("error executing sqlite3 statement: %v", err),
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Write([]byte("<script>window.location = '/admin#/users/edited';</script>"))
|
||||||
|
db.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ListUsers() common.Handler {
|
func ListUsers() common.Handler {
|
||||||
|
|
||||||
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
||||||
|
|
Binary file not shown.
|
@ -1,12 +0,0 @@
|
||||||
{
|
|
||||||
"admin": {
|
|
||||||
"password": "$2a$04$ZAf88Bao4Q768vKfCaKBlOqtPumwKwFhrcpBCdfMWWFX69wyhgTqi",
|
|
||||||
"realname": "Administrator",
|
|
||||||
"email": "admin@localhost.com"
|
|
||||||
},
|
|
||||||
"gabriel": {
|
|
||||||
"password": "$2a$04$KrhZ1q6FpOGqs0FVKMYhQ.BTYeVXztnjrM9RbK.0buI1OHfmyNEAy",
|
|
||||||
"realname": "Gabriel Simmer",
|
|
||||||
"email": "gabriel@localhost.com"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -77,8 +77,8 @@ const useredit = {
|
||||||
<div>
|
<div>
|
||||||
<h3>Edit User</h3>
|
<h3>Edit User</h3>
|
||||||
<form enctype="multipart/form-data" action="/admin/edituser" method="post">
|
<form enctype="multipart/form-data" action="/admin/edituser" method="post">
|
||||||
<label for="title">Username</label>
|
<label for="username">Username</label>
|
||||||
<input type="text" id="title" name="title" :value="user.username">
|
<input type="text" id="username" name="username" :value="user.username">
|
||||||
<label for="email">Email</label>
|
<label for="email">Email</label>
|
||||||
<input type="text" id="email" name="email" :value="user.email">
|
<input type="text" id="email" name="email" :value="user.email">
|
||||||
<label for="realname">Real Name</label>
|
<label for="realname">Real Name</label>
|
||||||
|
|
|
@ -17,7 +17,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/gmemstr/feeds"
|
"github.com/gorilla/feeds"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|
|
@ -83,6 +83,11 @@ func Init() *mux.Router {
|
||||||
admin.CreateEpisode(),
|
admin.CreateEpisode(),
|
||||||
)).Methods("POST")
|
)).Methods("POST")
|
||||||
|
|
||||||
|
r.Handle("/admin/edituser", Handle(
|
||||||
|
auth.RequireAuthorization(),
|
||||||
|
admin.EditUser(),
|
||||||
|
)).Methods("POST")
|
||||||
|
|
||||||
r.Handle("/admin/newuser", Handle(
|
r.Handle("/admin/newuser", Handle(
|
||||||
auth.RequireAuthorization(),
|
auth.RequireAuthorization(),
|
||||||
admin.AddUser(),
|
admin.AddUser(),
|
||||||
|
|
Loading…
Reference in a new issue