Skip to content

Next

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Support
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
  • Sign in / Register
Minds Backend - Engine
Minds Backend - Engine
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
    • Insights
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
    • Locked Files
  • Issues 243
    • Issues 243
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 34
    • Merge Requests 34
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Charts
  • Security & Compliance
    • Security & Compliance
    • Dependency List
  • Packages
    • Packages
    • List
    • Container Registry
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Minds
  • Minds Backend - EngineMinds Backend - Engine
  • Merge Requests
  • !307

Open
Opened 1 week ago by Brian Hatchet@brianhatchet:speech_balloon:
  • Report abuse
Report abuse

Epic/permissions open channels

Let's get this closed off so we can get all the e2e tests up and running before I commit the next big block of permissions

Edited 21 hours ago by Brian Hatchet

Check out, review, and merge locally

Step 1. Fetch and check out the branch for this merge request

git fetch origin
git checkout -b epic/permissions-open-channels origin/epic/permissions-open-channels

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git fetch origin
git checkout origin/master
git merge --no-ff epic/permissions-open-channels

Step 4. Push the result of the merge to GitLab

git push origin master

Note that pushing to GitLab requires write access to this repository.

Tip: You can also checkout merge requests locally by following these guidelines.

Request to merge epic/permissions-open-channels into master
The source branch is 1 commit behind the target branch
Open in Web IDE
  • Email patches
  • Plain diff
