FusionDirectory
class_ACLPermissions.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2019-2020 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
22 {
23  static protected $letters = [
24  'r' => 'read',
25  'w' => 'write',
26  'c' => 'create',
27  'd' => 'delete',
28  'm' => 'move',
29  ];
30 
31  protected $read;
32  protected $write;
33  protected $create;
34  protected $delete;
35  protected $move;
36 
37  /* Rights on self only */
38  protected $self;
39 
40  public function __construct (string $rights = '')
41  {
42  foreach (static::$letters as $letter => $var) {
43  $this->$var = (strpos($rights, $letter) !== FALSE);
44  }
45  $this->self = (strpos($rights, 's') !== FALSE);
46  }
47 
48  public function toString (bool $readOnly = FALSE): string
49  {
50  $string = ($this->self ? 's' : '');
51  if ($readOnly) {
52  return $string.($this->read ? 'r' : '');
53  } else {
54  foreach (static::$letters as $letter => $var) {
55  if ($this->$var) {
56  $string .= $letter;
57  }
58  }
59  return $string;
60  }
61  }
62 
63  public function __toString ()
64  {
65  return $this->toString(FALSE);
66  }
67 
68  public function merge (ACLPermissions $merge)
69  {
70  foreach (static::$letters as $var) {
71  $this->$var = ($this->$var || $merge->$var);
72  }
73  }
74 
75  public function isSelf (): bool
76  {
77  return $this->self;
78  }
79 
80  public function isFull (): bool
81  {
82  return ($this->read && $this->write && $this->create && $this->delete && $this->move);
83  }
84 }