Skip to content

Commit a2c2ae4

Browse files
committed
update
1 parent 01a3bc3 commit a2c2ae4

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/Contract/UserContract.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public function assignRole(RoleContract $role);
4040
*/
4141
public function removeRole(RoleContract $role);
4242

43+
/**
44+
* 删除所有已绑定的角色.
45+
*
46+
* @return void
47+
*/
48+
public function removeAllRole();
49+
4350
/**
4451
* 按名称查找用户.
4552
*

src/Traits/User.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public function removeRole(RoleContract $role)
4646
$this->roles()->detach($role);
4747
}
4848

49+
/**
50+
* 删除所有已绑定的角色.
51+
*
52+
* @return void
53+
*/
54+
public function removeAllRole()
55+
{
56+
$this->roles()->detach(
57+
$this->roles()->column('id')
58+
);
59+
}
60+
4961
/**
5062
* 检查是否有此权限.
5163
*
@@ -55,16 +67,11 @@ public function removeRole(RoleContract $role)
5567
*/
5668
public function can($permission)
5769
{
58-
$permissions = [];
59-
foreach ($this->roles as $role) {
60-
$permissions = array_merge(
61-
$permissions,
62-
array_column($role->permissions->toArray(), 'name')
63-
);
70+
if ($this->isSuper()) {
71+
return true;
6472
}
6573

66-
$permissions = array_unique($permissions);
67-
74+
$permissions = $this->getAllPermissions()->column('name');
6875
return in_array($permission, $permissions);
6976
}
7077

@@ -99,10 +106,10 @@ public function getAllPermissions(): Collection
99106
{
100107
$permissions = [];
101108
foreach ($this->roles as $role) {
102-
$permissions = array_unique(array_merge($permissions, $role->permissions->column('id')));
109+
$permissions = array_unique(array_merge($permissions, $role->permissions->column('name')));
103110
}
104111

105-
$permissions = Permission::whereIn('id', implode(',', $permissions))->select();
112+
$permissions = Permission::whereIn('name', implode(',', $permissions))->select();
106113

107114
return $permissions;
108115
}

0 commit comments

Comments
 (0)