1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Session\Store; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Microboard\Models\Role; |
9
|
|
|
|
10
|
|
|
class Factory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Store |
14
|
|
|
*/ |
15
|
|
|
private Store $session; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Factory constructor. |
19
|
|
|
* |
20
|
|
|
* @param Store $session |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Store $session) |
23
|
|
|
{ |
24
|
|
|
$this->session = $session; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* creates permissions of the given resource for the given role. |
29
|
|
|
* |
30
|
|
|
* @param Role $role |
31
|
|
|
* @param array $resources |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
public function createResourcesPermissionsFor(Role $role, array $resources) |
35
|
|
|
{ |
36
|
|
|
foreach ($resources as $model => $abilities) { |
37
|
|
|
$this->createPermissionsFor($role, $model, $abilities); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* creates resource's permissions for the given role. |
43
|
|
|
* |
44
|
|
|
* @param Role $role |
45
|
|
|
* @param string $model |
46
|
|
|
* @param array|null $abilities |
47
|
|
|
* @throws Exception |
48
|
|
|
*/ |
49
|
|
|
public function createPermissionsFor(Role $role, $model, $abilities = []) |
50
|
|
|
{ |
51
|
|
|
if ($model !== 'dashboard') { |
52
|
|
|
$model = Str::of($model)->snake()->plural(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (empty($abilities)) { |
56
|
|
|
$abilities = ['viewAny', 'view', 'create', 'update', 'delete']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (! method_exists($role, 'permissions')) { |
60
|
|
|
throw new Exception('Role not accepted or don\'t have permissions relationship'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($abilities as $ability) { |
64
|
|
|
$role->permissions()->firstOrCreate([ |
65
|
|
|
'name' => "{$model}-{$ability}", |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $title |
72
|
|
|
* @param string $body |
73
|
|
|
* @param string $level |
74
|
|
|
*/ |
75
|
|
|
public function message($title, $body = '', $level = 'info') |
76
|
|
|
{ |
77
|
|
|
$this->session->flash('flash_notifications.title', $title); |
78
|
|
|
$this->session->flash('flash_notifications.body', $body); |
79
|
|
|
$this->session->flash('flash_notifications.level', $level); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $title |
84
|
|
|
* @param string $body |
85
|
|
|
*/ |
86
|
|
|
public function success($title, $body = '') |
87
|
|
|
{ |
88
|
|
|
$this->message($title, $body, 'success'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $title |
93
|
|
|
* @param string $body |
94
|
|
|
*/ |
95
|
|
|
public function error($title, $body = '') |
96
|
|
|
{ |
97
|
|
|
$this->message($title, $body, 'danger'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $title |
102
|
|
|
* @param string $body |
103
|
|
|
*/ |
104
|
|
|
public function warning($title, $body = '') |
105
|
|
|
{ |
106
|
|
|
$this->message($title, $body, 'warning'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|