Skip to content

Commit

Permalink
Added basic controller, service, model, view and spec files for admin…
Browse files Browse the repository at this point in the history
… role crud for users, fixed implementation for role permission relation to allow proper nesting of permissions, added test
  • Loading branch information
zakhenry committed Jan 14, 2016
1 parent b739787 commit ae23f36
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 42 deletions.
31 changes: 24 additions & 7 deletions api/app/Models/Relations/RolePermissionRelation.php
Expand Up @@ -10,10 +10,11 @@

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
{
Expand All @@ -31,15 +32,13 @@ class RolePermissionRelation extends HasMany
public function __construct($roleKey)
{
$this->roleKey = $roleKey;
$this->related = new Role;
$this->localKey = $this->getPlainForeignKey();
}

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();
}

/**
Expand All @@ -58,4 +57,22 @@ protected function hydratePermissions($permissions)

return $permissionModels;
}

public function addEagerConstraints(array $models)
{
}

public function get(){

$storage = $this->getGate()->getStorage();

$permissions = $this->getItemsRecursively(Item::TYPE_PERMISSION, $storage->getChildren($this->roleKey));
return new Collection($this->hydratePermissions($permissions));
}

public function getPlainForeignKey()
{
return 'name';
}

}
48 changes: 22 additions & 26 deletions api/config/permissions/articles.php
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
16 changes: 16 additions & 0 deletions api/tests/integration/RoleTest.php
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]);
}

}
16 changes: 14 additions & 2 deletions app/src/app/admin/users/editUser/editUser.ts
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
40 changes: 40 additions & 0 deletions app/src/app/admin/users/editUser/roles/roles.spec.ts
@@ -0,0 +1,40 @@
namespace app.admin.users.editUser.roles {

describe('User Role admin', () => {

let RolesController:RolesController,
$scope:ng.IScope,
$rootScope:ng.IRootScopeService,
$q:ng.IQService,
fullUserInfo:common.models.User = common.models.UserMock.entity({});

beforeEach(() => {

module('app');

inject(($controller, _$rootScope_, _$q_, _notificationService_, _$location_, _regionService_) => {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$q = _$q_;

RolesController = $controller(app.admin.users.editUser.roles.namespace + '.controller', {
$scope: $scope,
fullUserInfo: fullUserInfo,
regions: _regionService_.supportedRegions,
});
});

});

describe('User Roles admin', () => {


it('should do something', () => {

});

});

});

}
12 changes: 12 additions & 0 deletions app/src/app/admin/users/editUser/roles/roles.tpl.html
@@ -0,0 +1,12 @@
<md-card flex novalidate ng-form="RolesController.rolesForm" class="md-whiteframe-z2" layout-padding>

<md-toolbar>
<div class="md-toolbar-tools">
<h2>Roles</h2>
</div>
</md-toolbar>

<pre highlight="">{{RolesController.roles|json}}</pre>


</md-card>
27 changes: 27 additions & 0 deletions app/src/app/admin/users/editUser/roles/roles.ts
@@ -0,0 +1,27 @@
namespace app.admin.users.editUser.roles {

export const namespace = 'app.admin.users.editUser.roles';

export class RolesController {

public rolesForm:ng.IFormController;

static $inject = [
'fullUserInfo',
'roles',
];

constructor(
public user:common.models.User,
public roles:any[]
) {
}
}

angular.module(namespace, [])
.controller(namespace+'.controller', RolesController);
}




2 changes: 1 addition & 1 deletion app/src/app/admin/users/listing/userListing.ts
Expand Up @@ -34,7 +34,7 @@ namespace app.admin.users.listing {
}
},
data: {
title: "Find Users",
title: "List Users",
icon: 'group',
navigation: true,
}
Expand Down
@@ -1,6 +1,6 @@
<section layout="row" id="shopping-list-builder" layout-align="space-around start">

<md-card flex novalidate class="md-whiteframe-z2" layout-padding>
<md-card flex class="md-whiteframe-z2" layout-padding>

<md-toolbar>
<div class="md-toolbar-tools">
Expand Down
5 changes: 1 addition & 4 deletions app/src/common/models/role/roleAssignmentModel.ts
Expand Up @@ -15,7 +15,4 @@ namespace common.models {

}

}



}
19 changes: 19 additions & 0 deletions app/src/common/models/role/roleModel.ts
@@ -0,0 +1,19 @@
namespace common.models {

export class Role extends AbstractModel{

protected __primaryKey = 'key';

public key:string;
public description:string;
public isDefault:boolean;
public type:string;

constructor(data:any, exists:boolean = false) {
super(data, exists);
this.hydrate(data, exists);
}

}

}

0 comments on commit ae23f36

Please sign in to comment.