12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 16-4-28
- * Time: 下午5:09
- */
- namespace backend\models;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Html;
- class PermissionForm extends Model
- {
- public $name;
- public $description;
- public function rules(){
- return [
- [['name'],'string','max'=>255],
- [['name','description'],'required'],
- ['description','filter','filter'=>function($value){
- return Html::encode($value);
- }],
- ];
- }
- //自动设置 created_at updated_at
- public function behaviors()
- {
- return [
- \yii\behaviors\TimestampBehavior::className(), //自动设置 created_at updated_at
- ];
- }
- public function attributeLabels(){
- return [
- 'name'=>'角色名称',
- 'description'=>'角色描述',
- ];
- }
- public function save(){
- if($this->validate()){
- $authManager = Yii::$app->authManager;
- $role = $authManager->createPermission($this->name);
- // $role->description = '创建了 ' . $this->name. '角色、部门、权限组';
- /* $role->description = $this->description; */
- $authManager->add($role);
- return true;
- }else{
- return false;
- }
- }
- public function update($name){
- if($this->validate()){
- $authManager = Yii::$app->authManager;
- $role = $authManager->getPermission($name);
- if(!$role) return false;
- $authManager->remove($role);
- $role = $authManager->createPermission($this->name);
- $role->description = $this->description;
- $authManager->add($role);
- return true;
- }
- return false;
- }
- }
|