Skip to content

Commit

Permalink
Merge pull request #47 from mostafamaklad/v1.6
Browse files Browse the repository at this point in the history
V1.6.0
  • Loading branch information
mostafamaklad committed Feb 17, 2018
2 parents 0680024 + c5e5a63 commit f8d99ab
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## 1.6.0 - 2018-02-17

### Added
- Officially support `laravel 5.6`
- Improve Lumen support

## 1.5.3 - 2018-02-07

### Added
Expand Down
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[![Laravel 5.3.x][ico-laravel-5.3]][link-laravel-5.3]
[![Laravel 5.4.x][ico-laravel-5.4]][link-laravel-5.4]
[![Laravel 5.5.x][ico-laravel-5.5]][link-laravel-5.5]
[![Laravel 5.6.x][ico-laravel-5.6]][link-laravel-5.6]

This package allows you to manage user permissions and roles in a database.
It is inspired from [laravel-permission][link-laravel-permission]. Same code same every thing but it is compatible with [laravel-mongodb][link-laravel-mongodb]
Expand Down Expand Up @@ -167,13 +168,33 @@ You can install the package via Composer:
composer require mostafamaklad/laravel-permission-mongodb
```

Copy `vendor/mostafamaklad/laravel-permission-mongodb/config/permission.php` to `config/permission.php`.
Copy the required files:

In `bootstrap/app.php`, add the following code below other services providers:
```bash
cp vendor/mostafamaklad/laravel-permission-mongodb/config/permission.php config/permission.php
```

You will also need to create another configuration file at `config/auth.php`. Get it on the Laravel repository or just run the following command:

```bash
curl -Ls https://raw.githubusercontent.com/laravel/lumen-framework/5.5/config/auth.php -o config/auth.php
```

Then, in `bootstrap/app.php`, register the middlewares:

```php
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'permission' => Maklad\Permission\Middlewares\PermissionMiddleware::class,
'role' => Maklad\Permission\Middlewares\RoleMiddleware::class,
]);
```

As well as the configuration and the service provider:

```php
$app->register(Maklad\Permission\PermissionServiceProvider::class);
$app->configure('permission');
$app->register(Maklad\Permission\PermissionServiceProvider::class);
```

## Usage
Expand Down Expand Up @@ -723,6 +744,8 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-laravel-5.4]: https://img.shields.io/badge/Laravel-5.4.x-brightgreen.svg?style=flat-square
[link-laravel-5.5]: https://laravel.com/docs/5.5
[ico-laravel-5.5]: https://img.shields.io/badge/Laravel-5.5.x-brightgreen.svg?style=flat-square
[link-laravel-5.6]: https://laravel.com/docs/5.6
[ico-laravel-5.6]: https://img.shields.io/badge/Laravel-5.6.x-brightgreen.svg?style=flat-square

[link-travis]: https://travis-ci.org/mostafamaklad/laravel-permission-mongodb
[ico-travis]: https://img.shields.io/travis/mostafamaklad/laravel-permission-mongodb/master.svg?style=flat-square
Expand Down
4 changes: 2 additions & 2 deletions src/Middlewares/PermissionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class PermissionMiddleware
*/
public function handle($request, Closure $next, $permission)
{
if (auth()->guest()) {
if (app('auth')->guest()) {
$helpers = new Helpers();
throw new UserNotLoggedIn(403, $helpers->getUserNotLoggedINMessage());
}

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


if (! auth()->user()->hasAnyPermission($permissions)) {
if (! app('auth')->user()->hasAnyPermission($permissions)) {
$helpers = new Helpers();
throw new UserNotLoggedIn(403, $helpers->getUnauthorizedPermissionMessage(implode(', ', $permissions)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Middlewares/RoleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class RoleMiddleware
*/
public function handle($request, Closure $next, $role)
{
if (auth()->guest()) {
if (app('auth')->guest()) {
$helpers = new Helpers();
throw new UserNotLoggedIn(403, $helpers->getUserNotLoggedINMessage());
}

$roles = \is_array($role) ? $role : \explode('|', $role);

if (! auth()->user()->hasAnyRole($roles)) {
if (! app('auth')->user()->hasAnyRole($roles)) {
$helpers = new Helpers();
throw new UserNotLoggedIn(403, $helpers->getUnauthorizedRoleMessage(implode(', ', $roles)));
}
Expand Down
30 changes: 4 additions & 26 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
namespace Maklad\Permission\Models;

use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Maklad\Permission\Contracts\PermissionInterface;
use Maklad\Permission\Contracts\RoleInterface;
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
use Maklad\Permission\Exceptions\RoleAlreadyExists;
Expand Down Expand Up @@ -33,13 +31,13 @@ class Role extends Model implements RoleInterface
*/
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');

parent::__construct($attributes);

$this->helpers = new Helpers();

$this->setTable(\config('permission.collection_names.roles'));
$this->setTable(config('permission.collection_names.roles'));
}

/**
Expand Down Expand Up @@ -92,26 +90,6 @@ public static function findOrCreate(string $name, $guardName = null): RoleInterf
return $role;
}

/**
* A role may be given various permissions.
* @return BelongsToMany
*/
public function permissions(): BelongsToMany
{
return $this->belongsToMany(
\config('permission.models.permission'),
\config('permission.collection_names.role_has_permissions')
);
}

/**
* A role belongs to some users of the model associated with its guard.
*/
public function users(): BelongsToMany
{
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
}

/**
* Find a role by its name and guard name.
*
Expand All @@ -123,7 +101,7 @@ public function users(): BelongsToMany
*/
public static function findByName(string $name, $guardName = null): RoleInterface
{
$guardName = $guardName ?? \config('auth.defaults.guard');
$guardName = $guardName ?? config('auth.defaults.guard');

$role = static::where('name', $name)
->where('guard_name', $guardName)
Expand All @@ -149,7 +127,7 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
public function hasPermissionTo($permission): bool
{
if (\is_string($permission)) {
$permission = \app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
}

if (! $this->getGuardNames()->contains($permission->guard_name)) {
Expand Down
39 changes: 30 additions & 9 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Maklad\Permission\Contracts\PermissionInterface as Permission;
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
use Maklad\Permission\Helpers;
Expand All @@ -16,6 +17,26 @@
*/
trait HasPermissions
{
/**
* A role may be given various permissions.
* @return BelongsToMany
*/
public function permissions(): BelongsToMany
{
return $this->belongsToMany(
config('permission.models.permission'),
config('permission.collection_names.role_has_permissions')
);
}

/**
* A role belongs to some users of the model associated with its guard.
*/
public function users(): BelongsToMany
{
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
}

/**
* Grant the given permission(s) to a role.
*
Expand All @@ -26,7 +47,7 @@ trait HasPermissions
*/
public function givePermissionTo(...$permissions)
{
$permissions = \collect($permissions)
$permissions = collect($permissions)
->flatten()
->map(function ($permission) {
return $this->getStoredPermission($permission);
Expand Down Expand Up @@ -68,7 +89,7 @@ public function syncPermissions(...$permissions)
*/
public function revokePermissionTo(...$permissions)
{
\collect($permissions)
collect($permissions)
->flatten()
->map(function ($permission) {
$permission = $this->getStoredPermission($permission);
Expand Down Expand Up @@ -115,12 +136,12 @@ protected function ensureModelSharesGuard(Model $roleOrPermission)
protected function getGuardNames(): Collection
{
if ($this->guard_name) {
return \collect($this->guard_name);
return collect($this->guard_name);
}

return \collect(\config('auth.guards'))
return collect(config('auth.guards'))
->map(function ($guard) {
return \config("auth.providers.{$guard['provider']}.model");
return config("auth.providers.{$guard['provider']}.model");
})
->filter(function ($model) {
return \get_class($this) === $model;
Expand All @@ -130,7 +151,7 @@ protected function getGuardNames(): Collection

protected function getDefaultGuardName(): string
{
$default = \config('auth.defaults.guard');
$default = config('auth.defaults.guard');

return $this->getGuardNames()->first() ?: $default;
}
Expand All @@ -140,7 +161,7 @@ protected function getDefaultGuardName(): string
*/
public function forgetCachedPermissions()
{
\app(PermissionRegistrar::class)->forgetCachedPermissions();
app(PermissionRegistrar::class)->forgetCachedPermissions();
}

/**
Expand All @@ -153,11 +174,11 @@ public function forgetCachedPermissions()
private function convertToPermissionModels($permissions): Collection
{
if (\is_array($permissions)) {
$permissions = \collect($permissions);
$permissions = collect($permissions);
}

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

$permissions = $permissions->map(function ($permission) {
Expand Down

0 comments on commit f8d99ab

Please sign in to comment.