|
| 1 | +-- ============================================================================ |
| 2 | +-- Role-based widget visibility — Studio Pro's "Visible: based on module roles" |
| 3 | +-- ============================================================================ |
| 4 | +-- |
| 5 | +-- A widget can be shown to some module roles only: |
| 6 | +-- |
| 7 | +-- VisibleForRoles: [Module.Role, Module.OtherRole] |
| 8 | +-- |
| 9 | +-- It is stored in the widget's ConditionalVisibilitySettings.ModuleRoles, the |
| 10 | +-- same element that holds a `Visible: [expression]` condition, and the two |
| 11 | +-- combine as they do in Studio Pro: the widget shows when the user has one of |
| 12 | +-- the roles AND the expression holds. `describe` prints the list back. |
| 13 | +-- |
| 14 | +-- Works on the built-in widgets — buttons, containers, inputs, text, data |
| 15 | +-- views, … Each name is a by-name reference written Module.Role; an unknown |
| 16 | +-- role is reported by mx check (CE1613), like any other by-name reference. |
| 17 | +-- Pluggable widgets (Data grid 2, Gallery, …) get no conditional visibility |
| 18 | +-- from mxcli: wrap one in a container and put the roles on the container. |
| 19 | +-- |
| 20 | +-- Hiding a widget is not security: a hidden button's microflow can still be |
| 21 | +-- called. Grant execute on the microflow to the same roles. |
| 22 | +-- |
| 23 | +-- ============================================================================ |
| 24 | + |
| 25 | +create module RoleVisTest; |
| 26 | + |
| 27 | +create module role RoleVisTest.Admin; |
| 28 | +create module role RoleVisTest.Editor; |
| 29 | +create module role RoleVisTest.Viewer; |
| 30 | + |
| 31 | +create persistent entity RoleVisTest.Document ( |
| 32 | + Title: string(200) not null, |
| 33 | + IsLocked: boolean default false |
| 34 | +); |
| 35 | + |
| 36 | +/** |
| 37 | + * Deletes one document. |
| 38 | + * |
| 39 | + * @param $Document The document to delete |
| 40 | + */ |
| 41 | +create microflow RoleVisTest.ACT_Document_Delete ( |
| 42 | + $Document: RoleVisTest.Document |
| 43 | +) |
| 44 | +begin |
| 45 | + delete $Document; |
| 46 | +end; |
| 47 | +/ |
| 48 | + |
| 49 | +grant execute on microflow RoleVisTest.ACT_Document_Delete to RoleVisTest.Admin; |
| 50 | + |
| 51 | +/** |
| 52 | + * - btnEdit: shown to editors and admins. |
| 53 | + * - btnDelete: shown to admins only. |
| 54 | + * - btnUnlock: roles AND an expression — shown to admins, and only while the |
| 55 | + * document is locked. |
| 56 | + * - txtTitle / cntAdmin: any built-in widget takes the property, a container |
| 57 | + * included, which also hides everything inside it. |
| 58 | + * |
| 59 | + * @param $Document The document shown on the page |
| 60 | + */ |
| 61 | +create page RoleVisTest.Document_Detail |
| 62 | +( |
| 63 | + params: { $Document: RoleVisTest.Document }, |
| 64 | + title: 'Document', |
| 65 | + layout: Atlas_Core.PopupLayout |
| 66 | +) |
| 67 | +{ |
| 68 | + layoutgrid lgDocument { |
| 69 | + row row1 { |
| 70 | + column col1 (desktopwidth: autofill) { |
| 71 | + dataview dvDocument (datasource: $Document) { |
| 72 | + textbox txtTitle ( |
| 73 | + label: 'Title', |
| 74 | + attribute: Title, |
| 75 | + visibleforroles: [RoleVisTest.Admin, RoleVisTest.Editor] |
| 76 | + ) |
| 77 | + container cntAdmin (class: 'card', visibleforroles: [RoleVisTest.Admin]) { |
| 78 | + dynamictext txtAdminNote (content: 'Visible to administrators only') |
| 79 | + } |
| 80 | + footer footer1 { |
| 81 | + actionbutton btnEdit ( |
| 82 | + caption: 'Save', |
| 83 | + action: save_changes, |
| 84 | + visibleforroles: [RoleVisTest.Admin, RoleVisTest.Editor] |
| 85 | + ) |
| 86 | + actionbutton btnDelete ( |
| 87 | + caption: 'Delete', |
| 88 | + action: microflow RoleVisTest.ACT_Document_Delete(Document: $Document), |
| 89 | + buttonstyle: danger, |
| 90 | + visibleforroles: [RoleVisTest.Admin] |
| 91 | + ) |
| 92 | + actionbutton btnUnlock ( |
| 93 | + caption: 'Unlock', |
| 94 | + action: save_changes, |
| 95 | + visible: [IsLocked], |
| 96 | + visibleforroles: [RoleVisTest.Admin] |
| 97 | + ) |
| 98 | + actionbutton btnClose (caption: 'Close', action: close_page) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +-- Prints the VisibleForRoles lists back. |
| 107 | +describe page RoleVisTest.Document_Detail; |
0 commit comments