Commit 31f45fc5 authored by Brian Hatchet's avatar Brian Hatchet :speech_balloon:

MR changes

1 merge request!307Epic/permissions open channels
Pipeline #80587986 failed with stages
in 4 minutes and 38 seconds
......@@ -4,6 +4,7 @@ namespace Minds\Core\Permissions\Delegates;
use Minds\Traits\MagicAttributes;
use Minds\Core\Permissions\Roles\Roles;
use Minds\Core\Permissions\Roles\Role;
class ChannelRoleCalculator extends BaseRoleCalculator
{
......@@ -20,7 +21,7 @@ class ChannelRoleCalculator extends BaseRoleCalculator
*
* @return Role
*/
public function calculate($entity)
public function calculate($entity): Role
{
if (isset($this->channels[$entity->getOwnerGUID()])) {
return $this->channels[$entity->getOwnerGUID()];
......
......@@ -7,6 +7,7 @@ use Minds\Core\Di\Di;
use Minds\Core\Permissions\Roles\Roles;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\User;
use Minds\Core\Permissions\Roles;
class GroupRoleCalculator extends BaseRoleCalculator
{
......@@ -32,7 +33,7 @@ class GroupRoleCalculator extends BaseRoleCalculator
*
* @return Role
*/
public function calculate($entity)
public function calculate($entity): Role
{
if (isset($this->groups[$entity->getAccessId()])) {
return $this->groups[$entity->getAccessId()];
......
......@@ -6,7 +6,6 @@ use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Data\Call;
use Minds\Core\Entities\Actions\Save;
use Minds\Core\Permissions\Entities\EntityPermissions;
/*
* Manager for managing entity specific permissions
......@@ -31,11 +30,12 @@ class Manager
}
/**
* Save permissions for an entity and propegate it to linked objects
* @param mixed $entity a minds entity that implements the save function
* @param Permissions $permissions the flag to apply to the entity
*/
public function save($entity, EntityPermissions $permissions)
* Save permissions for an entity and propegate it to linked objects.
*
* @param mixed $entity a minds entity that implements the save function
* @param Permissions $permissions the flag to apply to the entity
*/
public function save($entity, EntityPermissions $permissions): void
{
$entity->setAllowComments($permissions->getAllowComments());
......
......@@ -3,8 +3,6 @@
namespace Minds\Core\Permissions;
use Minds\Core\Di\Di;
use Minds\Core\Permissions\Permissions;
use Minds\Core\EntitiesBuilder;
/*
* Manager for managing role based permissions
......@@ -22,13 +20,15 @@ class Manager
/**
* 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
* 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
* - 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 = [])
public function getList(array $opts = []): Permissions
{
$opts = array_merge([
'user_guid' => null,
......@@ -46,6 +46,8 @@ class Manager
throw new \InvalidArgumentException('Entity is not a user');
}
$roles = new Roles();
/** @var Permissions */
$permissions = new Permissions($user, null, $entitiesBuilder);
if (is_array($entities)) {
......
......@@ -33,11 +33,6 @@ class Permissions implements \JsonSerializable
/** @var EntitiesBuilder */
private $entitiesBuilder;
public function setUser(User $user)
{
throw new ImmutableException('User can only be set in the constructor');
}
public function __construct(User $user, Roles $roles = null, EntitiesBuilder $entitiesBuilder = null)
{
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
......@@ -54,6 +49,16 @@ class Permissions implements \JsonSerializable
$this->groupRoleCalculator = new GroupRoleCalculator($this->user, $this->roles, $entitiesBuilder);
}
/**
* Permissions are user aware. This bomb function is to keep the user from being changed after instantiation.
*
* @throws ImmutableException
*/
public function setUser(User $user): void
{
throw new ImmutableException('User can only be set in the constructor');
}
/**
* Takes an array of entities and checks their permissions
* Builds up collections of permissions based on the user's relationships to the entity
......@@ -63,14 +68,14 @@ class Permissions implements \JsonSerializable
*
* @param array entities an array of entities for calculating permissions
*/
public function calculate(array $entities = [])
public function calculate(array $entities = []): void
{
foreach ($entities as $entity) {
$this->entities[$entity->getGUID()] = $this->getRoleForEntity($entity);
}
}
private function getRoleForEntity($entity)
private function getRoleForEntity($entity): Role
{
$role = null;
//Access id is the best way to determine what the parent entity is
......@@ -102,7 +107,7 @@ class Permissions implements \JsonSerializable
*
* @return array serialized objects
*/
public function export()
public function export(): array
{
$export = [];
$export['user'] = $this->user->export();
......@@ -116,7 +121,7 @@ class Permissions implements \JsonSerializable
/**
* @return array channel guids with the user's role
*/
public function getChannels()
public function getChannels(): array
{
return $this->channelRoleCalculator->getChannels();
}
......@@ -124,7 +129,7 @@ class Permissions implements \JsonSerializable
/**
* @return array group guids with the user's role
*/
public function getGroups()
public function getGroups(): array
{
return $this->groupRoleCalculator->getGroups();
}
......@@ -132,7 +137,7 @@ class Permissions implements \JsonSerializable
/**
* @return array serialized objects
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->export();
}
......
......@@ -6,7 +6,7 @@ use Zend\Permissions\Rbac;
abstract class BaseRole extends Rbac\Role implements \JsonSerializable
{
public function export()
public function export(): array
{
$export = [];
$export['name'] = $this->getName();
......@@ -15,7 +15,7 @@ abstract class BaseRole extends Rbac\Role implements \JsonSerializable
return $export;
}
public function jsonSerialize()
public function jsonSerialize(): array
{
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