1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use LaravelFlare\Flare\Permissions\Permissions; |
7
|
|
|
|
8
|
|
|
class AdminManager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Base Class. |
12
|
|
|
* |
13
|
|
|
* The Base Class for Model Admin's |
14
|
|
|
*/ |
15
|
|
|
const BASE_CLASS = 'LaravelFlare\Flare\Admin\Admin'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Admin Config Key. |
19
|
|
|
* |
20
|
|
|
* Key which defined where in the Flare Admin Config to |
21
|
|
|
* load the Admin classes from. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const ADMIN_KEY = 'admin'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* __construct. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the array used for the Admin Menu. |
36
|
|
|
* |
37
|
|
|
* This is temporary but it shouldn't be a breaking |
38
|
|
|
* change when it is altered or removed. |
39
|
|
|
* |
40
|
|
|
* @return |
41
|
|
|
*/ |
42
|
|
|
public function getAdminMenu() |
43
|
|
|
{ |
44
|
|
|
return $this->getAdminClasses(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets Admin classes based on the current users permissions |
49
|
|
|
* which have been set. If a Admin class has not had the |
50
|
|
|
* Permissions provided, it will be displayed by default. |
51
|
|
|
* |
52
|
|
|
* @return |
53
|
|
|
*/ |
54
|
|
|
public function getAdminClasses() |
55
|
|
|
{ |
56
|
|
|
$classCollection = []; |
57
|
|
|
|
58
|
|
|
if (!defined('static::ADMIN_KEY')) { |
59
|
|
|
return $classCollection; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$classCollection = $this->getSubAdminClasses(\Flare::config(static::ADMIN_KEY)); |
63
|
|
|
|
64
|
|
|
return $classCollection; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Takes an array of classes and returns the |
69
|
|
|
* classes which are available with the |
70
|
|
|
* current permissions/policy set. |
71
|
|
|
* |
72
|
|
|
* @param array $classes |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getSubAdminClasses(array $classes) |
77
|
|
|
{ |
78
|
|
|
$classCollection = []; |
79
|
|
|
|
80
|
|
|
foreach ($classes as $key => $class) { |
81
|
|
|
if ($this->usableClass($key)) { |
82
|
|
|
$classCollection[] = [$key => $this->getSubAdminClasses($class)]; |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->usableClass($class)) { |
87
|
|
|
$classCollection[] = $class; |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $classCollection; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns an instance of the Admin. |
97
|
|
|
* |
98
|
|
|
* @return Admin |
99
|
|
|
*/ |
100
|
|
|
public static function getAdminInstance() |
101
|
|
|
{ |
102
|
|
|
if (!$requested = Admin::getRequested()) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return new $requested(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Register Admin Routes. |
111
|
|
|
* |
112
|
|
|
* Loops through all of the Admin classes in the collection |
113
|
|
|
* and registers their Admin Routes. |
114
|
|
|
*/ |
115
|
|
|
public function registerRoutes(Router $router) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->registerSubRoutes($this->getAdminClasses()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Loops through an array of classes |
122
|
|
|
* and registers their Route recursively. |
123
|
|
|
* |
124
|
|
|
* @param array $classes |
125
|
|
|
*/ |
126
|
|
|
public function registerSubRoutes(array $classes) |
127
|
|
|
{ |
128
|
|
|
foreach ($classes as $key => $class) { |
129
|
|
|
if (is_array($class)) { |
130
|
|
|
if ($this->usableClass($key)) { |
131
|
|
|
$this->registerAdminRoutes($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->registerSubRoutes($class); |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
$this->registerAdminRoutes($class); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Registers an individual group of Admin routes. |
143
|
|
|
* |
144
|
|
|
* @param string $class |
145
|
|
|
*/ |
146
|
|
|
public function registerAdminRoutes($class) |
147
|
|
|
{ |
148
|
|
|
(new $class())->registerRoutes(\App::make(\Illuminate\Routing\Router::class)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Determines if a class is usable. |
153
|
|
|
* |
154
|
|
|
* @param string $class |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
private function usableClass($class) |
159
|
|
|
{ |
160
|
|
|
if (!is_scalar($class) || !class_exists($class)) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($class == static::BASE_CLASS) { |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Determines if a class is available for the current User. |
173
|
|
|
* |
174
|
|
|
* @param string $class |
175
|
|
|
* |
176
|
|
|
* @return boolean |
177
|
|
|
*/ |
178
|
|
|
private function availableClass($class) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
if (!Permissions::check($class)) { |
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return true; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.