Skip to content

Commit

Permalink
Updated doc/comments for group extension
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Feb 16, 2020
1 parent 60d4db8 commit 24b8761
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions user/group/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ func (s *Extension) GetName() string {
return "user-group"
}

// GetParamValueFromRequest is a convenience function to extract the value of a named param for a request
func GetParamValueFromRequest(paramName string) func(r *http.Request) (s string, err error) {
return func(r *http.Request) (s string, err error) {
return mux.Vars(r)[paramName], nil
}
}

// PostInit adds the default roues, if DisableDefaultRoutes is false
func (s *Extension) PostInit(app *nibbler.Application) error {
if !s.DisableDefaultRoutes {
app.Router.HandleFunc(app.Config.ApiPrefix+"/group/composite", s.SessionExtension.EnforceLoggedIn(s.GetUserCompositeRequestHandler)).Methods("GET")
Expand All @@ -55,6 +57,7 @@ func (s *Extension) PostInit(app *nibbler.Application) error {
return nil
}

// GetModels provides all relevant models for stuff like SQLExtension initialization
func GetModels() []interface{} {
var models []interface{}
models = append(models, nibbler.Group{})
Expand Down
2 changes: 2 additions & 0 deletions user/group/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *Extension) GetGroupMembershipsForUser(userId string) ([]nibbler.GroupMe
return s.PersistenceExtension.GetGroupMembershipsForUser(userId)
}

// CreateGroupMembershipRequestHandler will handle an http request with path param "groupId", and a membership request body
func (s *Extension) CreateGroupMembershipRequestHandler(w http.ResponseWriter, r *http.Request) {
membership, err := getMembershipFromBody(r)
if err != nil {
Expand Down Expand Up @@ -50,6 +51,7 @@ func (s *Extension) CreateGroupMembershipRequestHandler(w http.ResponseWriter, r
nibbler.Write200Json(w, string(resultJson))
}

// getMembershipFromBody parses the request body into a GroupMembership struct
func getMembershipFromBody(r *http.Request) (*nibbler.GroupMembership, error) {
if r.Body == nil {
return nil, errors.New("no body provided")
Expand Down
10 changes: 7 additions & 3 deletions user/group/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ const DeleteGroupPrivilegeAction = "delete-group-privilege"
const ListGroupsAction = "list-groups"
const RemoveMemberFromGroupAction = "remove-member-from-group"

// allows all groups in the groupIdList to perform the provided action on the targetGroupId. If targetGroupId is blank,
// it means "all resources/groups"
// AddPrivilegeToGroups adds a specific privilege definition to save to multiple groups. It allows all groups in the
// groupIdList to perform the provided action on the targetGroupId. If targetGroupId is blank, it means
// "all resources/groups"
func (s *Extension) AddPrivilegeToGroups(
groupIdList []string,
targetGroupId string,
Expand Down Expand Up @@ -72,6 +73,7 @@ func (s *Extension) HasPrivilegeOnResource(userId, resourceId, action string) (b
return len(privileges) == 0, nil
}

// DeleteGroupPrivilegeRequestHandler handles an http request with a privilege in its body and "groupId" in the path params
func (s *Extension) DeleteGroupPrivilegeRequestHandler(w http.ResponseWriter, r *http.Request) {
priv, err := getPrivilegeFromBody(r)
if err != nil {
Expand Down Expand Up @@ -127,6 +129,7 @@ func (s *Extension) DeleteGroupPrivilegeRequestHandler(w http.ResponseWriter, r
nibbler.Write200Json(w, "{\"result\":\"ok\"")
}

// CreateGroupPrivilegeRequestHandler handles an http request with a path param of groupId and body that is a Privilege
func (s *Extension) CreateGroupPrivilegeRequestHandler(w http.ResponseWriter, r *http.Request) {
priv, err := getPrivilegeFromBody(r)
if err != nil {
Expand Down Expand Up @@ -161,14 +164,15 @@ func (s *Extension) CreateGroupPrivilegeRequestHandler(w http.ResponseWriter, r
}
}

if err := s.PersistenceExtension.AddPrivilegeToGroups([]string{ priv.GroupID }, priv.ResourceID, priv.Action); err != nil {
if err := s.PersistenceExtension.AddPrivilegeToGroups([]string{priv.GroupID}, priv.ResourceID, priv.Action); err != nil {
nibbler.Write500Json(w, err.Error())
return
}

nibbler.Write200Json(w, "{\"result\":\"ok\"")
}

// getPrivilegeFromBody parses the request body into a GroupPrivilege struct
func getPrivilegeFromBody(r *http.Request) (*nibbler.GroupPrivilege, error) {
if r.Body == nil {
return nil, errors.New("no body provided")
Expand Down

0 comments on commit 24b8761

Please sign in to comment.