edusoho是一开源网络教学平台,对21.4.2的代码进行一下简单分析.
src\ApiBundle\Api\Annotation\Access.PHP
PHP类Access的作用:鉴权,设置角色权限
PHP类Access的定义情况:
PHP类Access的调用逻辑
成员函数canAccess的流程图:
canAccess的在整个项目中哪里出现了:
源代码如下:
namespace ApiBundle\Api\Annotation;
use Biz\Common\CommonException;
/**
* @Annotation
* @Target({"METHOD"})
*/
class Access
{
private $roles;
private $rolesArray = array('ROLE_USER');
public function __construct(array $data)
{
foreach ($data as $key => $value) {
$method = 'set'.str_replace('_', '', $key);
if (!method_exists($this, $method)) {
throw CommonException::NOTFOUND_METHOD();
}
$this->$method($value);
}
}
public function setRoles($roles)
{
$this->roles = $roles;
}
/**
* @param $currentUserRoles
*
* @return bool
*/
public function canAccess($currentUserRoles)
{
$roles = $this->rolesArray = explode(',', $this->roles);
if (empty($roles)) {
return true;
}
foreach ($roles as $role) {
if (in_array($role, $currentUserRoles)) {
return true;
}
}
return false;
}
}