]> git.mxchange.org Git - friendica.git/commitdiff
Rename PermissionSet::fetchIDForPost to PermissionSet::getIdFromACL
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 5 Nov 2019 13:27:22 +0000 (08:27 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 20 Jan 2020 12:32:47 +0000 (07:32 -0500)
- Allow creating/retrieving a permission set id with arbitrary parameters
- Rename ACLformatter->sanitize to ACLFormatter->sanitizeItem
- Move PermissionSet::sortPermissions to ACLformatter->sanitize

src/Database/PostUpdate.php
src/Model/Item.php
src/Model/PermissionSet.php
src/Util/ACLFormatter.php

index 64737f604f831e1aba747751c27359969c1e4aef..94d00853abc650b086dd67297c4fbe408978123c 100644 (file)
@@ -204,14 +204,20 @@ class PostUpdate
                        }
 
                        if (empty($item['psid'])) {
-                               $item['psid'] = PermissionSet::fetchIDForPost($item);
-                       } else {
-                               $item['allow_cid'] = null;
-                               $item['allow_gid'] = null;
-                               $item['deny_cid'] = null;
-                               $item['deny_gid'] = null;
+                               $item['psid'] = PermissionSet::getIdFromACL(
+                                       $item['uid'],
+                                       $item['allow_cid'],
+                                       $item['allow_gid'],
+                                       $item['deny_cid'],
+                                       $item['deny_gid']
+                               );
                        }
 
