Skip to content

Commit

Permalink
Merge pull request #359 from spira/feature/admin-user-role-crud
Browse files Browse the repository at this point in the history
Feature/admin user role crud
  • Loading branch information
Jeremy Sik committed Jan 27, 2016
2 parents 62d9147 + 7bd1664 commit 803e27e
Show file tree
Hide file tree
Showing 54 changed files with 2,715 additions and 482 deletions.
2 changes: 1 addition & 1 deletion api/app/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ class Permission extends BaseModel
*
* @var array
*/
protected $fillable = ['key', 'description'];
protected $fillable = ['key', 'description', 'parent_role_key'];
}
64 changes: 51 additions & 13 deletions api/app/Models/Relations/RolePermissionRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,90 @@

namespace App\Models\Relations;

use App\Models\Role;
use Spira\Rbac\Item\Item;
use App\Models\Permission;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spira\Core\Model\Collection\Collection;
use Spira\Rbac\Item\Item;

class RolePermissionRelation extends HasMany
{
use GateTrait;

/**
* @var string
* @var array
*/
private $roleKey;
private $roleKeys = [];

/**
* RolePermissionRelation constructor.
* @param string $roleKey
* @param Role $parent
*/
public function __construct($roleKey)
public function __construct(Role $parent)
{
$this->roleKey = $roleKey;
$this->related = $parent;
$this->parent = $parent;

$this->localKey = 'key';
$this->foreignKey = $parent->getKey();
}

public function getResults()
{
$storage = $this->getGate()->getStorage();

$permissions = $this->getItemsRecursively(Item::TYPE_PERMISSION, $storage->getChildren($this->roleKey));

return new Collection($this->hydratePermissions($permissions));
return $this->get();
}

/**
* @param Item[] $permissions
* @param $roleKey
* @return array
*/
protected function hydratePermissions($permissions)
protected function hydratePermissions($permissions, $roleKey)
{
$permissionModels = [];
foreach ($permissions as $permission) {
$permissionModels[] = new Permission([
'key' => $permission->name,
'description' => $permission->description,
'parent_role_key' => $roleKey,
]);
}

return $permissionModels;
}

/**
* @param array $models
*/
public function addEagerConstraints(array $models)
{
$this->roleKeys = collect($models)->pluck('key');
}

/**
* @return Collection
*/
public function get()
{
$storage = $this->getGate()->getStorage();
$allPermissions = new Collection;

if (empty($this->roleKeys)) {
$this->roleKeys = [$this->foreignKey];
}

collect($this->roleKeys)->each(function ($roleKey) use ($storage, $allPermissions) {
$permissions = $this->getItemsRecursively(Item::TYPE_PERMISSION, $storage->getChildren($roleKey));
foreach ($this->hydratePermissions($permissions, $roleKey) as $permission) {
$allPermissions->push($permission);
}
});

return $allPermissions;
}

public function getPlainForeignKey()
{
return 'parent_role_key';
}
}
2 changes: 1 addition & 1 deletion api/app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Role extends BaseModel

public function permissions()
{
return new RolePermissionRelation($this->key);
return new RolePermissionRelation($this);
}

public static function findOrNew($id, $columns = ['*'])
Expand Down
1 change: 1 addition & 0 deletions api/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class User extends IndexedModel implements AuthenticatableContract, SocialiteAut
'username',
'first_name',
'last_name',
'country',
'email_confirmed',
'timezone_identifier',
'avatar_img_url',
Expand Down
Loading

0 comments on commit 803e27e

Please sign in to comment.