mirror of
https://github.com/hcengineering/platform.git
synced 2025-06-05 07:14:22 +00:00
uberf-7764: improve space permissions query (#6236)
Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
parent
7c1a1619e8
commit
c675f45491
@ -1495,59 +1495,80 @@ export const permissionsStore = writable<PermissionsStore>({
|
|||||||
ap: {},
|
ap: {},
|
||||||
whitelist: new Set()
|
whitelist: new Set()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const spaceTypesQuery = createQuery(true)
|
||||||
const permissionsQuery = createQuery(true)
|
const permissionsQuery = createQuery(true)
|
||||||
|
type TargetClassesProjection = Record<Ref<Class<Space>>, number>
|
||||||
|
|
||||||
permissionsQuery.query(core.class.Space, {}, (res) => {
|
spaceTypesQuery.query(core.class.SpaceType, {}, (types) => {
|
||||||
const whitelistedSpaces = new Set<Ref<Space>>()
|
const targetClasses = types.reduce<TargetClassesProjection>((acc, st) => {
|
||||||
const permissionsBySpace: PermissionsBySpace = {}
|
acc[st.targetClass] = 1
|
||||||
const accountsByPermission: AccountsByPermission = {}
|
return acc
|
||||||
const client = getClient()
|
}, {})
|
||||||
const hierarchy = client.getHierarchy()
|
|
||||||
const me = getCurrentAccount()
|
|
||||||
|
|
||||||
for (const s of res) {
|
permissionsQuery.query(
|
||||||
if (hierarchy.isDerived(s._class, core.class.TypedSpace)) {
|
core.class.Space,
|
||||||
const type = client.getModel().findAllSync(core.class.SpaceType, { _id: (s as TypedSpace).type })[0]
|
{},
|
||||||
const mixin = type?.targetClass
|
(res) => {
|
||||||
|
const whitelistedSpaces = new Set<Ref<Space>>()
|
||||||
|
const permissionsBySpace: PermissionsBySpace = {}
|
||||||
|
const accountsByPermission: AccountsByPermission = {}
|
||||||
|
const client = getClient()
|
||||||
|
const hierarchy = client.getHierarchy()
|
||||||
|
const me = getCurrentAccount()
|
||||||
|
|
||||||
if (mixin === undefined) {
|
for (const s of res) {
|
||||||
permissionsBySpace[s._id] = new Set()
|
if (hierarchy.isDerived(s._class, core.class.TypedSpace)) {
|
||||||
accountsByPermission[s._id] = {}
|
const type = client.getModel().findAllSync(core.class.SpaceType, { _id: (s as TypedSpace).type })[0]
|
||||||
continue
|
const mixin = type?.targetClass
|
||||||
}
|
|
||||||
|
|
||||||
const asMixin = hierarchy.as(s, mixin)
|
if (mixin === undefined) {
|
||||||
const roles = client.getModel().findAllSync(core.class.Role, { attachedTo: type._id })
|
permissionsBySpace[s._id] = new Set()
|
||||||
const myRoles = roles.filter((r) => ((asMixin as any)[r._id] ?? []).includes(me._id))
|
accountsByPermission[s._id] = {}
|
||||||
permissionsBySpace[s._id] = new Set(myRoles.flatMap((r) => r.permissions))
|
continue
|
||||||
|
|
||||||
accountsByPermission[s._id] = {}
|
|
||||||
|
|
||||||
for (const role of roles) {
|
|
||||||
const assignment: Array<Ref<Account>> = (asMixin as any)[role._id] ?? []
|
|
||||||
|
|
||||||
if (assignment.length === 0) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const permissionId of role.permissions) {
|
|
||||||
if (accountsByPermission[s._id][permissionId] === undefined) {
|
|
||||||
accountsByPermission[s._id][permissionId] = new Set()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assignment.forEach((acc) => accountsByPermission[s._id][permissionId].add(acc))
|
const asMixin = hierarchy.as(s, mixin)
|
||||||
|
const roles = client.getModel().findAllSync(core.class.Role, { attachedTo: type._id })
|
||||||
|
const myRoles = roles.filter((r) => ((asMixin as any)[r._id] ?? []).includes(me._id))
|
||||||
|
permissionsBySpace[s._id] = new Set(myRoles.flatMap((r) => r.permissions))
|
||||||
|
|
||||||
|
accountsByPermission[s._id] = {}
|
||||||
|
|
||||||
|
for (const role of roles) {
|
||||||
|
const assignment: Array<Ref<Account>> = (asMixin as any)[role._id] ?? []
|
||||||
|
|
||||||
|
if (assignment.length === 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const permissionId of role.permissions) {
|
||||||
|
if (accountsByPermission[s._id][permissionId] === undefined) {
|
||||||
|
accountsByPermission[s._id][permissionId] = new Set()
|
||||||
|
}
|
||||||
|
|
||||||
|
assignment.forEach((acc) => accountsByPermission[s._id][permissionId].add(acc))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
whitelistedSpaces.add(s._id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
whitelistedSpaces.add(s._id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
permissionsStore.set({
|
permissionsStore.set({
|
||||||
ps: permissionsBySpace,
|
ps: permissionsBySpace,
|
||||||
ap: accountsByPermission,
|
ap: accountsByPermission,
|
||||||
whitelist: whitelistedSpaces
|
whitelist: whitelistedSpaces
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
{
|
||||||
|
projection: {
|
||||||
|
_id: 1,
|
||||||
|
type: 1,
|
||||||
|
...targetClasses
|
||||||
|
} as any
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
export function getCollaborationUser (): CollaborationUser {
|
export function getCollaborationUser (): CollaborationUser {
|
||||||
|
Loading…
Reference in New Issue
Block a user