Pipeline #80250424 passed for f7700208 on epic/permissions-open-channels
          Requires 2 more approvals from Devs and Deployers.
          Emiliano Balbuena
          Emiliano Balbuena
          Ben Hayward
          Ben Hayward
          Rami Albatal
          Rami Albatal
          Mark Harding
          Mark Harding
          Martin Santangelo
          Martin Santangelo
          Ready to be merged automatically. Ask someone with write access to this repository to merge this request

          Deletes source branch

          • Discussion 15
          • Commits 10
          • Pipelines 8
          • Changes 37
          0/14 threads resolved
          • Loading...
          • Brian Hatchet :speech_balloon: @brianhatchet changed milestone to %sprint: Modest Monkey 1 week ago

            changed milestone to %sprint: Modest Monkey

          • Brian Hatchet :speech_balloon: @brianhatchet added Squad::Green scoped label 1 week ago

            added Squad::Green scoped label

          • Brian Hatchet :speech_balloon: @brianhatchet added 1 commit 6 days ago

            added 1 commit

            • 34aca270 - Linting

            Compare with previous version

          • Brian Hatchet :speech_balloon: @brianhatchet added 6 commits 1 day ago

            added 6 commits

            • 34aca270...d684846f - 5 commits from branch master
            • f7700208 - Merge remote-tracking branch 'origin/master' into epic/permissions-open-channels

            Compare with previous version

          • Brian Hatchet :speech_balloon: @brianhatchet unmarked as a Work In Progress 21 hours ago

            unmarked as a Work In Progress

          • Brian Hatchet :speech_balloon: @brianhatchet changed the description 21 hours ago

            changed the description

          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 27 minutes ago
            Core/Permissions/Permissions.php
            21 /** @var bool */
            22 private $isBanned = false;
            23 /** @var User */
            24 private $user;
            25 /** @var Roles */
            26 private $roles;
            27 /** @var array */
            28 private $entities;
            29 /** @var ChannelRoleCalculator */
            30 private $channelRoleCalculator;
            31 /** @var GroupRoleCalculator */
            32 private $groupRoleCalculator;
            33 /** @var EntitiesBuilder */
            34 private $entitiesBuilder;
            35
            36 public function setUser(User $user)
            • Mark Harding
              Mark Harding @markeharding · 27 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Delegates/ChannelRoleCalculator.php
            8 class ChannelRoleCalculator extends BaseRoleCalculator
            9 {
            10 use MagicAttributes;
            11
            12 private $channels = [];
            13
            14 /**
            15 * Retrieves permissions for an entity relative to the user's role in a channel
            16 * Retrieves the role from the in memory cache if we've seen this channel before during this request
            17 * Else checks the user's membership against the channel.
            18 *
            19 * @param $entity an entity from a channel
            20 *
            21 * @return Role
            22 */
            23 public function calculate($entity)
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Delegates/GroupRoleCalculator.php
            20 public function __construct(User $user, Roles $roles, EntitiesBuilder $entitiesBuilder = null)
            21 {
            22 parent::__construct($user, $roles);
            23 $this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
            24 }
            25
            26 /**
            27 * Retrieves permissions for an entity relative to the user's role in a group
            28 * Retrieves the role from the in memory cache if we've seen this group before during this request
            29 * Else gets the group and checks the user's membership.
            30 *
            31 * @param $entity an entity belonging to a group
            32 *
            33 * @return Role
            34 */
            35 public function calculate($entity)
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Entities/Manager.php
            23 public function __construct(
            24 EntitiesBuilder $entitiesBuilder = null,
            25 Call $db = null,
            26 Save $save = null)
            27 {
            28 $this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
            29 $this->db = $db ?: new Call('entities_by_time');
            30 $this->save = $save ?: new Save(); //Mockable, else instantiate a new one on save.
            31 }
            32
            33 /**
            34 * Save permissions for an entity and propegate it to linked objects
            35 * @param mixed $entity a minds entity that implements the save function
            36 * @param Permissions $permissions the flag to apply to the entity
            37 */
            38 public function save($entity, EntityPermissions $permissions)
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Manager.php
            30
            31 22 /**
            32 * Save permissions for an entity and propegate it to linked objects
            33 * @param mixed $entity a minds entity that implements the save function
            34 * @param Permissions $permissions the flag to apply to the entity
            35 */
            36 public function save($entity, Permissions $permissions)
            23 * Takes a user_guid and list of entity guids
            24 * Builds up a permissions object
            25 * Permissions contains the user's role per entity, channel and group
            26 * @param array $opts
            27 * - user_guid: long, the user's guid for calculating permissions
            28 * - guids: array long, the list of entities to permit
            29 * @return Permissions A map of channels, groups and entities with the user's role for each
            30 */
            31 public function getList(array $opts = [])
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            26 private $roles;
            27 /** @var array */
            28 private $entities;
            29 /** @var ChannelRoleCalculator */
            30 private $channelRoleCalculator;
            31 /** @var GroupRoleCalculator */
            32 private $groupRoleCalculator;
            33 /** @var EntitiesBuilder */
            34 private $entitiesBuilder;
            35
            36 public function setUser(User $user)
            37 {
            38 throw new ImmutableException('User can only be set in the constructor');
            39 }
            40
            41 public function __construct(User $user, Roles $roles = null, EntitiesBuilder $entitiesBuilder = null)
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              I don't think we should have functions above the constructor?

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            51 $this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
            52 $this->channels[$user->getGUID()] = $user;
            53 $this->channelRoleCalculator = new ChannelRoleCalculator($this->user, $this->roles);
            54 $this->groupRoleCalculator = new GroupRoleCalculator($this->user, $this->roles, $entitiesBuilder);
            55 }
            56
            57 /**
            58 * Takes an array of entities and checks their permissions
            59 * Builds up collections of permissions based on the user's relationships to the entity
            60 * Any found channels and their roles are accessible in the channelRoleCalculator
            61 * Any found groups and their roles are in the groupRoleCalculator
            62 * All requested entities and the user's role is available in $this->entities.
            63 *
            64 * @param array entities an array of entities for calculating permissions
            65 */
            66 public function calculate(array $entities = [])
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            58 * Takes an array of entities and checks their permissions
            59 * Builds up collections of permissions based on the user's relationships to the entity
            60 * Any found channels and their roles are accessible in the channelRoleCalculator
            61 * Any found groups and their roles are in the groupRoleCalculator
            62 * All requested entities and the user's role is available in $this->entities.
            63 *
            64 * @param array entities an array of entities for calculating permissions
            65 */
            66 public function calculate(array $entities = [])
            67 {
            68 foreach ($entities as $entity) {
            69 $this->entities[$entity->getGUID()] = $this->getRoleForEntity($entity);
            70 }
            71 }
            72
            73 private function getRoleForEntity($entity)
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            90 if ($this->isAdmin) {
            91 $role = $this->roles->getRole(Roles::ROLE_ADMIN);
            92 }
            93 if ($this->isBanned) {
            94 $role = $this->roles->getRole(Roles::ROLE_BANNED);
            95 }
            96
            97 return $role;
            98 }
            99
            100 /**
            101 * Export the nested objects.
            102 *
            103 * @return array serialized objects
            104 */
            105 public function export()
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            104 */
            105 public function export()
            106 {
            107 $export = [];
            108 $export['user'] = $this->user->export();
            109 $export['channels'] = $this->getChannels();
            110 $export['groups'] = $this->getGroups();
            111 $export['entities'] = $this->entities;
            112
            113 return $export;
            114 }
            115
            116 /**
            117 * @return array channel guids with the user's role
            118 */
            119 public function getChannels()
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            112
            113 return $export;
            114 }
            115
            116 /**
            117 * @return array channel guids with the user's role
            118 */
            119 public function getChannels()
            120 {
            121 return $this->channelRoleCalculator->getChannels();
            122 }
            123
            124 /**
            125 * @return array group guids with the user's role
            126 */
            127 public function getGroups()
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Permissions.php
            120 {
            121 return $this->channelRoleCalculator->getChannels();
            122 }
            123
            124 /**
            125 * @return array group guids with the user's role
            126 */
            127 public function getGroups()
            128 {
            129 return $this->groupRoleCalculator->getGroups();
            130 }
            131
            132 /**
            133 * @return array serialized objects
            134 */
            135 public function jsonSerialize()
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Roles/BaseRole.php
            1 <?php
            2
            3 namespace Minds\Core\Permissions\Roles;
            4
            5 use Zend\Permissions\Rbac;
            6
            7 abstract class BaseRole extends Rbac\Role implements \JsonSerializable
            8 {
            9 public function export()
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Return type would be nice

            • Please register or sign in to reply
          • Mark Harding
            Mark Harding @markeharding started a thread on the diff 21 minutes ago
            Core/Permissions/Manager.php
            54 41
            55 foreach ($this->db->getRow('activity:entitylink:'.$entity->getGUID()) as $parentGuid => $ts) {
            56 $activity = $this->entitiesBuilder->single($parentGuid);
            57 $activity->setAllowComments($permissions->getAllowComments());
            58 $this->save
            59 ->setEntity($activity)
            60 ->save();
            42 $user = $this->entitiesBuilder->single($opts['user_guid']);
            43 $entities = $this->entitiesBuilder->get($opts);
            44
            45 if ($user->getType() !== 'user') {
            46 throw new \InvalidArgumentException('Entity is not a user');
            47 }
            48
            49 /** @var Permissions */
            50 $permissions = new Permissions($user, null, $entitiesBuilder);
            • Mark Harding
              Mark Harding @markeharding · 21 minutes ago
              Owner

              Should 2nd param not be roles injected from above?

            • Please register or sign in to reply
          • Mark Harding @markeharding added MR::Requires Changes scoped label 21 minutes ago

            added MR::Requires Changes scoped label

          • Mark Harding
            Mark Harding @markeharding · 21 minutes ago
            Owner

            Looks great. How are we feeling about caching?

          • You're only seeing other activity in the feed. To add a comment, switch to one of the following options.
          Please register or sign in to reply
          Assignee
          Brian Hatchet's avatar
          Brian Hatchet @brianhatchet
          Assign to
          sprint: Modest Monkey
          Milestone
          sprint: Modest Monkey
          Assign milestone
          None
          Time tracking
          No estimate or time spent
          2
          Labels
          MR::Requires Changes Squad::Green
          Assign labels
          • View project labels
          Lock merge request
          Unlocked
          7
          7 participants
          user avatar
          Emiliano Balbuena
          user avatar
          Ben Hayward
          user avatar
          Rami Albatal
          user avatar
          Mark Harding
          user avatar
          Martin Santangelo
          user avatar
          Marcelo Rivera
          user avatar
          Brian Hatchet
          Reference: minds/engine!307