|
| 1 | +--- |
| 2 | +description: >- |
| 3 | + Use the UI Property Permissions to restrict access to specific properties in |
| 4 | + the Backoffice UI. |
| 5 | +--- |
| 6 | + |
| 7 | +# Property Level UI Permissions |
| 8 | + |
| 9 | +## Document Property Value User Permissions |
| 10 | + |
| 11 | +Umbraco provides a feature called Document Property Value User Permissions. This feature can restrict access to specific Document property values for certain user groups. By default, all the built-in User Groups have read and write permissions for all properties. However, you can limit a User Group's permissions for specific properties through the UI. |
| 12 | + |
| 13 | +If a User Group doesn't have write access to a property, the property will be read-only for that User Group. If a User Group doesn't have read access to a property, the property will be hidden from that User Group. |
| 14 | + |
| 15 | +{% hint style="info" %} |
| 16 | +The Document Property Value User Permissions are not enforced on the server side. This means a user can still access the property value through the API, even if the property is restricted in the UI. |
| 17 | +{% endhint %} |
| 18 | + |
| 19 | +## Write custom Property Level Permissions |
| 20 | + |
| 21 | +It is possible to manipulate the permissions via code. This can be achieved through the Guard Managers available on all Content-Type-based Workspace Contexts. |
| 22 | + |
| 23 | +These are the available guards: |
| 24 | + |
| 25 | +* `propertyViewGuard` - Manages rules for the visibility of properties. |
| 26 | +* `propertyWriteGuard` - Manages rules for the writability of properties. |
| 27 | +* `readOnlyGuard` (This will be removed in the future. Use `propertyWriteGuard` instead) |
| 28 | + |
| 29 | +The following guide demonstrates how to implement custom rules from a Workspace Context that appends rules to the Guard Managers. |
| 30 | + |
| 31 | +### Register a Workspace Context |
| 32 | + |
| 33 | +Register a [Workspace Context](https://github.com/madsrasmussen/UmbracoDocs/blob/180d6e9eb7ab722a24b7b209c71de03cbe811e00/15/umbraco-cms/customizing/extending-overview/extension-types/workspaces/workspace-context.md) to enable appending code to run when a workspace is initialized. |
| 34 | + |
| 35 | +**Manifest** |
| 36 | + |
| 37 | +{% code title="manifest.ts" %} |
| 38 | +```typescript |
| 39 | +import { UMB_WORKSPACE_CONDITION_ALIAS } from "@umbraco-cms/backoffice/workspace"; |
| 40 | +import { UMB_DOCUMENT_WORKSPACE_ALIAS } from "@umbraco-cms/backoffice/document"; |
| 41 | + |
| 42 | +const manifest: UmbExtensionManifest = { |
| 43 | + type: "workspaceContext", |
| 44 | + name: "My Document Property Permission Workspace Context", |
| 45 | + alias: "My.WorkspaceContext.DocumentPropertyPermission", |
| 46 | + api: () => import("./my-document-property-permission.workspace-context.js"), |
| 47 | + conditions: [ |
| 48 | + { |
| 49 | + alias: UMB_WORKSPACE_CONDITION_ALIAS, |
| 50 | + match: UMB_DOCUMENT_WORKSPACE_ALIAS, |
| 51 | + }, |
| 52 | + ], |
| 53 | +}; |
| 54 | +``` |
| 55 | +{% endcode %} |
| 56 | + |
| 57 | +### Write a general rule |
| 58 | + |
| 59 | +The following example adds code for the Workspace Context to set up a single rule preventing writing to all properties. |
| 60 | + |
| 61 | +**Workspace Context** |
| 62 | + |
| 63 | +{% code title="WorkspaceContext.ts" %} |
| 64 | +```typescript |
| 65 | +import { UmbControllerBase } from "@umbraco-cms/backoffice/class-api"; |
| 66 | +import type { UmbControllerHost } from "@umbraco-cms/backoffice/controller-api"; |
| 67 | +import { UMB_DOCUMENT_WORKSPACE_CONTEXT } from "@umbraco-cms/backoffice/document"; |
| 68 | +import { UmbVariantId } from "@umbraco-cms/backoffice/variant"; |
| 69 | + |
| 70 | +export class MyDocumentPropertyPermissionWorkspaceContext extends UmbControllerBase { |
| 71 | + constructor(host: UmbControllerHost) { |
| 72 | + super(host); |
| 73 | + |
| 74 | + // Consume the document workspace context |
| 75 | + this.consumeContext( |
| 76 | + UMB_DOCUMENT_WORKSPACE_CONTEXT, |
| 77 | + (context) => { |
| 78 | + |
| 79 | + // Create a rule: |
| 80 | + const rule = { |
| 81 | + unique: 'myCustomRuleIdentifyer', |
| 82 | + permitted: false, |
| 83 | + message: "None of these properties are writable because of my custom restriction.", |
| 84 | + } |
| 85 | + // Add the rule to the write guard |
| 86 | + context?.propertyWriteGuard.addRule(rule); |
| 87 | + } |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +export { MyDocumentPropertyPermissionWorkspaceContext as api }; |
| 93 | +``` |
| 94 | +{% endcode %} |
| 95 | + |
| 96 | +This showed how to append a general rule to all properties or variants. This can be made more specific. Therefore, the following example shows how to make a rule that applies to a specific property. |
| 97 | + |
| 98 | +### Write a rule for a specific property |
| 99 | + |
| 100 | +The following example adds code to retrieve the `unique` value for a given property. This is then used to create a rule that only prevents writing to that property. |
| 101 | + |
| 102 | +**Workspace Context** |
| 103 | + |
| 104 | +{% code title="WorkspaceContext.ts" %} |
| 105 | +```typescript |
| 106 | +import { UmbControllerBase } from "@umbraco-cms/backoffice/class-api"; |
| 107 | +import type { UmbControllerHost } from "@umbraco-cms/backoffice/controller-api"; |
| 108 | +import { UMB_DOCUMENT_WORKSPACE_CONTEXT } from "@umbraco-cms/backoffice/document"; |
| 109 | +import { UmbVariantId } from "@umbraco-cms/backoffice/variant"; |
| 110 | + |
| 111 | +export class MyDocumentPropertyPermissionWorkspaceContext extends UmbControllerBase { |
| 112 | + constructor(host: UmbControllerHost) { |
| 113 | + super(host); |
| 114 | + |
| 115 | + // Consume the document workspace context |
| 116 | + this.consumeContext( |
| 117 | + UMB_DOCUMENT_WORKSPACE_CONTEXT, |
| 118 | + (context) => { |
| 119 | + |
| 120 | + // Observe the specific property of the Content Type, to retrieve the unique. |
| 121 | + this.observe(context?.structure.propertyStructureByAlias('myNoneWritableProperty'), (property) => { |
| 122 | + if(property) { |
| 123 | + // Create a guard rule: |
| 124 | + const rule = { |
| 125 | + unique: 'myCustomRuleIdentifyer', |
| 126 | + permitted: false, |
| 127 | + message: "The property is not writable because of my custom restriction.", |
| 128 | + propertyType: { |
| 129 | + unique: property.unique |
| 130 | + } |
| 131 | + } |
| 132 | + // Add the rule to the write guard |
| 133 | + context.propertyWriteGuard.addRule(rule); |
| 134 | + } |
| 135 | + }); |
| 136 | + } |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +export { MyDocumentPropertyPermissionWorkspaceContext as api }; |
| 142 | +``` |
| 143 | +{% endcode %} |
| 144 | + |
| 145 | +The next example will adjust the rule so it only prevents writing on a specific culture. |
| 146 | + |
| 147 | +### Write a rule for a specific property or a specific variant |
| 148 | + |
| 149 | +The following example shows how you can make your rule very specific by targeting a property and a `VariantID`. |
| 150 | + |
| 151 | +**Adjusting the rule for the Workspace Context:** |
| 152 | + |
| 153 | +{% code title="WorkspaceContext.ts" %} |
| 154 | +```typescript |
| 155 | +import type { UmbVariantId } from '@umbraco-cms/backoffice/variant'; |
| 156 | + |
| 157 | +... |
| 158 | + |
| 159 | + // Create a guard rule: |
| 160 | + const rule = { |
| 161 | + unique: 'myCustomRuleIdentifyer', |
| 162 | + permitted: false, |
| 163 | + message: "The property is not writable because of my custom restriction.", |
| 164 | + propertyType: { |
| 165 | + unique: property.unique |
| 166 | + } |
| 167 | + variantId: UmbVariantId.CreateFromPartial({culture: 'en-US'}); |
| 168 | + |
| 169 | +... |
| 170 | +``` |
| 171 | +{% endcode %} |
| 172 | +
|
| 173 | +You are in charge of the combination, from targeting everything to targeting a specific property on a specific variant. The last combination purely targets a variant. This means that all properties with values of that variant is also available. |
0 commit comments