dolibarr 25.0.0-alpha
AccessPolicyConfig.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
24require_once DOL_DOCUMENT_ROOT.'/modulebuilder/class/AccessRuleExpression.class.php';
25require_once DOL_DOCUMENT_ROOT.'/modulebuilder/class/PredicateRule.class.php';
26require_once DOL_DOCUMENT_ROOT.'/modulebuilder/class/AndRule.class.php';
27require_once DOL_DOCUMENT_ROOT.'/modulebuilder/class/OrRule.class.php';
28
37{
38 const MAX_DEPTH = 4;
39 const MAX_NODES = 32;
40 const MAX_ALTERNATIVES = 3;
41 const MAX_MESSAGE_LENGTH = 128;
42
44 private $expression;
45
47 private $message;
48
54 public function __construct(AccessRuleExpression $expression, string $message = '')
55 {
56 if (strlen($message) > self::MAX_MESSAGE_LENGTH || !preg_match('/^[A-Za-z0-9_]*$/D', $message)) {
57 throw new \InvalidArgumentException('The access policy message must be a translation key');
58 }
59 $nodecount = 0;
60 self::checkBounds($expression, 1, $nodecount);
61
62 $this->expression = $expression;
63 $this->message = $message;
64 }
65
75 public static function fromAlternatives(array $alternatives, string $message = ''): ?self
76 {
77 if (count($alternatives) > self::MAX_ALTERNATIVES) {
78 throw new \InvalidArgumentException('An access policy accepts at most '.self::MAX_ALTERNATIVES.' alternatives');
79 }
80
81 $rules = array();
82 foreach ($alternatives as $alternative) {
83 if (!is_array($alternative)) {
84 throw new \InvalidArgumentException('An access policy alternative must be a list of predicates');
85 }
86 if (empty($alternative)) {
87 continue;
88 }
89 $predicates = array();
90 foreach (array_values(array_unique($alternative, SORT_REGULAR)) as $name) {
91 if (!is_string($name)) {
92 throw new \InvalidArgumentException('An access policy predicate name must be a string');
93 }
94 $predicates[] = new PredicateRule($name);
95 }
96 $rules[] = (count($predicates) == 1) ? $predicates[0] : new AndRule($predicates);
97 }
98
99 if (empty($rules)) {
100 return null;
101 }
102 return new self((count($rules) == 1) ? $rules[0] : new OrRule($rules), $message);
103 }
104
112 public static function fromJson(string $json): self
113 {
114 $data = json_decode($json, true);
115 if (!is_array($data) || json_last_error() !== JSON_ERROR_NONE) {
116 throw new \InvalidArgumentException('The serialized access policy is not a JSON object');
117 }
118 if (!isset($data['expression'])) {
119 throw new \InvalidArgumentException('The serialized access policy has no expression');
120 }
121 $message = isset($data['message']) ? $data['message'] : '';
122 if (!is_string($message)) {
123 throw new \InvalidArgumentException('The access policy message must be a translation key');
124 }
125
126 return new self(self::buildNode($data['expression'], 1), $message);
127 }
128
134 public function toJson(): string
135 {
136 return (string) json_encode(array('expression' => $this->expression->toArray(), 'message' => $this->message), JSON_UNESCAPED_SLASHES);
137 }
138
143 {
144 return $this->expression;
145 }
146
150 public function getMessage(): string
151 {
152 return $this->message;
153 }
154
163 private static function buildNode($data, int $depth): AccessRuleExpression
164 {
165 if ($depth > self::MAX_DEPTH) {
166 throw new \InvalidArgumentException('The access policy is deeper than '.self::MAX_DEPTH.' levels');
167 }
168 if (!is_array($data) || !isset($data['type']) || !is_string($data['type'])) {
169 throw new \InvalidArgumentException('An access policy node must have a type');
170 }
171
172 if ($data['type'] === PredicateRule::TYPE) {
173 if (!isset($data['name']) || !is_string($data['name'])) {
174 throw new \InvalidArgumentException('An access policy predicate must have a name');
175 }
176 return new PredicateRule($data['name']);
177 }
178
179 if ($data['type'] !== AndRule::TYPE && $data['type'] !== OrRule::TYPE) {
180 throw new \InvalidArgumentException('Unknown access policy node type: '.$data['type']);
181 }
182 if (!isset($data['rules']) || !is_array($data['rules'])) {
183 throw new \InvalidArgumentException('An access policy '.$data['type'].' node must have a list of rules');
184 }
185 $rules = array();
186 foreach ($data['rules'] as $child) {
187 $rules[] = self::buildNode($child, $depth + 1);
188 }
189 return ($data['type'] === AndRule::TYPE) ? new AndRule($rules) : new OrRule($rules);
190 }
191
201 private static function checkBounds(AccessRuleExpression $node, int $depth, int &$nodecount)
202 {
203 $nodecount++;
204 if ($depth > self::MAX_DEPTH) {
205 throw new \InvalidArgumentException('The access policy is deeper than '.self::MAX_DEPTH.' levels');
206 }
207 if ($nodecount > self::MAX_NODES) {
208 throw new \InvalidArgumentException('The access policy has more than '.self::MAX_NODES.' nodes');
209 }
210 if ($node instanceof AndRule || $node instanceof OrRule) {
211 foreach ($node->getRules() as $child) {
212 self::checkBounds($child, $depth + 1, $nodecount);
213 }
214 }
215 }
216}
Immutable access policy of the pages generated by ModuleBuilder: a rule tree and a deny message.
static buildNode($data, int $depth)
Build a node of the tree from its serialized form.
static fromJson(string $json)
Load a serialized policy, as produced by toJson().
toJson()
Serialize the policy.
static fromAlternatives(array $alternatives, string $message='')
Build a policy from the editor of ModuleBuilder: the predicates of an alternative are combined with A...
__construct(AccessRuleExpression $expression, string $message='')
static checkBounds(AccessRuleExpression $node, int $depth, int &$nodecount)
Check that a tree stays within MAX_DEPTH and MAX_NODES.
Conjunction node of the access rule tree: met when all its rules are met.
Disjunction node of the access rule tree: met when at least one of its rules is met.
Leaf of the access rule tree: one whitelisted condition on the user.
Node of the access rule tree of the pages generated by ModuleBuilder.