|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Uccello\Core\Support\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Uccello\Core\Models\Entity; |
|
6
|
|
|
use Uccello\Core\Models\Module; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
use Uccello\Core\Support\Scopes; |
|
9
|
|
|
use Illuminate\Support\Facades\Auth; |
|
10
|
|
|
use Illuminate\Support\Facades\Schema; |
|
11
|
|
|
use Uccello\Core\Models\Domain; |
|
12
|
|
|
|
|
13
|
|
|
trait UccelloModule |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The "booting" method of the model. |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static function boot() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::boot(); |
|
23
|
|
|
|
|
24
|
|
|
if (static::isFilteredByUser()) { |
|
25
|
|
|
static::addGlobalScope(new Scopes\AssignedUser); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected static function isFilteredByUser() |
|
30
|
|
|
{ |
|
31
|
|
|
$isFilteredByUser = false; |
|
32
|
|
|
|
|
33
|
|
|
$user = Auth::user(); |
|
34
|
|
|
|
|
35
|
|
|
if ($user && !$user->is_admin) { |
|
|
|
|
|
|
36
|
|
|
$module = static::getModuleFromClass(static::class); |
|
37
|
|
|
|
|
38
|
|
|
if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
|
39
|
|
|
$isFilteredByUser = true; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $isFilteredByUser; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function scopeInDomain($query, ?Domain $domain, $withDescendants=false) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
|
49
|
|
|
// Activate descendant view if the user is allowed |
|
50
|
|
|
if (Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
|
|
|
|
|
|
51
|
|
|
$domainsIds = $domain->findDescendants()->pluck('id'); |
|
52
|
|
|
$query = $query::whereIn('domain_id', $domainsIds); |
|
53
|
|
|
} else { |
|
54
|
|
|
$query = $query::where('domain_id', $domain->id); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $query; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getTableAttribute() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->table; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getModuleAttribute() |
|
67
|
|
|
{ |
|
68
|
|
|
return static::getModuleFromClass(get_class($this)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected static function getModuleFromClass($className) |
|
72
|
|
|
{ |
|
73
|
|
|
$modules = Cache::rememberForever('modules_by_model_class', function () { |
|
74
|
|
|
$modulesGroupedByModelClass = collect(); |
|
75
|
|
|
Module::all()->map(function ($item) use ($modulesGroupedByModelClass) { |
|
76
|
|
|
$modulesGroupedByModelClass[$item->model_class] = $item; |
|
77
|
|
|
return $modulesGroupedByModelClass; |
|
78
|
|
|
}); |
|
79
|
|
|
return $modulesGroupedByModelClass; |
|
80
|
|
|
}); |
|
81
|
|
|
return $modules[(string) $className] ?? null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getUuidAttribute() |
|
85
|
|
|
{ |
|
86
|
|
|
$uuid = null; |
|
87
|
|
|
|
|
88
|
|
|
$entity = Entity::where('module_id', $this->module->getKey()) |
|
89
|
|
|
->where('record_id', $this->getKey()) |
|
|
|
|
|
|
90
|
|
|
->first(); |
|
91
|
|
|
|
|
92
|
|
|
if ($entity) { |
|
93
|
|
|
$uuid = $entity->getKey(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $uuid; |
|
97
|
|
|
} |
|
98
|
|
|
} |