1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uccello\Core\Support\Scopes; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Scope; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Uccello\Core\Models\Domain; |
10
|
|
|
use Uccello\Core\Models\Entity; |
11
|
|
|
|
12
|
|
|
class AssignedUser implements Scope |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Apply the scope to a given Eloquent query builder. |
16
|
|
|
* |
17
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
18
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function apply(Builder $builder, Model $model) |
22
|
|
|
{ |
23
|
|
|
$user = Auth::user(); |
24
|
|
|
|
25
|
|
|
// Records assigned to a group to which the user belongs |
26
|
|
|
if ($user && !$user->is_admin) { |
|
|
|
|
27
|
|
|
$builder->whereIn($model->getTable().'.assigned_user_id', $user->getAllowedGroupUuids()); |
|
|
|
|
28
|
|
|
|
29
|
|
|
// Records assigned to an user with roles subordonate to the roles of the user |
30
|
|
|
$builder->orWhereIn($model->getTable().'.assigned_user_id', function ($query) use ($user) { |
31
|
|
|
$entityTable = with(new Entity)->getTable(); |
32
|
|
|
$privilegesTable = env('UCCELLO_TABLE_PREFIX', 'uccello_').'privileges'; |
33
|
|
|
|
34
|
|
|
$domain = request()->has('domain') ? request('domain') : Domain::first(); |
35
|
|
|
$subordonateRolesIds = $user->subordonateRolesIdsOnDomain($domain); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$query->select($entityTable.'.id') |
38
|
|
|
->from($entityTable) |
39
|
|
|
->join($privilegesTable, function ($join) use($entityTable, $privilegesTable, $subordonateRolesIds) { |
40
|
|
|
$join->on("$privilegesTable.user_id", '=', $entityTable.'.record_id') |
41
|
|
|
->whereIn("$privilegesTable.role_id", $subordonateRolesIds); |
42
|
|
|
}) |
43
|
|
|
->where("$entityTable.module_id", ucmodule('user')->id ?? null); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
// Records created by the user |
47
|
|
|
if (!empty($model->module)) { |
48
|
|
|
$builder->orWhereIn($model->getTable().'.'.$model->getKeyName(), function ($query) use($model) { |
49
|
|
|
$query->select('record_id') |
50
|
|
|
->from(with(new Entity)->getTable()) |
51
|
|
|
->where('module_id', $model->module->id ?? null) |
|
|
|
|
52
|
|
|
->where('creator_id', auth()->id()); |
53
|
|
|
}); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|