RBACPermissionForm.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-01-29 21:15
  7. */
  8. namespace backend\models\form;
  9. use Yii;
  10. use yii\rbac\Permission;
  11. class RBACPermissionForm extends \yii\base\Model
  12. {
  13. public $route;
  14. public $method;
  15. public $description;
  16. public $sort;
  17. public $group;
  18. public $category;
  19. public function rules(){
  20. return [
  21. [['route', 'method', 'description', 'group', 'category'], 'required'],
  22. [['sort'], 'number'],
  23. [['sort'], 'default', 'value'=>0],
  24. [
  25. ['route'],
  26. 'match',
  27. 'pattern' => '/^[\/].*/',
  28. 'message' => Yii::t('app', Yii::t('app', 'Must begin with "/" like "/module/controller/action" format')),
  29. 'on' => 'permission'
  30. ],
  31. ];
  32. }
  33. public function getName()
  34. {
  35. return $this->route . ":" . $this->method;
  36. }
  37. public function getData()
  38. {
  39. return json_encode([
  40. 'group' => $this->group,
  41. 'sort' => $this->sort,
  42. 'category' => $this->category,
  43. ]);
  44. }
  45. public function setAttributes($values, $safeOnly = true)
  46. {
  47. if( $values instanceof Permission){
  48. $temp = explode(":", $values->name);
  49. $this->route = $temp[0];
  50. $this->method = $temp[1];
  51. $this->description = $values->description;
  52. $data = json_decode($values->data, true);
  53. $this->sort = $data['sort'];
  54. $this->group = $data['group'];
  55. $this->category = $data['category'];
  56. }else{
  57. parent::setAttributes($values, $safeOnly);
  58. }
  59. }
  60. public function attributeLabels()
  61. {
  62. return [
  63. "route" => Yii::t('app', 'Route'),
  64. "method" => Yii::t('app', 'HTTP Method'),
  65. "description" => Yii::t('app', 'Description'),
  66. "group" => Yii::t('app', 'Group'),
  67. "category" => Yii::t('app', 'Category'),
  68. "sort" => Yii::t('app', 'Sort'),
  69. "name" => Yii::t('app', 'Role'),
  70. ];
  71. }
  72. }