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 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
36{
37 const ACTION_ADD = 'add';
38 const ACTION_UPDATE = 'update';
39 const ACTION_DELETE = 'delete';
40
41 const SCOPE_OBJECT = 'object';
42 const SCOPE_RIGHT = 'right';
43
45 public $module;
46
48 public $descriptorFile;
49
51 public $permissions;
52
54 public $objectName;
55
57 public $scope;
58
60 public $actionType;
61
63 public $rightKey;
64
66 public $rightLabel;
67
69 public $rightCrud;
70
83 private function __construct(
84 string $module,
85 string $descriptorFile,
86 array $permissions,
87 string $objectName,
88 string $scope,
89 string $actionType,
90 ?int $rightKey = null,
91 ?string $rightLabel = null,
92 ?string $rightCrud = null
93 ) {
94 if ($module === '') {
95 throw new \InvalidArgumentException('RightsSyncCommand requires a module name');
96 }
97 if ($descriptorFile === '') {
98 throw new \InvalidArgumentException('RightsSyncCommand requires a descriptor file path');
99 }
100 if (!in_array($scope, array(self::SCOPE_OBJECT, self::SCOPE_RIGHT), true)) {
101 throw new \InvalidArgumentException('RightsSyncCommand got an unknown scope: '.$scope);
102 }
103 if (!in_array($actionType, array(self::ACTION_ADD, self::ACTION_UPDATE, self::ACTION_DELETE), true)) {
104 throw new \InvalidArgumentException('RightsSyncCommand got an unknown action: '.$actionType);
105 }
106 if ($scope === self::SCOPE_OBJECT) {
107 if ($objectName === '') {
108 throw new \InvalidArgumentException('An object-scoped RightsSyncCommand requires an object name');
109 }
110 if ($actionType === self::ACTION_UPDATE) {
111 throw new \InvalidArgumentException('There is no object-scoped update in ModuleBuilder');
112 }
113 }
114 if ($scope === self::SCOPE_RIGHT && $actionType !== self::ACTION_ADD && $rightKey === null) {
115 throw new \InvalidArgumentException('A right-scoped '.$actionType.' requires a right key');
116 }
117 if ($scope === self::SCOPE_RIGHT && $actionType !== self::ACTION_DELETE && ($rightLabel === null || $rightCrud === null)) {
118 throw new \InvalidArgumentException('A right-scoped '.$actionType.' requires a label and a crud code');
119 }
120
121 $this->module = $module;
122 $this->descriptorFile = $descriptorFile;
123 $this->permissions = $permissions;
124 $this->objectName = $objectName;
125 $this->scope = $scope;
126 $this->actionType = $actionType;
127 $this->rightKey = $rightKey;
128 $this->rightLabel = $rightLabel;
129 $this->rightCrud = $rightCrud;
130 }
131
141 public static function forObjectCreation(string $module, string $descriptorFile, array $permissions, string $objectName): self
142 {
143 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_OBJECT, self::ACTION_ADD);
144 }
145
155 public static function forObjectDeletion(string $module, string $descriptorFile, array $permissions, string $objectName): self
156 {
157 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_OBJECT, self::ACTION_DELETE);
158 }
159
171 public static function forRightAddition(string $module, string $descriptorFile, array $permissions, string $objectName, string $label, string $crud): self
172 {
173 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_RIGHT, self::ACTION_ADD, null, $label, $crud);
174 }
175
188 public static function forRightUpdate(string $module, string $descriptorFile, array $permissions, int $rightKey, string $objectName, string $label, string $crud): self
189 {
190 return new self($module, $descriptorFile, $permissions, $objectName, self::SCOPE_RIGHT, self::ACTION_UPDATE, $rightKey, $label, $crud);
191 }
192
202 public static function forRightDeletion(string $module, string $descriptorFile, array $permissions, int $rightKey): self
203 {
204 return new self($module, $descriptorFile, $permissions, '', self::SCOPE_RIGHT, self::ACTION_DELETE, $rightKey);
205 }
206}
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)