Skip to content

Commit

Permalink
Merge 47a4ed6 into 66e1519
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Jan 15, 2016
2 parents 66e1519 + 47a4ed6 commit 6328457
Show file tree
Hide file tree
Showing 32 changed files with 893 additions and 138 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'];
}
59 changes: 46 additions & 13 deletions api/app/Models/Relations/RolePermissionRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,85 @@

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 Collection
*/
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
* @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 = (new Collection($models))->pluck('key');
}

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

$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
48 changes: 22 additions & 26 deletions api/config/permissions/articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,93 +26,89 @@
],
ArticleController::class.'@getOne' => [
'type' => 'permission',
'description' => 'Get one',
'description' => 'Get one article',
],
ArticleController::class.'@getAllLocalizations' => [
'type' => 'permission',
'description' => 'Get localizations',
'description' => 'Get all localizations for article',
],
ArticleController::class.'@getOneLocalization' => [
'type' => 'permission',
'description' => 'Get localization',
'description' => 'Get localization for article',
],
ArticleController::class.'@postOne' => [
'type' => 'permission',
'description' => 'Post',
'description' => 'Create new article',
],
ArticleController::class.'@putOne' => [
'type' => 'permission',
'description' => 'Put',
'description' => 'Create new article',
],
ArticleController::class.'@patchOne' => [
'type' => 'permission',
'description' => 'Patch',
'description' => 'Update article',
],
ArticleController::class.'@deleteOne' => [
'type' => 'permission',
'description' => 'Delete',
'description' => 'Delete article',
],
ArticleController::class.'@putOneLocalization' => [
'type' => 'permission',
'description' => 'Put localization',
],
ArticleController::class.'@syncMany' => [
'type' => 'permission',
'description' => 'Sync',
'description' => 'Add localization to article',
],
ArticlePermalinkController::class.'@getAll' => [
'type' => 'permission',
'description' => 'Get permalinks',
'description' => 'Get all article permalinks',
],
ArticleMetaController::class.'@getAll' => [
'type' => 'permission',
'description' => 'Get all meta',
'description' => 'Get all article meta',
],
ArticleMetaController::class.'@putMany' => [
'type' => 'permission',
'description' => 'Add meta',
'description' => 'Add article meta',
],
ArticleMetaController::class.'@deleteOne' => [
'type' => 'permission',
'description' => 'Delete meta',
'description' => 'Delete article meta',
],
ArticleCommentController::class.'@getAll' => [
'type' => 'permission',
'description' => 'Get all comments',
'description' => 'Get all article comments',
],
ArticleCommentController::class.'@postOne' => [
'type' => 'permission',
'description' => 'Post comment',
'description' => 'Post article comment',
],

ArticleTagController::class.'@getAll' => [
'type' => 'permission',
'description' => 'Get all tags',
'description' => 'Get all article tags',
],
ArticleTagController::class.'@putMany' => [
'type' => 'permission',
'description' => 'Add tags',
'description' => 'Add article tags',
],

ArticleSectionController::class.'@getAll' => [
'type' => 'permission',
'description' => 'Get all sections',
'description' => 'Get all article sections',
],
ArticleSectionController::class.'@postMany' => [
'type' => 'permission',
'description' => 'Post',
],
ArticleSectionController::class.'@deleteMany' => [
'type' => 'permission',
'description' => 'Delete many',
'description' => 'Delete many article sections',
],
ArticleSectionController::class.'@deleteOne' => [
'type' => 'permission',
'description' => 'Delete one',
'description' => 'Delete one article section',
],
ArticleSectionController::class.'@putOneChildLocalization' => [
'type' => 'permission',
'description' => 'Put section localization',
'description' => 'Add section localization to article',
],

ArticleUserRatingsController::class.'@putOne' => [
Expand All @@ -121,15 +117,15 @@
],
ArticleBookmarksController::class.'@putOne' => [
'type' => 'permission',
'description' => 'Add to bookmarks',
'description' => 'Add article to bookmarks',
],
ArticleUserRatingsController::class.'@deleteOne' => [
'type' => 'permission',
'description' => 'Remove article rating',
],
ArticleBookmarksController::class.'@deleteOne' => [
'type' => 'permission',
'description' => 'Remove from bookmarks',
'description' => 'Remove article from bookmarks',
],

