From b0c7ffd6a26ca13fc4b79499ed9730cca1d00040 Mon Sep 17 00:00:00 2001 From: Mostafa Maklad Date: Tue, 2 Jan 2018 17:09:49 +0300 Subject: [PATCH] try to check config --- src/Exceptions/InvalidPermissionModel.php | 11 +++++++++ src/Exceptions/InvalidRoleModel.php | 11 +++++++++ src/Helpers.php | 16 +++++++++++++ src/Models/Permission.php | 28 +++++++++++++++++------ 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 src/Exceptions/InvalidPermissionModel.php create mode 100644 src/Exceptions/InvalidRoleModel.php diff --git a/src/Exceptions/InvalidPermissionModel.php b/src/Exceptions/InvalidPermissionModel.php new file mode 100644 index 0000000..2cab594 --- /dev/null +++ b/src/Exceptions/InvalidPermissionModel.php @@ -0,0 +1,11 @@ +first()) { - $name = $attributes['name']; + $name = $attributes['name']; $guardName = $attributes['guard_name']; - $helpers = new Helpers(); + $helpers = new Helpers(); throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName)); } @@ -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); } /** @@ -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)); }