cxfoot/models/common/auth/CommonRoleEditForm.php
2023-10-24 14:54:18 +08:00

120 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2021年9月13日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\auth;
use app\models\auth\Role;
use app\models\auth\RolePermission;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonRoleEditForm extends Model
{
public $name;
public $remark;
public $creator_user_id;
public $permissions;
public $model;
public $cx_mch_id;
public $allows;
public function rules()
{
return [
[['name', 'remark', 'permissions'], 'trim'],
[['name', 'remark', 'permissions'], 'string'],
[['creator_user_id', 'cx_mch_id'], 'integer'],
[['model', 'allows'], 'safe'],
[['model', 'name', 'remark', 'permissions', 'creator_user_id', 'allows'], 'required'],
];
}
public function attributeLabels(): array {
return [
'name' => '名称',
'remark' => '备注',
'permissions' => '权限',
];
}
public function save()
{
if(!$this->validate()){
return $this->getModelError();
}
$t = \Yii::$app->db->beginTransaction();
if($this->model->isNewRecord){
$this->model->cx_mch_id = $this->cx_mch_id;
//只能创建一个门店管理员角色
$exists = Role::find()
->where([
'is_delete' => 0,
'cx_mch_id' => $this->cx_mch_id,
'name' => $this->name
])->exists();
if($exists && $this->name == "财务"){
return $this->apiReturnError("财务角色已创建,无需再创建");
}
if($exists && $this->name == "运营"){
return $this->apiReturnError("运营角色已创建,无需再创建");
}
if($exists && $this->name == "BOSS账号"){
return $this->apiReturnError("BOSS账号角色已创建无需再创建");
}
if($exists && $this->name == "总部管理员"){
return $this->apiReturnError("总部管理员角色已创建,无需再创建");
}
}
$this->model->name = $this->name;
$this->model->creator_user_id = $this->creator_user_id;
$this->model->remark = $this->remark;
if(!$this->model->save()){
$t->rollBack();
return $this->getModelError($this->model);
}
$role_permission = RolePermission::findOne(['role_id' => $this->model->id, 'is_delete' => 0, 'cx_mch_id' => $this->cx_mch_id]);
if($role_permission == null){
$role_permission = new RolePermission();
$role_permission->cx_mch_id = $this->cx_mch_id;
$role_permission->role_id = $this->model->id;
}
$role_permission->permissions = $this->checkPermissions($this->permissions, $this->allows, true);
if(!$role_permission->save()){
$t->rollBack();
return $this->getModelError($role_permission);
}
$t->commit();
return $this->apiReturnSuccess("保存成功");
}
private function checkPermissions($permissions, $allows, $encode = false)
{
$permissions = is_array($permissions) ? $permissions : json_decode($permissions,true);
$_permissions = [];
foreach ($permissions as $permission){
$permission = trim($permission);
if(strlen($permission) == 0)
continue;
if(in_array($permission, $allows) || !$allows){
array_push($_permissions, $permission);
}
}
$_permissions = array_unique($_permissions);
return $encode ? json_encode($_permissions, JSON_UNESCAPED_UNICODE) : $_permissions;
}
}