PermissionForm.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 16-4-28
  6. * Time: 下午5:09
  7. */
  8. namespace backend\models;
  9. use Yii;
  10. use yii\base\Model;
  11. use yii\helpers\Html;
  12. class PermissionForm extends Model
  13. {
  14. public $name;
  15. public $description;
  16. public function rules(){
  17. return [
  18. [['name'],'string','max'=>255],
  19. [['name','description'],'required'],
  20. ['description','filter','filter'=>function($value){
  21. return Html::encode($value);
  22. }],
  23. ];
  24. }
  25. //自动设置 created_at updated_at
  26. public function behaviors()
  27. {
  28. return [
  29. \yii\behaviors\TimestampBehavior::className(), //自动设置 created_at updated_at
  30. ];
  31. }
  32. public function attributeLabels(){
  33. return [
  34. 'name'=>'角色名称',
  35. 'description'=>'角色描述',
  36. ];
  37. }
  38. public function save(){
  39. if($this->validate()){
  40. $authManager = Yii::$app->authManager;
  41. $role = $authManager->createPermission($this->name);
  42. // $role->description = '创建了 ' . $this->name. '角色、部门、权限组';
  43. /* $role->description = $this->description; */
  44. $authManager->add($role);
  45. return true;
  46. }else{
  47. return false;
  48. }
  49. }
  50. public function update($name){
  51. if($this->validate()){
  52. $authManager = Yii::$app->authManager;
  53. $role = $authManager->getPermission($name);
  54. if(!$role) return false;
  55. $authManager->remove($role);
  56. $role = $authManager->createPermission($this->name);
  57. $role->description = $this->description;
  58. $authManager->add($role);
  59. return true;
  60. }
  61. return false;
  62. }
  63. }