RoleService.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Admin\Service;
  3. /**
  4. * RoleService
  5. */
  6. class RoleService extends CommonService {
  7. /**
  8. * 添加角色
  9. * @param array $role 角色信息
  10. * @return array
  11. */
  12. public function addRole($role) {
  13. $Role = $this->getD();
  14. if (false === ($role = $Role->create($role))) {
  15. return $this->errorResultReturn($Role->getError());
  16. }
  17. if (false === $Role->add($role)) {
  18. return $this->errorResultReturn('系统错误!');
  19. }
  20. return $this->resultReturn(true);
  21. }
  22. /**
  23. * 更新角色信息
  24. * @return
  25. */
  26. public function updateRole($role) {
  27. $Role = $this->getD();
  28. if (false === ($role = $Role->create($role))) {
  29. return $this->errorResultReturn($Role->getError());
  30. }
  31. if ($role['id'] == $role['pid']) {
  32. $role['pid'] = 0;
  33. }
  34. if (false === $Role->save($role)) {
  35. return $this->errorResultReturn('系统错误!');
  36. }
  37. return $this->resultReturn(true);
  38. }
  39. /**
  40. * 分配角色权限
  41. * @param int $roleId 角色id
  42. * @param array $access 权限访问数组
  43. * @return array
  44. */
  45. public function assignAccess($roleId, array $access) {
  46. $Access = M('Access');
  47. $Access->startTrans();
  48. $Access->where("role_id={$roleId}")->delete();
  49. if (0 === count($access)) {
  50. $Access->commit();
  51. return $this->resultReturn(true, '清楚数据成功!');
  52. }
  53. $newAccess = array();
  54. foreach ($access as $item) {
  55. $item = explode(':', $item);
  56. $newAccess[] = array('role_id' => $roleId, 'node_id' => $item[0]);
  57. }
  58. // 插入新权限
  59. if (false === $Access->addAll($newAccess)) {
  60. $Access->rollback();
  61. return $this->errorResultReturn('分配权限失败!');
  62. }
  63. $Access->commit();
  64. return $this->resultReturn(true);
  65. }
  66. /**
  67. * 得到带有层级的role数据
  68. * @return array
  69. */
  70. public function getRoles() {
  71. $category = new \Org\Util\Category($this->getModelName(),
  72. array('id', 'pid', 'name'));
  73. return $category->getList();
  74. }
  75. /**
  76. * 得到子角色的id
  77. * @param int $id 角色id
  78. * @return array
  79. */
  80. public function getSonRoleIds($id) {
  81. $sRoles = $this->getM()->field('id')->where("pid={$id}")->select();
  82. $sids = array();
  83. if (is_null($sRoles)) {
  84. return $sids;
  85. }
  86. foreach ($sRoles as $sRole) {
  87. $sids[] = $sRole['id'];
  88. $sids = array_merge($sids, $this->getSonRoleIds($sRole['id']));
  89. }
  90. return $sids;
  91. }
  92. /**
  93. * 是否存在角色
  94. * @param int $id 角色id
  95. * @return boolean
  96. */
  97. public function existRole($id) {
  98. return !is_null($this->getM()->getById($id));
  99. }
  100. protected function getModelName() {
  101. return 'Role';
  102. }
  103. }