|
@@ -176,22 +176,19 @@ class DataScopeBaseModel extends Model
|
|
|
|
|
|
|
|
public function scopeTopAndEmployeeClear($query, $user, $search)
|
|
public function scopeTopAndEmployeeClear($query, $user, $search)
|
|
|
{
|
|
{
|
|
|
- $top_depart_id = "top_depart_id";
|
|
|
|
|
-
|
|
|
|
|
$table = $query->getQuery()->from;
|
|
$table = $query->getQuery()->from;
|
|
|
- $alias = $table; // 默认为原表名
|
|
|
|
|
|
|
+ $alias = $table; // 默认别名就是全表名
|
|
|
|
|
|
|
|
- // 如果 $table 里包含 " as ",说明有别名,截取别名部分
|
|
|
|
|
|
|
+ // 如果包含 " as ",说明有别名,直接截取最后的别名部分即可
|
|
|
if (strpos($table, ' as ') !== false) {
|
|
if (strpos($table, ' as ') !== false) {
|
|
|
$segments = explode(' as ', $table);
|
|
$segments = explode(' as ', $table);
|
|
|
- $table = trim(end($segments));
|
|
|
|
|
-
|
|
|
|
|
- $top_depart_id = $table . '.top_depart_id';
|
|
|
|
|
|
|
+ $alias = trim(end($segments)); // 真正的别名:i
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $query->where($top_depart_id, $user['top_depart_id']);
|
|
|
|
|
|
|
+ // 统一使用别名进行公司隔离
|
|
|
|
|
+ $query->where($alias . '.top_depart_id', $user['top_depart_id']);
|
|
|
|
|
|
|
|
- // 如果是超级管理员,直接返回,拥有最高权限
|
|
|
|
|
|
|
+ // 如果是超级管理员,直接放行
|
|
|
if ($user['is_admin'] == Employee::IS_ADMIN_TWO) {
|
|
if ($user['is_admin'] == Employee::IS_ADMIN_TWO) {
|
|
|
return $query;
|
|
return $query;
|
|
|
}
|
|
}
|
|
@@ -204,13 +201,13 @@ class DataScopeBaseModel extends Model
|
|
|
$relationTable = defined($className . '::table_column') ? constant($className . '::table_column') : '';
|
|
$relationTable = defined($className . '::table_column') ? constant($className . '::table_column') : '';
|
|
|
$relationTableId = defined($className . '::table_id_column') ? constant($className . '::table_id_column') : '';
|
|
$relationTableId = defined($className . '::table_id_column') ? constant($className . '::table_id_column') : '';
|
|
|
|
|
|
|
|
- // 2. 新增:成员表配置
|
|
|
|
|
|
|
+ // 2. 成员表配置
|
|
|
$detailTable = defined($className . '::detail_table_column') ? constant($className . '::detail_table_column') : '';
|
|
$detailTable = defined($className . '::detail_table_column') ? constant($className . '::detail_table_column') : '';
|
|
|
|
|
|
|
|
- // 使用一个闭包将“负责人”或“成员”的权限求并集(OR 关系)
|
|
|
|
|
|
|
+ // 负责人 or 成员 权限并集
|
|
|
$query->where(function ($groupQuery) use ($relationTable, $relationTableId, $detailTable, $user, $alias) {
|
|
$query->where(function ($groupQuery) use ($relationTable, $relationTableId, $detailTable, $user, $alias) {
|
|
|
|
|
|
|
|
- // 逻辑 A:负责人层级判定(原逻辑:项目/节点/任务负责人及上级负责人穿透)
|
|
|
|
|
|
|
+ // 逻辑 A:负责人层级判定
|
|
|
$groupQuery->whereExists(function ($subQuery) use ($relationTable, $relationTableId, $user, $alias) {
|
|
$groupQuery->whereExists(function ($subQuery) use ($relationTable, $relationTableId, $user, $alias) {
|
|
|
$subQuery->from($relationTable)
|
|
$subQuery->from($relationTable)
|
|
|
->whereColumn($relationTable . ".{$relationTableId}", $alias . '.id')
|
|
->whereColumn($relationTable . ".{$relationTableId}", $alias . '.id')
|
|
@@ -219,14 +216,13 @@ class DataScopeBaseModel extends Model
|
|
|
->where($relationTable . '.top_depart_id', $user['top_depart_id']);
|
|
->where($relationTable . '.top_depart_id', $user['top_depart_id']);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // 逻辑 B:成员判定(新逻辑:仅当前层级成员可见,无向下穿透性)
|
|
|
|
|
- // 直接用 orWhereExists,只要定义了成员表就必然生效
|
|
|
|
|
|
|
+ // 逻辑 B:成员判定
|
|
|
if (!empty($detailTable)) {
|
|
if (!empty($detailTable)) {
|
|
|
$groupQuery->orWhereExists(function ($subQuery) use ($detailTable, $relationTableId, $user, $alias) {
|
|
$groupQuery->orWhereExists(function ($subQuery) use ($detailTable, $relationTableId, $user, $alias) {
|
|
|
$subQuery->from($detailTable)
|
|
$subQuery->from($detailTable)
|
|
|
- ->whereColumn($detailTable . ".{$relationTableId}", $alias . '.id') // 同样关联主表 ID
|
|
|
|
|
|
|
+ ->whereColumn($detailTable . ".{$relationTableId}", $alias . '.id')
|
|
|
->where($detailTable . '.data_id', $user['id'])
|
|
->where($detailTable . '.data_id', $user['id'])
|
|
|
- ->where($detailTable . '.type', 1) // 严格限制:只看“1人”,排除“2设备”
|
|
|
|
|
|
|
+ ->where($detailTable . '.type', 1)
|
|
|
->where($detailTable . '.del_time', 0)
|
|
->where($detailTable . '.del_time', 0)
|
|
|
->where($detailTable . '.top_depart_id', $user['top_depart_id']);
|
|
->where($detailTable . '.top_depart_id', $user['top_depart_id']);
|
|
|
});
|
|
});
|