107 lines
2.8 KiB
PHP
107 lines
2.8 KiB
PHP
<?php
|
||
|
||
namespace app\models\auth;
|
||
|
||
use Yii;
|
||
use app\models\Model;
|
||
|
||
|
||
/**
|
||
* This is the model class for table "{{%auth_role}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $creator_user_id 创建者用户ID
|
||
* @property string $name 名称
|
||
* @property string $remark 备注
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $is_delete 是否删除:0=否,1=是
|
||
* @property int $deleted_at 删除时间
|
||
*/
|
||
class Role extends \yii\db\ActiveRecord
|
||
{
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%auth_role}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['name', 'remark'], 'trim'],
|
||
[['cx_mch_id', 'creator_user_id', 'name', 'remark'], 'required'],
|
||
[['cx_mch_id', 'creator_user_id', 'created_at', 'updated_at', 'is_delete', 'deleted_at'], 'integer'],
|
||
[['name'], 'string', 'max' => 50],
|
||
[['remark'], 'string', 'max' => 255],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'creator_user_id' => '创建者用户ID',
|
||
'name' => '名称',
|
||
'remark' => '备注',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'is_delete' => '是否删除:0=否,1=是',
|
||
'deleted_at' => '删除时间',
|
||
];
|
||
}
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
$this->htmlTagFilter();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function htmlTagFilter()
|
||
{
|
||
$this->name = Model::htmlTagFilter($this->name);
|
||
$this->remark = Model::htmlTagFilter($this->remark);
|
||
}
|
||
|
||
|
||
public function getPermission()
|
||
{
|
||
$cx_mch_id = Model::getCxMchId();
|
||
return $this->hasOne(RolePermission::className(), ['role_id' => 'id'])->where(['is_delete' => 0, 'cx_mch_id' => $cx_mch_id]);
|
||
}
|
||
|
||
|
||
public static function deleteRolePermission($role_id, $cx_mch_id)
|
||
{
|
||
$model = RolePermission::findOne(['id' => $role_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0]);
|
||
if($model == null){
|
||
return Model::asReturnError("角色权限不存在");
|
||
}
|
||
$model->is_delete = 1;
|
||
if(!$model->save()){
|
||
return (new Model)->getModelError($model);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
}
|
||
|