-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
actually check permissions to fix tests
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
- Loading branch information
Showing
11 changed files
with
313 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/render" | ||
accounts "github.com/owncloud/ocis/accounts/pkg/service/v0" | ||
"github.com/owncloud/ocis/ocis-pkg/roles" | ||
"github.com/owncloud/ocis/ocs/pkg/service/v0/data" | ||
"github.com/owncloud/ocis/ocs/pkg/service/v0/response" | ||
) | ||
|
||
// RequireAdmin middleware is used to require the user in context to be an admin / have account management permissions | ||
func RequireAdmin(opts ...Option) func(next http.Handler) http.Handler { | ||
opt := newOptions(opts...) | ||
|
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
// get roles from context | ||
roleIDs, ok := roles.ReadRoleIDsFromContext(r.Context()) | ||
if !ok { | ||
render.Render(w, r, response.ErrRender(data.MetaUnauthorized.StatusCode, "Unauthorized")) | ||
return | ||
} | ||
|
||
// check if permission is present in roles of the authenticated account | ||
if opt.RoleManager.FindPermissionByID(r.Context(), roleIDs, accounts.AccountManagementPermissionID) != nil { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
render.Render(w, r, response.ErrRender(data.MetaUnauthorized.StatusCode, "Unauthorized")) | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/cs3org/reva/pkg/user" | ||
"github.com/go-chi/chi" | ||
"github.com/go-chi/render" | ||
accounts "github.com/owncloud/ocis/accounts/pkg/service/v0" | ||
"github.com/owncloud/ocis/ocis-pkg/roles" | ||
"github.com/owncloud/ocis/ocs/pkg/service/v0/data" | ||
"github.com/owncloud/ocis/ocs/pkg/service/v0/response" | ||
) | ||
|
||
// RequireSelfOrAdmin middleware is used to require the requesting user to be an admin or the requested user himself | ||
func RequireSelfOrAdmin(opts ...Option) func(next http.Handler) http.Handler { | ||
opt := newOptions(opts...) | ||
|
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
u, ok := user.ContextGetUser(r.Context()) | ||
if !ok { | ||
render.Render(w, r, response.ErrRender(data.MetaUnauthorized.StatusCode, "Unauthorized")) | ||
return | ||
} | ||
if u.Id == nil || u.Id.OpaqueId == "" { | ||
render.Render(w, r, response.ErrRender(data.MetaBadRequest.StatusCode, "user is missing an id")) | ||
return | ||
} | ||
// get roles from context | ||
roleIDs, ok := roles.ReadRoleIDsFromContext(r.Context()) | ||
if !ok { | ||
render.Render(w, r, response.ErrRender(data.MetaUnauthorized.StatusCode, "Unauthorized")) | ||
return | ||
} | ||
|
||
// check if account management permission is present in roles of the authenticated account | ||
if opt.RoleManager.FindPermissionByID(r.Context(), roleIDs, accounts.AccountManagementPermissionID) != nil { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
// check if self management permission is present in roles of the authenticated account | ||
if opt.RoleManager.FindPermissionByID(r.Context(), roleIDs, accounts.SelfManagementPermissionID) != nil { | ||
userid := chi.URLParam(r, "userid") | ||
if userid == "" || userid == u.Id.OpaqueId || userid == u.Username { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
} | ||
|
||
render.Render(w, r, response.ErrRender(data.MetaUnauthorized.StatusCode, "Unauthorized")) | ||
|
||
}) | ||
} | ||
} |
Oops, something went wrong.