-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(console): update tenant settings access per user tenant scopes
- Loading branch information
1 parent
2ccc54a
commit ac1da23
Showing
11 changed files
with
288 additions
and
166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useLogto } from '@logto/react'; | ||
import { TenantScope, getTenantOrganizationId } from '@logto/schemas'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
|
||
import { TenantsContext } from '@/contexts/TenantsProvider'; | ||
|
||
const useCurrentTenantScopes = () => { | ||
const { currentTenantId, isInitComplete } = useContext(TenantsContext); | ||
const { isAuthenticated, getOrganizationTokenClaims } = useLogto(); | ||
|
||
const [scopes, setScopes] = useState<string[]>([]); | ||
const [canInviteMember, setCanInviteMember] = useState(false); | ||
const [canRemoveMember, setCanRemoveMember] = useState(false); | ||
const [canUpdateMemberRole, setCanUpdateMemberRole] = useState(false); | ||
const [canManageTenant, setCanManageTenant] = useState(false); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (isAuthenticated && isInitComplete) { | ||
const organizationId = getTenantOrganizationId(currentTenantId); | ||
const claims = await getOrganizationTokenClaims(organizationId); | ||
const allScopes = claims?.scope?.split(' ') ?? []; | ||
setScopes(allScopes); | ||
|
||
for (const scope of allScopes) { | ||
switch (scope) { | ||
case TenantScope.InviteMember: { | ||
setCanInviteMember(true); | ||
break; | ||
} | ||
case TenantScope.RemoveMember: { | ||
setCanRemoveMember(true); | ||
break; | ||
} | ||
case TenantScope.UpdateMemberRole: { | ||
setCanUpdateMemberRole(true); | ||
break; | ||
} | ||
case TenantScope.ManageTenant: { | ||
setCanManageTenant(true); | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
})(); | ||
}, [currentTenantId, getOrganizationTokenClaims, isAuthenticated, isInitComplete]); | ||
|
||
return { | ||
canInviteMember, | ||
canRemoveMember, | ||
canUpdateMemberRole, | ||
canManageTenant, | ||
scopes, | ||
}; | ||
}; | ||
|
||
export default useCurrentTenantScopes; |
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
Oops, something went wrong.