mirror of
https://github.com/flarum/core.git
synced 2025-05-06 15:35:38 +02:00
API: Add User::hasPermissionLike() and User::getPermissions()
This commit is contained in:
parent
4705600d47
commit
ece23de750
@ -77,7 +77,7 @@ class User extends Model
|
|||||||
/**
|
/**
|
||||||
* An array of permissions that this user has.
|
* An array of permissions that this user has.
|
||||||
*
|
*
|
||||||
* @var array|null
|
* @var string[]|null
|
||||||
*/
|
*/
|
||||||
protected $permissions = null;
|
protected $permissions = null;
|
||||||
|
|
||||||
@ -378,12 +378,38 @@ class User extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_null($this->permissions)) {
|
if (is_null($this->permissions)) {
|
||||||
$this->permissions = $this->permissions()->lists('permission')->all();
|
$this->permissions = $this->getPermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
return in_array($permission, $this->permissions);
|
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
|
* Get the notification types that should be alerted to this user, according
|
||||||
* to their preferences.
|
* to their preferences.
|
||||||
@ -595,6 +621,16 @@ class User extends Model
|
|||||||
return Permission::whereIn('group_id', $groupIds);
|
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.
|
* Define the relationship with the user's access tokens.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user