InitController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/3/2
  6. * Time: 11:55
  7. */
  8. namespace console\controllers;
  9. use backend\models\AdminUser;
  10. use common\models\User;
  11. use ReflectionClass;
  12. use yii\console\Controller;
  13. use yii\web\HttpException;
  14. class InitController extends Controller{
  15. public function actionUser(){
  16. $model = User::find()->one();
  17. if(empty($model)){
  18. $model= new User();
  19. $model->username = 'zhixiaowulian';
  20. $model->setPassword('zhixiaowulian');
  21. $model->generateAuthKey();
  22. $model->created_at = time();
  23. $model->updated_at = time();
  24. if($model->validate() && $model->save()){
  25. }else{
  26. return new HttpException(402,'Initialize account failed ');
  27. }
  28. }else{
  29. throw new HttpException(402,'Initialize account failed ');
  30. }
  31. }
  32. public function actionAdmin(){
  33. $model = AdminUser::find()->one();
  34. if(empty($model)){
  35. $model = new AdminUser();
  36. $model->username = 'admin';
  37. $model->setPassword('admin');
  38. $model->generateAuthKey();
  39. $model->created_at = time();
  40. $model->updated_at = time();
  41. if($model->validate()){
  42. $model->save();
  43. }else{
  44. return new HttpException(402,'Initialize account failed ');
  45. }
  46. }else{
  47. throw new HttpException(402,'Initialize account failed ');
  48. }
  49. }
  50. /**
  51. * 初始化权限
  52. */
  53. public function actionAuth(){
  54. $list = [];
  55. $FileArray = $this->getControllers();
  56. foreach ($FileArray as $file) {
  57. if ($file != 'SiteController.php') { //过滤Site
  58. $method = $this->getMethodList('backend\\controllers\\' . strstr($file, '.', true));
  59. $list[] = $method;
  60. }
  61. }
  62. $authManager = \Yii::$app->authManager;
  63. foreach ($list as $data) {
  64. $name = $data['name'];
  65. $description = $data['comment'];
  66. foreach ($data['method'] as $method) {
  67. $per = $authManager->getPermission($name . '::' . $method['name']);
  68. if (!$per) {
  69. self::createPermission($data['name'] . '::' . $method['name'], $method['comment']);
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * 初始化角色与用户绑定
  76. * @param int $userid
  77. * @param string $rolename
  78. */
  79. public function actionRole($userid = 1,$rolename ='system'){
  80. $auth = \Yii::$app->authManager;
  81. $role = $auth->createRole($rolename);
  82. $auth->add($role);
  83. $permissions = $auth->getPermissions();
  84. foreach ($permissions as $permission){
  85. $auth->addChild($role,$permission);
  86. }
  87. $auth->assign($role,$userid);
  88. }
  89. /**
  90. * 控制器列表
  91. * @return array
  92. */
  93. private function getControllers(){
  94. $Dir = \Yii::getAlias('@backend').'/controllers/';
  95. $fileArray = [];
  96. if( is_dir($Dir) ) {
  97. if (false != ($Handle = opendir($Dir))) {
  98. while (false != ($File = readdir($Handle))) {
  99. if ($File != '.' && $File != '..' && strpos($File, '.')) {
  100. $fileArray[] = $File;
  101. }
  102. }
  103. closedir($Handle);
  104. }
  105. }
  106. return $fileArray;
  107. }
  108. /**
  109. * 获取类中可访问方法及注释
  110. * @param $classname
  111. * @return array
  112. */
  113. private function getMethodList($classname){
  114. $class = new ReflectionClass($classname);
  115. $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
  116. $classMap = [];
  117. $classMap['name'] = $class->getShortName();
  118. $classMap['comment'] = $this->getComment($class);
  119. $classMap['method'] = [];
  120. foreach ($methods as $method){
  121. if(strlen($method->name) > 7 && substr($method->name,0,6) == 'action'){
  122. $temp['name'] = $method->getName();
  123. $temp['comment'] = $this->getComment($method);
  124. $classMap['method'][] = $temp;
  125. }
  126. }
  127. return $classMap;
  128. }
  129. /**
  130. * 提取注释
  131. * @param $reflection
  132. * @return string
  133. */
  134. private function getComment($reflection){
  135. $comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($reflection->getDocComment(), '/'))), "\r", '');
  136. if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
  137. $comment = trim(substr($comment, 0, $matches[0][1]));
  138. }
  139. return $comment;
  140. }
  141. /**
  142. * 创建权限
  143. */
  144. static function createPermission($name, $description)
  145. {
  146. $auth = \Yii::$app->authManager;
  147. $createPost = $auth->createPermission($name);
  148. $createPost->description = $description;
  149. $auth->add($createPost);
  150. }
  151. }