//special permissions (hierarchy or rules)
Expand Down
2 changes: 2 additions & 0 deletions api/config/permissions/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
UserController::class.'@patchOne',
UserController::class.'@deleteOne',
UserProfileController::class.'@getOne',
UserProfileController::class.'@patchOne',
UserProfileController::class.'@putOne',
],
],
];
16 changes: 16 additions & 0 deletions api/tests/integration/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@ public function testGetAll()
$this->assertJsonArray();
$this->assertJsonMultipleEntries();
}

public function testGetAllWithNestedPermissions()
{

$this->withAuthorization()->getJson('/roles', ['with-nested' => 'permissions']);

$this->assertResponseOk();
$this->shouldReturnJson();
$this->assertJsonArray();
$this->assertJsonMultipleEntries();

$result = json_decode($this->response->getContent(), true);

$this->assertArrayHasKey('_permissions', $result[0]);
}

}
7 changes: 4 additions & 3 deletions app/src/app/admin/users/editUser/editUser.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
> div {
padding: 20px;
}

md-icon {
color: @primary-contrast;
}
}

.details {
Expand Down Expand Up @@ -36,8 +40,5 @@
}
}

md-icon {
color: @primary-contrast;
}
}

6 changes: 4 additions & 2 deletions app/src/app/admin/users/editUser/editUser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace app.admin.users.editUser {
timezones:common.services.timezones.ITimezoneDefinition,
fullUserInfo:common.models.User = common.models.UserMock.entity(),
genderOptions:common.models.IGenderOption[] = common.models.UserProfile.genderOptions,
providerTypes:string[] = common.models.UserSocialLogin.providerTypes
providerTypes:string[] = common.models.UserSocialLogin.providerTypes,
roles:common.models.Role[] = common.models.RoleMock.collection()
;

beforeEach(() => {
Expand Down Expand Up @@ -49,7 +50,8 @@ namespace app.admin.users.editUser {
providerTypes: providerTypes,
regions: _regionService_.supportedRegions,
$location: _$location_,
$stateParams: $stateParams
$stateParams: $stateParams,
roles: roles,
});

});
Expand Down
16 changes: 14 additions & 2 deletions app/src/app/admin/users/editUser/editUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace app.admin.users.editUser {
controller: namespace+'.controller',
controllerAs: 'ProfileController',
templateUrl: 'templates/app/user/profile/profile.tpl.html',
},
['roles@'+namespace]: {
controller: namespace+'.roles.controller',
controllerAs: 'RolesController',
templateUrl: 'templates/app/admin/users/editUser/roles/roles.tpl.html',
}
},
resolve: /*@ngInject*/{
Expand All @@ -31,7 +36,7 @@ namespace app.admin.users.editUser {
return timezonesService.getAllTimezones();
},
fullUserInfo:(userService:common.services.user.UserService, $stateParams:IEditUserStateParams) => {
return userService.getModel($stateParams.userId, ['userCredential', 'userProfile', 'socialLogins', 'uploadedAvatar']);
return userService.getModel($stateParams.userId, ['userCredential', 'userProfile', 'socialLogins', 'uploadedAvatar', 'roles']);
},
genderOptions:() => {
return common.models.UserProfile.genderOptions;
Expand All @@ -41,6 +46,9 @@ namespace app.admin.users.editUser {
},
regions:(regionService:common.services.region.RegionService) => {
return regionService.supportedRegions;
},
roles:(roleService:common.services.role.RoleService):ng.IPromise<common.models.Role[]> => {
return roleService.getAllModels<common.models.Role>(['permissions']);
}
},
data: {
Expand Down Expand Up @@ -74,6 +82,7 @@ namespace app.admin.users.editUser {
'regions',
'providerTypes',
'fullUserInfo',
'roles',

//EditUserController
'$mdDialog',
Expand All @@ -91,6 +100,7 @@ namespace app.admin.users.editUser {
providerTypes:string[],
fullUserInfo:common.models.User,

public roles:any[],
private $mdDialog:ng.material.IDialogService,
private $state:ng.ui.IStateService
) {
Expand Down Expand Up @@ -142,7 +152,9 @@ namespace app.admin.users.editUser {

}

angular.module(namespace, [])
angular.module(namespace, [
namespace + '.roles',
])
.config(EditUserConfig)
.controller(namespace+'.controller', EditUserController);
}
Expand Down
Loading

0 comments on commit 6328457

Please sign in to comment.