Skip to content

Commit

Permalink
premission append added, resource permission generate refactored, per…
Browse files Browse the repository at this point in the history
…mission view refactored, config refactored, read me updated
  • Loading branch information
Nur Alam committed Feb 24, 2023
1 parent dda17aa commit f945268
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 171 deletions.
347 changes: 270 additions & 77 deletions README.md

Large diffs are not rendered by default.

45 changes: 18 additions & 27 deletions config/permission-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,55 @@
* Custom permissions
*/
'custom-permissions' => [
//

],

/**
* Define controller namespace
* Permission generate controller's namespace
*
* By Default permissions will be generated from all controller's routes
*
* [Note: permissions will be generated from those controller which contains the defined whole or prefix of controller namespace]
*/
'controller-namespace-prefixes' => [
'permission-generate-controllers' => [
'App\Http\Controllers',
],

/**
* Exclude routes by route name
*/
'exclude-routes' => [
// route.name
],

/**
* Exclude routes by controller whole namespace or sub/prefix of controller namespace
* Exclude routes by controller's namespace
*
* By default all auth controller's routes will be excluded from being generated as permission names
*
* [Note: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
* [Note: Exclude routes by defining App\Http\Controller\SomeController::class or namespace-prefix]
*/
'exclude-controllers' => [
// exclude every route which associate with the prefix of controller namespace
'App\Http\Controllers\Auth',
],

/**
* Cache the rendered permission names
* Exclude routes by route name
*/
'cache-permissions' => [
'cacheable' => true,
'cache-driver' => env('CACHE_DRIVER', 'file'),
'exclude-routes' => [
// route.name
],

/**
* Permission card size
*
* [NT: Predefined permission cards works on bootstrap]
* Cache the rendered permission names
*/
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
'cache-permissions' => true,

/**
* These tage used to generate permissions on given resources
* ---------------------------------------------------------------------------------------------------------
* This config only used if you want to generate permission names from resources instead of routes
* ---------------------------------------------------------------------------------------------------------
*
* These actions used to generate permissions on given resources
*
* [Ex: create-posts,'edit-posts','view-list-posts' etc]
* [Ex: If resource is posts, then permission will be (create-posts,'edit-posts','view-posts') etc]
*/
'resource-permission-tags' => [
'resource-actions' => [
'create',
'edit',
'show',
'delete',
'view-list'
'view',
],
];
Binary file added img/append-permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed img/department-rouets.png
Binary file not shown.
Binary file removed img/permissible-routes-output.png
Binary file not shown.
Binary file added img/permission-title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/routes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions resources/views/permission.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</div>

@foreach($permissions as $key => $values)
<div class="{{ config('permission-generator.card-size-class') }}">
<div class="col-md-3 col-lg-3 col-sm-12">
<div class="card permission-card">
<div class="card-header permission-header">
<div class="card-title">
Expand All @@ -30,7 +30,7 @@
@foreach($values as $route)
<li>
<input type="checkbox" name="permissions[]" value="{{ $route['name'] }}" id="{{ $route['name'] }}" {{ in_array($route['name'],$rolePermissions) ? 'checked' : '' }}>
<label class="form-check-label" for="{{ $route['name'] }}">{{ $route['title'] }}</label>
<label class="form-check-label" for="{{ $route['name'] }}">{{ $route['text'] }}</label>
</li>
@endforeach
</ul>
Expand Down
6 changes: 4 additions & 2 deletions src/Facades/PermissionsView.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use RadiateCode\PermissionNameGenerator\Html\Builder;

/**
* @method static Builder withRolePermissions(string $roleName, array $rolePermissions, string $permissionsSaveUrl = null)
* @method static Application|Factory|View view(string $view, array $data = [])
* @method static Builder make(string $view, array $data = [])
* @method static Builder withPermissions(array $resources)
* @method static Builder markRolePermissions(string $roleName, array $rolePermissions, string $permissionsSaveUrl = null)
* @method static Application|Factory|View render()
*
* @see Builder
*/
Expand Down
53 changes: 21 additions & 32 deletions src/Html/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ class Builder

protected $url = null;

protected string $source = 'routes';
protected array $permissions = [];

protected array $resources = [];
protected string $view;

protected array $viewData = [];

/**
* Mark or tick the stored role's permissions
*
* @param string $roleName
* @param array $rolePermissions
* @param string|null $permissionsSaveUrl // role permissions save url
*
* @return $this
*/
public function withRolePermissions(
public function markRolePermissions(
string $roleName,
array $rolePermissions,
string $permissionsSaveUrl = null
Expand All @@ -42,52 +46,46 @@ public function withRolePermissions(
}

/**
* Permissions generate from route names
* Permissions
*
* @param array $permissions
* @return Builder
*/
public function fromRoutes()
public function withPermissions(array $permissions)
{
$this->source = 'routes';
$this->permissions = $permissions;

return $this;
}

/**
* Permissions generate from resources
* @param string $view
* @param array $data
*
* @param array $resources
* @return Builder
*/
public function fromResources(array $resources)
public function make(string $view, array $data = [])
{
$this->source = 'resources';
$this->view = $view;

$this->resources = $resources;
$this->viewData = $data;

return $this;
}

/**
* @param string $view
* @param array $data
*
* @return Application|Factory|\Illuminate\Contracts\View\View
*/
public function view(string $view, array $data = [])
public function render()
{
return \view($view, $data)
->with('permissionCards', $this->render())
return \view($this->view, $this->viewData)
->with('permissionCards', $this->cards())
->with('permissionScripts', $this->scripts());
}

protected function render(array $permissions = []): string
protected function cards(): string
{

return View::make(
'permission-generator::permission',
[
'permissions' => $this->resolvePermissions(),
'permissions' => $this->permissions,
'roleName' => $this->roleName,
'rolePermissions' => $this->rolePermissions,
]
Expand All @@ -98,13 +96,4 @@ protected function scripts(): string
{
return View::make('permission-generator::scripts', ['url' => $this->url])->render();
}

protected function resolvePermissions(): array
{
if ($this->source == 'routes') {
return Permissions::make()->fromRoutes()->get();
}

return Permissions::make()->fromResources($this->resources)->get();
}
}
3 changes: 2 additions & 1 deletion src/PermissionNameServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function boot()
__DIR__.'/../resources/views' => resource_path(
'views/vendor/permission-generator'
),
]
],
'permission-views'
);

$this->publishes(
Expand Down
4 changes: 2 additions & 2 deletions src/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function customPermissions(): Permissions
if (!is_array($item)) {
$this->permissions[$key][] = [
'name' => $item,
'title' => ucwords(str_replace($this->splitter, ' ', $item)),
'text' => ucwords(str_replace($this->splitter, ' ', $item)),
];

continue;
Expand Down Expand Up @@ -114,7 +114,7 @@ protected function getCachedPermissions()

protected function hasCachedPermissions(): bool
{
return config('permission-generator.cache-permissions.cacheable')
return config('permission-generator.cache-permissions')
&& Cache::has(Constant::CACHE_PERMISSIONS);
}

Expand Down
42 changes: 16 additions & 26 deletions src/Services/ResourcePermissionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,43 @@ public function __construct(array $resources)

public function generate()
{
$tags = config(
'permission-generator.resource-permission-tags'
$actions = config(
'permission-generator.resource-actions'
);

$resourcePermissions = [];

$onlyPermissionNames = [];

foreach ($this->resources as $resource) {
foreach ($this->resources as $resource => $val) {

// check is resource has includes key, if so then merge includes tags with global tags
if (
is_array($resource)
&& array_key_exists('includes', $resource)
&& !empty($resource['includes'])
) {
$tags = array_merge($tags, $resource['includes']);
if (is_string($resource)) {
if (is_array($val) && !empty($val)) {
$actions = array_merge($actions, $val);
}
} else {
$resource = $val;
}

foreach ($tags as $tag) {
// check is the resource has excludes key, if so then exclude the tag form generating permissions
if (
is_array($resource)
&& array_key_exists('excludes', $resource)
&& !empty($resource['excludes'])
&& in_array($tag, $resource['excludes'])
) {
continue;
}
foreach ($actions as $action) {

$key = $resource . '-permission';
$key = $resource . '-permissions';

// remove this special char from tag, and make it slugable
$tagSlug = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_'], '-', $tag);
$actionSlug = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_'], '-', $action);

// remove hyphens
$tagTitle = str_replace('-', ' ', $tagSlug);
$actionTitle = str_replace('-', ' ', $actionSlug);

// generate permission name
$permissionName = $tagSlug . '-' . $resource;
$permissionTitle = ucwords("{$tagTitle} {$resource}");
$permissionName = $actionSlug . '-' . $resource;
$permissionTitle = ucwords("{$actionTitle} {$resource}");

$onlyPermissionNames[] = $permissionName;

$resourcePermissions[$key][] = [
'name' => $permissionName,
'title' => $permissionTitle,
'text' => $permissionTitle,
];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Services/RoutePermissionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RoutePermissionGenerator
public function generate()
{
$this->controllerNamespacePrefixes = config(
'permission-generator.controller-namespace-prefixes'
'permission-generator.permission-generate-controllers'
);

$this->globalExcludeControllers = config(
Expand Down Expand Up @@ -84,7 +84,7 @@ public function generate()

$permissions[$key][] = [
'name' => $routeName, // permission name
'title' => ucwords(str_replace($splitter, ' ', $routeName)), // permission title
'text' => ucwords(str_replace($splitter, ' ', $routeName)), // permission title
];

$onlyPermissionNames[] = $routeName;
Expand Down

0 comments on commit f945268

Please sign in to comment.