use GetUserFromName in handlers
This commit is contained in:
@@ -38,7 +38,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
// put the user name into the session. future endpoints after this one
|
// put the user name into the session. future endpoints after this one
|
||||||
// are wrapped with WithUserSession() which will get the name from the
|
// are wrapped with WithUserSession() which will get the name from the
|
||||||
// session and put the row into the request context.
|
// session and put the row into the request context
|
||||||
session.Values["user"] = user.Name
|
session.Values["user"] = user.Name
|
||||||
sessLogSave(w, r, session)
|
sessLogSave(w, r, session)
|
||||||
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
||||||
@@ -150,12 +150,8 @@ func (c *Controller) ServeChangePassword(w http.ResponseWriter, r *http.Request)
|
|||||||
http.Error(w, "please provide a username", 400)
|
http.Error(w, "please provide a username", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := &model.User{}
|
user := c.DB.GetUserFromName(username)
|
||||||
err := c.DB.
|
if user == nil {
|
||||||
Where("name = ?", username).
|
|
||||||
First(user).
|
|
||||||
Error
|
|
||||||
if gorm.IsRecordNotFoundError(err) {
|
|
||||||
http.Error(w, "couldn't find a user with that name", 400)
|
http.Error(w, "couldn't find a user with that name", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -167,10 +163,6 @@ func (c *Controller) ServeChangePassword(w http.ResponseWriter, r *http.Request)
|
|||||||
func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Request) {
|
func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Request) {
|
||||||
session := r.Context().Value(contextSessionKey).(*sessions.Session)
|
session := r.Context().Value(contextSessionKey).(*sessions.Session)
|
||||||
username := r.URL.Query().Get("user")
|
username := r.URL.Query().Get("user")
|
||||||
user := &model.User{}
|
|
||||||
c.DB.
|
|
||||||
Where("name = ?", username).
|
|
||||||
First(user)
|
|
||||||
passwordOne := r.FormValue("password_one")
|
passwordOne := r.FormValue("password_one")
|
||||||
passwordTwo := r.FormValue("password_two")
|
passwordTwo := r.FormValue("password_two")
|
||||||
err := validatePasswords(passwordOne, passwordTwo)
|
err := validatePasswords(passwordOne, passwordTwo)
|
||||||
@@ -180,6 +172,7 @@ func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Reques
|
|||||||
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
user := c.DB.GetUserFromName(username)
|
||||||
user.Password = passwordOne
|
user.Password = passwordOne
|
||||||
c.DB.Save(user)
|
c.DB.Save(user)
|
||||||
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
||||||
@@ -191,12 +184,8 @@ func (c *Controller) ServeDeleteUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "please provide a username", 400)
|
http.Error(w, "please provide a username", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := &model.User{}
|
user := c.DB.GetUserFromName(username)
|
||||||
err := c.DB.
|
if user == nil {
|
||||||
Where("name = ?", username).
|
|
||||||
First(user).
|
|
||||||
Error
|
|
||||||
if gorm.IsRecordNotFoundError(err) {
|
|
||||||
http.Error(w, "couldn't find a user with that name", 400)
|
http.Error(w, "couldn't find a user with that name", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -207,10 +196,7 @@ func (c *Controller) ServeDeleteUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (c *Controller) ServeDeleteUserDo(w http.ResponseWriter, r *http.Request) {
|
func (c *Controller) ServeDeleteUserDo(w http.ResponseWriter, r *http.Request) {
|
||||||
username := r.URL.Query().Get("user")
|
username := r.URL.Query().Get("user")
|
||||||
user := &model.User{}
|
user := c.DB.GetUserFromName(username)
|
||||||
c.DB.
|
|
||||||
Where("name = ?", username).
|
|
||||||
First(user)
|
|
||||||
c.DB.Delete(user)
|
c.DB.Delete(user)
|
||||||
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user