94 lines
2.5 KiB
PHP
94 lines
2.5 KiB
PHP
<?php
|
|
namespace app\models;
|
|
|
|
use core\base\Model;
|
|
|
|
/**
|
|
* 员工工位权限模型
|
|
*/
|
|
class EmployeePermission extends Model
|
|
{
|
|
protected $table = 'employee_station_permission';
|
|
|
|
/**
|
|
* 获取某员工所有工位权限(返回 station_type => is_allowed 的键值对)
|
|
*/
|
|
public function getByEmployee($employeeId)
|
|
{
|
|
$rows = $this->where(['employee_id = :eid'], [':eid' => $employeeId])->fetchAll();
|
|
$permissions = [];
|
|
foreach ($rows as $row) {
|
|
$permissions[$row['station_type']] = (int)$row['is_allowed'];
|
|
}
|
|
return $permissions;
|
|
}
|
|
|
|
/**
|
|
* 检查员工是否有某个工位的访问权限
|
|
* 超级管理员和管理员始终返回 true
|
|
*/
|
|
public function canAccess($employeeId, $role, $stationType)
|
|
{
|
|
// 超级管理员和管理员拥有所有工位权限
|
|
if ($role === \core\base\Controller::ROLE_SUPER_ADMIN || $role === \core\base\Controller::ROLE_ADMIN) {
|
|
return true;
|
|
}
|
|
|
|
$row = $this->where([
|
|
'employee_id = :eid AND station_type = :stype'
|
|
], [
|
|
':eid' => $employeeId,
|
|
':stype' => $stationType
|
|
])->fetch();
|
|
|
|
// 如果没配置过权限,默认允许访问
|
|
if (!$row) {
|
|
return true;
|
|
}
|
|
|
|
return (int)$row['is_allowed'] === 1;
|
|
}
|
|
|
|
/**
|
|
* 获取某员工允许访问的工位类型列表
|
|
*/
|
|
public function getAllowedStations($employeeId)
|
|
{
|
|
$rows = $this->where([
|
|
'employee_id = :eid AND is_allowed = 1'
|
|
], [':eid' => $employeeId])->fetchAll();
|
|
|
|
return array_column($rows, 'station_type');
|
|
}
|
|
|
|
/**
|
|
* 批量设置员工工位权限
|
|
* @param int $employeeId 员工ID
|
|
* @param array $permissions 格式:['station_type' => 1/0, ...]
|
|
*/
|
|
public function savePermissions($employeeId, $permissions)
|
|
{
|
|
// 先删除旧权限
|
|
$this->where(['employee_id = :eid'], [':eid' => $employeeId])->deleteWhere();
|
|
|
|
// 批量插入新权限
|
|
foreach ($permissions as $stationType => $isAllowed) {
|
|
$this->add([
|
|
'employee_id' => $employeeId,
|
|
'station_type' => $stationType,
|
|
'is_allowed' => $isAllowed ? 1 : 0,
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除某员工所有权限记录
|
|
*/
|
|
public function deleteByEmployee($employeeId)
|
|
{
|
|
return $this->where(['employee_id = :eid'], [':eid' => $employeeId])->deleteWhere();
|
|
}
|
|
}
|