Role.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\admin;
  10. //use fecshop\models\mysqldb\cms\StaticBlock;
  11. use Yii;
  12. use fecshop\services\Service;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Role extends Service
  18. {
  19. const ADMIN_ROLEIDS_RESOURCES = 'admin_roleids_resources';
  20. public $numPerPage = 20;
  21. public $productViewAllRoleKey = 'catalog_product_view_all';
  22. public $productEditAllRoleKey = 'catalog_product_edit_all';
  23. public $productSaveAllRoleKey = 'catalog_product_save_all';
  24. public $productRemoveAllRoleKey = 'catalog_product_remove_all';
  25. protected $_roleModelName = '\fecshop\models\mysqldb\admin\Role';
  26. protected $_roleModel;
  27. protected $_current_role_resources;
  28. /**
  29. * language attribute.
  30. */
  31. protected $_lang_attr = [
  32. ];
  33. public function init()
  34. {
  35. parent::init();
  36. list($this->_roleModelName, $this->_roleModel) = Yii::mapGet($this->_roleModelName);
  37. }
  38. public function getPrimaryKey()
  39. {
  40. return 'role_id';
  41. }
  42. public function getByPrimaryKey($primaryKey)
  43. {
  44. if ($primaryKey) {
  45. $one = $this->_roleModel->findOne($primaryKey);
  46. foreach ($this->_lang_attr as $attrName) {
  47. if (isset($one[$attrName])) {
  48. $one[$attrName] = unserialize($one[$attrName]);
  49. }
  50. }
  51. return $one;
  52. } else {
  53. return new $this->_roleModelName();
  54. }
  55. }
  56. /*
  57. * example filter:
  58. * [
  59. * 'numPerPage' => 20,
  60. * 'pageNum' => 1,
  61. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  62. 'where' => [
  63. ['>','price',1],
  64. ['<=','price',10]
  65. * ['sku' => 'uk10001'],
  66. * ],
  67. * 'asArray' => true,
  68. * ]
  69. */
  70. public function coll($filter = '')
  71. {
  72. $query = $this->_roleModel->find();
  73. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  74. $coll = $query->all();
  75. if (!empty($coll)) {
  76. foreach ($coll as $k => $one) {
  77. foreach ($this->_lang_attr as $attr) {
  78. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  79. }
  80. $coll[$k] = $one;
  81. }
  82. }
  83. //var_dump($one);
  84. return [
  85. 'coll' => $coll,
  86. 'count'=> $query->limit(null)->offset(null)->count(),
  87. ];
  88. }
  89. /**
  90. * @param $one|array
  91. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  92. */
  93. public function saveRole($one)
  94. {
  95. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  96. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  97. if (!($this->validateRoleName($one))) {
  98. Yii::$service->helper->errors->add('The role name exists, you must define a unique role_name');
  99. return null;
  100. }
  101. if ($primaryVal) {
  102. $model = $this->_roleModel->findOne($primaryVal);
  103. if (!$model) {
  104. Yii::$service->helper->errors->add('role {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  105. return null;
  106. }
  107. } else {
  108. $model = new $this->_roleModelName();
  109. $model->created_at = time();
  110. }
  111. $model->updated_at = time();
  112. foreach ($this->_lang_attr as $attrName) {
  113. if (is_array($one[$attrName]) && !empty($one[$attrName])) {
  114. $one[$attrName] = serialize($one[$attrName]);
  115. }
  116. }
  117. $primaryKey = $this->getPrimaryKey();
  118. $model = Yii::$service->helper->ar->save($model, $one);
  119. $primaryVal = $model[$primaryKey];
  120. return $model;
  121. }
  122. /**
  123. * @param array $one ,example
  124. * [
  125. * 'role_id' => xx,
  126. * 'role_name' => 'xxxx',
  127. * 'role_description' => 'xxxx',
  128. * 'resources' => [3, 5, 76, 876, 999],
  129. * @return boolean
  130. * save role info and resources
  131. * ]
  132. */
  133. public function saveRoleAndResources($one){
  134. $roleData = [];
  135. if (isset($one['role_id'])) {
  136. $roleData['role_id'] = $one['role_id'];
  137. }
  138. if (isset($one['role_name'])) {
  139. $roleData['role_name'] = $one['role_name'];
  140. } else {
  141. Yii::$service->helper->errors->add('role name can not empty');
  142. return false;
  143. }
  144. if (isset($one['role_description'])) {
  145. $roleData['role_description'] = $one['role_description'];
  146. }
  147. $primaryKey = $this->getPrimaryKey();
  148. $roleModel = $this->saveRole($roleData);
  149. if ($roleModel) {
  150. $roleId = $roleModel[$primaryKey];
  151. if ($roleId && isset($one['resources'])) {
  152. $resources = $one['resources'];
  153. if (is_array($resources) && !empty($resources)) {
  154. Yii::$service->admin->roleUrlKey->repeatSaveRoleUrlKey($roleId, $resources);
  155. return true;
  156. }
  157. }
  158. }
  159. Yii::$service->helper->errors->add('save role and resource fail');
  160. return false;
  161. }
  162. protected function validateRoleName($one)
  163. {
  164. $role_name = $one['role_name'];
  165. $id = $this->getPrimaryKey();
  166. $primaryVal = isset($one[$id]) ? $one[$id] : '';
  167. $where = [
  168. 'role_name' => $role_name,
  169. ];
  170. $query = $this->_roleModel->find()->asArray();
  171. $query->where($where);
  172. if ($primaryVal) {
  173. $query->andWhere(['<>', $id, $primaryVal]);
  174. }
  175. $one = $query->one();
  176. if (!empty($one)) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. public function remove($ids)
  182. {
  183. if (!$ids) {
  184. Yii::$service->helper->errors->add('remove id is empty');
  185. return false;
  186. }
  187. if (is_array($ids) && !empty($ids)) {
  188. foreach ($ids as $id) {
  189. $model = $this->_roleModel->findOne($id);
  190. $model->delete();
  191. // delete user role
  192. Yii::$service->admin->userRole->removeByRoleId($id);
  193. Yii::$service->admin->roleUrlKey->removeByRoleId($id);
  194. }
  195. } else {
  196. $id = $ids;
  197. $model = $this->_roleModel->findOne($id);
  198. $model->delete();
  199. // delete user role
  200. Yii::$service->admin->userRole->removeByRoleId($id);
  201. Yii::$service->admin->roleUrlKey->removeByRoleId($id);
  202. }
  203. return true;
  204. }
  205. /**
  206. * @return array
  207. * 得到当前用户的可用的resources数组
  208. */
  209. public function getCurrentRoleResources(){
  210. if (!$this->_current_role_resources) {
  211. if (Yii::$app->user->isGuest) {
  212. return [];
  213. }
  214. $user = Yii::$app->user->identity;
  215. $userId = $user->Id;
  216. // 通过userId得到这个用户所在的用户组
  217. $userRoles = Yii::$service->admin->userRole->coll([
  218. 'where' => [
  219. [
  220. 'user_id' => $userId,
  221. ]
  222. ],
  223. 'fetchAll' => true,
  224. ]);
  225. $role_ids = [];
  226. if (is_array($userRoles['coll']) && !empty($userRoles['coll'])) {
  227. foreach ($userRoles['coll'] as $one) {
  228. $role_ids[] = $one['role_id'];
  229. }
  230. }
  231. if (empty($role_ids)) {
  232. return [];
  233. }
  234. $this->_current_role_resources = $this->getRoleResourcesByRoleIds($role_ids);
  235. }
  236. return $this->_current_role_resources;
  237. }
  238. /**
  239. * @param array $role_ids
  240. * @return array , 包含url_key_id的数组
  241. * 通过$role_ids数组,获得相应的所有url_key_id数组
  242. */
  243. public function getRoleResourcesByRoleIds($role_ids){
  244. if (empty($role_ids)) {
  245. return [];
  246. }
  247. sort($role_ids);
  248. $role_ids_cache_str = self::ADMIN_ROLEIDS_RESOURCES . implode('-', $role_ids);
  249. $resources = Yii::$app->cache->get($role_ids_cache_str);
  250. if (!$resources) {
  251. // 通过role_ids 得到url_keys
  252. $roleUrlKeys = Yii::$service->admin->roleUrlKey->coll([
  253. 'where' => [
  254. ['in', 'role_id', $role_ids]
  255. ],
  256. 'fetchAll' => true,
  257. ]);
  258. $roleUrlKeyIds = [];
  259. if (is_array($roleUrlKeys['coll']) && !empty($roleUrlKeys['coll'])) {
  260. foreach ($roleUrlKeys['coll'] as $one) {
  261. if (!isset($roleUrlKeyIds[$one['url_key_id']])) {
  262. $roleUrlKeyIds[$one['url_key_id']] = $one['url_key_id'];
  263. }
  264. }
  265. }
  266. $urlKeys = Yii::$service->admin->urlKey->coll([
  267. 'where' => [
  268. ['in', 'id', $roleUrlKeyIds]
  269. ],
  270. 'fetchAll' => true,
  271. ]);
  272. $urlKeyIds = [];
  273. if (is_array($urlKeys['coll']) && !empty($urlKeys['coll'])) {
  274. foreach ($urlKeys['coll'] as $one) {
  275. if (!isset($urlKeyIds[$one['url_key']])) {
  276. $urlKeyIds[$one['url_key']] = $one['url_key'];
  277. }
  278. }
  279. }
  280. Yii::$app->cache->set($role_ids_cache_str, $urlKeyIds);
  281. return $urlKeyIds;
  282. }
  283. return $resources;
  284. }
  285. }