add returns after rendering errors and simplify loop condition

This commit is contained in:
David Christofas committed 2022-09-02 11:54:17 +02:00
1 parent 6a9d4efef6
commit e36cd4fa7f
1 file changed
+31 -16
+31 -16
View File
@@ -159,6 +159,7 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) {
userID, err := url.PathUnescape(userID) userID, err := url.PathUnescape(userID)
if err != nil { if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed") errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed")
return
} }
if userID == "" { if userID == "" {
@@ -175,11 +176,20 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) {
} else { } else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
} }
return
} }
sel := strings.Split(r.URL.Query().Get("$select"), ",") sel := strings.Split(r.URL.Query().Get("$select"), ",")
exp := strings.Split(r.URL.Query().Get("$expand"), ",") exp := strings.Split(r.URL.Query().Get("$expand"), ",")
if slices.Contains(sel, "drive") || slices.Contains(sel, "drives") || slices.Contains(exp, "drive") || slices.Contains(exp, "drives") { if slices.Contains(sel, "drive") || slices.Contains(sel, "drives") || slices.Contains(exp, "drive") || slices.Contains(exp, "drives") {
wdu, err := url.Parse(g.config.Spaces.WebDavBase + g.config.Spaces.WebDavPath) wdu, err := url.Parse(g.config.Spaces.WebDavBase + g.config.Spaces.WebDavPath)
if err != nil {
g.logger.Err(err).
Str("webdav_base", g.config.Spaces.WebDavBase).
Str("webdav_path", g.config.Spaces.WebDavPath).
Msg("error parsing webdav URL")
render.Status(r, http.StatusInternalServerError)
return
}
f := listStorageSpacesUserFilter(user.GetId()) f := listStorageSpacesUserFilter(user.GetId())
// use the unrestricted flag to get all possible spaces // use the unrestricted flag to get all possible spaces
// users with the canListAllSpaces permission should see all spaces // users with the canListAllSpaces permission should see all spaces
@@ -231,6 +241,7 @@ func (g Graph) DeleteUser(w http.ResponseWriter, r *http.Request) {
userID, err := url.PathUnescape(userID) userID, err := url.PathUnescape(userID)
if err != nil { if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed") errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed")
return
} }
if userID == "" { if userID == "" {
@@ -248,25 +259,26 @@ func (g Graph) DeleteUser(w http.ResponseWriter, r *http.Request) {
}) })
if err != nil { if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "could not read spaces") errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "could not read spaces")
return
} }
for _, sp := range lspr.GetStorageSpaces() { for _, sp := range lspr.GetStorageSpaces() {
if sp.SpaceType == "personal" { if !(sp.SpaceType == "personal" && sp.Owner.Id.OpaqueId == userID) {
if 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")
}
break
}
} }
// 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) err = g.identityBackend.DeleteUser(r.Context(), userID)
@@ -277,6 +289,7 @@ func (g Graph) DeleteUser(w http.ResponseWriter, r *http.Request) {
errcode.Render(w, r) errcode.Render(w, r)
} else { } else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
} }
} }
@@ -293,6 +306,7 @@ func (g Graph) PatchUser(w http.ResponseWriter, r *http.Request) {
nameOrID, err := url.PathUnescape(nameOrID) nameOrID, err := url.PathUnescape(nameOrID)
if err != nil { if err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed") errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping user id failed")
return
} }
if nameOrID == "" { if nameOrID == "" {
@@ -328,6 +342,7 @@ func (g Graph) PatchUser(w http.ResponseWriter, r *http.Request) {
} else { } else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
} }
return
} }
currentUser := ctxpkg.ContextMustGetUser(r.Context()) currentUser := ctxpkg.ContextMustGetUser(r.Context())