AuthRole.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace common\models\rbac;
  3. use Yii;
  4. use common\traits\Tree;
  5. use common\helpers\TreeHelper;
  6. /**
  7. * This is the model class for table "{{%rbac_auth_role}}".
  8. *
  9. * @property int $id 主键
  10. * @property int|null $merchant_id 商户id
  11. * @property int|null $store_id 店铺ID
  12. * @property string $title 标题
  13. * @property string $app_id 应用
  14. * @property int|null $pid 上级id
  15. * @property int|null $level 级别
  16. * @property int|null $sort 排序
  17. * @property int|null $operating_type 运营类型
  18. * @property double|null $annual_fee 年费
  19. * @property string $tree 树
  20. * @property int|null $is_default 是否默认角色
  21. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int|null $created_at 添加时间
  23. * @property int|null $updated_at 修改时间
  24. */
  25. class AuthRole extends \common\models\base\BaseModel
  26. {
  27. use Tree;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%rbac_auth_role}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['title'], 'required'],
  42. [['title'], 'isUniqueTitle'],
  43. [['title'], 'trim'],
  44. [['merchant_id', 'store_id', 'operating_type', 'pid', 'level', 'sort', 'is_default', 'status', 'created_at', 'updated_at'], 'integer'],
  45. [['title'], 'string', 'max' => 50],
  46. [['app_id'], 'string', 'max' => 20],
  47. [['tree'], 'string', 'max' => 300],
  48. [['annual_fee'], 'number', 'min' => 0],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => '主键',
  58. 'merchant_id' => '商户id',
  59. 'store_id' => '店铺ID',
  60. 'title' => '角色名称',
  61. 'app_id' => '应用',
  62. 'pid' => '父级',
  63. 'level' => '级别',
  64. 'sort' => '排序',
  65. 'annual_fee' => '年费',
  66. 'operating_type' => '运营类型',
  67. 'tree' => '树',
  68. 'is_default' => '是否默认角色',
  69. 'status' => '状态',
  70. 'created_at' => '添加时间',
  71. 'updated_at' => '修改时间',
  72. ];
  73. }
  74. /**
  75. * @param $attribute
  76. */
  77. public function isUniqueTitle($attribute)
  78. {
  79. $merchant_id = $this->merchant_id;
  80. !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
  81. $model = self::find()->where([
  82. 'merchant_id' => $merchant_id,
  83. 'title' => $this->title
  84. ])->one();
  85. if ($model && $model->id != $this->id) {
  86. $this->addError($attribute, '角色名称已存在');
  87. }
  88. }
  89. /**
  90. * @return \yii\db\ActiveQuery
  91. */
  92. public function getAuthItemChild()
  93. {
  94. return $this->hasMany(AuthItemChild::class, ['role_id' => 'id']);
  95. }
  96. /**
  97. * @return bool
  98. */
  99. public function beforeDelete()
  100. {
  101. $childIds = self::find()
  102. ->select(['id'])
  103. ->where(['like', 'tree', $this->tree . TreeHelper::prefixTreeKey($this->id) . '%', false])
  104. ->column();
  105. $childIds[] = $this->id;
  106. AuthItemChild::deleteAll(['in', 'role_id', $childIds]);
  107. AuthAssignment::deleteAll(['in', 'role_id', $childIds]);
  108. $this->autoDeleteTree();
  109. return parent::beforeDelete();
  110. }
  111. }
粤ICP备19079148号