]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/PermissionSet.php
Merge pull request #10446 from MrPetovan/bug/10439-addon-settings-forms
[friendica.git] / src / Model / PermissionSet.php
index de943c977c3841ae5292c5af2ea3c57b776bf174..9138d46b7fedd03972d728b9dd653f38ef2deb5c 100644 (file)
@@ -1,19 +1,39 @@
 <?php
 /**
- * @file src/Model/PermissionSet.php
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
 
 namespace Friendica\Model;
 
-use Friendica\Core\L10n;
-use Friendica\Database\DBA;
+use Friendica\BaseModel;
 use Friendica\DI;
-use Friendica\Network\HTTPException;
 
 /**
  * functions for interacting with the permission set of an object (item, photo, event, ...)
+ *
+ * @property int uid
+ * @property string allow_cid
+ * @property string allow_gid
+ * @property string deny_cid
+ * @property string deny_gid
  */
-class PermissionSet
+class PermissionSet extends BaseModel
 {
        /**
         * Fetch the id of a given permission set. Generate a new one when needed
@@ -24,7 +44,9 @@ class PermissionSet
         * @param string|null $deny_cid  Disallowed contact IDs - empty = no one
         * @param string|null $deny_gid  Disallowed group IDs   - empty = no one
         * @return int id
-        * @throws HTTPException\InternalServerErrorException
+        * @throws \Exception
+        * @deprecated since 2020.03, use Repository\PermissionSet instead
+        * @see \Friendica\Repository\PermissionSet->getIdFromACL
         */
        public static function getIdFromACL(
                int $uid,
@@ -33,38 +55,7 @@ class PermissionSet
                string $deny_cid = null,
                string $deny_gid = null
        ) {
-               $ACLFormatter = DI::aclFormatter();
-
-               $allow_cid = $ACLFormatter->sanitize($allow_cid);
-               $allow_gid = $ACLFormatter->sanitize($allow_gid);
-               $deny_cid = $ACLFormatter->sanitize($deny_cid);
-               $deny_gid = $ACLFormatter->sanitize($deny_gid);
-
-               // Public permission
-               if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) {
-                       return 0;
-               }
-
-               $condition = [
-                       'uid' => $uid,
-                       'allow_cid' => $allow_cid,
-                       'allow_gid' => $allow_gid,
-                       'deny_cid'  => $deny_cid,
-                       'deny_gid'  => $deny_gid
-               ];
-               $permissionset = DBA::selectFirst('permissionset', ['id'], $condition);
-
-               if (DBA::isResult($permissionset)) {
-                       $psid = $permissionset['id'];
-               } else {
-                       if (DBA::insert('permissionset', $condition, true)) {
-                               $psid = DBA::lastInsertId();
-                       } else {
-                               throw new HTTPException\InternalServerErrorException(L10n::t('Unable to create a new permission set.'));
-                       }
-               }
-
-               return $psid;
+               return DI::permissionSet()->getIdFromACL($uid, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
        }
 
        /**
@@ -75,36 +66,13 @@ class PermissionSet
         *
         * @return array of permission set ids.
         * @throws \Exception
+        * @deprecated since 2020.03, use Repository\PermissionSet instead
+        * @see \Friendica\Repository\PermissionSet->selectByContactId
         */
-       static public function get($uid, $contact_id)
+       public static function get($uid, $contact_id)
        {
-               if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
-                       $groups = Group::getIdsByContactId($contact_id);
-               }
-
-               if (empty($groups) || !is_array($groups)) {
-                       return [];
-               }
-
-               $group_str = '<<>>'; // should be impossible to match
-
-               foreach ($groups as $g) {
-                       $group_str .= '|<' . intval($g) . '>';
-               }
-
-               $contact_str = '<' . $contact_id . '>';
-
-               $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
-                       AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
-                       $uid, $contact_str, $group_str, $contact_str, $group_str];
-
-               $ret = DBA::select('permissionset', ['id'], $condition);
-               $set = [];
-               while ($permission = DBA::fetch($ret)) {
-                       $set[] = $permission['id'];
-               }
-               DBA::close($ret);
+               $permissionSets = DI::permissionSet()->selectByContactId($contact_id, $uid);
 
-               return $set;
+               return $permissionSets->column('id');
        }
 }