EQMS-669 Add role creation utilities (#5273)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov 2024-04-10 12:59:51 +07:00 committed by GitHub
parent 9d414439b9
commit babbc490f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 74 additions and 40 deletions

View File

@ -14,11 +14,9 @@
-->
<script lang="ts">
import presentation, { getClient } from '@hcengineering/presentation'
import setting from '@hcengineering/setting'
import { createSpaceTypeRole } from '@hcengineering/setting'
import { Modal, ModernEditbox } from '@hcengineering/ui'
import core, { AttachedData, Attribute, PropertyType, Ref, Role, SpaceType } from '@hcengineering/core'
import { ArrOf, TypeRef } from '@hcengineering/model'
import { getEmbeddedLabel } from '@hcengineering/platform'
import { AttachedData, Role, SpaceType } from '@hcengineering/core'
import settingRes from '../../../plugin'
import { clearSettingsStore } from '../../../store'
@ -37,28 +35,7 @@
return
}
const roleId = await client.addCollection(
core.class.Role,
core.space.Model,
type._id,
type._class,
'roles',
roleData
)
// Create role as an attribute of space type's mixin
await client.createDoc(
core.class.Attribute,
core.space.Model,
{
name: roleId,
attributeOf: type.targetClass,
type: ArrOf(TypeRef(core.class.Account)),
label: getEmbeddedLabel(`Role: ${name}`),
editor: setting.component.RoleAssignmentEditor
},
`role-${roleId}` as Ref<Attribute<PropertyType>>
)
await createSpaceTypeRole(client, type, roleData)
}
$: canSave = roleData.name.trim() !== ''

View File

@ -34,6 +34,7 @@
"dependencies": {
"@hcengineering/platform": "^0.6.9",
"@hcengineering/core": "^0.6.28",
"@hcengineering/model": "^0.6.7",
"@hcengineering/templates": "^0.6.7",
"@hcengineering/ui": "^0.6.11"
},

View File

@ -13,14 +13,29 @@
// limitations under the License.
//
import core, { Class, ClassifierKind, Data, Doc, Ref, SpaceType, TxOperations } from '@hcengineering/core'
import core, {
AttachedData,
Attribute,
Class,
ClassifierKind,
Data,
PropertyType,
Ref,
Role,
Space,
SpaceType,
TxOperations
} from '@hcengineering/core'
import { ArrOf, TypeRef } from '@hcengineering/model'
import { getEmbeddedLabel } from '@hcengineering/platform'
import setting from './index'
export async function createSpaceType (
export async function createSpaceType<T extends SpaceType> (
client: TxOperations,
data: Omit<Data<SpaceType>, 'targetClass'>,
_id: Ref<SpaceType>
): Promise<Ref<SpaceType>> {
data: Omit<Data<T>, 'targetClass'>,
_id: Ref<T>,
_class: Ref<Class<T>> = core.class.SpaceType
): Promise<Ref<T>> {
const descriptorObj = client.getModel().findObject(data.descriptor)
if (descriptorObj === undefined) {
throw new Error('Descriptor is not found in the model')
@ -29,7 +44,7 @@ export async function createSpaceType (
const baseClassClazz = client.getHierarchy().getClass(descriptorObj.baseClass)
// NOTE: it is important for this id to be consistent when re-creating the same
// space type with the same id as it will happen during every migration if type is created by the system
const spaceTypeMixinId = `${_id}:type:mixin` as Ref<Class<Doc>>
const spaceTypeMixinId = `${_id}:type:mixin` as Ref<Class<Space>>
await client.createDoc(
core.class.Mixin,
core.space.Model,
@ -43,15 +58,56 @@ export async function createSpaceType (
)
return await client.createDoc(
core.class.SpaceType,
_class,
core.space.Model,
{
shortDescription: data.shortDescription,
descriptor: data.descriptor,
roles: data.roles,
name: data.name,
targetClass: spaceTypeMixinId
},
{ ...data, targetClass: spaceTypeMixinId } as unknown as Data<T>,
_id
)
}
export async function createSpaceTypeRole (
client: TxOperations,
type: Pick<SpaceType, '_id' | '_class' | 'targetClass'>,
data: AttachedData<Role>,
_id?: Ref<Role> | undefined
): Promise<Ref<Role>> {
const name = data.name.trim()
const roleId = await client.addCollection(
core.class.Role,
core.space.Model,
type._id,
type._class,
'roles',
data,
_id
)
await client.createDoc(
core.class.Attribute,
core.space.Model,
{
name: roleId,
attributeOf: type.targetClass,
type: ArrOf(TypeRef(core.class.Account)),
label: getEmbeddedLabel(`Role: ${name}`),
editor: setting.component.RoleAssignmentEditor
},
`role-${roleId}` as Ref<Attribute<PropertyType>>
)
return roleId
}
export async function createSpaceTypeRoles (
tx: TxOperations,
typeId: Ref<SpaceType>,
roles: Pick<Role, '_id' | 'name' | 'permissions'>[]
): Promise<void> {
const spaceType = await tx.findOne(core.class.SpaceType, { _id: typeId })
if (spaceType === undefined) return
for (const { _id, name, permissions } of roles) {
await createSpaceTypeRole(tx, spaceType, { name, permissions }, _id)
}
}