rename GetUserFromName -> GetUserByName

This commit is contained in:
sentriz
2020-07-16 22:59:55 +01:00
parent 19e4958f2f
commit f34a0b48f9
5 changed files with 22 additions and 10 deletions

View File

@@ -151,7 +151,7 @@ func (c *Controller) ServeChangePassword(r *http.Request) *Response {
code: 400,
}
}
user := c.DB.GetUserFromName(username)
user := c.DB.GetUserByName(username)
if user == nil {
return &Response{
err: "couldn't find a user with that name",
@@ -176,7 +176,7 @@ func (c *Controller) ServeChangePasswordDo(r *http.Request) *Response {
flashW: []string{err.Error()},
}
}
user := c.DB.GetUserFromName(username)
user := c.DB.GetUserByName(username)
user.Password = passwordOne
c.DB.Save(user)
return &Response{redirect: "/admin/home"}
@@ -190,7 +190,7 @@ func (c *Controller) ServeDeleteUser(r *http.Request) *Response {
code: 400,
}
}
user := c.DB.GetUserFromName(username)
user := c.DB.GetUserByName(username)
if user == nil {
return &Response{
err: "couldn't find a user with that name",
@@ -207,7 +207,7 @@ func (c *Controller) ServeDeleteUser(r *http.Request) *Response {
func (c *Controller) ServeDeleteUserDo(r *http.Request) *Response {
username := r.URL.Query().Get("user")
user := c.DB.GetUserFromName(username)
user := c.DB.GetUserByName(username)
if user.IsAdmin {
return &Response{
redirect: "/admin/home",

View File

@@ -16,7 +16,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
return
}
user := c.DB.GetUserFromName(username)
user := c.DB.GetUserByName(username)
if user == nil || password != user.Password {
sessAddFlashW(session, []string{"invalid username / password"})
sessLogSave(session, w, r)
@@ -26,7 +26,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
// put the user name into the session. future endpoints after this one
// are wrapped with WithUserSession() which will get the name from the
// session and put the row into the request context
session.Values["user"] = user.Name
session.Values["user"] = user.ID
sessLogSave(session, w, r)
http.Redirect(w, r, c.Path("/admin/home"), http.StatusSeeOther)
}

View File

@@ -21,7 +21,7 @@ func (c *Controller) WithUserSession(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// session exists at this point
session := r.Context().Value(CtxSession).(*sessions.Session)
username, ok := session.Values["user"].(string)
userID, ok := session.Values["user"].(int)
if !ok {
sessAddFlashW(session, []string{"you are not authenticated"})
sessLogSave(session, w, r)
@@ -29,7 +29,7 @@ func (c *Controller) WithUserSession(next http.Handler) http.Handler {
return
}
// take username from sesion and add the user row to the context
user := c.DB.GetUserFromName(username)
user := c.DB.GetUserByID(userID)
if user == nil {
// the username in the client's session no longer relates to a
// user in the database (maybe the user was deleted)