add a simple auth function
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
e1aff8d16b
commit
87fe581353
10 changed files with 206 additions and 8 deletions
|
|
@ -21,6 +21,10 @@ func NewRepoHandler(svc *core.RepoService) *RepoHandler {
|
|||
}
|
||||
|
||||
func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
if err := Authorize(r.Context(), ActionRepoCreate, 0); err != nil {
|
||||
Error(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
var req CreateRepoRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid JSON")
|
||||
|
|
@ -44,6 +48,10 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h *RepoHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
if err := Authorize(r.Context(), ActionRepoList, 0); err != nil {
|
||||
Error(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
repos, err := h.svc.List(r.Context())
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, err.Error())
|
||||
|
|
@ -63,7 +71,10 @@ func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||
Error(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := Authorize(r.Context(), ActionRepoRead, id); err != nil {
|
||||
Error(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
repo, err := h.svc.Get(r.Context(), id)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
Error(w, http.StatusNotFound, "repo not found")
|
||||
|
|
@ -82,7 +93,10 @@ func (h *RepoHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|||
Error(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := Authorize(r.Context(), ActionRepoUpdate, id); err != nil {
|
||||
Error(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
var req UpdateRepoRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid JSON")
|
||||
|
|
@ -113,7 +127,10 @@ func (h *RepoHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|||
Error(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := Authorize(r.Context(), ActionRepoDelete, id); err != nil {
|
||||
Error(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
if err := h.svc.Delete(r.Context(), id); errors.Is(err, store.ErrNotFound) {
|
||||
Error(w, http.StatusNotFound, "repo not found")
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue