]> git.mxchange.org Git - friendica.git/blob - src/Security/PermissionSet/Entity/PermissionSet.php
Fixing tests - maybe
[friendica.git] / src / Security / PermissionSet / Entity / PermissionSet.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Security\PermissionSet\Entity;
23
24 use Friendica\BaseEntity;
25 use Friendica\Security\PermissionSet\Repository\PermissionSet as PermissionSetRepository;
26
27 /**
28  * @property-read int|null $id
29  * @property-read int      $uid
30  * @property-read string[] $allow_cid
31  * @property-read string[] $allow_gid
32  * @property-read string[] $deny_cid
33  * @property-read string[] $deny_gid
34  */
35 class PermissionSet extends BaseEntity
36 {
37         /** @var int|null */
38         protected $id;
39         /** @var int */
40         protected $uid;
41         /** @var string[] */
42         protected $allow_cid;
43         /** @var string[] */
44         protected $allow_gid;
45         /** @var string[] */
46         protected $deny_cid;
47         /** @var string[] */
48         protected $deny_gid;
49
50         /**
51          * @param int|null $id
52          * @param int      $uid
53          * @param string[] $allow_cid
54          * @param string[] $allow_gid
55          * @param string[] $deny_cid
56          * @param string[] $deny_gid
57          *
58          * @see \Friendica\Security\PermissionSet\Factory\PermissionSet
59          */
60         public function __construct(int $uid, array $allow_cid = [], array $allow_gid = [], array $deny_cid = [], array $deny_gid = [], int $id = null)
61         {
62                 $this->id        = $id;
63                 $this->uid       = $uid;
64                 $this->allow_cid = $allow_cid;
65                 $this->allow_gid = $allow_gid;
66                 $this->deny_cid  = $deny_cid;
67                 $this->deny_gid  = $deny_gid;
68         }
69
70         /**
71          * Checks, if the current PermissionSet is a/the public PermissionSet
72          *
73          * @return bool
74          */
75         public function isPublic(): bool
76         {
77                 return (($this->id === PermissionSetRepository::PUBLIC) ||
78                                 (is_null($this->id) &&
79                                  empty($this->allow_cid) &&
80                                  empty($this->allow_gid) &&
81                                  empty($this->deny_cid) &&
82                                  empty($this->deny_gid)));
83         }
84
85         /**
86          * Creates a new Entity with a new allowed_cid list (wipes the id because it isn't the same entity anymore)
87          *
88          * @param array $allow_cid
89          *
90          * @return $this
91          */
92         public function withAllowedContacts(array $allow_cid): PermissionSet
93         {
94                 $clone = clone $this;
95
96                 $clone->allow_cid = $allow_cid;
97                 $clone->id        = null;
98
99                 return $clone;
100         }
101 }