Small improvements.
This commit is contained in:
parent
0252dd3188
commit
45843e328d
|
@ -16,8 +16,8 @@ export const hasAdminPermissions = sdk.users.hasAdminPermissions
|
||||||
export const hasBuilderPermissions = sdk.users.hasBuilderPermissions
|
export const hasBuilderPermissions = sdk.users.hasBuilderPermissions
|
||||||
export const hasAppBuilderPermissions = sdk.users.hasAppBuilderPermissions
|
export const hasAppBuilderPermissions = sdk.users.hasAppBuilderPermissions
|
||||||
|
|
||||||
async function getGroups(groupIds: string[]) {
|
async function getGroups(groupIds?: string[]) {
|
||||||
if (groupIds.length) {
|
if (groupIds?.length) {
|
||||||
const db = context.getGlobalDB()
|
const db = context.getGlobalDB()
|
||||||
return await db.getMultiple<UserGroup>(groupIds, { allowMissing: true })
|
return await db.getMultiple<UserGroup>(groupIds, { allowMissing: true })
|
||||||
}
|
}
|
||||||
|
@ -34,21 +34,16 @@ export async function creatorsInList(
|
||||||
groups = await getGroups(groupIds)
|
groups = await getGroups(groupIds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return users.map(user => isCreatorSync(user, groups || []))
|
return users.map(user => isCreatorSync(user, groups))
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetches groups if no provided, but is async and shouldn't be looped with
|
// fetches groups if no provided, but is async and shouldn't be looped with
|
||||||
export async function isCreatorAsync(
|
export async function isCreatorAsync(user: User | ContextUser) {
|
||||||
user: User | ContextUser,
|
const groups = await getGroups(user.userGroups)
|
||||||
groups?: UserGroup[]
|
|
||||||
) {
|
|
||||||
if (!groups) {
|
|
||||||
groups = await getGroups(user.userGroups || [])
|
|
||||||
}
|
|
||||||
return isCreatorSync(user, groups)
|
return isCreatorSync(user, groups)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCreatorSync(user: User | ContextUser, groups: UserGroup[]) {
|
export function isCreatorSync(user: User | ContextUser, groups?: UserGroup[]) {
|
||||||
const isCreatorByUserDefinition = sdk.users.isCreator(user)
|
const isCreatorByUserDefinition = sdk.users.isCreator(user)
|
||||||
if (!isCreatorByUserDefinition && user) {
|
if (!isCreatorByUserDefinition && user) {
|
||||||
return isCreatorByGroupMembership(user, groups)
|
return isCreatorByGroupMembership(user, groups)
|
||||||
|
@ -58,12 +53,12 @@ export function isCreatorSync(user: User | ContextUser, groups: UserGroup[]) {
|
||||||
|
|
||||||
function isCreatorByGroupMembership(
|
function isCreatorByGroupMembership(
|
||||||
user: User | ContextUser,
|
user: User | ContextUser,
|
||||||
groups: UserGroup[]
|
groups?: UserGroup[]
|
||||||
) {
|
) {
|
||||||
const userGroups = groups.filter(
|
const userGroups = groups?.filter(
|
||||||
group => user.userGroups?.indexOf(group._id!) !== -1
|
group => user.userGroups?.indexOf(group._id!) !== -1
|
||||||
)
|
)
|
||||||
if (userGroups.length > 0) {
|
if (userGroups && userGroups.length > 0) {
|
||||||
return userGroups.some(group =>
|
return userGroups.some(group =>
|
||||||
Object.values(group.roles || {}).includes(BUILTIN_ROLE_IDS.ADMIN)
|
Object.values(group.roles || {}).includes(BUILTIN_ROLE_IDS.ADMIN)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue