Skip to content

Commit eb7a455

Browse files
committed
fix the inconsistent naming of the -method
1 parent a85e8f1 commit eb7a455

8 files changed

+73
-14
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All Notable changes to `laravel-permission` will be documented in this file
44

5+
## 1.0.1 - 2015-09-30
6+
7+
### Fixed
8+
- Fixed the inconsistent naming of the `hasPermission`-method.
9+
510
## 1.0.0 - 2015-09-16
611

712
### Added

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ $user->revokePermissionTo('edit articles');
9191

9292
You can test if a user has a permission:
9393
```php
94-
$user->hasPermission('edit articles');
94+
$user->hasPermissionTo('edit articles');
9595
```
9696

9797
Saved permissions will be registered with the `Illuminate\Auth\Access\Gate`-class. So you can

src/Models/Permission.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public static function findByName($name)
3333
{
3434
$permission = static::where('name', $name)->first();
3535

36-
if (!$permission) throw new PermissionDoesNotExist();
36+
if (!$permission) {
37+
throw new PermissionDoesNotExist();
38+
}
3739

3840
return $permission;
3941
}

src/Models/Role.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public static function findByName($name)
3535
{
3636
$role = static::where('name', $name)->first();
3737

38-
if (!$role) throw new RoleDoesNotExist();
38+
if (!$role) {
39+
throw new RoleDoesNotExist();
40+
}
3941

4042
return $role;
4143
}

src/PermissionRegistrar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function registerPermissions()
4646
$this->getPermissions()->map(function ($permission) {
4747

4848
$this->gate->define($permission->name, function ($user) use ($permission) {
49-
return $user->hasPermission($permission);
49+
return $user->hasPermissionTo($permission);
5050
});
5151

5252
});

src/Traits/HasRoles.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function hasRole($roles)
8181
*
8282
* @return bool
8383
*/
84-
public function hasPermission($permission)
84+
public function hasPermissionTo($permission)
8585
{
8686
if (is_string($permission)) {
8787
$permission = Permission::findByName($permission);
@@ -90,11 +90,39 @@ public function hasPermission($permission)
9090
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission);
9191
}
9292

93+
/**
94+
* @deprecated deprecated since version 1.0.1, use hasPermissionTo instead
95+
*
96+
* Determine if the user may perform the given permission.
97+
*
98+
* @param Permission $permission
99+
*
100+
* @return bool
101+
*/
102+
public function hasPermission($permission)
103+
{
104+
return $this->hasPermissionTo($permission);
105+
}
106+
107+
/**
108+
* Determine if the user has, via roles, has the given permission.
109+
*
110+
* @param Permission $permission
111+
*
112+
* @return bool
113+
*/
93114
protected function hasPermissionViaRole(Permission $permission)
94115
{
95116
return $this->hasRole($permission->roles);
96117
}
97118

119+
/**
120+
* Determine if the user has has the given permission.
121+
*
122+
* @param Permission $permission
123+
*
124+
* @return bool
125+
*/
98126
protected function hasDirectPermission(Permission $permission)
99127
{
100128
if (is_string($permission)) {

tests/GateTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function it_can_determine_if_a_user_has_a_permission_when_using_roles()
1616

1717
$this->testUser->assignRole($this->testRole);
1818

19-
$this->assertTrue($this->testUser->hasPermission($this->testPermission));
19+
$this->assertTrue($this->testUser->hasPermissionTo($this->testPermission));
2020

2121
$this->assertTrue($this->reloadPermissions());
2222

tests/HasRolesTest.php

+30-8
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function it_can_determine_if_a_user_has_one_of_the_given_roles()
6363
*/
6464
public function it_can_determine_that_the_user_does_not_have_a_permission()
6565
{
66-
$this->assertFalse($this->testUser->hasPermission('edit-articles'));
66+
$this->assertFalse($this->testUser->hasPermissionTo('edit-articles'));
6767
}
6868

6969
/**
@@ -75,7 +75,7 @@ public function it_can_assign_a_permission_to_a_role()
7575

7676
$this->testUser->assignRole('testRole');
7777

78-
$this->assertTrue($this->testUser->hasPermission('edit-articles'));
78+
$this->assertTrue($this->testUser->hasPermissionTo('edit-articles'));
7979
}
8080

8181
/**
@@ -87,11 +87,11 @@ public function it_can_revoke_a_permission_from_a_role()
8787

8888
$this->testUser->assignRole('testRole');
8989

90-
$this->assertTrue($this->testUser->hasPermission('edit-articles'));
90+
$this->assertTrue($this->testUser->hasPermissionTo('edit-articles'));
9191

9292
$this->testRole->revokePermissionTo('edit-articles');
9393

94-
$this->assertFalse($this->testUser->hasPermission('edit-articles'));
94+
$this->assertFalse($this->testUser->hasPermissionTo('edit-articles'));
9595
}
9696

9797
/**
@@ -103,7 +103,7 @@ public function it_can_assign_a_permission_to_a_role_using_objects()
103103

104104
$this->testUser->assignRole($this->testRole);
105105

106-
$this->assertTrue($this->testUser->hasPermission($this->testPermission));
106+
$this->assertTrue($this->testUser->hasPermissionTo($this->testPermission));
107107
}
108108

109109
/**
@@ -115,7 +115,7 @@ public function it_can_assign_a_permission_to_a_user()
115115

116116
$this->refreshTestUser();
117117

118-
$this->assertTrue($this->testUser->hasPermission($this->testPermission));
118+
$this->assertTrue($this->testUser->hasPermissionTo($this->testPermission));
119119
}
120120

121121
/**
@@ -127,12 +127,34 @@ public function it_can_revoke_a_permission_from_a_user()
127127

128128
$this->refreshTestUser();
129129

130-
$this->assertTrue($this->testUser->hasPermission($this->testPermission));
130+
$this->assertTrue($this->testUser->hasPermissionTo($this->testPermission));
131131

132132
$this->testUser->revokePermissionTo($this->testPermission);
133133

134134
$this->refreshTestUser();
135135

136-
$this->assertFalse($this->testUser->hasPermission($this->testPermission));
136+
$this->assertFalse($this->testUser->hasPermissionTo($this->testPermission));
137+
}
138+
139+
/**
140+
* @test
141+
*
142+
* @deprecated
143+
*/
144+
public function it_can_check_permissions_with_the_deprecated_has_permission_method()
145+
{
146+
$this->assertSame(
147+
$this->testUser->hasPermissionTo($this->testPermission),
148+
$this->testUser->hasPermission($this->testPermission)
149+
);
150+
151+
$this->testUser->givePermissionTo($this->testPermission);
152+
153+
$this->refreshTestUser();
154+
155+
$this->assertSame(
156+
$this->testUser->hasPermissionTo($this->testPermission),
157+
$this->testUser->hasPermission($this->testPermission)
158+
);
137159
}
138160
}

0 commit comments

Comments
 (0)