Commit 1111178b authored by Brian Hatchet's avatar Brian Hatchet
Browse files

CR updates

parent 3b020fa8
Pipeline #75491124 passed with stages
in 9 minutes and 14 seconds
......@@ -23,12 +23,13 @@ class roles implements Interfaces\Api
$manager = Di::_()->get('Permissions\Manager');
$opts = [
'user_guid' => $pages[0],
'guids' => $_GET['guids']
'guids' => $_GET['guids'],
];
$permissions = $manager->getList($opts);
return Factory::response([
'status' => 'success',
'roles' => $permissions
'roles' => $permissions,
]);
} catch (Exception $ex) {
return Factory::response([
......
......@@ -12,10 +12,11 @@ class ChannelRoleCalculator extends BaseRoleCalculator
private $channels = [];
/**
* @param $entity an entity from a channel
* Retrieves permissions for an entity relative to the user's role in a channel
* Retrieves the role from the in memory cache if we've seen this channel before during this request
* Else checks the user's membership against the channel
* Else checks the user's membership against the channel.
*
* @param $entity an entity from a channel
*
* @return Role
*/
......
......@@ -24,10 +24,11 @@ class GroupRoleCalculator extends BaseRoleCalculator
}
/**
* @param $entity an entity belonging to a group
* Retrieves permissions for an entity relative to the user's role in a group
* Retrieves the role from the in memory cache if we've seen this group before during this request
* Else gets the group and checks the user's membership
* Else gets the group and checks the user's membership.
*
* @param $entity an entity belonging to a group
*
* @return Role
*/
......
......@@ -3,44 +3,54 @@
namespace Minds\Core\Permissions;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Data\Call;
use Minds\Core\Permissions\Roles;
use Minds\Core\Permissions\Permissions;
/*
* Manager for managing role based permissions
*/
class Manager {
class Manager
{
/** @var EntityBuilder */
private $entityBuilder;
public function __construct($entityBuilder = null) {
public function __construct($entityBuilder = null)
{
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
}
public function getList(array $opts = []) {
/**
* Takes a user_guid and list of entity guids
* Builds up a permissions object
* Permissions contains the user's role per entity, channel and group
* @param array $opts
* - user_guid: long, the user's guid for calculating permissions
* - guids: array long, the list of entities to permit
* @return Permissions A map of channels, groups and entities with the user's role for each
*/
public function getList(array $opts = [])
{
$opts = array_merge([
'user_guid' => null,
'guids' => []
'guids' => [],
], $opts);
if ($opts['user_guid'] === null) {
throw new \InvalidArgumentException('user_guid is required');
}
$user = $this->entitiesBuilder->single($opts['user_guid']);
$entities = $this->entitiesBuilder->get($opts);
error_log(var_export($user->getGroupMembership(), true));
if ($user->getType() !== 'user') {
throw new \InvalidArgumentException('Entity is not a user');
}
/** @var Permissions */
$permissions = new Permissions($user);
if(is_array($entities)) {
error_log('calculating');
if (is_array($entities)) {
$permissions->calculate($entities);
}
return $permissions;
}
}
......@@ -2,14 +2,12 @@
namespace Minds\Core\Permissions;
use Minds\Core\Di\Di;
use Minds\Traits\MagicAttributes;
use Minds\Entities\User;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Permissions\Roles\Roles;
use Minds\Core\Permissions\Delegates\ChannelRoleCalculator;
use Minds\Core\Permissions\Delegates\GroupRoleCalculator;
use Minds\Common\Access;
class Permissions implements \JsonSerializable
......@@ -46,13 +44,13 @@ class Permissions implements \JsonSerializable
}
/**
* @param array entities an array of entities for calculating permissions
* Takes an array of entities and checks their permissions
* Builds up collections of permissions based on the user's relationships to the entity
* Any found channels and their roles are accessible in the channelRoleCalculator
* Any found groups and their roles are in the groupRoleCalculator
* All requested entities and the user's role is available in $this->entities
* @return void
* All requested entities and the user's role is available in $this->entities.
*
* @param array entities an array of entities for calculating permissions
*/
public function calculate(array $entities = [])
{
......@@ -88,7 +86,11 @@ class Permissions implements \JsonSerializable
return $role;
}
/* Export the nested objects */
/**
* Export the nested objects.
*
* @return array serialized objects
*/
public function export()
{
$export = [];
......@@ -100,14 +102,25 @@ class Permissions implements \JsonSerializable
return $export;
}
public function getChannels() {
/**
* @return array channel guids with the user's role
*/
public function getChannels()
{
return $this->channelRoleCalculator->getChannels();
}
public function getGroups() {
/**
* @return array group guids with the user's role
*/
public function getGroups()
{
return $this->groupRoleCalculator->getGroups();
}
/**
* @return array serialized objects
*/
public function jsonSerialize()
{
return $this->export();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment