From 4ef5d671f37f9690f69eaeca532e2be4a053e03f Mon Sep 17 00:00:00 2001 From: gmemstr Date: Fri, 17 Nov 2017 13:59:53 -0800 Subject: [PATCH] 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. --- README.md | 2 +- admin/admin.go | 89 +++++++++++++++++++++++++++++++++++++++ assets/config/users.db | Bin 20480 -> 20480 bytes assets/config/users.json | 12 ------ assets/web/static/app.js | 4 +- generate_rss.go | 2 +- router/router.go | 5 +++ 7 files changed, 98 insertions(+), 16 deletions(-) delete mode 100644 assets/config/users.json diff --git a/README.md b/README.md index 051a1ea..2b959a5 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ To produce a product that is easy to deploy and easier to use when hosting a pod ## 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) diff --git a/admin/admin.go b/admin/admin.go index 50c8e3d..cfdc821 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -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("")) + 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("")) + db.Close() + + return nil + } +} + func ListUsers() common.Handler { return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError { diff --git a/assets/config/users.db b/assets/config/users.db index 91ef3eb15bc9150eb58485a0235e4cbc7498241d..e5f311a92d5c18a7a717bd1c557ecab73e8f6090 100644 GIT binary patch delta 137 zcmZozz}T>Wae_1>+e8^>RyGDbPK}KzbNRXXXE8AG>of2#;@96SsG!V0SzkUooPmLX zL%fkyJ%*8;LA;SgTstu(H#1MgC{e}0L?taN$JbLYCEq;Lpdut9yxh#Zs?4`E$;aO$ nH6yaXq|`7gGbbb@)63ns*gwxJw6fI4$+^@KWWeT+^85w>F`y=W delta 137 zcmZozz}T>Wae_1>%S0JxRu%?5#;A=cbNRU$7#NuN^%?kQ@#}9ER8Z!htS_G(p2En^ zAl}HTuAP{Yo0+F#l&E50q7vnpW?|u!m~RqjZe~&Do#veA?UdtRSQ1d0Tkc)%mQj>k n;N+Z=<{KXF7GY*tUYU^|QkdxoG9a_Kq$sf@zi9JEd42-`h}$TV diff --git a/assets/config/users.json b/assets/config/users.json deleted file mode 100644 index ec73880..0000000 --- a/assets/config/users.json +++ /dev/null @@ -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" - } -} \ No newline at end of file diff --git a/assets/web/static/app.js b/assets/web/static/app.js index ecfbc5f..59d2e1d 100644 --- a/assets/web/static/app.js +++ b/assets/web/static/app.js @@ -77,8 +77,8 @@ const useredit = {

Edit User

- - + + diff --git a/generate_rss.go b/generate_rss.go index 082bcad..c0f0a5d 100644 --- a/generate_rss.go +++ b/generate_rss.go @@ -17,7 +17,7 @@ import ( "encoding/json" "github.com/fsnotify/fsnotify" - "github.com/gmemstr/feeds" + "github.com/gorilla/feeds" ) type Config struct { diff --git a/router/router.go b/router/router.go index 9d7226e..9fdaf72 100644 --- a/router/router.go +++ b/router/router.go @@ -83,6 +83,11 @@ func Init() *mux.Router { admin.CreateEpisode(), )).Methods("POST") + r.Handle("/admin/edituser", Handle( + auth.RequireAuthorization(), + admin.EditUser(), + )).Methods("POST") + r.Handle("/admin/newuser", Handle( auth.RequireAuthorization(), admin.AddUser(),