Fix: Handel error of checkAdmin

This commit is contained in:
2021-12-12 15:08:44 +08:00
parent d2c852d57a
commit 047f15426b
3 changed files with 4 additions and 3 deletions

View File

@@ -25,6 +25,7 @@ func (api *API) HandleReset(w http.ResponseWriter, r *http.Request) {
// check token
err = api.CheckAdmin(w, r)
if err != nil {
api.HandleError(w, r, err)
return
}
@@ -54,6 +55,7 @@ func (api *API) HandleWalk(w http.ResponseWriter, r *http.Request) {
// check token match
err = api.CheckAdmin(w, r)
if err != nil {
api.HandleError(w, r, err)
return
}

View File

@@ -42,6 +42,7 @@ func (api *API) HandleInsertTag(w http.ResponseWriter, r *http.Request) {
// check if user is admin
err := api.CheckAdmin(w, r)
if err != nil {
api.HandleError(w, r, err)
return
}
@@ -102,6 +103,7 @@ func (api *API) HandleUpdateTag(w http.ResponseWriter, r *http.Request) {
// check if user is admin
err := api.CheckAdmin(w, r)
if err != nil {
api.HandleError(w, r, err)
return
}

View File

@@ -148,18 +148,15 @@ func (api *API) CheckAdmin(w http.ResponseWriter, r *http.Request) error {
session, _ := api.store.Get(r, api.defaultSessionName)
userId, ok := session.Values["userId"]
if !ok {
api.HandleError(w, r, ErrNotLoggedIn)
return ErrNotLoggedIn
}
user, err := api.Db.GetUserById(userId.(int64))
if err != nil {
api.HandleError(w, r, err)
return err
}
if user.Role != database.RoleAdmin {
api.HandleError(w, r, ErrNotAdmin)
return ErrNotAdmin
}