Skip to content

Commit

Permalink
fix: missing organization roles to user when added as member (#389)
Browse files Browse the repository at this point in the history
Signed-off-by: Kush Sharma <[email protected]>
  • Loading branch information
kushsharma authored Oct 17, 2023
1 parent 13c0afd commit d9d6fe9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const (
Disabled State = "disabled"

AdminPermission = schema.UpdatePermission
AdminRole = schema.OwnerRelationName
AdminRelation = schema.OwnerRelationName
AdminRole = schema.RoleOrganizationOwner
MemberRole = schema.RoleOrganizationViewer
)

type Repository interface {
Expand Down
13 changes: 13 additions & 0 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ func (s Service) Create(ctx context.Context, org Organization) (Organization, er
}

func (s Service) AddMember(ctx context.Context, orgID, relationName string, principal authenticate.Principal) error {
roleID := MemberRole
if relationName == schema.OwnerRelationName {
roleID = AdminRole
}
if _, err := s.policyService.Create(ctx, policy.Policy{
RoleID: roleID,
ResourceID: orgID,
ResourceType: schema.OrganizationNamespace,
PrincipalID: principal.ID,
PrincipalType: principal.Type,
}); err != nil {
return err
}
if _, err := s.relationService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: orgID,
Expand Down
12 changes: 8 additions & 4 deletions internal/bootstrap/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ const (
SuperUserPrincipal = "app/superuser"

// Roles
RoleOrganizationViewer = "app_organization_viewer"
RoleOrganizationOwner = "app_organization_owner"

RoleProjectOwner = "app_project_owner"
RoleProjectManager = "app_project_manager"
RoleProjectViewer = "app_project_viewer"
GroupOwnerRole = "app_group_owner"
GroupMemberRole = "app_group_member"

GroupOwnerRole = "app_group_owner"
GroupMemberRole = "app_group_member"
)

var (
Expand Down Expand Up @@ -259,7 +263,7 @@ var PredefinedRoles = []RoleDefinition{
// org
{
Title: "Organization Owner",
Name: "app_organization_owner",
Name: RoleOrganizationOwner,
Permissions: []string{
"app_organization_administer",
},
Expand Down Expand Up @@ -292,7 +296,7 @@ var PredefinedRoles = []RoleDefinition{
},
{
Title: "Organization Viewer",
Name: "app_organization_viewer",
Name: RoleOrganizationViewer,
Permissions: []string{
"app_organization_get",
},
Expand Down
10 changes: 8 additions & 2 deletions test/e2e/regression/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ func (s *APIRegressionTestSuite) TestOrganizationAPI() {
s.Assert().NoError(err)
s.Assert().Equal(1, len(orgUsersResp.GetUsers()))
s.Assert().Equal(testbench.OrgAdminEmail, orgUsersResp.GetUsers()[0].Email)

orgCreatedPolicies, err := s.testBench.AdminClient.ListPolicies(ctxOrgAdminAuth, &frontierv1beta1.ListPoliciesRequest{
OrgId: createOrgResp.GetOrganization().GetId(),
})
s.Assert().NoError(err)
s.Assert().Equal(1, len(orgCreatedPolicies.GetPolicies()))
})
s.Run("2. user attached to an org as member should have no basic permission other than membership", func() {
createOrgResp, err := s.testBench.Client.CreateOrganization(ctxOrgAdminAuth, &frontierv1beta1.CreateOrganizationRequest{
Expand Down Expand Up @@ -1021,7 +1027,7 @@ func (s *APIRegressionTestSuite) TestRelationAPI() {
_, err = s.testBench.Client.CreateRelation(ctxOrgAdminAuth, &frontierv1beta1.CreateRelationRequest{Body: &frontierv1beta1.RelationRequestBody{
Object: schema.JoinNamespaceAndResourceID(schema.OrganizationNamespace, existingOrg.GetOrganization().GetId()),
Subject: schema.JoinNamespaceAndResourceID(schema.UserPrincipal, createUserResp.GetUser().GetId()),
Relation: organization.AdminRole,
Relation: organization.AdminRelation,
}})
s.Assert().NoError(err)

Expand Down Expand Up @@ -1050,7 +1056,7 @@ func (s *APIRegressionTestSuite) TestRelationAPI() {
_, err = s.testBench.Client.CreateRelation(ctxOrgAdminAuth, &frontierv1beta1.CreateRelationRequest{Body: &frontierv1beta1.RelationRequestBody{
Object: schema.JoinNamespaceAndResourceID(schema.OrganizationNamespace, existingOrg.GetOrganization().GetId()),
Subject: schema.JoinNamespaceAndResourceID(schema.UserPrincipal, createUserResp.GetUser().GetId()),
Relation: organization.AdminRole,
Relation: organization.AdminRelation,
}})
s.Assert().NoError(err)

Expand Down

0 comments on commit d9d6fe9

Please sign in to comment.