AuthController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. class AuthController extends AdminController
  3. {
  4. /**
  5. * 权限管理首页
  6. */
  7. public function actionIndex()
  8. {
  9. $this->render('index');
  10. }
  11. /**
  12. * 获取列表
  13. */
  14. public function actionGetItems(){
  15. $params = CommonFn::getPageParams();
  16. $sort = Yii::app()->request->getParam('sort', '');
  17. $order = Yii::app()->request->getParam('order', 'asc');
  18. $type = Yii::app()->request->getParam('type');
  19. $auth = Yii::app()->authManager;
  20. $all_items = $auth->getAuthItems($type);
  21. $item_ids = array();
  22. foreach ($all_items as $k => $v){
  23. $temps = array();
  24. if ($type > 0){
  25. $children = $auth->getItemChildren($k);
  26. foreach ($children as $m => $n){
  27. $temps[] = $m;
  28. }
  29. }
  30. $item_ids[$k] = array('_id' => $k, 'name' => $k, 'desc' => $v->description, 'children' => implode(',', $temps));
  31. }
  32. $total = count($item_ids);
  33. $more = array();
  34. foreach ($item_ids as $v){
  35. $more[$v['name']] = array('value' => $v['name'], 'text' => $v['name'], 'desc' => $v['desc']);
  36. }
  37. if ($sort == 'name'){
  38. if ($order == 'asc'){
  39. ksort($more);
  40. ksort($item_ids);
  41. } else {
  42. krsort($more);
  43. krsort($item_ids);
  44. }
  45. }
  46. $more = array_values($more);
  47. $item_ids = array_values($item_ids);
  48. $rows = array_slice($item_ids, $params['offset'], $params['limit']);
  49. echo CommonFn::composeDatagridData($rows, $total, $more);
  50. }
  51. /**
  52. * 新增条目
  53. */
  54. public function actionInsertItem(){
  55. $auth = Yii::app()->authManager;
  56. $name = Yii::app()->request->getParam('name');
  57. $desc = Yii::app()->request->getParam('desc');
  58. $children = Yii::app()->request->getParam('children');
  59. $type = Yii::app()->request->getParam('type');
  60. if ($type <= 0){
  61. CommonFn::requestAjax(false, '操作不用自定义');
  62. }
  63. $children = array_filter(explode(',', $children));
  64. if ($auth->getAuthItem($name)){
  65. CommonFn::requestAjax(false, '名称重复');
  66. }
  67. $auth->createAuthItem($name, $type, $description=$desc);
  68. foreach ($children as $v){
  69. if ($auth->getAuthItem($v)){
  70. $auth->addItemChild($name, $v);
  71. }
  72. }
  73. $auth->save();
  74. CommonFn::requestAjax();
  75. }
  76. /**
  77. * 更新条目
  78. */
  79. public function actionUpdateItem(){
  80. $auth = Yii::app()->authManager;
  81. $_id = Yii::app()->request->getParam('_id');
  82. $name = Yii::app()->request->getParam('name');
  83. $desc = Yii::app()->request->getParam('desc');
  84. $type = Yii::app()->request->getParam('type');
  85. $children = Yii::app()->request->getParam('children');
  86. $children = array_filter(explode(',', $children));
  87. if (!$auth->getAuthItem($_id)){
  88. CommonFn::requestAjax(false, '原条目不存在');
  89. }
  90. $item = $auth->getAuthItem($_id);
  91. $item->setDescription($desc);
  92. if ($type > 0){
  93. $temp = array_keys($auth->getItemChildren($_id));
  94. foreach ($temp as $v){
  95. if (!in_array($v, $children)){
  96. $auth->removeItemChild($_id, $v);
  97. }
  98. }
  99. foreach ($children as $v){
  100. if ($auth->getAuthItem($v) && !$auth->hasItemChild($_id, $v)){
  101. $auth->addItemChild($_id, $v);
  102. }
  103. }
  104. if ($name != $_id){
  105. if ($_id == $auth->super_admin){
  106. CommonFn::requestAjax(false, '不能修改超级管理员');
  107. }
  108. if ($auth->getAuthItem($name)){
  109. CommonFn::requestAjax(false, '名称已存在');
  110. }
  111. $item->setName($name);
  112. }
  113. }
  114. $auth->save();
  115. CommonFn::requestAjax();
  116. }
  117. /**
  118. * 删除条目
  119. */
  120. public function actionRemoveItem(){
  121. $auth = Yii::app()->authManager;
  122. $_id = Yii::app()->request->getParam('_id');
  123. if (!$auth->getAuthItem($_id)){
  124. CommonFn::requestAjax(false, '原条目不存在');
  125. }
  126. if ($_id == $auth->super_admin){
  127. CommonFn::requestAjax(false, '不能删除超级管理员');
  128. }
  129. $auth->removeAuthItem($_id);
  130. $auth->save();
  131. CommonFn::requestAjax();
  132. }
  133. /**
  134. * 更新全部的操作
  135. */
  136. public function actionScanOperation(){
  137. $auth = Yii::app()->authManager;
  138. $all_controllers = $this->_getControllers();
  139. $all_actions = array();
  140. $action_names = array();
  141. foreach ($all_controllers as $v){
  142. $data = $this->_getControllerInfo($v, true);
  143. foreach ($data['action'] as $m => $n){
  144. $all_actions[] = array('name' => $n, 'desc' => $m);
  145. $action_names[] = $n;
  146. }
  147. }
  148. $old_auth_items = $auth->getAuthItems(0);
  149. foreach ($old_auth_items as $k => $v){
  150. if (!in_array($k, $action_names)){
  151. $auth->removeAuthItem($k);
  152. }
  153. }
  154. foreach ($all_actions as $v){
  155. if (!$auth->getAuthItem($v['name'])){
  156. $auth->createAuthItem($v['name'], 0, $v['desc']);
  157. }
  158. }
  159. $auth->save();
  160. CommonFn::requestAjax();
  161. }
  162. /**
  163. * 获取全部的控制器
  164. */
  165. private function _getControllers() {
  166. $contPath = Yii::app()->getControllerPath();
  167. $controllers = $this->_scanDir($contPath);
  168. //Scan modules
  169. $modules = Yii::app()->getModules();
  170. $modControllers = array();
  171. foreach ($modules as $mod_id => $mod) {
  172. $moduleControllersPath = Yii::app()->getModule($mod_id)->controllerPath;
  173. $modControllers = $this->_scanDir($moduleControllersPath, $mod_id, "", $modControllers);
  174. }
  175. return array_merge($controllers, $modControllers);
  176. }
  177. /**
  178. * 扫描文件夹
  179. */
  180. private function _scanDir($contPath, $module="", $subdir="", $controllers = array()) {
  181. $handle = opendir($contPath);
  182. $del = "-";
  183. while (($file = readdir($handle)) !== false) {
  184. $filePath = $contPath . DIRECTORY_SEPARATOR . $file;
  185. if (is_file($filePath)) {
  186. if (preg_match("/^(.+)Controller.php$/", basename($file))) {
  187. if ($this->_extendsController($filePath)) {
  188. $controllers[] = (($module) ? $module . $del : "") .
  189. (($subdir) ? $subdir . "." : "") .
  190. str_replace(".php", "", $file);
  191. }
  192. }
  193. } else if (is_dir($filePath) && $file != "." && $file != "..") {
  194. $controllers = $this->_scanDir($filePath, $module, $file, $controllers);
  195. }
  196. }
  197. return $controllers;
  198. }
  199. /**
  200. * 是否基于验证控制器
  201. */
  202. private function _extendsController($controller) {
  203. $c = basename(str_replace(".php", "", $controller));
  204. if (!class_exists($c, false)) {
  205. include_once $controller;
  206. }
  207. $cont = new $c($c);
  208. if ($cont instanceof Controller) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. /**
  214. * 获取一个控制器的信息
  215. */
  216. private function _getControllerInfo($controller, $getAll = false, $delete = false) {
  217. $del = "-";
  218. $actions = array();
  219. $allowed = array();
  220. $auth = Yii::app()->authManager;
  221. //Check if it's a module controller
  222. if (substr_count($controller, $del)) {
  223. $c = explode($del, $controller);
  224. $controller = $c[1];
  225. $module = $c[0] . $del;
  226. $contPath = Yii::app()->getModule($c[0])->getControllerPath();
  227. $control = $contPath . DIRECTORY_SEPARATOR . str_replace(".", DIRECTORY_SEPARATOR, $controller) . ".php";
  228. } else {
  229. $module = "";
  230. $contPath = Yii::app()->getControllerPath();
  231. $control = $contPath . DIRECTORY_SEPARATOR . str_replace(".", DIRECTORY_SEPARATOR, $controller) . ".php";
  232. }
  233. $h = file($control);
  234. for ($i = 0; $i < count($h); $i++) {
  235. $line = trim($h[$i]);
  236. if (preg_match("/^(.+)function( +)action*/", $line)) {
  237. $posAct = strpos(trim($line), "action");
  238. $posPar = strpos(trim($line), "(");
  239. $action = trim(substr(trim($line),$posAct, $posPar-$posAct));
  240. $patterns[0] = '/\s*/m';
  241. $patterns[1] = '#\((.*)\)#';
  242. $patterns[2] = '/\{/m';
  243. $replacements[2] = '';
  244. $replacements[1] = '';
  245. $replacements[0] = '';
  246. $action = preg_replace($patterns, $replacements, trim($action));
  247. $itemId = $module . str_replace("Controller", "", $controller) .
  248. preg_replace("/action/", "", $action, 1);
  249. if ($action != "actions") {
  250. if ($getAll) {
  251. $actions[$module . $action] = $itemId;
  252. if (in_array($itemId, $this->allowedAccess())) {
  253. $allowed[] = $itemId;
  254. }
  255. } else {
  256. if (in_array($itemId, $this->allowedAccess())) {
  257. $allowed[] = $itemId;
  258. } else {
  259. if ($auth->getAuthItem($itemId) === null && !$delete) {
  260. if (!in_array($itemId, $this->allowedAccess())) {
  261. $actions[$module . $action] = $itemId;
  262. }
  263. } else if ($auth->getAuthItem($itemId) !== null && $delete) {
  264. if (!in_array($itemId, $this->allowedAccess())) {
  265. $actions[$module . $action] = $itemId;
  266. }
  267. }
  268. }
  269. }
  270. } else {
  271. //load controller
  272. if (!class_exists($controller, false)) {
  273. require($control);
  274. }
  275. $tmp = array();
  276. $controller_obj = new $controller($controller, $module);
  277. //Get actions
  278. $controller_actions = $controller_obj->actions();
  279. foreach ($controller_actions as $cAction => $value) {
  280. $itemId = $module . str_replace("Controller", "", $controller) . ucfirst($cAction);
  281. if ($getAll) {
  282. $actions[$cAction] = $itemId;
  283. if (in_array($itemId, $this->allowedAccess())) {
  284. $allowed[] = $itemId;
  285. }
  286. } else {
  287. if (in_array($itemId, $this->allowedAccess())) {
  288. $allowed[] = $itemId;
  289. } else {
  290. if ($auth->getAuthItem($itemId) === null && !$delete) {
  291. if (!in_array($itemId, $this->allowedAccess())) {
  292. $actions[$cAction] = $itemId;
  293. }
  294. } else if ($auth->getAuthItem($itemId) !== null && $delete) {
  295. if (!in_array($itemId, $this->allowedAccess())) {
  296. $actions[$cAction] = $itemId;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. return array('action' => $actions, 'allow'=> $allowed, 'delete' => $delete);
  306. }
  307. }