Skip to content

Commit f4ab255

Browse files
authored
Merge pull request #75 from unisoncomputing/cp/org-creation-ga
Allow everyone to create Orgs
2 parents 2aef1c4 + 03667e6 commit f4ab255

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

src/Share/Web/Authorization.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ assertUserHasProjectPermission rolePermission mayReqUserId projId = do
667667
guardM $ PG.runTransaction $ do
668668
Q.userHasProjectPermission mayReqUserId projId rolePermission
669669

670+
assertUserIsSuperadmin :: UserId -> MaybeT WebApp ()
671+
assertUserIsSuperadmin userId = do
672+
guardM . lift . PG.runTransaction $ Q.isSuperadmin userId
673+
670674
maybePermissionFailure :: WrapperPermissions -> MaybeT WebApp a -> WebApp (Either AuthZFailure a)
671675
maybePermissionFailure perm m = do
672676
runMaybeT m >>= \case
@@ -687,9 +691,11 @@ checkUserIsOrgMember reqUserId orgUserId = do
687691
PG.runTransaction $
688692
Q.isOrgMember reqUserId orgUserId
689693

690-
checkCreateOrg :: UserId -> UserId -> WebApp (Either AuthZFailure AuthZ.AuthZReceipt)
691-
checkCreateOrg reqUserId ownerUserId = maybePermissionFailure (OrgPermission (OrgCreate ownerUserId)) $ do
692-
guardM . PG.runTransaction $ Q.isSuperadmin reqUserId
694+
checkCreateOrg :: UserId -> UserId -> Bool -> WebApp (Either AuthZFailure AuthZ.AuthZReceipt)
695+
checkCreateOrg reqUserId ownerUserId isCommercial = maybePermissionFailure (OrgPermission (OrgCreate ownerUserId)) $ do
696+
if isCommercial
697+
then assertUserIsSuperadmin reqUserId
698+
else assertUserIsSuperadmin reqUserId <|> assertUsersEqual reqUserId ownerUserId
693699
pure $ AuthZ.UnsafeAuthZReceipt Nothing
694700

695701
checkReadOrgRolesList :: UserId -> OrgId -> WebApp (Either AuthZFailure AuthZ.AuthZReceipt)

src/Share/Web/Share/Orgs/Impl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ server =
6161
orgCreateEndpoint :: UserId -> CreateOrgRequest -> WebApp OrgDisplayInfo
6262
orgCreateEndpoint callerUserId (CreateOrgRequest {name, handle, avatarUrl, email, owner = ownerHandle, isCommercial}) = do
6363
User {user_id = ownerUserId} <- PG.runTransaction (UserQ.userByHandle ownerHandle) `whenNothingM` respondError (EntityMissing (ErrorID "missing-user") "Owner not found")
64-
authZReceipt <- AuthZ.permissionGuard $ AuthZ.checkCreateOrg callerUserId ownerUserId
64+
authZReceipt <- AuthZ.permissionGuard $ AuthZ.checkCreateOrg callerUserId ownerUserId isCommercial
6565
orgId <- PG.runTransactionOrRespondError $ OrgOps.createOrg authZReceipt name handle email avatarUrl ownerUserId callerUserId isCommercial
6666
PG.runTransaction $ OrgQ.orgDisplayInfoOf id orgId
6767

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"body": {
3+
"isCommercial": false,
4+
"orgId": "ORG-<UUID>",
5+
"user": {
6+
"avatarUrl": "https://example.com/staples.png",
7+
"handle": "orgco",
8+
"name": "OrgCo",
9+
"userId": "U-<UUID>"
10+
}
11+
},
12+
"status": [
13+
{
14+
"status_code": 200
15+
}
16+
]
17+
}

transcripts/share-apis/orgs/run.zsh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ new_member="$(create_user newuser)"
1010
# Org creation workflow
1111

1212
# Should fail
13-
fetch "$unauthorized_user" POST org-create-unauthorized '/orgs' '{
13+
fetch "$unauthorized_user" POST org-create-unauthorized-commercial '/orgs' '{
1414
"name": "ACME",
1515
"handle": "acme",
1616
"avatarUrl": "https://example.com/anvil.png",
@@ -19,6 +19,16 @@ fetch "$unauthorized_user" POST org-create-unauthorized '/orgs' '{
1919
"isCommercial": true
2020
}'
2121

22+
# Should succeed, regular users can create non-commercial orgs
23+
fetch "$unauthorized_user" POST org-create-non-commercial-non-admin '/orgs' '{
24+
"name": "OrgCo",
25+
"handle": "orgco",
26+
"avatarUrl": "https://example.com/staples.png",
27+
"owner": "unauthorized",
28+
"email": "[email protected]",
29+
"isCommercial": false
30+
}'
31+
2232
# Admin can create an org and assign any owner.
2333
fetch "$admin_user" POST org-create-by-admin-non-commercial '/orgs' '{
2434
"name": "Noncom",

0 commit comments

Comments
 (0)