]> git.mxchange.org Git - friendica.git/blob - src/Repository/PermissionSet.php
Merge pull request #10160 from annando/issue-10156
[friendica.git] / src / Repository / 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\Repository;
23
24 use Friendica\BaseRepository;
25 use Friendica\Collection;
26 use Friendica\Database\Database;
27 use Friendica\Model;
28 use Friendica\Network\HTTPException;
29 use Friendica\Util\ACLFormatter;
30 use Psr\Log\LoggerInterface;
31
32 class PermissionSet extends BaseRepository
33 {
34         /** @var int Virtual permission set id for public permission */
35         const PUBLIC = 0;
36
37         protected static $table_name = 'permissionset';
38
39         protected static $model_class = Model\PermissionSet::class;
40
41         protected static $collection_class = Collection\PermissionSets::class;
42
43         /** @var ACLFormatter */
44         private $aclFormatter;
45
46         public function __construct(Database $dba, LoggerInterface $logger, ACLFormatter $aclFormatter)
47         {
48                 parent::__construct($dba, $logger);
49
50                 $this->aclFormatter = $aclFormatter;
51         }
52
53         /**
54          * @param array $data
55          * @return Model\PermissionSet
56          */
57         protected function create(array $data)
58         {
59                 return new Model\PermissionSet($this->dba, $this->logger, $data);
60         }
61
62         /**
63          * @param array $condition
64          * @return Model\PermissionSet
65          * @throws \Friendica\Network\HTTPException\NotFoundException
66          */
67         public function selectFirst(array $condition)
68         {
69                 if (isset($condition['id']) && !$condition['id']) {
70                         return $this->create([
71                                 'id' => self::PUBLIC,
72                                 'uid' => $condition['uid'] ?? 0,
73                                 'allow_cid' => '',
74                                 'allow_gid' => '',
75                                 'deny_cid' => '',
76                                 'deny_gid' => '',
77                         ]);
78                 }
79
80                 return parent::selectFirst($condition);
81         }
82
83         /**
84          * @param array $condition
85          * @param array $params
86          * @return Collection\PermissionSets
87          * @throws \Exception
88          */
89         public function select(array $condition = [], array $params = [])
90         {
91                 return parent::select($condition, $params);
92         }
93
94         /**
95          * @param array    $condition
96          * @param array    $params
97          * @param int|null $min_id
98          * @param int|null $max_id
99          * @param int      $limit
100          * @return Collection\PermissionSets
101          * @throws \Exception
102          */
103         public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
104         {
105                 return parent::selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
106         }
107
108         /**
109          * Fetch the id of a given permission set. Generate a new one when needed
110          *
111          * @param int         $uid
112          * @param string|null $allow_cid Allowed contact IDs    - empty = everyone
113          * @param string|null $allow_gid Allowed group IDs      - empty = everyone
114          * @param string|null $deny_cid  Disallowed contact IDs - empty = no one
115          * @param string|null $deny_gid  Disallowed group IDs   - empty = no one
116          * @return int id
117          * @throws \Exception
118          */
119         public function getIdFromACL(
120                 int $uid,
121                 string $allow_cid = null,
122                 string $allow_gid = null,
123                 string $deny_cid = null,
124                 string $deny_gid = null
125         ) {
126                 $allow_cid = $this->aclFormatter->sanitize($allow_cid);
127                 $allow_gid = $this->aclFormatter->sanitize($allow_gid);
128                 $deny_cid = $this->aclFormatter->sanitize($deny_cid);
129                 $deny_gid = $this->aclFormatter->sanitize($deny_gid);
130
131                 // Public permission
132                 if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) {
133                         return self::PUBLIC;
134                 }
135
136                 $condition = [
137                         'uid' => $uid,
138                         'allow_cid' => $allow_cid,
139                         'allow_gid' => $allow_gid,
140                         'deny_cid'  => $deny_cid,
141                         'deny_gid'  => $deny_gid
142                 ];
143
144                 try {
145                         $permissionset = $this->selectFirst($condition);
146                 } catch(HTTPException\NotFoundException $exception) {
147                         $permissionset = $this->insert($condition);
148                 }
149
150                 return $permissionset->id;
151         }
152
153         /**
154          * Returns a permission set collection for a given contact
155          *
156          * @param integer $contact_id Contact id of the visitor
157          * @param integer $uid        User id whom the items belong, used for ownership check.
158          *
159          * @return Collection\PermissionSets
160          * @throws \Exception
161          */
162         public function selectByContactId($contact_id, $uid)
163         {
164                 $cdata = Model\Contact::getPublicAndUserContacID($contact_id, $uid);
165                 if (!empty($cdata)) {
166                         $public_contact_str = '<' . $cdata['public'] . '>';
167                         $user_contact_str = '<' . $cdata['user'] . '>';
168                         $contact_id = $cdata['user'];
169                 } else {
170                         $public_contact_str = '<' . $contact_id . '>';
171                         $user_contact_str = '';
172                 }
173
174                 $groups = [];
175                 if (!empty($user_contact_str) && $this->dba->exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
176                         $groups = Model\Group::getIdsByContactId($contact_id);
177                 }
178
179                 $group_str = '<<>>'; // should be impossible to match
180                 foreach ($groups as $group_id) {
181                         $group_str .= '|<' . preg_quote($group_id) . '>';
182                 }
183
184                 if (!empty($user_contact_str)) {
185                         $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR `deny_cid` REGEXP ? OR deny_gid REGEXP ?)
186                                 AND (allow_cid REGEXP ? OR allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
187                                 $uid, $user_contact_str, $public_contact_str, $group_str,
188                                 $user_contact_str, $public_contact_str, $group_str];
189                 } else {
190                         $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
191                                 AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
192                                 $uid, $public_contact_str, $group_str, $public_contact_str, $group_str];
193                 }
194
195                 return $this->select($condition);
196         }
197 }