123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2017/3/2
- * Time: 11:55
- */
- namespace console\controllers;
- use backend\models\AdminUser;
- use common\models\User;
- use ReflectionClass;
- use yii\console\Controller;
- use yii\web\HttpException;
- class InitController extends Controller{
- public function actionUser(){
- $model = User::find()->one();
- if(empty($model)){
- $model= new User();
- $model->username = 'zhixiaowulian';
- $model->setPassword('zhixiaowulian');
- $model->generateAuthKey();
- $model->created_at = time();
- $model->updated_at = time();
- if($model->validate() && $model->save()){
- }else{
- return new HttpException(402,'Initialize account failed ');
- }
- }else{
- throw new HttpException(402,'Initialize account failed ');
- }
- }
- public function actionAdmin(){
- $model = AdminUser::find()->one();
- if(empty($model)){
- $model = new AdminUser();
- $model->username = 'admin';
- $model->setPassword('admin');
- $model->generateAuthKey();
- $model->created_at = time();
- $model->updated_at = time();
- if($model->validate()){
- $model->save();
- }else{
- return new HttpException(402,'Initialize account failed ');
- }
- }else{
- throw new HttpException(402,'Initialize account failed ');
- }
- }
- /**
- * 初始化权限
- */
- public function actionAuth(){
- $list = [];
- $FileArray = $this->getControllers();
- foreach ($FileArray as $file) {
- if ($file != 'SiteController.php') { //过滤Site
- $method = $this->getMethodList('backend\\controllers\\' . strstr($file, '.', true));
- $list[] = $method;
- }
- }
- $authManager = \Yii::$app->authManager;
- foreach ($list as $data) {
- $name = $data['name'];
- $description = $data['comment'];
- foreach ($data['method'] as $method) {
- $per = $authManager->getPermission($name . '::' . $method['name']);
- if (!$per) {
- self::createPermission($data['name'] . '::' . $method['name'], $method['comment']);
- }
- }
- }
- }
- /**
- * 初始化角色与用户绑定
- * @param int $userid
- * @param string $rolename
- */
- public function actionRole($userid = 1,$rolename ='system'){
- $auth = \Yii::$app->authManager;
- $role = $auth->createRole($rolename);
- $auth->add($role);
- $permissions = $auth->getPermissions();
- foreach ($permissions as $permission){
- $auth->addChild($role,$permission);
- }
- $auth->assign($role,$userid);
- }
- /**
- * 控制器列表
- * @return array
- */
- private function getControllers(){
- $Dir = \Yii::getAlias('@backend').'/controllers/';
- $fileArray = [];
- if( is_dir($Dir) ) {
- if (false != ($Handle = opendir($Dir))) {
- while (false != ($File = readdir($Handle))) {
- if ($File != '.' && $File != '..' && strpos($File, '.')) {
- $fileArray[] = $File;
- }
- }
- closedir($Handle);
- }
- }
- return $fileArray;
- }
- /**
- * 获取类中可访问方法及注释
- * @param $classname
- * @return array
- */
- private function getMethodList($classname){
- $class = new ReflectionClass($classname);
- $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
- $classMap = [];
- $classMap['name'] = $class->getShortName();
- $classMap['comment'] = $this->getComment($class);
- $classMap['method'] = [];
- foreach ($methods as $method){
- if(strlen($method->name) > 7 && substr($method->name,0,6) == 'action'){
- $temp['name'] = $method->getName();
- $temp['comment'] = $this->getComment($method);
- $classMap['method'][] = $temp;
- }
- }
- return $classMap;
- }
- /**
- * 提取注释
- * @param $reflection
- * @return string
- */
- private function getComment($reflection){
- $comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($reflection->getDocComment(), '/'))), "\r", '');
- if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
- $comment = trim(substr($comment, 0, $matches[0][1]));
- }
- return $comment;
- }
- /**
- * 创建权限
- */
- static function createPermission($name, $description)
- {
- $auth = \Yii::$app->authManager;
- $createPost = $auth->createPermission($name);
- $createPost->description = $description;
- $auth->add($createPost);
- }
- }
|