Roles.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Qii\Library;
  3. /**
  4. * 权限认证
  5. * @author Jinhui Zhu <jinhui.zhu@live.cn> 2016-01-15 11:23
  6. * 用法:
  7. * $roles = array(
  8. * 'user' => array('add', 'update', 'remove', 'view')
  9. * );
  10. * $user = array(
  11. * 'user' => array('add')
  12. * );
  13. * $rolesCls = new \Qii\Library\Roles();
  14. * $rolesCls->setRoles($roles);
  15. * $rolesCls->setUserRoles($user);
  16. *
  17. * $bool = $rolesCls->verify('user.update');//user下update权限
  18. * $bool = $rolesCls->verify('user');//user下所有权限
  19. * $bool = $rolesCls->verify('user.*');//user下所有权限
  20. * var_dump($bool);
  21. * $array = $rolesCls->batchVeryfy(array('user.add', 'user.update', 'user.remove'));
  22. * print_r($array);
  23. */
  24. class Roles
  25. {
  26. /**
  27. * 系统权限表
  28. * @var array $roles
  29. */
  30. private $roles = array();
  31. /**
  32. * 用户权限表
  33. * @var array $roles
  34. */
  35. private $userRoles = array();
  36. /**
  37. * 初始化,可以将系统权限和用户权限都带上
  38. */
  39. public function __construct(array $roles = array(), array $userRoles = array())
  40. {
  41. $this->roles = $roles;
  42. $this->userRoles = $userRoles;
  43. }
  44. /**
  45. * 设置系统权限
  46. * @param array $roles
  47. */
  48. public function setRoles(array $roles)
  49. {
  50. $this->roles = $roles;
  51. }
  52. /**
  53. * 获取系统权限
  54. * @return array
  55. */
  56. public function getRoles()
  57. {
  58. return $this->roles;
  59. }
  60. /**
  61. * 设置用户权限
  62. * @param array $userRoles
  63. */
  64. public function setUserRoles(array $userRoles)
  65. {
  66. $this->userRoles = $userRoles;
  67. }
  68. /**
  69. * 获取用户权限
  70. */
  71. public function getUserRoles()
  72. {
  73. return $this->userRoles;
  74. }
  75. /**
  76. * 验证用户是否有权限执行相关操作
  77. * @param string $operation
  78. * @return bool
  79. */
  80. public function verify($operation = 'user.add')
  81. {
  82. list($controller, $action) = array_pad(explode('.', $operation), 2, '*');
  83. if ($controller == '*') return true;
  84. $this->roles[$controller] = isset($this->roles[$controller]) ? $this->roles[$controller] : array();
  85. //如果权限列表里边没有对应的权限就返回true
  86. if (!in_array($action, $this->roles[$controller])) {
  87. return true;
  88. }
  89. //如果操作类型为*也返回true
  90. if ($action == '*') {
  91. return true;
  92. }
  93. $this->userRoles[$controller] = isset($this->userRoles[$controller]) ? $this->userRoles[$controller] : array();
  94. //如果用户权限列表中找不到对应权限就返回false
  95. if (!in_array($action, $this->userRoles[$controller])) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * 批量验证用户权限
  102. * @param array $operations
  103. * @return array
  104. */
  105. public function batchVeryfy(array $operations = array())
  106. {
  107. $result = array();
  108. foreach ($operations AS $val) {
  109. $result[$val] = $this->verify($val);
  110. }
  111. return $result;
  112. }
  113. }