Skip to content

Commit

Permalink
add returns after rendering errors and simplify loop condition
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Sep 2, 2022
1 parent 6a9d4ef commit cdd433b
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions services/graph/pkg/service/v0/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) {
userID, err := url.PathUnescape(userID)
if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed")
return
}

if userID == "" {
Expand All @@ -175,6 +176,7 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) {
} else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
}
return
}
sel := strings.Split(r.URL.Query().Get("$select"), ",")
exp := strings.Split(r.URL.Query().Get("$expand"), ",")
Expand Down Expand Up @@ -231,6 +233,7 @@ func (g Graph) DeleteUser(w http.ResponseWriter, r *http.Request) {
userID, err := url.PathUnescape(userID)
if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed")
return
}

if userID == "" {
Expand All @@ -248,25 +251,26 @@ func (g Graph) DeleteUser(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "could not read spaces")
return
}
for _, sp := range lspr.GetStorageSpaces() {
if sp.SpaceType == "personal" {
if sp.Owner.Id.OpaqueId == userID {
// TODO: check if request contains a homespace and if, check if requesting user has the privilege to
// delete it and make sure it is not deleting its own homespace
// needs modification of the cs3api
_, err := g.gatewayClient.DeleteStorageSpace(r.Context(), &storageprovider.DeleteStorageSpaceRequest{
Opaque: opaque,
Id: &storageprovider.StorageSpaceId{
OpaqueId: sp.Id.OpaqueId,
},
})
if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "could not delete homespace")
}
break
}
if !(sp.SpaceType == "personal" && sp.Owner.Id.OpaqueId == userID) {
continue
}
// TODO: check if request contains a homespace and if, check if requesting user has the privilege to
// delete it and make sure it is not deleting its own homespace
// needs modification of the cs3api
_, err := g.gatewayClient.DeleteStorageSpace(r.Context(), &storageprovider.DeleteStorageSpaceRequest{
Opaque: opaque,
Id: &storageprovider.StorageSpaceId{
OpaqueId: sp.Id.OpaqueId,
},
})
if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "could not delete homespace")
return
}
break
}

err = g.identityBackend.DeleteUser(r.Context(), userID)
Expand All @@ -277,6 +281,7 @@ func (g Graph) DeleteUser(w http.ResponseWriter, r *http.Request) {
errcode.Render(w, r)
} else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
}

Expand All @@ -293,6 +298,7 @@ func (g Graph) PatchUser(w http.ResponseWriter, r *http.Request) {
nameOrID, err := url.PathUnescape(nameOrID)
if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed")
return
}

if nameOrID == "" {
Expand Down Expand Up @@ -328,6 +334,7 @@ func (g Graph) PatchUser(w http.ResponseWriter, r *http.Request) {
} else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
}
return
}

currentUser := ctxpkg.ContextMustGetUser(r.Context())
Expand Down

0 comments on commit cdd433b

Please sign in to comment.