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';
40 const MAX_ALTERNATIVES = 3;
41 const MAX_MESSAGE_LENGTH = 128;
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');
62 $this->expression = $expression;
63 $this->message = $message;
75 public static function fromAlternatives(array $alternatives,
string $message =
''): ?self
77 if (count($alternatives) > self::MAX_ALTERNATIVES) {
78 throw new \InvalidArgumentException(
'An access policy accepts at most '.self::MAX_ALTERNATIVES.
' alternatives');
82 foreach ($alternatives as $alternative) {
83 if (!is_array($alternative)) {
84 throw new \InvalidArgumentException(
'An access policy alternative must be a list of predicates');
86 if (empty($alternative)) {
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');
96 $rules[] = (count($predicates) == 1) ? $predicates[0] :
new AndRule($predicates);
102 return new self((count($rules) == 1) ? $rules[0] :
new OrRule($rules), $message);
112 public static function fromJson(
string $json): self
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');
118 if (!isset($data[
'expression'])) {
119 throw new \InvalidArgumentException(
'The serialized access policy has no expression');
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');
136 return (
string) json_encode(array(
'expression' => $this->expression->toArray(),
'message' => $this->message), JSON_UNESCAPED_SLASHES);
144 return $this->expression;
152 return $this->message;
165 if ($depth > self::MAX_DEPTH) {
166 throw new \InvalidArgumentException(
'The access policy is deeper than '.self::MAX_DEPTH.
' levels');
168 if (!is_array($data) || !isset($data[
'type']) || !is_string($data[
'type'])) {
169 throw new \InvalidArgumentException(
'An access policy node must have a type');
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');
179 if ($data[
'type'] !== AndRule::TYPE && $data[
'type'] !== OrRule::TYPE) {
180 throw new \InvalidArgumentException(
'Unknown access policy node type: '.$data[
'type']);
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');
186 foreach ($data[
'rules'] as $child) {
189 return ($data[
'type'] === AndRule::TYPE) ?
new AndRule($rules) :
new OrRule($rules);
204 if ($depth > self::MAX_DEPTH) {
205 throw new \InvalidArgumentException(
'The access policy is deeper than '.self::MAX_DEPTH.
' levels');
207 if ($nodecount > self::MAX_NODES) {
208 throw new \InvalidArgumentException(
'The access policy has more than '.self::MAX_NODES.
' nodes');
211 foreach ($node->getRules() as $child) {
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.