dolibarr 25.0.0-alpha
RightsSyncCommand.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 ATM Consulting <support@atm-consulting.fr>
3 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
37{
38 const ACTION_ADD = 'add';
39 const ACTION_UPDATE = 'update';
40 const ACTION_DELETE = 'delete';
41
42 const SCOPE_OBJECT = 'object';
43 const SCOPE_RIGHT = 'right';
44
46 public $module;
47
49 public $descriptorFile;
50
52 public $permissions;
53
55 public $objectName;
56
58 public $scope;
59
61 public $actionType;
62
64 public $rightKey;
65
67 public $rightLabel;
68
70 public $rightCrud;
71
84 private function __construct(
85 string $module,
86 string $descriptorFile,
87 array $permissions,
88 string $objectName,
89 string $scope,
90 string $actionType,
91 ?int $rightKey = null,
92 ?string $rightLabel = null,
93 ?string $rightCrud = null
94 ) {
95 if ($module === '') {
96 throw new \InvalidArgumentException('RightsSyncCommand requires a module name');
97 }
98 if ($descriptorFile === '') {
99 throw new \InvalidArgumentException('RightsSyncCommand requires a descriptor file path');
100 }
101 if (!in_array($scope, array(self::SCOPE_OBJECT, self::SCOPE_RIGHT), true)) {
102 throw new \InvalidArgumentException('RightsSyncCommand got an unknown scope: '.$scope);
103 }
104 if (!in_array($actionType, array(self::ACTION_ADD, self::ACTION_UPDATE, self::ACTION_DELETE), true)) {
105 throw new \InvalidArgumentException('RightsSyncCommand got an unknown action: '.$actionType);
106 }
107 if ($scope === self::SCOPE_OBJECT) {
108 if ($objectName === '') {
109 throw new \InvalidArgumentException('An object-scoped RightsSyncCommand requires an object name');
110 }
111 if ($actionType === self::ACTION_UPDATE) {
112 throw new \InvalidArgumentException('There is no object-scoped update in ModuleBuilder');
113 }
114 }
115 if ($scope === self::SCOPE_RIGHT && $actionType !== self::ACTION_ADD && $rightKey === null) {
116 throw new \InvalidArgumentException('A right-scoped '.$actionType.' requires a right key');
117 }
118 if ($scope === self::SCOPE_RIGHT && $actionType !== self::ACTION_DELETE && ($rightLabel === null || $rightCrud === null)) {
119 throw new \InvalidArgumentException('A right-scoped '.$actionType.' requires a label and a crud code');
120 }
121
122 $this->module = $module;
123 $this->descriptorFile = $descriptorFile;
124 $this->permissions = $permissions;
125 $this->objectName = $objectName;
126 $this->scope = $scope;
127 $this->actionType = $actionType;
128 $this->rightKey = $rightKey;
129 $this->rightLabel = $rightLabel;
130 $this->rightCrud = $rightCrud;
131 }
132
142 public static function forObjectCreation(string $module, string $descriptorFile, array $permissions, string $objectName): self
143 {
144 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_OBJECT, self::ACTION_ADD);
145 }
146
156 public static function forObjectDeletion(string $module, string $descriptorFile, array $permissions, string $objectName): self
157 {
158 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_OBJECT, self::ACTION_DELETE);
159 }
160
172 public static function forRightAddition(string $module, string $descriptorFile, array $permissions, string $objectName, string $label, string $crud): self
173 {
174 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_RIGHT, self::ACTION_ADD, null, $label, $crud);
175 }
176
189 public static function forRightUpdate(string $module, string $descriptorFile, array $permissions, int $rightKey, string $objectName, string $label, string $crud): self
190 {
191 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_RIGHT, self::ACTION_UPDATE, $rightKey, $label, $crud);
192 }
193
203 public static function forRightDeletion(string $module, string $descriptorFile, array $permissions, int $rightKey): self
204 {
205 return new self($module, $descriptorFile, $permissions, '', self::SCOPE_RIGHT, self::ACTION_DELETE, $rightKey);
206 }
207}
Immutable request describing one permissions sync to perform on a module descriptor.
static forObjectDeletion(string $module, string $descriptorFile, array $permissions, string $objectName)
Drop every right attached to an object being deleted.
static forRightDeletion(string $module, string $descriptorFile, array $permissions, int $rightKey)
Remove one right, addressed by its index in the rights array.
static forRightUpdate(string $module, string $descriptorFile, array $permissions, int $rightKey, string $objectName, string $label, string $crud)
Replace one right, addressed by its index in the rights array.
static forRightAddition(string $module, string $descriptorFile, array $permissions, string $objectName, string $label, string $crud)
Append one right to the descriptor.
static forObjectCreation(string $module, string $descriptorFile, array $permissions, string $objectName)
Declare the three CRUD rights of a newly generated object.
__construct(string $module, string $descriptorFile, array $permissions, string $objectName, string $scope, string $actionType, ?int $rightKey=null, ?string $rightLabel=null, ?string $rightCrud=null)
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193