Skip to content

Commit

Permalink
Merge b0c7ffd into 86e647e
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Jan 2, 2018
2 parents 86e647e + b0c7ffd commit 94cc5c9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidPermissionModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Maklad\Permission\Exceptions;

/**
* Class InvalidPermissionModel
* @package Maklad\Permission\Exceptions
*/
class InvalidPermissionModel extends MakladException
{
}
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidRoleModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Maklad\Permission\Exceptions;

/**
* Class InvalidRoleModel
* @package Maklad\Permission\Exceptions
*/
class InvalidRoleModel extends MakladException
{
}
16 changes: 16 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,20 @@ public function getRoleDoesNotExistMessage(string $name, string $guardName): str
{
return "There is no role named `{$name}` for guard `{$guardName}`.";
}

/**
* @return string
*/
public function getInvalidRoleModelMessage(): string
{
return 'Invalid Role model in the configuration file.';
}

/**
* @return string
*/
public function getInvalidPermissionModelMessage(): string
{
return 'Invalid Permission model in the configuration file.';
}
}
28 changes: 21 additions & 7 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Maklad\Permission\Contracts\PermissionInterface;
use Maklad\Permission\Contracts\RoleInterface;
use Maklad\Permission\Exceptions\InvalidPermissionModel;
use Maklad\Permission\Exceptions\InvalidRoleModel;
use Maklad\Permission\Exceptions\PermissionAlreadyExists;
use Maklad\Permission\Exceptions\PermissionDoesNotExist;
use Maklad\Permission\Helpers;
Expand Down Expand Up @@ -42,6 +45,7 @@ public function __construct(array $attributes = [])

/**
* Create new Permission
*
* @param array $attributes
*
* @return $this|\Illuminate\Database\Eloquent\Model
Expand All @@ -55,9 +59,9 @@ public static function create(array $attributes = [])
'guard_name',
$attributes['guard_name']
)->first()) {
$name = $attributes['name'];
$name = $attributes['name'];
$guardName = $attributes['guard_name'];
$helpers = new Helpers();
$helpers = new Helpers();
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName));
}

Expand All @@ -71,13 +75,23 @@ public static function create(array $attributes = [])
/**
* A permission can be applied to roles.
* @return BelongsToMany
* @throws \Maklad\Permission\Exceptions\InvalidPermissionModel
* @throws \Maklad\Permission\Exceptions\InvalidRoleModel
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(
\config('permission.models.role'),
\config('permission.collection_names.role_has_permissions')
);
$roleModel = \config('permission.models.role');
$permissionModel = \config('permission.models.permission');
$helpers = new Helpers();

if ($roleModel instanceof RoleInterface) {
throw new InvalidRoleModel($helpers->getInvalidRoleModelMessage());
}
if ($permissionModel instanceof PermissionInterface) {
throw new InvalidPermissionModel($helpers->getInvalidPermissionModelMessage());
}

return $this->belongsToMany($roleModel, $permissionModel);
}

/**
Expand All @@ -104,7 +118,7 @@ public static function findByName(string $name, $guardName = null): PermissionIn

$permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();

if (! $permission) {
if ( ! $permission) {
$helpers = new Helpers();
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName));
}
Expand Down

0 comments on commit 94cc5c9

Please sign in to comment.