-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor sdk-utils to export additional functions and types
- Loading branch information
1 parent
c55daf1
commit 466adbd
Showing
5 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts
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,77 @@ | ||
import { db } from 'astro:db'; | ||
import { tsPermissions, tsUsers } from '../../db/tsTables'; | ||
import type { AvailableLists, PermissionsList } from '../types'; | ||
import { combineRanks, verifyRank } from '../utils'; | ||
|
||
/** | ||
* Retrieves a list of permissions based on the specified list type. | ||
* | ||
* @param list - The type of list to retrieve. Can be one of 'all', 'owners', 'admins', 'editors', or 'visitors'. | ||
* @returns A promise that resolves to an array of permissions lists. | ||
* | ||
* The function performs the following actions based on the list type: | ||
* - 'all': Retrieves all users and their permissions, then categorizes them into owners, admins, editors, and visitors. | ||
* - 'owners': Retrieves users with 'owner' permissions. | ||
* - 'admins': Retrieves users with 'admin' permissions. | ||
* - 'editors': Retrieves users with 'editor' permissions. | ||
* - 'visitors': Retrieves users with 'visitor' permissions. | ||
* | ||
* The function uses the following helper functions: | ||
* - `verifyRank`: Verifies the rank of users based on the existing users and current permitted users. | ||
* - `combineRanks`: Combines users of a specific rank into a single list. | ||
* | ||
* @example | ||
* ```typescript | ||
* const owners = await getPermissionsLists('owners'); | ||
* console.log(owners); | ||
* ``` | ||
*/ | ||
export async function getPermissionsLists(list: AvailableLists): Promise<PermissionsList[]> { | ||
switch (list) { | ||
case 'all': { | ||
const currentPermittedUsers = await db.select().from(tsPermissions); | ||
const existingUsers = await db.select().from(tsUsers); | ||
|
||
const owners = verifyRank(existingUsers, currentPermittedUsers, 'owner'); | ||
|
||
const admins = verifyRank(existingUsers, currentPermittedUsers, 'admin'); | ||
|
||
const editors = verifyRank(existingUsers, currentPermittedUsers, 'editor'); | ||
|
||
const visitors = verifyRank(existingUsers, currentPermittedUsers, 'visitor'); | ||
|
||
return [ | ||
...combineRanks('owner', owners), | ||
...combineRanks('admin', admins), | ||
...combineRanks('editor', editors), | ||
...combineRanks('visitor', visitors), | ||
]; | ||
} | ||
case 'owners': { | ||
const currentPermittedUsers = await db.select().from(tsPermissions); | ||
const existingUsers = await db.select().from(tsUsers); | ||
|
||
return verifyRank(existingUsers, currentPermittedUsers, 'owner'); | ||
} | ||
case 'admins': { | ||
const currentPermittedUsers = await db.select().from(tsPermissions); | ||
const existingUsers = await db.select().from(tsUsers); | ||
|
||
return verifyRank(existingUsers, currentPermittedUsers, 'admin'); | ||
} | ||
case 'editors': { | ||
const currentPermittedUsers = await db.select().from(tsPermissions); | ||
const existingUsers = await db.select().from(tsUsers); | ||
|
||
return verifyRank(existingUsers, currentPermittedUsers, 'editor'); | ||
} | ||
case 'visitors': { | ||
const currentPermittedUsers = await db.select().from(tsPermissions); | ||
const existingUsers = await db.select().from(tsUsers); | ||
|
||
return verifyRank(existingUsers, currentPermittedUsers, 'visitor'); | ||
} | ||
} | ||
} | ||
|
||
export default getPermissionsLists; |
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
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