Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZQMS-834: Fix roles ids and names #5520

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions models/core/src/spaceType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { ArrOf, Prop, TypeRef, type Builder } from '@hcengineering/model'
import { type Asset } from '@hcengineering/platform'
import { getRoleAttributeBaseProps } from '@hcengineering/core'
import { getRoleAttributeLabel } from '@hcengineering/core'

import { TSpacesTypeData } from './security'
import core from './component'
Expand All @@ -30,10 +30,10 @@ const roles = [

export function defineSpaceType (builder: Builder): void {
for (const role of roles) {
const { label, id } = getRoleAttributeBaseProps(role, role._id)
const label = getRoleAttributeLabel(role.name)
const roleAssgtType = ArrOf(TypeRef(core.class.Account))

Prop(roleAssgtType, label)(TSpacesTypeData.prototype, id)
Prop(roleAssgtType, label)(TSpacesTypeData.prototype, role._id)
}

builder.createModel(TSpacesTypeData)
Expand Down
26 changes: 2 additions & 24 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import {
Account,
AccountRole,
AnyAttribute,
AttachedData,
AttachedDoc,
Attribute,
Class,
ClassifierKind,
Collection,
Expand All @@ -36,7 +34,6 @@ import {
IndexKind,
Obj,
Permission,
PropertyType,
Ref,
Role,
Space,
Expand Down Expand Up @@ -609,27 +606,8 @@ export async function checkPermission (
/**
* @public
*/
export interface RoleAttributeBaseProps {
label: IntlString
id: Ref<Attribute<PropertyType>>
}

/**
* @public
*/
export function getRoleAttributeBaseProps (data: AttachedData<Role>, roleId: Ref<Role>): RoleAttributeBaseProps {
const name = data.name.trim()
const label = getEmbeddedLabel(`Role: ${name}`)
const id = getRoleAttributeId(roleId)

return { label, id }
}

/**
* @public
*/
export function getRoleAttributeId (roleId: Ref<Role>): Ref<Attribute<PropertyType>> {
return `role-${roleId}` as Ref<Attribute<PropertyType>>
export function getRoleAttributeLabel (roleName: string): IntlString {
return getEmbeddedLabel(`Role: ${roleName.trim()}`)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { IntlString } from '@hcengineering/platform'
import { ButtonKind, ButtonSize } from '@hcengineering/ui'

export let object: TypedSpace
export let object: TypedSpace | undefined
export let label: IntlString
export let value: Ref<Account>[]
export let onChange: ((refs: Ref<Account>[]) => void) | undefined
Expand All @@ -30,13 +30,15 @@
$: members = object?.members ?? []
</script>

<AccountArrayEditor
{value}
{label}
readonly={readonly || (object.private && members.length === 0)}
includeItems={object.private ? members : undefined}
{onChange}
{size}
width={width ?? 'min-content'}
{kind}
/>
{#if object !== undefined}
<AccountArrayEditor
{value}
{label}
readonly={readonly || (object.private && members.length === 0)}
includeItems={object.private ? members : undefined}
{onChange}
{size}
width={width ?? 'min-content'}
{kind}
/>
{/if}
36 changes: 12 additions & 24 deletions plugins/setting/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@

import core, {
AttachedData,
Attribute,
Class,
ClassifierKind,
Data,
PropertyType,
Ref,
Role,
Space,
SpaceType,
TxOperations,
TypeAny as TypeAnyType,
getRoleAttributeBaseProps
getRoleAttributeLabel
} from '@hcengineering/core'
import { TypeAny } from '@hcengineering/model'
import { getEmbeddedLabel, IntlString } from '@hcengineering/platform'
Expand Down Expand Up @@ -71,18 +69,13 @@ export async function createSpaceType<T extends SpaceType> (
export interface RoleAttributeProps {
label: IntlString
roleType: TypeAnyType
id: Ref<Attribute<PropertyType>>
}

export function getRoleAttributeProps (data: AttachedData<Role>, roleId: Ref<Role>): RoleAttributeProps {
const baseProps = getRoleAttributeBaseProps(data, roleId)
const roleType = TypeAny(
setting.component.RoleAssignmentEditor,
baseProps.label,
setting.component.RoleAssignmentEditor
)
export function getRoleAttributeProps (name: string): RoleAttributeProps {
const label = getRoleAttributeLabel(name)
const roleType = TypeAny(setting.component.RoleAssignmentEditor, label, setting.component.RoleAssignmentEditor)

return { ...baseProps, roleType }
return { label, roleType }
}

export async function createSpaceTypeRole (
Expand All @@ -101,19 +94,14 @@ export async function createSpaceTypeRole (
_id
)

const { label, roleType, id } = getRoleAttributeProps(data, roleId)
const { label, roleType } = getRoleAttributeProps(data.name)

await client.createDoc(
core.class.Attribute,
core.space.Model,
{
name: roleId,
attributeOf: type.targetClass,
type: roleType,
label
},
id
)
await client.createDoc(core.class.Attribute, core.space.Model, {
name: roleId,
attributeOf: type.targetClass,
type: roleType,
label
})

return roleId
}
Expand Down
10 changes: 7 additions & 3 deletions server-plugins/setting-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//

import contact, { Person, PersonAccount, getFirstName, getLastName } from '@hcengineering/contact'
import core, { Account, Doc, Ref, Role, Tx, TxProcessor, TxUpdateDoc, getRoleAttributeId } from '@hcengineering/core'
import core, { Account, Doc, Ref, Role, Tx, TxProcessor, TxUpdateDoc } from '@hcengineering/core'
import { getEmbeddedLabel, translate } from '@hcengineering/platform'
import type { TriggerControl } from '@hcengineering/server-core'
import setting, { Integration } from '@hcengineering/setting'
Expand Down Expand Up @@ -101,8 +101,12 @@ export async function OnRoleNameUpdate (tx: Tx, control: TriggerControl): Promis
if (updateTx.operations?.name === undefined) return []

// Update the related mixin attribute
const roleAttrId = getRoleAttributeId(updateTx.objectId)
const updAttrTx = control.txFactory.createTxUpdateDoc(core.class.Attribute, core.space.Model, roleAttrId, {
const roleAttribute = await control.modelDb.findOne(core.class.Attribute, {
name: updateTx.objectId
})
if (roleAttribute === undefined) return []

const updAttrTx = control.txFactory.createTxUpdateDoc(core.class.Attribute, core.space.Model, roleAttribute._id, {
label: getEmbeddedLabel(updateTx.operations.name)
})

Expand Down