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); } }