From eb7a455cd7c9a25750294df3baae1f5eb96bdbf9 Mon Sep 17 00:00:00 2001 From: freek Date: Wed, 30 Sep 2015 20:19:46 +0200 Subject: [PATCH] fix the inconsistent naming of the -method --- CHANGELOG.md | 5 +++++ README.md | 2 +- src/Models/Permission.php | 4 +++- src/Models/Role.php | 4 +++- src/PermissionRegistrar.php | 2 +- src/Traits/HasRoles.php | 30 ++++++++++++++++++++++++++++- tests/GateTest.php | 2 +- tests/HasRolesTest.php | 38 +++++++++++++++++++++++++++++-------- 8 files changed, 73 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63438f429..154ae4f2c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All Notable changes to `laravel-permission` will be documented in this file +## 1.0.1 - 2015-09-30 + +### Fixed +- Fixed the inconsistent naming of the `hasPermission`-method. + ## 1.0.0 - 2015-09-16 ### Added diff --git a/README.md b/README.md index c522b6d0d..03971e5cc 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ $user->revokePermissionTo('edit articles'); You can test if a user has a permission: ```php -$user->hasPermission('edit articles'); +$user->hasPermissionTo('edit articles'); ``` Saved permissions will be registered with the `Illuminate\Auth\Access\Gate`-class. So you can diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 715d9f02f..0da8fc2dc 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -33,7 +33,9 @@ public static function findByName($name) { $permission = static::where('name', $name)->first(); - if (!$permission) throw new PermissionDoesNotExist(); + if (!$permission) { + throw new PermissionDoesNotExist(); + } return $permission; } diff --git a/src/Models/Role.php b/src/Models/Role.php index 88a1d0e27..213f60074 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -35,7 +35,9 @@ public static function findByName($name) { $role = static::where('name', $name)->first(); - if (!$role) throw new RoleDoesNotExist(); + if (!$role) { + throw new RoleDoesNotExist(); + } return $role; } diff --git a/src/PermissionRegistrar.php b/src/PermissionRegistrar.php index e63e02282..5b8df04da 100644 --- a/src/PermissionRegistrar.php +++ b/src/PermissionRegistrar.php @@ -46,7 +46,7 @@ public function registerPermissions() $this->getPermissions()->map(function ($permission) { $this->gate->define($permission->name, function ($user) use ($permission) { - return $user->hasPermission($permission); + return $user->hasPermissionTo($permission); }); }); diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index 8ecf301b4..f2e07ea26 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -81,7 +81,7 @@ public function hasRole($roles) * * @return bool */ - public function hasPermission($permission) + public function hasPermissionTo($permission) { if (is_string($permission)) { $permission = Permission::findByName($permission); @@ -90,11 +90,39 @@ public function hasPermission($permission) return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); } + /** + * @deprecated deprecated since version 1.0.1, use hasPermissionTo instead + * + * Determine if the user may perform the given permission. + * + * @param Permission $permission + * + * @return bool + */ + public function hasPermission($permission) + { + return $this->hasPermissionTo($permission); + } + + /** + * Determine if the user has, via roles, has the given permission. + * + * @param Permission $permission + * + * @return bool + */ protected function hasPermissionViaRole(Permission $permission) { return $this->hasRole($permission->roles); } + /** + * Determine if the user has has the given permission. + * + * @param Permission $permission + * + * @return bool + */ protected function hasDirectPermission(Permission $permission) { if (is_string($permission)) { diff --git a/tests/GateTest.php b/tests/GateTest.php index 697a428cd..4c2f8fbb7 100644 --- a/tests/GateTest.php +++ b/tests/GateTest.php @@ -16,7 +16,7 @@ public function it_can_determine_if_a_user_has_a_permission_when_using_roles() $this->testUser->assignRole($this->testRole); - $this->assertTrue($this->testUser->hasPermission($this->testPermission)); + $this->assertTrue($this->testUser->hasPermissionTo($this->testPermission)); $this->assertTrue($this->reloadPermissions()); diff --git a/tests/HasRolesTest.php b/tests/HasRolesTest.php index 1facc4cfe..2a6d8194c 100644 --- a/tests/HasRolesTest.php +++ b/tests/HasRolesTest.php @@ -63,7 +63,7 @@ public function it_can_determine_if_a_user_has_one_of_the_given_roles() */ public function it_can_determine_that_the_user_does_not_have_a_permission() { - $this->assertFalse($this->testUser->hasPermission('edit-articles')); + $this->assertFalse($this->testUser->hasPermissionTo('edit-articles')); } /** @@ -75,7 +75,7 @@ public function it_can_assign_a_permission_to_a_role() $this->testUser->assignRole('testRole'); - $this->assertTrue($this->testUser->hasPermission('edit-articles')); + $this->assertTrue($this->testUser->hasPermissionTo('edit-articles')); } /** @@ -87,11 +87,11 @@ public function it_can_revoke_a_permission_from_a_role() $this->testUser->assignRole('testRole'); - $this->assertTrue($this->testUser->hasPermission('edit-articles')); + $this->assertTrue($this->testUser->hasPermissionTo('edit-articles')); $this->testRole->revokePermissionTo('edit-articles'); - $this->assertFalse($this->testUser->hasPermission('edit-articles')); + $this->assertFalse($this->testUser->hasPermissionTo('edit-articles')); } /** @@ -103,7 +103,7 @@ public function it_can_assign_a_permission_to_a_role_using_objects() $this->testUser->assignRole($this->testRole); - $this->assertTrue($this->testUser->hasPermission($this->testPermission)); + $this->assertTrue($this->testUser->hasPermissionTo($this->testPermission)); } /** @@ -115,7 +115,7 @@ public function it_can_assign_a_permission_to_a_user() $this->refreshTestUser(); - $this->assertTrue($this->testUser->hasPermission($this->testPermission)); + $this->assertTrue($this->testUser->hasPermissionTo($this->testPermission)); } /** @@ -127,12 +127,34 @@ public function it_can_revoke_a_permission_from_a_user() $this->refreshTestUser(); - $this->assertTrue($this->testUser->hasPermission($this->testPermission)); + $this->assertTrue($this->testUser->hasPermissionTo($this->testPermission)); $this->testUser->revokePermissionTo($this->testPermission); $this->refreshTestUser(); - $this->assertFalse($this->testUser->hasPermission($this->testPermission)); + $this->assertFalse($this->testUser->hasPermissionTo($this->testPermission)); + } + + /** + * @test + * + * @deprecated + */ + public function it_can_check_permissions_with_the_deprecated_has_permission_method() + { + $this->assertSame( + $this->testUser->hasPermissionTo($this->testPermission), + $this->testUser->hasPermission($this->testPermission) + ); + + $this->testUser->givePermissionTo($this->testPermission); + + $this->refreshTestUser(); + + $this->assertSame( + $this->testUser->hasPermissionTo($this->testPermission), + $this->testUser->hasPermission($this->testPermission) + ); } }