Skip to content

Commit

Permalink
Merge 32b5904 into ba702a9
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Jan 1, 2018
2 parents ba702a9 + 32b5904 commit 01aa27a
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 58 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 7.0
- 7.1
- 7.2

matrix:
fast_finish: true
Expand All @@ -12,11 +13,15 @@ env:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""

matrix:
exclude:
- php: 7.2
env: COMPOSER_FLAGS="--prefer-lowest"

sudo: false

services:
- mongodb
- mysql

addons:
apt:
Expand All @@ -27,9 +32,6 @@ addons:

before_script:
- pecl install mongodb
- mysql -e 'create database unittest;'
- travis_retry composer self-update
- travis_retry composer install --no-interaction
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All Notable changes to `laravel-permission-mongodb` will be documented in this file.

## 1.4.0 - 2018-01-01

### Added
- Officially Support `laravel 5.5`

## 1.3.5 - 2017-10-18

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"illuminate/auth": "^5.2.0",
"illuminate/container": "^5.2.0",
"illuminate/contracts": "^5.2.0",
"jenssegers/mongodb": "^3.0|3.3.0-alpha"
"jenssegers/mongodb": "^3.0"
},
"require-dev": {
"codeclimate/php-test-reporter": "^0.4.4",
Expand Down
9 changes: 0 additions & 9 deletions config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,4 @@
*/

'cache_expiration_time' => 60 * 24,

/*
* By default we'll make an entry in the application log when the permissions
* could not be loaded. Normally this only occurs while installing the packages.
*
* If for some reason you want to disable that logging, set this value to false.
*/

'log_registration_exception' => true,
];
92 changes: 92 additions & 0 deletions src/Directives/PermissionDirectives.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Maklad\Permission\Directives;

use Illuminate\View\Compilers\BladeCompiler;

/**
* Class PermissionDirectives
* @package Maklad\Permission\Directives
*/
class PermissionDirectives
{
private $bladeCompiler;

public function __construct(BladeCompiler $bladeCompiler)
{
$this->bladeCompiler = $bladeCompiler;
}

/**
* Declare role directive
*/
public function roleDirective()
{
$this->bladeCompiler->directive('role', function ($arguments) {
list($role, $guard) = $this->extractRoleGuard($arguments);

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
});

$this->bladeCompiler->directive('endrole', function () {
return '<?php endif; ?>';
});
}

/**
* Declare hasrole directive
*/
public function hasroleDirective()
{
$this->bladeCompiler->directive('hasrole', function ($arguments) {
list($role, $guard) = $this->extractRoleGuard($arguments);

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
});
$this->bladeCompiler->directive('endhasrole', function () {
return '<?php endif; ?>';
});
}

/**
* Declare hasanyrole directive
*/
public function hasanyroleDirective()
{
$this->bladeCompiler->directive('hasanyrole', function ($arguments) {
list($roles, $guard) = $this->extractRoleGuard($arguments);

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
});
$this->bladeCompiler->directive('endhasanyrole', function () {
return '<?php endif; ?>';
});
}

/**
* Declare hasallroles directive
*/
public function hasallrolesDirective()
{
$this->bladeCompiler->directive('hasallroles', function ($arguments) {
list($roles, $guard) = $this->extractRoleGuard($arguments);

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
});
$this->bladeCompiler->directive('endhasallroles', function () {
return '<?php endif; ?>';
});
}

/**
* @param $arguments
*
* @return array
*/
private function extractRoleGuard($arguments): array
{
$arguments = preg_replace('(\(|\)| )', '', $arguments);

return \explode(',', $arguments . ',');
}
}
49 changes: 8 additions & 41 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
declare(strict_types=1);

namespace Maklad\Permission;

use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
use Maklad\Permission\Contracts\PermissionInterface as Permission;
use Maklad\Permission\Contracts\RoleInterface as Role;
use Maklad\Permission\Directives\PermissionDirectives;

/**
* Class PermissionServiceProvider
Expand All @@ -17,13 +17,13 @@ class PermissionServiceProvider extends ServiceProvider
public function boot(PermissionRegistrar $permissionLoader)
{
$this->publishes([
__DIR__ . '/../config/permission.php' => $this->app->configPath() . '/permission.php'
__DIR__ . '/../config/permission.php' => $this->app->configPath() . '/permission.php',
], 'config');

if ($this->app->runningInConsole()) {
$this->commands([
Commands\CreateRole::class,
Commands\CreatePermission::class
Commands\CreatePermission::class,
]);
}

Expand Down Expand Up @@ -53,45 +53,12 @@ protected function registerModelBindings()
protected function registerBladeExtensions()
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$bladeCompiler->directive('role', function ($arguments) {
$arguments = preg_replace('(\(|\)| )', '', $arguments);
list($role, $guard) = \explode(',', $arguments . ',');
$permissionDirectives = new PermissionDirectives($bladeCompiler);

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
});
$bladeCompiler->directive('endrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasrole', function ($arguments) {
$arguments = preg_replace('(\(|\)| )', '', $arguments);
list($role, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
});
$bladeCompiler->directive('endhasrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasanyrole', function ($arguments) {
$arguments = preg_replace('(\(|\)| )', '', $arguments);
list($roles, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
});
$bladeCompiler->directive('endhasanyrole', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('hasallroles', function ($arguments) {
$arguments = preg_replace('(\(|\)| )', '', $arguments);
list($roles, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
});
$bladeCompiler->directive('endhasallroles', function () {
return '<?php endif; ?>';
});
$permissionDirectives->roleDirective();
$permissionDirectives->hasroleDirective();
$permissionDirectives->hasanyroleDirective();
$permissionDirectives->hasallrolesDirective();
});
}
}
6 changes: 3 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,12 @@ public function getPermissionNames(): Collection
*/
private function convertToRoleModels($roles): Collection
{
if (\is_array($roles)) {
$roles = \collect($roles);
if (is_array($roles)) {
$roles = collect($roles);
}

if (! $roles instanceof Collection) {
$roles = \collect([$roles]);
$roles = collect([$roles]);
}

$roles = $roles->map(function ($role) {
Expand Down

0 comments on commit 01aa27a

Please sign in to comment.