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