Skip to content

Commit

Permalink
configstore: add actions and roles
Browse files Browse the repository at this point in the history
Add initial actions and roles.
An action represents a specific action on a resource.
Roles are predefined roles (not saved on storage) that can do some actions.
  • Loading branch information
sgotti committed Dec 12, 2019
1 parent a438a06 commit 0886349
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/agola/cmd/orgmemberadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func orgMemberAdd(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)

log.Infof("adding/updating member %q to organization %q with role %q", orgMemberAddOpts.username, orgMemberAddOpts.orgname, orgMemberAddOpts.role)
_, _, err := gwclient.AddOrgMember(context.TODO(), orgMemberAddOpts.orgname, orgMemberAddOpts.username, gwapitypes.MemberRole(orgMemberAddOpts.role))
_, _, err := gwclient.AddOrgMember(context.TODO(), orgMemberAddOpts.orgname, orgMemberAddOpts.username, gwapitypes.OrgMemberRole(orgMemberAddOpts.role))
if err != nil {
return errors.Errorf("failed to add/update organization member: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/services/configstore/action/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

type OrgMemberResponse struct {
User *types.User
Role types.MemberRole
Role types.OrgMemberRole
}

func orgMemberResponse(orgUser *readdb.OrgUser) *OrgMemberResponse {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (h *ActionHandler) CreateOrg(ctx context.Context, org *types.Organization)
ID: uuid.NewV4().String(),
OrganizationID: org.ID,
UserID: org.CreatorUserID,
MemberRole: types.MemberRoleOwner,
MemberRole: types.OrgMemberRoleOwner,
}
orgmemberj, err := json.Marshal(orgmember)
if err != nil {
Expand Down Expand Up @@ -221,8 +221,8 @@ func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error {

// AddOrgMember add/updates an org member.
// TODO(sgotti) handle invitation when implemented
func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string, role types.MemberRole) (*types.OrganizationMember, error) {
if !types.IsValidMemberRole(role) {
func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string, role types.OrgMemberRole) (*types.OrganizationMember, error) {
if !types.IsValidOrgMemberRole(role) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid role %q", role))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/configstore/action/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func (h *ActionHandler) DeleteUserToken(ctx context.Context, userRef, tokenName

type UserOrgsResponse struct {
Organization *types.Organization
Role types.MemberRole
Role types.OrgMemberRole
}

func userOrgsResponse(userOrg *readdb.UserOrg) *UserOrgsResponse {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/configstore/readdb/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func scanOrgMembers(rows *sql.Rows) ([]*types.OrganizationMember, []string, erro

type OrgUser struct {
User *types.User
Role types.MemberRole
Role types.OrgMemberRole
}

// TODO(sgotti) implement cursor fetching
Expand Down Expand Up @@ -348,7 +348,7 @@ func (r *ReadDB) GetOrgUsers(tx *db.Tx, orgID string) ([]*OrgUser, error) {

type UserOrg struct {
Organization *types.Organization
Role types.MemberRole
Role types.OrgMemberRole
}

// TODO(sgotti) implement cursor fetching
Expand Down
4 changes: 2 additions & 2 deletions internal/services/gateway/action/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *ActionHandler) IsOrgOwner(ctx context.Context, orgID string) (bool, err
if userOrg.Organization.ID != orgID {
continue
}
if userOrg.Role == cstypes.MemberRoleOwner {
if userOrg.Role == cstypes.OrgMemberRoleOwner {
return true, nil
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func (h *ActionHandler) IsProjectOwner(ctx context.Context, ownerType cstypes.Co
if userOrg.Organization.ID != ownerID {
continue
}
if userOrg.Role == cstypes.MemberRoleOwner {
if userOrg.Role == cstypes.OrgMemberRoleOwner {
return true, nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/gateway/action/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type OrgMembersResponse struct {

type OrgMemberResponse struct {
User *cstypes.User
Role cstypes.MemberRole
Role cstypes.OrgMemberRole
}

func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) (*OrgMembersResponse, error) {
Expand Down Expand Up @@ -143,7 +143,7 @@ type AddOrgMemberResponse struct {
User *cstypes.User
}

func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string, role cstypes.MemberRole) (*AddOrgMemberResponse, error) {
func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string, role cstypes.OrgMemberRole) (*AddOrgMemberResponse, error) {
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
Expand Down
10 changes: 5 additions & 5 deletions internal/services/gateway/api/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func createOrgMemberResponse(user *cstypes.User, role cstypes.MemberRole) *gwapitypes.OrgMemberResponse {
func createOrgMemberResponse(user *cstypes.User, role cstypes.OrgMemberRole) *gwapitypes.OrgMemberResponse {
return &gwapitypes.OrgMemberResponse{
User: createUserResponse(user),
Role: gwapitypes.MemberRole(role),
Role: gwapitypes.OrgMemberRole(role),
}
}

Expand Down Expand Up @@ -229,12 +229,12 @@ func (h *OrgMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func createAddOrgMemberResponse(org *cstypes.Organization, user *cstypes.User, role cstypes.MemberRole) *gwapitypes.AddOrgMemberResponse {
func createAddOrgMemberResponse(org *cstypes.Organization, user *cstypes.User, role cstypes.OrgMemberRole) *gwapitypes.AddOrgMemberResponse {
return &gwapitypes.AddOrgMemberResponse{
Organization: createOrgResponse(org),
OrgMemberResponse: gwapitypes.OrgMemberResponse{
User: createUserResponse(user),
Role: gwapitypes.MemberRole(role),
Role: gwapitypes.OrgMemberRole(role),
},
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (h *AddOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
return
}

ares, err := h.ah.AddOrgMember(ctx, orgRef, userRef, cstypes.MemberRole(req.Role))
ares, err := h.ah.AddOrgMember(ctx, orgRef, userRef, cstypes.OrgMemberRole(req.Role))
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions services/configstore/api/types/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
)

type AddOrgMemberRequest struct {
Role cstypes.MemberRole
Role cstypes.OrgMemberRole
}

type OrgMemberResponse struct {
User *cstypes.User
Role cstypes.MemberRole
Role cstypes.OrgMemberRole
}
2 changes: 1 addition & 1 deletion services/configstore/api/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ type CreateUserTokenResponse struct {

type UserOrgsResponse struct {
Organization *cstypes.Organization
Role cstypes.MemberRole
Role cstypes.OrgMemberRole
}
2 changes: 1 addition & 1 deletion services/configstore/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (c *Client) DeleteOrg(ctx context.Context, orgRef string) (*http.Response,
return c.getResponse(ctx, "DELETE", fmt.Sprintf("/orgs/%s", orgRef), nil, jsonContent, nil)
}

func (c *Client) AddOrgMember(ctx context.Context, orgRef, userRef string, role cstypes.MemberRole) (*cstypes.OrganizationMember, *http.Response, error) {
func (c *Client) AddOrgMember(ctx context.Context, orgRef, userRef string, role cstypes.OrgMemberRole) (*cstypes.OrganizationMember, *http.Response, error) {
req := &csapitypes.AddOrgMemberRequest{
Role: role,
}
Expand Down
Loading

0 comments on commit 0886349

Please sign in to comment.