Skip to content

Commit

Permalink
fix the inconsistent naming of the -method
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Sep 30, 2015
1 parent a85e8f1 commit eb7a455
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
Expand Down
30 changes: 29 additions & 1 deletion src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/GateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
38 changes: 30 additions & 8 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand All @@ -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'));
}

/**
Expand All @@ -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'));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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)
);
}
}

0 comments on commit eb7a455

Please sign in to comment.