mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 12:25:57 +08:00
API: Add User::hasPermissionLike() and User::getPermissions()
This commit is contained in:
parent
29b74f1263
commit
e14fe8bc03
@ -77,7 +77,7 @@ class User extends Model
|
||||
/**
|
||||
* An array of permissions that this user has.
|
||||
*
|
||||
* @var array|null
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected $permissions = null;
|
||||
|
||||
@ -378,12 +378,38 @@ class User extends Model
|
||||
}
|
||||
|
||||
if (is_null($this->permissions)) {
|
||||
$this->permissions = $this->permissions()->lists('permission')->all();
|
||||
$this->permissions = $this->getPermissions();
|
||||
}
|
||||
|
||||
return in_array($permission, $this->permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the user has a permission that is like the given string,
|
||||
* based on their groups.
|
||||
*
|
||||
* @param string $match
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasPermissionLike($match)
|
||||
{
|
||||
if ($this->isAdmin()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is_null($this->permissions)) {
|
||||
$this->permissions = $this->getPermissions();
|
||||
}
|
||||
|
||||
foreach ($this->permissions as $permission) {
|
||||
if (substr($permission, -strlen($match)) === $match) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification types that should be alerted to this user, according
|
||||
* to their preferences.
|
||||
@ -595,6 +621,16 @@ class User extends Model
|
||||
return Permission::whereIn('group_id', $groupIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of permissions that the user has.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getPermissions()
|
||||
{
|
||||
return $this->permissions()->lists('permission')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the user's access tokens.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user