ch17.4
This commit is contained in:
@@ -150,3 +150,23 @@ func (app *application) requireActivatedUser(next http.HandlerFunc) http.Handler
|
||||
})
|
||||
return app.requireAuthenticatedUser(fn)
|
||||
}
|
||||
|
||||
func (app *application) requirePermissions(code string, next http.HandlerFunc) http.HandlerFunc {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
user := app.contextGetUser(r)
|
||||
|
||||
permissions, err := app.models.Permissions.GetAllForUser(user.ID)
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !permissions.Include(code) {
|
||||
app.notPermittedResponse(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
return app.requireActivatedUser(fn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user