You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
Start integrating Hugo in the new plugin
This commit is contained in:
46
users.go
46
users.go
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/asdine/storm"
|
||||
)
|
||||
|
||||
func usersHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
func usersHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
return usersGetHandler(c, w, r)
|
||||
@@ -29,8 +29,8 @@ func usersHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (in
|
||||
// usersGetHandler is used to handle the GET requests for /api/users. It can print a list
|
||||
// of users or a specific user. The password hash is always removed before being sent to the
|
||||
// client.
|
||||
func usersGetHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.us.Admin {
|
||||
func usersGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.User.Admin {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func usersGetHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
if r.URL.Path == "/" {
|
||||
users := []User{}
|
||||
|
||||
for _, user := range c.fm.Users {
|
||||
for _, user := range c.FM.Users {
|
||||
// Copies the user and removes the password.
|
||||
u := *user
|
||||
u.Password = ""
|
||||
@@ -62,7 +62,7 @@ func usersGetHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// Searches for the user and prints the one who matches.
|
||||
for _, user := range c.fm.Users {
|
||||
for _, user := range c.FM.Users {
|
||||
if user.ID != id {
|
||||
continue
|
||||
}
|
||||
@@ -76,8 +76,8 @@ func usersGetHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
|
||||
func usersPostHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.us.Admin {
|
||||
func usersPostHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.User.Admin {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ func usersPostHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
u.Password = pw
|
||||
|
||||
// Saves the user to the database.
|
||||
err = c.fm.db.Save(&u)
|
||||
err = c.FM.db.Save(&u)
|
||||
if err == storm.ErrAlreadyExists {
|
||||
return http.StatusConflict, err
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func usersPostHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// Saves the user to the memory.
|
||||
c.fm.Users[u.Username] = &u
|
||||
c.FM.Users[u.Username] = &u
|
||||
|
||||
// Set the Location header and return.
|
||||
w.Header().Set("Location", "/users/"+strconv.Itoa(u.ID))
|
||||
@@ -146,8 +146,8 @@ func usersPostHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func usersDeleteHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.us.Admin {
|
||||
func usersDeleteHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.User.Admin {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ func usersDeleteHandler(c *requestContext, w http.ResponseWriter, r *http.Reques
|
||||
return http.StatusNotFound, err
|
||||
}
|
||||
|
||||
err = c.fm.db.DeleteStruct(&User{ID: id})
|
||||
err = c.FM.db.DeleteStruct(&User{ID: id})
|
||||
if err == storm.ErrNotFound {
|
||||
return http.StatusNotFound, err
|
||||
}
|
||||
@@ -174,17 +174,17 @@ func usersDeleteHandler(c *requestContext, w http.ResponseWriter, r *http.Reques
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
for _, user := range c.fm.Users {
|
||||
for _, user := range c.FM.Users {
|
||||
if user.ID == id {
|
||||
delete(c.fm.Users, user.Username)
|
||||
delete(c.FM.Users, user.Username)
|
||||
}
|
||||
}
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.us.Admin && !(r.URL.Path == "/change-password" || r.URL.Path == "/change-css") {
|
||||
func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if !c.User.Admin && !(r.URL.Path == "/change-password" || r.URL.Path == "/change-css") {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
@@ -225,8 +225,8 @@ func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
c.us.Password = pw
|
||||
err = c.fm.db.UpdateField(&User{ID: c.us.ID}, "Password", pw)
|
||||
c.User.Password = pw
|
||||
err = c.FM.db.UpdateField(&User{ID: c.User.ID}, "Password", pw)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
@@ -235,8 +235,8 @@ func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
if sid == "change-css" {
|
||||
c.us.CSS = u.CSS
|
||||
err = c.fm.db.UpdateField(&User{ID: c.us.ID}, "CSS", u.CSS)
|
||||
c.User.CSS = u.CSS
|
||||
err = c.FM.db.UpdateField(&User{ID: c.User.ID}, "CSS", u.CSS)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
u.Commands = []string{}
|
||||
}
|
||||
|
||||
ouser, ok := c.fm.Users[u.Username]
|
||||
ouser, ok := c.FM.Users[u.Username]
|
||||
if !ok {
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
@@ -279,11 +279,11 @@ func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// Updates the whole User struct because we always are supposed
|
||||
// to send a new entire object.
|
||||
err = c.fm.db.Save(&u)
|
||||
err = c.FM.db.Save(&u)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
c.fm.Users[u.Username] = &u
|
||||
c.FM.Users[u.Username] = &u
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user