Skip to content

Commit

Permalink
Merge pull request #21 from mostafamaklad/v1.2
Browse files Browse the repository at this point in the history
V1.2.1
  • Loading branch information
mostafamaklad committed Sep 5, 2017
2 parents e6975cc + 6a50c92 commit be75d54
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ language: php

php:
- 7.1
- 7.2

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

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

sudo: false

services:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,12 @@

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

## 1.2.1 - 2017-09-05

### Added
- Let middleware use caching
- Allow logging while exceptions

## 1.2.0 - 2017-09-03

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -41,7 +41,7 @@
"monolog/monolog": "^1.22",
"orchestra/testbench": "~3.4.2|~5.5.0",
"php-coveralls/php-coveralls": "^1.0",
"phpunit/phpunit": "^6.2"
"phpunit/phpunit": "^5.7|6.2"
},
"autoload": {
"psr-4": {
Expand Down
48 changes: 24 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/Middlewares/PermissionMiddleware.php
Expand Up @@ -13,12 +13,16 @@ public function handle($request, Closure $next, $permission)
abort(403);
}

$permission = is_array($permission) ? $permission : explode('|', $permission);
$permissions = is_array($permission)
? $permission
: explode('|', $permission);

if (! Auth::user()->hasAnyPermission(...$permission)) {
abort(403);
foreach ($permissions as $permission) {
if (Auth::user()->can($permission)) {
return $next($request);
}
}

return $next($request);
abort(403);
}
}
1 change: 0 additions & 1 deletion src/Traits/HasPermissions.php
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Eloquent\Model;
use Maklad\Permission\Contracts\PermissionInterface as Permission;
use Maklad\Permission\Contracts\RoleInterface as Role;
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
use Maklad\Permission\PermissionRegistrar;

Expand Down
6 changes: 1 addition & 5 deletions src/Traits/HasRoles.php
Expand Up @@ -60,11 +60,7 @@ public function scopeRole(Builder $query, $roles): Builder
}

$roles = $roles->map(function ($role) {
if ($role instanceof Role) {
return $role;
}

return app(Role::class)->findByName($role, $this->getDefaultGuardName());
return $this->getStoredRole($role);
});

return $query->whereIn('role_ids', $roles->pluck('_id'));
Expand Down
12 changes: 11 additions & 1 deletion tests/MiddlewareTest.php
Expand Up @@ -56,6 +56,11 @@ public function a_user_can_access_a_route_protected_by_this_role_middleware_if_h
$this->runMiddleware(
$this->roleMiddleware, 'testRole|testRole2'
), 200);

$this->assertEquals(
$this->runMiddleware(
$this->roleMiddleware, ['testRole2', 'testRole']
), 200);
}

/** @test */
Expand Down Expand Up @@ -124,7 +129,12 @@ public function a_user_can_access_a_route_protected_by_this_permission_middlewar

$this->assertEquals(
$this->runMiddleware(
$this->permissionMiddleware, 'edit-articles|edit-news'
$this->permissionMiddleware, 'edit-news|edit-articles'
), 200);

$this->assertEquals(
$this->runMiddleware(
$this->permissionMiddleware, ['edit-news', 'edit-articles']
), 200);
}

Expand Down

0 comments on commit be75d54

Please sign in to comment.