+                       $item['allow_cid'] = null;
+                       $item['allow_gid'] = null;
+                       $item['deny_cid'] = null;
+                       $item['deny_gid'] = null;
+
                        if ($item['post-type'] == 0) {
                                if (!empty($item['type']) && ($item['type'] == 'note')) {
                                        $item['post-type'] = Item::PT_PERSONAL_NOTE;
index 9f2f45e74a28eb2cdaebe8a4300c39e9d7b7d6fe..38557c52cea7f5e60f7b568bea5cc4138cc2e390 100644 (file)
@@ -1856,7 +1856,18 @@ class Item
                }
 
                // Creates or assigns the permission set
-               $item['psid'] = PermissionSet::fetchIDForPost($item);
+               $item['psid'] = PermissionSet::getIdFromACL(
+                       $item['uid'],
+                       $item['allow_cid'],
+                       $item['allow_gid'],
+                       $item['deny_cid'],
+                       $item['deny_gid']
+               );
+
+               $item['allow_cid'] = null;
+               $item['allow_gid'] = null;
+               $item['deny_cid'] = null;
+               $item['deny_gid'] = null;
 
                // We are doing this outside of the transaction to avoid timing problems
                if (!self::insertActivity($item)) {
@@ -2729,7 +2740,13 @@ class Item
 
                $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? 1 : 0;
 
-               $psid = PermissionSet::fetchIDForPost($user);
+               $psid = PermissionSet::getIdFromACL(
+                       $user['uid'],
+                       $user['allow_cid'],
+                       $user['allow_gid'],
+                       $user['deny_cid'],
+                       $user['deny_gid']
+               );
 
                $forum_mode = ($prvgroup ? 2 : 1);
 
index d0e256d15363c9b71cd218e43afec21b8f658903..de943c977c3841ae5292c5af2ea3c57b776bf174 100644 (file)
@@ -2,9 +2,13 @@
 /**
  * @file src/Model/PermissionSet.php
  */
+
 namespace Friendica\Model;
 
+use Friendica\Core\L10n;
 use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Network\HTTPException;
 
 /**
  * functions for interacting with the permission set of an object (item, photo, event, ...)
@@ -14,51 +18,53 @@ class PermissionSet
        /**
         * Fetch the id of a given permission set. Generate a new one when needed
         *
-        * @param array $postarray The array from an item, picture or event post
+        * @param int         $uid
+        * @param string|null $allow_cid Allowed contact IDs    - empty = everyone
+        * @param string|null $allow_gid Allowed group IDs      - empty = everyone
+        * @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 \Exception
+        * @throws HTTPException\InternalServerErrorException
         */
-       public static function fetchIDForPost(&$postarray)
-       {
-               $condition = ['uid' => $postarray['uid'],
-                       'allow_cid' => self::sortPermissions($postarray['allow_cid'] ?? ''),
-                       'allow_gid' => self::sortPermissions($postarray['allow_gid'] ?? ''),
-                       'deny_cid'  => self::sortPermissions($postarray['deny_cid']  ?? ''),
-                       'deny_gid'  => self::sortPermissions($postarray['deny_gid']  ?? '')];
-
-               $set = DBA::selectFirst('permissionset', ['id'], $condition);
-
-               if (!DBA::isResult($set)) {
-                       DBA::insert('permissionset', $condition, true);
-
-                       $set = DBA::selectFirst('permissionset', ['id'], $condition);
+       public static function getIdFromACL(
+               int $uid,
+               string $allow_cid = null,
+               string $allow_gid = null,
+               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;
                }
 
-               $postarray['allow_cid'] = null;
-               $postarray['allow_gid'] = null;
-               $postarray['deny_cid'] = null;
-               $postarray['deny_gid'] = null;
-
-               return $set['id'];
-       }
-
-       private static function sortPermissions($permissionlist)
-       {
-               $cleaned_list = trim($permissionlist, '<>');
-
-               if (empty($cleaned_list)) {
-                       return $permissionlist;
+               $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.'));
+                       }
                }
 
-               $elements = explode('><', $cleaned_list);
-
-               if (count($elements) <= 1) {
-                       return $permissionlist;
-               }
-
-               asort($elements);
-
-               return '<' . implode('><', $elements) . '>';
+               return $psid;
        }
 
        /**
index a7d851508d4e22d1c9441b959eb541c56e2875b9..d79a73298b086ea1ec46308651c736538f14c45a 100644 (file)
@@ -12,30 +12,57 @@ final class ACLFormatter
        /**
         * Turn user/group ACLs stored as angle bracketed text into arrays
         *
-        * @param string|null $ids A angle-bracketed list of IDs
+        * @param string|null $acl_string A angle-bracketed list of IDs
         *
         * @return array The array based on the IDs (empty in case there is no list)
         */
-       public function expand(string $ids = null)
+       public function expand(string $acl_string = null)
        {
                // In case there is no ID list, return empty array (=> no ACL set)
-               if (!isset($ids)) {
+               if (!isset($acl_string)) {
                        return [];
                }
 
                // turn string array of angle-bracketed elements into numeric array
                // e.g. "<1><2><3>" => array(1,2,3);
-               preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $ids, $matches, PREG_PATTERN_ORDER);
+               preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $acl_string, $matches, PREG_PATTERN_ORDER);
 
                return $matches[1];
        }
 
+       /**
+        * Takes an arbitrary ACL string and sanitizes it for storage
+        *
+        * @param string|null $acl_string
+        * @return string
+        */
+       public function sanitize(string $acl_string = null)
+       {
+               if (empty($acl_string)) {
+                       return '';
+               }
+
+               $cleaned_list = trim($acl_string, '<>');
+
+               if (empty($cleaned_list)) {
+                       return '';
+               }
+
+               $elements = explode('><', $cleaned_list);
+
+               sort($elements);
+
+               array_walk($elements, [$this, 'sanitizeItem']);
+
+               return implode('', $elements);
+       }
+
        /**
         * Wrap ACL elements in angle brackets for storage
         *
         * @param string $item The item to sanitise
         */
-       private function sanitize(string &$item) {
+       private function sanitizeItem(string &$item) {
                // The item is an ACL int value
                if (intval($item)) {
                        $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
@@ -70,7 +97,7 @@ final class ACLFormatter
                }
 
                if (is_array($item)) {
-                       array_walk($item, [$this, 'sanitize']);
+                       array_walk($item, [$this, 'sanitizeItem']);
                        $return = implode('', $item);
                }
                return $return;