@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * |
25 | 25 | * @param array $relations |
26 | 26 | * @param string $sortBy |
27 | - * @param boolean $desc |
|
27 | + * @param integer $desc |
|
28 | 28 | * @param array $columns |
29 | 29 | * @return collection |
30 | 30 | */ |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | * @param integer $perPage |
43 | 43 | * @param array $relations |
44 | 44 | * @param string $sortBy |
45 | - * @param boolean $desc |
|
45 | + * @param integer $desc |
|
46 | 46 | * @param array $columns |
47 | 47 | * @return collection |
48 | 48 | */ |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * @param integer $perPage |
132 | 132 | * @param array $relations |
133 | 133 | * @param string $sortBy |
134 | - * @param boolean $desc |
|
134 | + * @param integer $desc |
|
135 | 135 | * @param array $columns |
136 | 136 | * @return collection |
137 | 137 | */ |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | * @param integer $perPage |
150 | 150 | * @param array $relations |
151 | 151 | * @param string $sortBy |
152 | - * @param boolean $desc |
|
152 | + * @param integer $desc |
|
153 | 153 | * @param array $columns |
154 | 154 | * @return collection |
155 | 155 | */ |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | * Update record in the storage based on the given |
413 | 413 | * condition. |
414 | 414 | * |
415 | - * @param var $value condition value |
|
415 | + * @param boolean $value condition value |
|
416 | 416 | * @param array $data |
417 | 417 | * @param string $attribute condition column name |
418 | 418 | * @return void |
@@ -489,7 +489,7 @@ discard block |
||
489 | 489 | * @param array $conditions array of conditions |
490 | 490 | * @param array $relations |
491 | 491 | * @param string $sortBy |
492 | - * @param boolean $desc |
|
492 | + * @param integer $desc |
|
493 | 493 | * @param array $columns |
494 | 494 | * @return collection |
495 | 495 | */ |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | * @param array $conditions array of conditions |
522 | 522 | * @param integer $perPage |
523 | 523 | * @param string $sortBy |
524 | - * @param boolean $desc |
|
524 | + * @param integer $desc |
|
525 | 525 | * @param array $columns |
526 | 526 | * @return collection |
527 | 527 | */ |
@@ -4,649 +4,649 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var model |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var model |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Create new AbstractRepository instance. |
|
16 | - */ |
|
17 | - public function __construct() |
|
18 | - { |
|
19 | - $this->model = \App::make($this->getModel()); |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * Fetch all records with relations from the storage. |
|
24 | - * |
|
25 | - * @param array $relations |
|
26 | - * @param string $sortBy |
|
27 | - * @param boolean $desc |
|
28 | - * @param array $columns |
|
29 | - * @return collection |
|
30 | - */ |
|
31 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
32 | - { |
|
33 | - $sort = $desc ? 'desc' : 'asc'; |
|
34 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Fetch all records with relations from storage in pages |
|
39 | - * that matche the given query. |
|
40 | - * |
|
41 | - * @param string $query |
|
42 | - * @param integer $perPage |
|
43 | - * @param array $relations |
|
44 | - * @param string $sortBy |
|
45 | - * @param boolean $desc |
|
46 | - * @param array $columns |
|
47 | - * @return collection |
|
48 | - */ |
|
49 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
50 | - { |
|
51 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
52 | - $conditionColumns = $this->model->searchable; |
|
53 | - $sort = $desc ? 'desc' : 'asc'; |
|
54 | - |
|
55 | - /** |
|
56 | - * Construct the select conditions for the model. |
|
57 | - */ |
|
58 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
59 | - |
|
60 | - if (count($conditionColumns)) |
|
61 | - { |
|
62 | - /** |
|
63 | - * Use the first element in the model columns to construct the first condition. |
|
64 | - */ |
|
65 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Loop through the rest of the columns to construct or where conditions. |
|
70 | - */ |
|
71 | - foreach ($conditionColumns as $column) |
|
72 | - { |
|
73 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Loop through the model relations. |
|
78 | - */ |
|
79 | - foreach ($relations as $relation) |
|
80 | - { |
|
81 | - /** |
|
82 | - * Remove the sub relation if exists. |
|
83 | - */ |
|
84 | - $relation = explode('.', $relation)[0]; |
|
85 | - |
|
86 | - /** |
|
87 | - * Try to fetch the relation repository from the core. |
|
88 | - */ |
|
89 | - if (\Core::$relation()) |
|
90 | - { |
|
91 | - /** |
|
92 | - * Construct the relation condition. |
|
93 | - */ |
|
94 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
95 | - |
|
96 | - $subModel->where(function ($q) use ($query, $relation){ |
|
97 | - |
|
98 | - /** |
|
99 | - * Get columns of the relation. |
|
100 | - */ |
|
101 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
102 | - |
|
103 | - if (count($subConditionColumns)) |
|
104 | - { |
|
105 | - /** |
|
106 | - * Use the first element in the relation model columns to construct the first condition. |
|
107 | - */ |
|
108 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Loop through the rest of the columns to construct or where conditions. |
|
113 | - */ |
|
114 | - foreach ($subConditionColumns as $subConditionColumn) |
|
115 | - { |
|
116 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | - } |
|
118 | - }); |
|
119 | - |
|
120 | - }); |
|
121 | - } |
|
122 | - } |
|
123 | - }); |
|
14 | + /** |
|
15 | + * Create new AbstractRepository instance. |
|
16 | + */ |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + $this->model = \App::make($this->getModel()); |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * Fetch all records with relations from the storage. |
|
24 | + * |
|
25 | + * @param array $relations |
|
26 | + * @param string $sortBy |
|
27 | + * @param boolean $desc |
|
28 | + * @param array $columns |
|
29 | + * @return collection |
|
30 | + */ |
|
31 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
32 | + { |
|
33 | + $sort = $desc ? 'desc' : 'asc'; |
|
34 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Fetch all records with relations from storage in pages |
|
39 | + * that matche the given query. |
|
40 | + * |
|
41 | + * @param string $query |
|
42 | + * @param integer $perPage |
|
43 | + * @param array $relations |
|
44 | + * @param string $sortBy |
|
45 | + * @param boolean $desc |
|
46 | + * @param array $columns |
|
47 | + * @return collection |
|
48 | + */ |
|
49 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
50 | + { |
|
51 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
52 | + $conditionColumns = $this->model->searchable; |
|
53 | + $sort = $desc ? 'desc' : 'asc'; |
|
54 | + |
|
55 | + /** |
|
56 | + * Construct the select conditions for the model. |
|
57 | + */ |
|
58 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
59 | + |
|
60 | + if (count($conditionColumns)) |
|
61 | + { |
|
62 | + /** |
|
63 | + * Use the first element in the model columns to construct the first condition. |
|
64 | + */ |
|
65 | + $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Loop through the rest of the columns to construct or where conditions. |
|
70 | + */ |
|
71 | + foreach ($conditionColumns as $column) |
|
72 | + { |
|
73 | + $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Loop through the model relations. |
|
78 | + */ |
|
79 | + foreach ($relations as $relation) |
|
80 | + { |
|
81 | + /** |
|
82 | + * Remove the sub relation if exists. |
|
83 | + */ |
|
84 | + $relation = explode('.', $relation)[0]; |
|
85 | + |
|
86 | + /** |
|
87 | + * Try to fetch the relation repository from the core. |
|
88 | + */ |
|
89 | + if (\Core::$relation()) |
|
90 | + { |
|
91 | + /** |
|
92 | + * Construct the relation condition. |
|
93 | + */ |
|
94 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
95 | + |
|
96 | + $subModel->where(function ($q) use ($query, $relation){ |
|
97 | + |
|
98 | + /** |
|
99 | + * Get columns of the relation. |
|
100 | + */ |
|
101 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
102 | + |
|
103 | + if (count($subConditionColumns)) |
|
104 | + { |
|
105 | + /** |
|
106 | + * Use the first element in the relation model columns to construct the first condition. |
|
107 | + */ |
|
108 | + $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Loop through the rest of the columns to construct or where conditions. |
|
113 | + */ |
|
114 | + foreach ($subConditionColumns as $subConditionColumn) |
|
115 | + { |
|
116 | + $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | + } |
|
118 | + }); |
|
119 | + |
|
120 | + }); |
|
121 | + } |
|
122 | + } |
|
123 | + }); |
|
124 | 124 | |
125 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
126 | - } |
|
125 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Fetch all records with relations from storage in pages. |
|
130 | - * |
|
131 | - * @param integer $perPage |
|
132 | - * @param array $relations |
|
133 | - * @param string $sortBy |
|
134 | - * @param boolean $desc |
|
135 | - * @param array $columns |
|
136 | - * @return collection |
|
137 | - */ |
|
138 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
139 | - { |
|
140 | - $sort = $desc ? 'desc' : 'asc'; |
|
141 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Fetch all records with relations based on |
|
146 | - * the given condition from storage in pages. |
|
147 | - * |
|
148 | - * @param array $conditions array of conditions |
|
149 | - * @param integer $perPage |
|
150 | - * @param array $relations |
|
151 | - * @param string $sortBy |
|
152 | - * @param boolean $desc |
|
153 | - * @param array $columns |
|
154 | - * @return collection |
|
155 | - */ |
|
156 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
157 | - { |
|
158 | - unset($conditions['page']); |
|
159 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
160 | - $sort = $desc ? 'desc' : 'asc'; |
|
161 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
162 | - } |
|
128 | + /** |
|
129 | + * Fetch all records with relations from storage in pages. |
|
130 | + * |
|
131 | + * @param integer $perPage |
|
132 | + * @param array $relations |
|
133 | + * @param string $sortBy |
|
134 | + * @param boolean $desc |
|
135 | + * @param array $columns |
|
136 | + * @return collection |
|
137 | + */ |
|
138 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
139 | + { |
|
140 | + $sort = $desc ? 'desc' : 'asc'; |
|
141 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Fetch all records with relations based on |
|
146 | + * the given condition from storage in pages. |
|
147 | + * |
|
148 | + * @param array $conditions array of conditions |
|
149 | + * @param integer $perPage |
|
150 | + * @param array $relations |
|
151 | + * @param string $sortBy |
|
152 | + * @param boolean $desc |
|
153 | + * @param array $columns |
|
154 | + * @return collection |
|
155 | + */ |
|
156 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
157 | + { |
|
158 | + unset($conditions['page']); |
|
159 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
160 | + $sort = $desc ? 'desc' : 'asc'; |
|
161 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Save the given model to the storage. |
|
166 | - * |
|
167 | - * @param array $data |
|
168 | - * @param boolean $saveLog |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - public function save(array $data, $saveLog = true) |
|
172 | - { |
|
173 | - $model = false; |
|
174 | - $modelClass = $this->model; |
|
175 | - $relations = []; |
|
176 | - |
|
177 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
178 | - /** |
|
179 | - * If the id is present in the data then select the model for updating, |
|
180 | - * else create new model. |
|
181 | - * @var array |
|
182 | - */ |
|
183 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
184 | - if ( ! $model) |
|
185 | - { |
|
186 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Construct the model object with the given data, |
|
191 | - * and if there is a relation add it to relations array, |
|
192 | - * then save the model. |
|
193 | - */ |
|
194 | - foreach ($data as $key => $value) |
|
195 | - { |
|
196 | - /** |
|
197 | - * If the attribute is a relation. |
|
198 | - */ |
|
199 | - $relation = camel_case($key); |
|
200 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
201 | - { |
|
202 | - /** |
|
203 | - * Check if the relation is a collection. |
|
204 | - */ |
|
205 | - if (class_basename($model->$relation) == 'Collection') |
|
206 | - { |
|
207 | - /** |
|
208 | - * If the relation has no value then marke the relation data |
|
209 | - * related to the model to be deleted. |
|
210 | - */ |
|
211 | - if ( ! $value || ! count($value)) |
|
212 | - { |
|
213 | - $relations[$relation] = 'delete'; |
|
214 | - } |
|
215 | - } |
|
216 | - if (is_array($value)) |
|
217 | - { |
|
218 | - /** |
|
219 | - * Loop through the relation data. |
|
220 | - */ |
|
221 | - foreach ($value as $attr => $val) |
|
222 | - { |
|
223 | - /** |
|
224 | - * Get the relation model. |
|
225 | - */ |
|
226 | - $relationBaseModel = \Core::$relation()->model; |
|
227 | - |
|
228 | - /** |
|
229 | - * Check if the relation is a collection. |
|
230 | - */ |
|
231 | - if (class_basename($model->$relation) == 'Collection') |
|
232 | - { |
|
233 | - /** |
|
234 | - * If the id is present in the data then select the relation model for updating, |
|
235 | - * else create new model. |
|
236 | - */ |
|
237 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
238 | - |
|
239 | - /** |
|
240 | - * If model doesn't exists. |
|
241 | - */ |
|
242 | - if ( ! $relationModel) |
|
243 | - { |
|
244 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Loop through the relation attributes. |
|
249 | - */ |
|
250 | - foreach ($val as $attr => $val) |
|
251 | - { |
|
252 | - /** |
|
253 | - * Prevent the sub relations or attributes not in the fillable. |
|
254 | - */ |
|
255 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
256 | - { |
|
257 | - $relationModel->$attr = $val; |
|
258 | - } |
|
259 | - } |
|
260 | - |
|
261 | - $relations[$relation][] = $relationModel; |
|
262 | - } |
|
263 | - /** |
|
264 | - * If not collection. |
|
265 | - */ |
|
266 | - else |
|
267 | - { |
|
268 | - /** |
|
269 | - * Prevent the sub relations. |
|
270 | - */ |
|
271 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
272 | - { |
|
273 | - |
|
274 | - /** |
|
275 | - * If the id is present in the data then select the relation model for updating, |
|
276 | - * else create new model. |
|
277 | - */ |
|
278 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
279 | - |
|
280 | - /** |
|
281 | - * If model doesn't exists. |
|
282 | - */ |
|
283 | - if ( ! $relationModel) |
|
284 | - { |
|
285 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
286 | - } |
|
287 | - |
|
288 | - foreach ($value as $relationAttribute => $relationValue) |
|
289 | - { |
|
290 | - /** |
|
291 | - * Prevent attributes not in the fillable. |
|
292 | - */ |
|
293 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
294 | - { |
|
295 | - $relationModel->$relationAttribute = $relationValue; |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - $relations[$relation] = $relationModel; |
|
300 | - } |
|
301 | - } |
|
302 | - } |
|
303 | - } |
|
304 | - } |
|
305 | - /** |
|
306 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
307 | - */ |
|
308 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
309 | - { |
|
310 | - $model->$key = $value; |
|
311 | - } |
|
312 | - } |
|
313 | - /** |
|
314 | - * Save the model. |
|
315 | - */ |
|
316 | - $model->save(); |
|
317 | - |
|
318 | - /** |
|
319 | - * Loop through the relations array. |
|
320 | - */ |
|
321 | - foreach ($relations as $key => $value) |
|
322 | - { |
|
323 | - /** |
|
324 | - * If the relation is marked for delete then delete it. |
|
325 | - */ |
|
326 | - if ($value == 'delete' && $model->$key()->count()) |
|
327 | - { |
|
328 | - $model->$key()->delete(); |
|
329 | - } |
|
330 | - /** |
|
331 | - * If the relation is an array. |
|
332 | - */ |
|
333 | - else if (gettype($value) == 'array') |
|
334 | - { |
|
335 | - $ids = []; |
|
336 | - /** |
|
337 | - * Loop through the relations. |
|
338 | - */ |
|
339 | - foreach ($value as $val) |
|
340 | - { |
|
341 | - switch (class_basename($model->$key())) |
|
342 | - { |
|
343 | - /** |
|
344 | - * If the relation is one to many then update it's foreign key with |
|
345 | - * the model id and save it then add its id to ids array to delete all |
|
346 | - * relations who's id isn't in the ids array. |
|
347 | - */ |
|
348 | - case 'HasMany': |
|
349 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
350 | - $val->$foreignKeyName = $model->id; |
|
351 | - $val->save(); |
|
352 | - $ids[] = $val->id; |
|
353 | - break; |
|
354 | - |
|
355 | - /** |
|
356 | - * If the relation is many to many then add it's id to the ids array to |
|
357 | - * attache these ids to the model. |
|
358 | - */ |
|
359 | - case 'BelongsToMany': |
|
360 | - $val->save(); |
|
361 | - $ids[] = $val->id; |
|
362 | - break; |
|
363 | - } |
|
364 | - } |
|
365 | - switch (class_basename($model->$key())) |
|
366 | - { |
|
367 | - /** |
|
368 | - * If the relation is one to many then delete all |
|
369 | - * relations who's id isn't in the ids array. |
|
370 | - */ |
|
371 | - case 'HasMany': |
|
372 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
373 | - break; |
|
374 | - |
|
375 | - /** |
|
376 | - * If the relation is many to many then |
|
377 | - * detach the previous data and attach |
|
378 | - * the ids array to the model. |
|
379 | - */ |
|
380 | - case 'BelongsToMany': |
|
381 | - $model->$key()->detach(); |
|
382 | - $model->$key()->attach($ids); |
|
383 | - break; |
|
384 | - } |
|
385 | - } |
|
386 | - /** |
|
387 | - * If the relation isn't array. |
|
388 | - */ |
|
389 | - else |
|
390 | - { |
|
391 | - switch (class_basename($model->$key())) |
|
392 | - { |
|
393 | - /** |
|
394 | - * If the relation is one to many or one to one. |
|
395 | - */ |
|
396 | - case 'HasOne': |
|
397 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
398 | - $value->$foreignKeyName = $model->id; |
|
399 | - $value->save(); |
|
400 | - break; |
|
401 | - } |
|
402 | - } |
|
403 | - } |
|
404 | - |
|
405 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
406 | - }); |
|
164 | + /** |
|
165 | + * Save the given model to the storage. |
|
166 | + * |
|
167 | + * @param array $data |
|
168 | + * @param boolean $saveLog |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + public function save(array $data, $saveLog = true) |
|
172 | + { |
|
173 | + $model = false; |
|
174 | + $modelClass = $this->model; |
|
175 | + $relations = []; |
|
176 | + |
|
177 | + \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
178 | + /** |
|
179 | + * If the id is present in the data then select the model for updating, |
|
180 | + * else create new model. |
|
181 | + * @var array |
|
182 | + */ |
|
183 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
184 | + if ( ! $model) |
|
185 | + { |
|
186 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Construct the model object with the given data, |
|
191 | + * and if there is a relation add it to relations array, |
|
192 | + * then save the model. |
|
193 | + */ |
|
194 | + foreach ($data as $key => $value) |
|
195 | + { |
|
196 | + /** |
|
197 | + * If the attribute is a relation. |
|
198 | + */ |
|
199 | + $relation = camel_case($key); |
|
200 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
201 | + { |
|
202 | + /** |
|
203 | + * Check if the relation is a collection. |
|
204 | + */ |
|
205 | + if (class_basename($model->$relation) == 'Collection') |
|
206 | + { |
|
207 | + /** |
|
208 | + * If the relation has no value then marke the relation data |
|
209 | + * related to the model to be deleted. |
|
210 | + */ |
|
211 | + if ( ! $value || ! count($value)) |
|
212 | + { |
|
213 | + $relations[$relation] = 'delete'; |
|
214 | + } |
|
215 | + } |
|
216 | + if (is_array($value)) |
|
217 | + { |
|
218 | + /** |
|
219 | + * Loop through the relation data. |
|
220 | + */ |
|
221 | + foreach ($value as $attr => $val) |
|
222 | + { |
|
223 | + /** |
|
224 | + * Get the relation model. |
|
225 | + */ |
|
226 | + $relationBaseModel = \Core::$relation()->model; |
|
227 | + |
|
228 | + /** |
|
229 | + * Check if the relation is a collection. |
|
230 | + */ |
|
231 | + if (class_basename($model->$relation) == 'Collection') |
|
232 | + { |
|
233 | + /** |
|
234 | + * If the id is present in the data then select the relation model for updating, |
|
235 | + * else create new model. |
|
236 | + */ |
|
237 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
238 | + |
|
239 | + /** |
|
240 | + * If model doesn't exists. |
|
241 | + */ |
|
242 | + if ( ! $relationModel) |
|
243 | + { |
|
244 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Loop through the relation attributes. |
|
249 | + */ |
|
250 | + foreach ($val as $attr => $val) |
|
251 | + { |
|
252 | + /** |
|
253 | + * Prevent the sub relations or attributes not in the fillable. |
|
254 | + */ |
|
255 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
256 | + { |
|
257 | + $relationModel->$attr = $val; |
|
258 | + } |
|
259 | + } |
|
260 | + |
|
261 | + $relations[$relation][] = $relationModel; |
|
262 | + } |
|
263 | + /** |
|
264 | + * If not collection. |
|
265 | + */ |
|
266 | + else |
|
267 | + { |
|
268 | + /** |
|
269 | + * Prevent the sub relations. |
|
270 | + */ |
|
271 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
272 | + { |
|
273 | + |
|
274 | + /** |
|
275 | + * If the id is present in the data then select the relation model for updating, |
|
276 | + * else create new model. |
|
277 | + */ |
|
278 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
279 | + |
|
280 | + /** |
|
281 | + * If model doesn't exists. |
|
282 | + */ |
|
283 | + if ( ! $relationModel) |
|
284 | + { |
|
285 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
286 | + } |
|
287 | + |
|
288 | + foreach ($value as $relationAttribute => $relationValue) |
|
289 | + { |
|
290 | + /** |
|
291 | + * Prevent attributes not in the fillable. |
|
292 | + */ |
|
293 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
294 | + { |
|
295 | + $relationModel->$relationAttribute = $relationValue; |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + $relations[$relation] = $relationModel; |
|
300 | + } |
|
301 | + } |
|
302 | + } |
|
303 | + } |
|
304 | + } |
|
305 | + /** |
|
306 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
307 | + */ |
|
308 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
309 | + { |
|
310 | + $model->$key = $value; |
|
311 | + } |
|
312 | + } |
|
313 | + /** |
|
314 | + * Save the model. |
|
315 | + */ |
|
316 | + $model->save(); |
|
317 | + |
|
318 | + /** |
|
319 | + * Loop through the relations array. |
|
320 | + */ |
|
321 | + foreach ($relations as $key => $value) |
|
322 | + { |
|
323 | + /** |
|
324 | + * If the relation is marked for delete then delete it. |
|
325 | + */ |
|
326 | + if ($value == 'delete' && $model->$key()->count()) |
|
327 | + { |
|
328 | + $model->$key()->delete(); |
|
329 | + } |
|
330 | + /** |
|
331 | + * If the relation is an array. |
|
332 | + */ |
|
333 | + else if (gettype($value) == 'array') |
|
334 | + { |
|
335 | + $ids = []; |
|
336 | + /** |
|
337 | + * Loop through the relations. |
|
338 | + */ |
|
339 | + foreach ($value as $val) |
|
340 | + { |
|
341 | + switch (class_basename($model->$key())) |
|
342 | + { |
|
343 | + /** |
|
344 | + * If the relation is one to many then update it's foreign key with |
|
345 | + * the model id and save it then add its id to ids array to delete all |
|
346 | + * relations who's id isn't in the ids array. |
|
347 | + */ |
|
348 | + case 'HasMany': |
|
349 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
350 | + $val->$foreignKeyName = $model->id; |
|
351 | + $val->save(); |
|
352 | + $ids[] = $val->id; |
|
353 | + break; |
|
354 | + |
|
355 | + /** |
|
356 | + * If the relation is many to many then add it's id to the ids array to |
|
357 | + * attache these ids to the model. |
|
358 | + */ |
|
359 | + case 'BelongsToMany': |
|
360 | + $val->save(); |
|
361 | + $ids[] = $val->id; |
|
362 | + break; |
|
363 | + } |
|
364 | + } |
|
365 | + switch (class_basename($model->$key())) |
|
366 | + { |
|
367 | + /** |
|
368 | + * If the relation is one to many then delete all |
|
369 | + * relations who's id isn't in the ids array. |
|
370 | + */ |
|
371 | + case 'HasMany': |
|
372 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
373 | + break; |
|
374 | + |
|
375 | + /** |
|
376 | + * If the relation is many to many then |
|
377 | + * detach the previous data and attach |
|
378 | + * the ids array to the model. |
|
379 | + */ |
|
380 | + case 'BelongsToMany': |
|
381 | + $model->$key()->detach(); |
|
382 | + $model->$key()->attach($ids); |
|
383 | + break; |
|
384 | + } |
|
385 | + } |
|
386 | + /** |
|
387 | + * If the relation isn't array. |
|
388 | + */ |
|
389 | + else |
|
390 | + { |
|
391 | + switch (class_basename($model->$key())) |
|
392 | + { |
|
393 | + /** |
|
394 | + * If the relation is one to many or one to one. |
|
395 | + */ |
|
396 | + case 'HasOne': |
|
397 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
398 | + $value->$foreignKeyName = $model->id; |
|
399 | + $value->save(); |
|
400 | + break; |
|
401 | + } |
|
402 | + } |
|
403 | + } |
|
404 | + |
|
405 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
406 | + }); |
|
407 | 407 | |
408 | - return $model->id; |
|
409 | - } |
|
408 | + return $model->id; |
|
409 | + } |
|
410 | 410 | |
411 | - /** |
|
412 | - * Update record in the storage based on the given |
|
413 | - * condition. |
|
414 | - * |
|
415 | - * @param var $value condition value |
|
416 | - * @param array $data |
|
417 | - * @param string $attribute condition column name |
|
418 | - * @return void |
|
419 | - */ |
|
420 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
421 | - { |
|
422 | - if ($attribute == 'id') |
|
423 | - { |
|
424 | - $model = $this->model->lockForUpdate()->find($value); |
|
425 | - $model ? $model->update($data) : 0; |
|
426 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
427 | - } |
|
428 | - else |
|
429 | - { |
|
430 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
431 | - $model->update($data); |
|
432 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
433 | - }); |
|
434 | - } |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Delete record from the storage based on the given |
|
439 | - * condition. |
|
440 | - * |
|
441 | - * @param var $value condition value |
|
442 | - * @param string $attribute condition column name |
|
443 | - * @return void |
|
444 | - */ |
|
445 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
446 | - { |
|
447 | - if ($attribute == 'id') |
|
448 | - { |
|
449 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
450 | - $model = $this->model->lockForUpdate()->find($value); |
|
451 | - if ( ! $model) |
|
452 | - { |
|
453 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
454 | - } |
|
411 | + /** |
|
412 | + * Update record in the storage based on the given |
|
413 | + * condition. |
|
414 | + * |
|
415 | + * @param var $value condition value |
|
416 | + * @param array $data |
|
417 | + * @param string $attribute condition column name |
|
418 | + * @return void |
|
419 | + */ |
|
420 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
421 | + { |
|
422 | + if ($attribute == 'id') |
|
423 | + { |
|
424 | + $model = $this->model->lockForUpdate()->find($value); |
|
425 | + $model ? $model->update($data) : 0; |
|
426 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
427 | + } |
|
428 | + else |
|
429 | + { |
|
430 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
431 | + $model->update($data); |
|
432 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
433 | + }); |
|
434 | + } |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Delete record from the storage based on the given |
|
439 | + * condition. |
|
440 | + * |
|
441 | + * @param var $value condition value |
|
442 | + * @param string $attribute condition column name |
|
443 | + * @return void |
|
444 | + */ |
|
445 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
446 | + { |
|
447 | + if ($attribute == 'id') |
|
448 | + { |
|
449 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
450 | + $model = $this->model->lockForUpdate()->find($value); |
|
451 | + if ( ! $model) |
|
452 | + { |
|
453 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
454 | + } |
|
455 | 455 | |
456 | - $model->delete(); |
|
457 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
458 | - }); |
|
459 | - } |
|
460 | - else |
|
461 | - { |
|
462 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
463 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
464 | - $model->delete(); |
|
465 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
466 | - }); |
|
467 | - }); |
|
468 | - } |
|
469 | - } |
|
456 | + $model->delete(); |
|
457 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
458 | + }); |
|
459 | + } |
|
460 | + else |
|
461 | + { |
|
462 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
463 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
464 | + $model->delete(); |
|
465 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
466 | + }); |
|
467 | + }); |
|
468 | + } |
|
469 | + } |
|
470 | 470 | |
471 | - /** |
|
472 | - * Fetch records from the storage based on the given |
|
473 | - * id. |
|
474 | - * |
|
475 | - * @param integer $id |
|
476 | - * @param array $relations |
|
477 | - * @param array $columns |
|
478 | - * @return object |
|
479 | - */ |
|
480 | - public function find($id, $relations = [], $columns = array('*')) |
|
481 | - { |
|
482 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
483 | - } |
|
471 | + /** |
|
472 | + * Fetch records from the storage based on the given |
|
473 | + * id. |
|
474 | + * |
|
475 | + * @param integer $id |
|
476 | + * @param array $relations |
|
477 | + * @param array $columns |
|
478 | + * @return object |
|
479 | + */ |
|
480 | + public function find($id, $relations = [], $columns = array('*')) |
|
481 | + { |
|
482 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
483 | + } |
|
484 | 484 | |
485 | - /** |
|
486 | - * Fetch records from the storage based on the given |
|
487 | - * condition. |
|
488 | - * |
|
489 | - * @param array $conditions array of conditions |
|
490 | - * @param array $relations |
|
491 | - * @param string $sortBy |
|
492 | - * @param boolean $desc |
|
493 | - * @param array $columns |
|
494 | - * @return collection |
|
495 | - */ |
|
496 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
497 | - { |
|
498 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
499 | - $sort = $desc ? 'desc' : 'asc'; |
|
500 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * Fetch the first record from the storage based on the given |
|
505 | - * condition. |
|
506 | - * |
|
507 | - * @param array $conditions array of conditions |
|
508 | - * @param array $relations |
|
509 | - * @param array $columns |
|
510 | - * @return object |
|
511 | - */ |
|
512 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
513 | - { |
|
514 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
515 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
516 | - } |
|
517 | - |
|
518 | - /** |
|
519 | - * Return the deleted models in pages based on the given conditions. |
|
520 | - * |
|
521 | - * @param array $conditions array of conditions |
|
522 | - * @param integer $perPage |
|
523 | - * @param string $sortBy |
|
524 | - * @param boolean $desc |
|
525 | - * @param array $columns |
|
526 | - * @return collection |
|
527 | - */ |
|
528 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
529 | - { |
|
530 | - unset($conditions['page']); |
|
531 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
532 | - $sort = $desc ? 'desc' : 'asc'; |
|
533 | - $model = $this->model->onlyTrashed(); |
|
534 | - |
|
535 | - if (count($conditions['conditionValues'])) |
|
536 | - { |
|
537 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
538 | - } |
|
539 | - |
|
540 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Restore the deleted model. |
|
545 | - * |
|
546 | - * @param integer $id |
|
547 | - * @return void |
|
548 | - */ |
|
549 | - public function restore($id) |
|
550 | - { |
|
551 | - $model = $this->model->onlyTrashed()->find($id); |
|
552 | - |
|
553 | - if ( ! $model) |
|
554 | - { |
|
555 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
556 | - } |
|
557 | - |
|
558 | - $model->restore(); |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Build the conditions recursively for the retrieving methods. |
|
563 | - * @param array $conditions |
|
564 | - * @return array |
|
565 | - */ |
|
566 | - protected function constructConditions($conditions, $model) |
|
567 | - { |
|
568 | - $conditionString = ''; |
|
569 | - $conditionValues = []; |
|
570 | - foreach ($conditions as $key => $value) |
|
571 | - { |
|
572 | - if ($key == 'and') |
|
573 | - { |
|
574 | - $conditions = $this->constructConditions($value, $model); |
|
575 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
576 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
577 | - } |
|
578 | - else if ($key == 'or') |
|
579 | - { |
|
580 | - $conditions = $this->constructConditions($value, $model); |
|
581 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
582 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
583 | - } |
|
584 | - else |
|
585 | - { |
|
586 | - if (is_array($value)) |
|
587 | - { |
|
588 | - $operator = $value['op']; |
|
589 | - if (strtolower($operator) == 'between') |
|
590 | - { |
|
591 | - $value1 = $value['val1']; |
|
592 | - $value2 = $value['val2']; |
|
593 | - } |
|
594 | - else |
|
595 | - { |
|
596 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
597 | - } |
|
598 | - } |
|
599 | - else |
|
600 | - { |
|
601 | - $operator = '='; |
|
602 | - } |
|
485 | + /** |
|
486 | + * Fetch records from the storage based on the given |
|
487 | + * condition. |
|
488 | + * |
|
489 | + * @param array $conditions array of conditions |
|
490 | + * @param array $relations |
|
491 | + * @param string $sortBy |
|
492 | + * @param boolean $desc |
|
493 | + * @param array $columns |
|
494 | + * @return collection |
|
495 | + */ |
|
496 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
497 | + { |
|
498 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
499 | + $sort = $desc ? 'desc' : 'asc'; |
|
500 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * Fetch the first record from the storage based on the given |
|
505 | + * condition. |
|
506 | + * |
|
507 | + * @param array $conditions array of conditions |
|
508 | + * @param array $relations |
|
509 | + * @param array $columns |
|
510 | + * @return object |
|
511 | + */ |
|
512 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
513 | + { |
|
514 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
515 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
516 | + } |
|
517 | + |
|
518 | + /** |
|
519 | + * Return the deleted models in pages based on the given conditions. |
|
520 | + * |
|
521 | + * @param array $conditions array of conditions |
|
522 | + * @param integer $perPage |
|
523 | + * @param string $sortBy |
|
524 | + * @param boolean $desc |
|
525 | + * @param array $columns |
|
526 | + * @return collection |
|
527 | + */ |
|
528 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
529 | + { |
|
530 | + unset($conditions['page']); |
|
531 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
532 | + $sort = $desc ? 'desc' : 'asc'; |
|
533 | + $model = $this->model->onlyTrashed(); |
|
534 | + |
|
535 | + if (count($conditions['conditionValues'])) |
|
536 | + { |
|
537 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
538 | + } |
|
539 | + |
|
540 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Restore the deleted model. |
|
545 | + * |
|
546 | + * @param integer $id |
|
547 | + * @return void |
|
548 | + */ |
|
549 | + public function restore($id) |
|
550 | + { |
|
551 | + $model = $this->model->onlyTrashed()->find($id); |
|
552 | + |
|
553 | + if ( ! $model) |
|
554 | + { |
|
555 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
556 | + } |
|
557 | + |
|
558 | + $model->restore(); |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Build the conditions recursively for the retrieving methods. |
|
563 | + * @param array $conditions |
|
564 | + * @return array |
|
565 | + */ |
|
566 | + protected function constructConditions($conditions, $model) |
|
567 | + { |
|
568 | + $conditionString = ''; |
|
569 | + $conditionValues = []; |
|
570 | + foreach ($conditions as $key => $value) |
|
571 | + { |
|
572 | + if ($key == 'and') |
|
573 | + { |
|
574 | + $conditions = $this->constructConditions($value, $model); |
|
575 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
576 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
577 | + } |
|
578 | + else if ($key == 'or') |
|
579 | + { |
|
580 | + $conditions = $this->constructConditions($value, $model); |
|
581 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
582 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
583 | + } |
|
584 | + else |
|
585 | + { |
|
586 | + if (is_array($value)) |
|
587 | + { |
|
588 | + $operator = $value['op']; |
|
589 | + if (strtolower($operator) == 'between') |
|
590 | + { |
|
591 | + $value1 = $value['val1']; |
|
592 | + $value2 = $value['val2']; |
|
593 | + } |
|
594 | + else |
|
595 | + { |
|
596 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
597 | + } |
|
598 | + } |
|
599 | + else |
|
600 | + { |
|
601 | + $operator = '='; |
|
602 | + } |
|
603 | 603 | |
604 | - if (strtolower($operator) == 'between') |
|
605 | - { |
|
606 | - $conditionString .= $key . ' >= ? and '; |
|
607 | - $conditionValues[] = $value1; |
|
608 | - |
|
609 | - $conditionString .= $key . ' <= ? {op} '; |
|
610 | - $conditionValues[] = $value2; |
|
611 | - } |
|
612 | - elseif (strtolower($operator) == 'in') |
|
613 | - { |
|
614 | - $conditionValues = array_merge($conditionValues, $value); |
|
615 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
616 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
617 | - } |
|
618 | - elseif (strtolower($operator) == 'null') |
|
619 | - { |
|
620 | - $conditionString .= $key . ' is null {op} '; |
|
621 | - } |
|
622 | - elseif (strtolower($operator) == 'not null') |
|
623 | - { |
|
624 | - $conditionString .= $key . ' is not null {op} '; |
|
625 | - } |
|
626 | - elseif (strtolower($operator) == 'has') |
|
627 | - { |
|
628 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
629 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
630 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
631 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
632 | - } |
|
633 | - else |
|
634 | - { |
|
635 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
636 | - $conditionValues[] = $value; |
|
637 | - } |
|
638 | - } |
|
639 | - } |
|
640 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
641 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
642 | - } |
|
643 | - |
|
644 | - /** |
|
645 | - * Abstract method that return the necessary |
|
646 | - * information (full model namespace) |
|
647 | - * needed to preform the previous actions. |
|
648 | - * |
|
649 | - * @return string |
|
650 | - */ |
|
651 | - abstract protected function getModel(); |
|
604 | + if (strtolower($operator) == 'between') |
|
605 | + { |
|
606 | + $conditionString .= $key . ' >= ? and '; |
|
607 | + $conditionValues[] = $value1; |
|
608 | + |
|
609 | + $conditionString .= $key . ' <= ? {op} '; |
|
610 | + $conditionValues[] = $value2; |
|
611 | + } |
|
612 | + elseif (strtolower($operator) == 'in') |
|
613 | + { |
|
614 | + $conditionValues = array_merge($conditionValues, $value); |
|
615 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
616 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
617 | + } |
|
618 | + elseif (strtolower($operator) == 'null') |
|
619 | + { |
|
620 | + $conditionString .= $key . ' is null {op} '; |
|
621 | + } |
|
622 | + elseif (strtolower($operator) == 'not null') |
|
623 | + { |
|
624 | + $conditionString .= $key . ' is not null {op} '; |
|
625 | + } |
|
626 | + elseif (strtolower($operator) == 'has') |
|
627 | + { |
|
628 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
629 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
630 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
631 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
632 | + } |
|
633 | + else |
|
634 | + { |
|
635 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
636 | + $conditionValues[] = $value; |
|
637 | + } |
|
638 | + } |
|
639 | + } |
|
640 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
641 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
642 | + } |
|
643 | + |
|
644 | + /** |
|
645 | + * Abstract method that return the necessary |
|
646 | + * information (full model namespace) |
|
647 | + * needed to preform the previous actions. |
|
648 | + * |
|
649 | + * @return string |
|
650 | + */ |
|
651 | + abstract protected function getModel(); |
|
652 | 652 | } |
653 | 653 | \ No newline at end of file |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | */ |
17 | 17 | public function __construct() |
18 | 18 | { |
19 | - $this->model = \App::make($this->getModel()); |
|
19 | + $this->model = \App::make($this->getModel()); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | /** |
@@ -55,14 +55,14 @@ discard block |
||
55 | 55 | /** |
56 | 56 | * Construct the select conditions for the model. |
57 | 57 | */ |
58 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
58 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
59 | 59 | |
60 | 60 | if (count($conditionColumns)) |
61 | 61 | { |
62 | 62 | /** |
63 | 63 | * Use the first element in the model columns to construct the first condition. |
64 | 64 | */ |
65 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
65 | + $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | */ |
71 | 71 | foreach ($conditionColumns as $column) |
72 | 72 | { |
73 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
73 | + $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -91,9 +91,9 @@ discard block |
||
91 | 91 | /** |
92 | 92 | * Construct the relation condition. |
93 | 93 | */ |
94 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
94 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
95 | 95 | |
96 | - $subModel->where(function ($q) use ($query, $relation){ |
|
96 | + $subModel->where(function($q) use ($query, $relation){ |
|
97 | 97 | |
98 | 98 | /** |
99 | 99 | * Get columns of the relation. |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | /** |
106 | 106 | * Use the first element in the relation model columns to construct the first condition. |
107 | 107 | */ |
108 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
108 | + $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | */ |
114 | 114 | foreach ($subConditionColumns as $subConditionColumn) |
115 | 115 | { |
116 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
116 | + $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
117 | 117 | } |
118 | 118 | }); |
119 | 119 | |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | $modelClass = $this->model; |
175 | 175 | $relations = []; |
176 | 176 | |
177 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
177 | + \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
178 | 178 | /** |
179 | 179 | * If the id is present in the data then select the model for updating, |
180 | 180 | * else create new model. |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
184 | 184 | if ( ! $model) |
185 | 185 | { |
186 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
186 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | /** |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | */ |
242 | 242 | if ( ! $relationModel) |
243 | 243 | { |
244 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
244 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | /** |
253 | 253 | * Prevent the sub relations or attributes not in the fillable. |
254 | 254 | */ |
255 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
255 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
256 | 256 | { |
257 | 257 | $relationModel->$attr = $val; |
258 | 258 | } |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | */ |
283 | 283 | if ( ! $relationModel) |
284 | 284 | { |
285 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
285 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | foreach ($value as $relationAttribute => $relationValue) |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | } |
428 | 428 | else |
429 | 429 | { |
430 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
430 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ |
|
431 | 431 | $model->update($data); |
432 | 432 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
433 | 433 | }); |
@@ -446,11 +446,11 @@ discard block |
||
446 | 446 | { |
447 | 447 | if ($attribute == 'id') |
448 | 448 | { |
449 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
449 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
450 | 450 | $model = $this->model->lockForUpdate()->find($value); |
451 | 451 | if ( ! $model) |
452 | 452 | { |
453 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
453 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
454 | 454 | } |
455 | 455 | |
456 | 456 | $model->delete(); |
@@ -459,8 +459,8 @@ discard block |
||
459 | 459 | } |
460 | 460 | else |
461 | 461 | { |
462 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
463 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
462 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
463 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($saveLog){ |
|
464 | 464 | $model->delete(); |
465 | 465 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
466 | 466 | }); |
@@ -497,7 +497,7 @@ discard block |
||
497 | 497 | { |
498 | 498 | $conditions = $this->constructConditions($conditions, $this->model); |
499 | 499 | $sort = $desc ? 'desc' : 'asc'; |
500 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
500 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
501 | 501 | } |
502 | 502 | |
503 | 503 | /** |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
538 | 538 | } |
539 | 539 | |
540 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
540 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
541 | 541 | } |
542 | 542 | |
543 | 543 | /** |
@@ -552,7 +552,7 @@ discard block |
||
552 | 552 | |
553 | 553 | if ( ! $model) |
554 | 554 | { |
555 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
555 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
556 | 556 | } |
557 | 557 | |
558 | 558 | $model->restore(); |
@@ -572,13 +572,13 @@ discard block |
||
572 | 572 | if ($key == 'and') |
573 | 573 | { |
574 | 574 | $conditions = $this->constructConditions($value, $model); |
575 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
575 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
576 | 576 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
577 | 577 | } |
578 | 578 | else if ($key == 'or') |
579 | 579 | { |
580 | 580 | $conditions = $this->constructConditions($value, $model); |
581 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
581 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
582 | 582 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
583 | 583 | } |
584 | 584 | else |
@@ -603,41 +603,41 @@ discard block |
||
603 | 603 | |
604 | 604 | if (strtolower($operator) == 'between') |
605 | 605 | { |
606 | - $conditionString .= $key . ' >= ? and '; |
|
606 | + $conditionString .= $key.' >= ? and '; |
|
607 | 607 | $conditionValues[] = $value1; |
608 | 608 | |
609 | - $conditionString .= $key . ' <= ? {op} '; |
|
609 | + $conditionString .= $key.' <= ? {op} '; |
|
610 | 610 | $conditionValues[] = $value2; |
611 | 611 | } |
612 | 612 | elseif (strtolower($operator) == 'in') |
613 | 613 | { |
614 | 614 | $conditionValues = array_merge($conditionValues, $value); |
615 | 615 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
616 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
616 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
617 | 617 | } |
618 | 618 | elseif (strtolower($operator) == 'null') |
619 | 619 | { |
620 | - $conditionString .= $key . ' is null {op} '; |
|
620 | + $conditionString .= $key.' is null {op} '; |
|
621 | 621 | } |
622 | 622 | elseif (strtolower($operator) == 'not null') |
623 | 623 | { |
624 | - $conditionString .= $key . ' is not null {op} '; |
|
624 | + $conditionString .= $key.' is not null {op} '; |
|
625 | 625 | } |
626 | 626 | elseif (strtolower($operator) == 'has') |
627 | 627 | { |
628 | 628 | $sql = $model->withTrashed()->has($key)->toSql(); |
629 | 629 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
630 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
630 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
631 | 631 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
632 | 632 | } |
633 | 633 | else |
634 | 634 | { |
635 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
635 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
636 | 636 | $conditionValues[] = $value; |
637 | 637 | } |
638 | 638 | } |
639 | 639 | } |
640 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
640 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
641 | 641 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
642 | 642 | } |
643 | 643 |
@@ -4,364 +4,364 @@ |
||
4 | 4 | |
5 | 5 | class UserRepository extends AbstractRepository |
6 | 6 | { |
7 | - /** |
|
8 | - * Return the model full namespace. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - protected function getModel() |
|
13 | - { |
|
14 | - return 'App\Modules\V1\Acl\AclUser'; |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * Return the logged in user account. |
|
19 | - * |
|
20 | - * @param array $relations |
|
21 | - * @return boolean |
|
22 | - */ |
|
23 | - public function account($relations = []) |
|
24 | - { |
|
25 | - $permissions = []; |
|
26 | - $user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id, $relations); |
|
27 | - foreach ($user->groups()->get() as $group) |
|
28 | - { |
|
29 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
30 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
31 | - }); |
|
32 | - } |
|
33 | - $user->permissions = $permissions; |
|
34 | - |
|
35 | - return $user; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Check if the logged in user or the given user |
|
40 | - * has the given permissions on the given model. |
|
41 | - * |
|
42 | - * @param string $nameOfPermission |
|
43 | - * @param string $model |
|
44 | - * @param boolean $user |
|
45 | - * @return boolean |
|
46 | - */ |
|
47 | - public function can($nameOfPermission, $model, $user = false ) |
|
48 | - { |
|
49 | - $user = $user ?: \JWTAuth::parseToken()->authenticate(); |
|
50 | - $permissions = []; |
|
51 | - |
|
52 | - if ( ! $user = $this->find($user->id, ['groups.permissions'])) |
|
53 | - { |
|
54 | - \ErrorHandler::tokenExpired(); |
|
55 | - } |
|
56 | - |
|
57 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
58 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
59 | - }); |
|
7 | + /** |
|
8 | + * Return the model full namespace. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + protected function getModel() |
|
13 | + { |
|
14 | + return 'App\Modules\V1\Acl\AclUser'; |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * Return the logged in user account. |
|
19 | + * |
|
20 | + * @param array $relations |
|
21 | + * @return boolean |
|
22 | + */ |
|
23 | + public function account($relations = []) |
|
24 | + { |
|
25 | + $permissions = []; |
|
26 | + $user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id, $relations); |
|
27 | + foreach ($user->groups()->get() as $group) |
|
28 | + { |
|
29 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
30 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
31 | + }); |
|
32 | + } |
|
33 | + $user->permissions = $permissions; |
|
34 | + |
|
35 | + return $user; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Check if the logged in user or the given user |
|
40 | + * has the given permissions on the given model. |
|
41 | + * |
|
42 | + * @param string $nameOfPermission |
|
43 | + * @param string $model |
|
44 | + * @param boolean $user |
|
45 | + * @return boolean |
|
46 | + */ |
|
47 | + public function can($nameOfPermission, $model, $user = false ) |
|
48 | + { |
|
49 | + $user = $user ?: \JWTAuth::parseToken()->authenticate(); |
|
50 | + $permissions = []; |
|
51 | + |
|
52 | + if ( ! $user = $this->find($user->id, ['groups.permissions'])) |
|
53 | + { |
|
54 | + \ErrorHandler::tokenExpired(); |
|
55 | + } |
|
56 | + |
|
57 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
58 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
59 | + }); |
|
60 | 60 | |
61 | - return in_array($nameOfPermission, $permissions); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Check if the logged in user has the given group. |
|
66 | - * |
|
67 | - * @param string $groupName |
|
68 | - * @param integer $userId |
|
69 | - * @return boolean |
|
70 | - */ |
|
71 | - public function hasGroup($groupName, $userId = fa;se) |
|
72 | - { |
|
73 | - $userId = $userId ?: \JWTAuth::parseToken()->authenticate()->id; |
|
74 | - $groups = $this->find($userId)->groups; |
|
75 | - return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Assign the given group ids to the given user. |
|
80 | - * |
|
81 | - * @param integer $user_id |
|
82 | - * @param array $group_ids |
|
83 | - * @return object |
|
84 | - */ |
|
85 | - public function assignGroups($user_id, $group_ids) |
|
86 | - { |
|
87 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
88 | - $user = $this->find($user_id); |
|
89 | - $user->groups()->detach(); |
|
90 | - $user->groups()->attach($group_ids); |
|
91 | - }); |
|
92 | - |
|
93 | - return $this->find($user_id); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Handle a login request to the application. |
|
98 | - * |
|
99 | - * @param array $credentials |
|
100 | - * @param boolean $adminLogin |
|
101 | - * @return array |
|
102 | - */ |
|
103 | - public function login($credentials, $adminLogin = false) |
|
104 | - { |
|
105 | - if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
106 | - { |
|
107 | - \ErrorHandler::loginFailed(); |
|
108 | - } |
|
109 | - else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
110 | - { |
|
111 | - \ErrorHandler::loginFailed(); |
|
112 | - } |
|
113 | - else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
114 | - { |
|
115 | - \ErrorHandler::loginFailed(); |
|
116 | - } |
|
117 | - else if ($user->blocked) |
|
118 | - { |
|
119 | - \ErrorHandler::userIsBlocked(); |
|
120 | - } |
|
121 | - else if ($token = \JWTAuth::attempt($credentials)) |
|
122 | - { |
|
123 | - return ['token' => $token]; |
|
124 | - } |
|
125 | - else |
|
126 | - { |
|
127 | - \ErrorHandler::loginFailed(); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Handle a social login request of the none admin to the application. |
|
133 | - * |
|
134 | - * @param array $credentials |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - public function loginSocial($credentials) |
|
138 | - { |
|
139 | - $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
140 | - $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
141 | - |
|
142 | - if ( ! $user->email) |
|
143 | - { |
|
144 | - \ErrorHandler::noSocialEmail(); |
|
145 | - } |
|
146 | - |
|
147 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
148 | - { |
|
149 | - $data = ['email' => $user->email, 'password' => '']; |
|
150 | - return $this->register($data); |
|
151 | - } |
|
152 | - else |
|
153 | - { |
|
154 | - if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
155 | - { |
|
156 | - \ErrorHandler::userAlreadyRegistered(); |
|
157 | - } |
|
158 | - return $this->login(['email' => $registeredUser->email, 'password' => ''], false); |
|
159 | - } |
|
160 | - } |
|
61 | + return in_array($nameOfPermission, $permissions); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Check if the logged in user has the given group. |
|
66 | + * |
|
67 | + * @param string $groupName |
|
68 | + * @param integer $userId |
|
69 | + * @return boolean |
|
70 | + */ |
|
71 | + public function hasGroup($groupName, $userId = fa;se) |
|
72 | + { |
|
73 | + $userId = $userId ?: \JWTAuth::parseToken()->authenticate()->id; |
|
74 | + $groups = $this->find($userId)->groups; |
|
75 | + return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Assign the given group ids to the given user. |
|
80 | + * |
|
81 | + * @param integer $user_id |
|
82 | + * @param array $group_ids |
|
83 | + * @return object |
|
84 | + */ |
|
85 | + public function assignGroups($user_id, $group_ids) |
|
86 | + { |
|
87 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
88 | + $user = $this->find($user_id); |
|
89 | + $user->groups()->detach(); |
|
90 | + $user->groups()->attach($group_ids); |
|
91 | + }); |
|
92 | + |
|
93 | + return $this->find($user_id); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Handle a login request to the application. |
|
98 | + * |
|
99 | + * @param array $credentials |
|
100 | + * @param boolean $adminLogin |
|
101 | + * @return array |
|
102 | + */ |
|
103 | + public function login($credentials, $adminLogin = false) |
|
104 | + { |
|
105 | + if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
106 | + { |
|
107 | + \ErrorHandler::loginFailed(); |
|
108 | + } |
|
109 | + else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
110 | + { |
|
111 | + \ErrorHandler::loginFailed(); |
|
112 | + } |
|
113 | + else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
114 | + { |
|
115 | + \ErrorHandler::loginFailed(); |
|
116 | + } |
|
117 | + else if ($user->blocked) |
|
118 | + { |
|
119 | + \ErrorHandler::userIsBlocked(); |
|
120 | + } |
|
121 | + else if ($token = \JWTAuth::attempt($credentials)) |
|
122 | + { |
|
123 | + return ['token' => $token]; |
|
124 | + } |
|
125 | + else |
|
126 | + { |
|
127 | + \ErrorHandler::loginFailed(); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Handle a social login request of the none admin to the application. |
|
133 | + * |
|
134 | + * @param array $credentials |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + public function loginSocial($credentials) |
|
138 | + { |
|
139 | + $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
140 | + $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
141 | + |
|
142 | + if ( ! $user->email) |
|
143 | + { |
|
144 | + \ErrorHandler::noSocialEmail(); |
|
145 | + } |
|
146 | + |
|
147 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
148 | + { |
|
149 | + $data = ['email' => $user->email, 'password' => '']; |
|
150 | + return $this->register($data); |
|
151 | + } |
|
152 | + else |
|
153 | + { |
|
154 | + if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
155 | + { |
|
156 | + \ErrorHandler::userAlreadyRegistered(); |
|
157 | + } |
|
158 | + return $this->login(['email' => $registeredUser->email, 'password' => ''], false); |
|
159 | + } |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * Handle a registration request. |
|
164 | - * |
|
165 | - * @param array $credentials |
|
166 | - * @return array |
|
167 | - */ |
|
168 | - public function register($credentials) |
|
169 | - { |
|
170 | - return ['token' => \JWTAuth::fromUser($this->model->create($credentials))]; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Logout the user. |
|
175 | - * |
|
176 | - * @return boolean |
|
177 | - */ |
|
178 | - public function logout() |
|
179 | - { |
|
180 | - return \JWTAuth::invalidate(\JWTAuth::getToken()); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Block the user. |
|
185 | - * |
|
186 | - * @param integer $user_id |
|
187 | - * @return object |
|
188 | - */ |
|
189 | - public function block($user_id) |
|
190 | - { |
|
191 | - if ( ! $user = $this->find($user_id)) |
|
192 | - { |
|
193 | - \ErrorHandler::notFound('user'); |
|
194 | - } |
|
195 | - if ( ! $this->hasGroup('Admin')) |
|
196 | - { |
|
197 | - \ErrorHandler::noPermissions(); |
|
198 | - } |
|
199 | - else if (\JWTAuth::parseToken()->authenticate()->id == $user_id) |
|
200 | - { |
|
201 | - \ErrorHandler::noPermissions(); |
|
202 | - } |
|
203 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
204 | - { |
|
205 | - \ErrorHandler::noPermissions(); |
|
206 | - } |
|
207 | - |
|
208 | - $user->blocked = 1; |
|
209 | - $user->save(); |
|
162 | + /** |
|
163 | + * Handle a registration request. |
|
164 | + * |
|
165 | + * @param array $credentials |
|
166 | + * @return array |
|
167 | + */ |
|
168 | + public function register($credentials) |
|
169 | + { |
|
170 | + return ['token' => \JWTAuth::fromUser($this->model->create($credentials))]; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Logout the user. |
|
175 | + * |
|
176 | + * @return boolean |
|
177 | + */ |
|
178 | + public function logout() |
|
179 | + { |
|
180 | + return \JWTAuth::invalidate(\JWTAuth::getToken()); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Block the user. |
|
185 | + * |
|
186 | + * @param integer $user_id |
|
187 | + * @return object |
|
188 | + */ |
|
189 | + public function block($user_id) |
|
190 | + { |
|
191 | + if ( ! $user = $this->find($user_id)) |
|
192 | + { |
|
193 | + \ErrorHandler::notFound('user'); |
|
194 | + } |
|
195 | + if ( ! $this->hasGroup('Admin')) |
|
196 | + { |
|
197 | + \ErrorHandler::noPermissions(); |
|
198 | + } |
|
199 | + else if (\JWTAuth::parseToken()->authenticate()->id == $user_id) |
|
200 | + { |
|
201 | + \ErrorHandler::noPermissions(); |
|
202 | + } |
|
203 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
204 | + { |
|
205 | + \ErrorHandler::noPermissions(); |
|
206 | + } |
|
207 | + |
|
208 | + $user->blocked = 1; |
|
209 | + $user->save(); |
|
210 | 210 | |
211 | - return $user; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Unblock the user. |
|
216 | - * |
|
217 | - * @param integer $user_id |
|
218 | - * @return object |
|
219 | - */ |
|
220 | - public function unblock($user_id) |
|
221 | - { |
|
222 | - if ( ! $this->hasGroup('Admin')) |
|
223 | - { |
|
224 | - \ErrorHandler::noPermissions(); |
|
225 | - } |
|
226 | - |
|
227 | - $user = $this->find($user_id); |
|
228 | - $user->blocked = 0; |
|
229 | - $user->save(); |
|
230 | - |
|
231 | - return $user; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Send a reset link to the given user. |
|
236 | - * |
|
237 | - * @param string $email |
|
238 | - * @return void |
|
239 | - */ |
|
240 | - public function sendReset($email) |
|
241 | - { |
|
242 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
243 | - { |
|
244 | - \ErrorHandler::notFound('email'); |
|
245 | - } |
|
246 | - |
|
247 | - $url = $this->config['resetLink']; |
|
248 | - $token = \Password::getRepository()->create($user); |
|
211 | + return $user; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Unblock the user. |
|
216 | + * |
|
217 | + * @param integer $user_id |
|
218 | + * @return object |
|
219 | + */ |
|
220 | + public function unblock($user_id) |
|
221 | + { |
|
222 | + if ( ! $this->hasGroup('Admin')) |
|
223 | + { |
|
224 | + \ErrorHandler::noPermissions(); |
|
225 | + } |
|
226 | + |
|
227 | + $user = $this->find($user_id); |
|
228 | + $user->blocked = 0; |
|
229 | + $user->save(); |
|
230 | + |
|
231 | + return $user; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Send a reset link to the given user. |
|
236 | + * |
|
237 | + * @param string $email |
|
238 | + * @return void |
|
239 | + */ |
|
240 | + public function sendReset($email) |
|
241 | + { |
|
242 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
243 | + { |
|
244 | + \ErrorHandler::notFound('email'); |
|
245 | + } |
|
246 | + |
|
247 | + $url = $this->config['resetLink']; |
|
248 | + $token = \Password::getRepository()->create($user); |
|
249 | 249 | |
250 | - \Mail::send('Acl:resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
251 | - $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
252 | - }); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Reset the given user's password. |
|
257 | - * |
|
258 | - * @param array $credentials |
|
259 | - * @return array |
|
260 | - */ |
|
261 | - public function resetPassword($credentials) |
|
262 | - { |
|
263 | - $token = false; |
|
264 | - $response = \Password::reset($credentials, function ($user, $password) use (&$token) { |
|
265 | - $user->password = bcrypt($password); |
|
266 | - $user->save(); |
|
267 | - |
|
268 | - $token = \JWTAuth::fromUser($user); |
|
269 | - }); |
|
270 | - |
|
271 | - switch ($response) { |
|
272 | - case \Password::PASSWORD_RESET: |
|
273 | - return ['token' => $token]; |
|
250 | + \Mail::send('Acl:resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
251 | + $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
252 | + }); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Reset the given user's password. |
|
257 | + * |
|
258 | + * @param array $credentials |
|
259 | + * @return array |
|
260 | + */ |
|
261 | + public function resetPassword($credentials) |
|
262 | + { |
|
263 | + $token = false; |
|
264 | + $response = \Password::reset($credentials, function ($user, $password) use (&$token) { |
|
265 | + $user->password = bcrypt($password); |
|
266 | + $user->save(); |
|
267 | + |
|
268 | + $token = \JWTAuth::fromUser($user); |
|
269 | + }); |
|
270 | + |
|
271 | + switch ($response) { |
|
272 | + case \Password::PASSWORD_RESET: |
|
273 | + return ['token' => $token]; |
|
274 | 274 | |
275 | - case \Password::INVALID_TOKEN: |
|
276 | - \ErrorHandler::invalidResetToken('token'); |
|
277 | - |
|
278 | - case \Password::INVALID_PASSWORD: |
|
279 | - \ErrorHandler::invalidResetPassword('email'); |
|
280 | - |
|
281 | - case \Password::INVALID_USER: |
|
282 | - \ErrorHandler::notFound('user'); |
|
283 | - |
|
284 | - default: |
|
285 | - \ErrorHandler::generalError(); |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Change the logged in user password. |
|
291 | - * |
|
292 | - * @param array $credentials |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function changePassword($credentials) |
|
296 | - { |
|
297 | - $user = \JWTAuth::parseToken()->authenticate(); |
|
298 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
299 | - { |
|
300 | - \ErrorHandler::invalidOldPassword(); |
|
301 | - } |
|
302 | - |
|
303 | - $user->password = $credentials['password']; |
|
304 | - $user->save(); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Refresh the expired login token. |
|
309 | - * |
|
310 | - * @return array |
|
311 | - */ |
|
312 | - public function refreshtoken() |
|
313 | - { |
|
314 | - $token = \JWTAuth::parseToken()->refresh(); |
|
315 | - |
|
316 | - return ['token' => $token]; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Paginate all users in the given group based on the given conditions. |
|
321 | - * |
|
322 | - * @param string $groupName |
|
323 | - * @param array $relations |
|
324 | - * @param integer $perPage |
|
325 | - * @param string $sortBy |
|
326 | - * @param boolean $desc |
|
327 | - * @return \Illuminate\Http\Response |
|
328 | - */ |
|
329 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
330 | - { |
|
331 | - unset($conditions['page']); |
|
332 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
333 | - $sort = $desc ? 'desc' : 'asc'; |
|
334 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
335 | - |
|
336 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
337 | - $q->where('name', $groupName); |
|
338 | - }); |
|
275 | + case \Password::INVALID_TOKEN: |
|
276 | + \ErrorHandler::invalidResetToken('token'); |
|
277 | + |
|
278 | + case \Password::INVALID_PASSWORD: |
|
279 | + \ErrorHandler::invalidResetPassword('email'); |
|
280 | + |
|
281 | + case \Password::INVALID_USER: |
|
282 | + \ErrorHandler::notFound('user'); |
|
283 | + |
|
284 | + default: |
|
285 | + \ErrorHandler::generalError(); |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Change the logged in user password. |
|
291 | + * |
|
292 | + * @param array $credentials |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function changePassword($credentials) |
|
296 | + { |
|
297 | + $user = \JWTAuth::parseToken()->authenticate(); |
|
298 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
299 | + { |
|
300 | + \ErrorHandler::invalidOldPassword(); |
|
301 | + } |
|
302 | + |
|
303 | + $user->password = $credentials['password']; |
|
304 | + $user->save(); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Refresh the expired login token. |
|
309 | + * |
|
310 | + * @return array |
|
311 | + */ |
|
312 | + public function refreshtoken() |
|
313 | + { |
|
314 | + $token = \JWTAuth::parseToken()->refresh(); |
|
315 | + |
|
316 | + return ['token' => $token]; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Paginate all users in the given group based on the given conditions. |
|
321 | + * |
|
322 | + * @param string $groupName |
|
323 | + * @param array $relations |
|
324 | + * @param integer $perPage |
|
325 | + * @param string $sortBy |
|
326 | + * @param boolean $desc |
|
327 | + * @return \Illuminate\Http\Response |
|
328 | + */ |
|
329 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
330 | + { |
|
331 | + unset($conditions['page']); |
|
332 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
333 | + $sort = $desc ? 'desc' : 'asc'; |
|
334 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
335 | + |
|
336 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
337 | + $q->where('name', $groupName); |
|
338 | + }); |
|
339 | 339 | |
340 | 340 | |
341 | - if (count($conditions['conditionValues'])) |
|
342 | - { |
|
343 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
344 | - } |
|
345 | - |
|
346 | - if ($perPage) |
|
347 | - { |
|
348 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
349 | - } |
|
350 | - |
|
351 | - return $model->orderBy($sortBy, $sort)->get(); |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Save the given data to the logged in user. |
|
356 | - * |
|
357 | - * @param array $credentials |
|
358 | - * @return object |
|
359 | - */ |
|
360 | - public function saveProfile($credentials) |
|
361 | - { |
|
362 | - $user = \JWTAuth::parseToken()->authenticate(); |
|
363 | - $user->save($credentials); |
|
364 | - |
|
365 | - return $user; |
|
366 | - } |
|
341 | + if (count($conditions['conditionValues'])) |
|
342 | + { |
|
343 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
344 | + } |
|
345 | + |
|
346 | + if ($perPage) |
|
347 | + { |
|
348 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
349 | + } |
|
350 | + |
|
351 | + return $model->orderBy($sortBy, $sort)->get(); |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Save the given data to the logged in user. |
|
356 | + * |
|
357 | + * @param array $credentials |
|
358 | + * @return object |
|
359 | + */ |
|
360 | + public function saveProfile($credentials) |
|
361 | + { |
|
362 | + $user = \JWTAuth::parseToken()->authenticate(); |
|
363 | + $user->save($credentials); |
|
364 | + |
|
365 | + return $user; |
|
366 | + } |
|
367 | 367 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | $user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id, $relations); |
27 | 27 | foreach ($user->groups()->get() as $group) |
28 | 28 | { |
29 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
29 | + $group->permissions->each(function($permission) use (&$permissions){ |
|
30 | 30 | $permissions[$permission->model][$permission->id] = $permission->name; |
31 | 31 | }); |
32 | 32 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * @param boolean $user |
45 | 45 | * @return boolean |
46 | 46 | */ |
47 | - public function can($nameOfPermission, $model, $user = false ) |
|
47 | + public function can($nameOfPermission, $model, $user = false) |
|
48 | 48 | { |
49 | 49 | $user = $user ?: \JWTAuth::parseToken()->authenticate(); |
50 | 50 | $permissions = []; |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | \ErrorHandler::tokenExpired(); |
55 | 55 | } |
56 | 56 | |
57 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
57 | + $user->groups->pluck('permissions')->each(function($permission) use (&$permissions, $model){ |
|
58 | 58 | $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
59 | 59 | }); |
60 | 60 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * @param integer $userId |
69 | 69 | * @return boolean |
70 | 70 | */ |
71 | - public function hasGroup($groupName, $userId = fa;se) |
|
71 | + public function hasGroup($groupName, $userId = fa; se) |
|
72 | 72 | { |
73 | 73 | $userId = $userId ?: \JWTAuth::parseToken()->authenticate()->id; |
74 | 74 | $groups = $this->find($userId)->groups; |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function assignGroups($user_id, $group_ids) |
86 | 86 | { |
87 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
87 | + \DB::transaction(function() use ($user_id, $group_ids) { |
|
88 | 88 | $user = $this->find($user_id); |
89 | 89 | $user->groups()->detach(); |
90 | 90 | $user->groups()->attach($group_ids); |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | $url = $this->config['resetLink']; |
248 | 248 | $token = \Password::getRepository()->create($user); |
249 | 249 | |
250 | - \Mail::send('Acl:resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
250 | + \Mail::send('Acl:resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function($m) use ($user) { |
|
251 | 251 | $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
252 | 252 | }); |
253 | 253 | } |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | public function resetPassword($credentials) |
262 | 262 | { |
263 | 263 | $token = false; |
264 | - $response = \Password::reset($credentials, function ($user, $password) use (&$token) { |
|
264 | + $response = \Password::reset($credentials, function($user, $password) use (&$token) { |
|
265 | 265 | $user->password = bcrypt($password); |
266 | 266 | $user->save(); |
267 | 267 |
@@ -1,11 +1,11 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\V1\Acl\AclUser::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
4 | + return [ |
|
5 | 5 | 'name' => $faker->name(), |
6 | 6 | 'email' => $faker->safeEmail(), |
7 | 7 | 'password' => bcrypt(str_random(10)), |
8 | 8 | 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
9 | 9 | 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
10 | - ]; |
|
10 | + ]; |
|
11 | 11 | }); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\V1\Acl\AclUser::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\V1\Acl\AclUser::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'name' => $faker->name(), |
6 | 6 | 'email' => $faker->safeEmail(), |
@@ -1,9 +1,9 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\V1\Acl\AclGroup::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
4 | + return [ |
|
5 | 5 | 'name' => $faker->unique->word(), |
6 | 6 | 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
7 | 7 | 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
8 | - ]; |
|
8 | + ]; |
|
9 | 9 | }); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\V1\Acl\AclGroup::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\V1\Acl\AclGroup::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'name' => $faker->unique->word(), |
6 | 6 | 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
@@ -1,10 +1,10 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\V1\Acl\AclPermission::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
4 | + return [ |
|
5 | 5 | 'name' => $faker->randomElement(['save', 'delete', 'find', 'paginate']), |
6 | 6 | 'model' => $faker->randomElement(['users', 'groups', 'settings', 'notifications']), |
7 | 7 | 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
8 | 8 | 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
9 | - ]; |
|
9 | + ]; |
|
10 | 10 | }); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\V1\Acl\AclPermission::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\V1\Acl\AclPermission::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'name' => $faker->randomElement(['save', 'delete', 'find', 'paginate']), |
6 | 6 | 'model' => $faker->randomElement(['users', 'groups', 'settings', 'notifications']), |
@@ -5,53 +5,53 @@ |
||
5 | 5 | */ |
6 | 6 | class AclPermissionObserver { |
7 | 7 | |
8 | - public function saving($model) |
|
9 | - { |
|
10 | - // |
|
11 | - } |
|
12 | - |
|
13 | - public function saved($model) |
|
14 | - { |
|
15 | - // |
|
16 | - } |
|
17 | - |
|
18 | - public function creating($model) |
|
19 | - { |
|
20 | - // |
|
21 | - } |
|
22 | - |
|
23 | - public function created($model) |
|
24 | - { |
|
25 | - // |
|
26 | - } |
|
27 | - |
|
28 | - public function updating($model) |
|
29 | - { |
|
30 | - // |
|
31 | - } |
|
32 | - |
|
33 | - public function updated($model) |
|
34 | - { |
|
35 | - // |
|
36 | - } |
|
37 | - |
|
38 | - public function deleting($model) |
|
39 | - { |
|
40 | - // |
|
41 | - } |
|
42 | - |
|
43 | - public function deleted($model) |
|
44 | - { |
|
45 | - // |
|
46 | - } |
|
47 | - |
|
48 | - public function restoring($model) |
|
49 | - { |
|
50 | - // |
|
51 | - } |
|
52 | - |
|
53 | - public function restored($model) |
|
54 | - { |
|
55 | - // |
|
56 | - } |
|
8 | + public function saving($model) |
|
9 | + { |
|
10 | + // |
|
11 | + } |
|
12 | + |
|
13 | + public function saved($model) |
|
14 | + { |
|
15 | + // |
|
16 | + } |
|
17 | + |
|
18 | + public function creating($model) |
|
19 | + { |
|
20 | + // |
|
21 | + } |
|
22 | + |
|
23 | + public function created($model) |
|
24 | + { |
|
25 | + // |
|
26 | + } |
|
27 | + |
|
28 | + public function updating($model) |
|
29 | + { |
|
30 | + // |
|
31 | + } |
|
32 | + |
|
33 | + public function updated($model) |
|
34 | + { |
|
35 | + // |
|
36 | + } |
|
37 | + |
|
38 | + public function deleting($model) |
|
39 | + { |
|
40 | + // |
|
41 | + } |
|
42 | + |
|
43 | + public function deleted($model) |
|
44 | + { |
|
45 | + // |
|
46 | + } |
|
47 | + |
|
48 | + public function restoring($model) |
|
49 | + { |
|
50 | + // |
|
51 | + } |
|
52 | + |
|
53 | + public function restored($model) |
|
54 | + { |
|
55 | + // |
|
56 | + } |
|
57 | 57 | } |
58 | 58 | \ No newline at end of file |
@@ -5,66 +5,66 @@ |
||
5 | 5 | */ |
6 | 6 | class AclUserObserver { |
7 | 7 | |
8 | - public function saving($model) |
|
9 | - { |
|
10 | - // |
|
11 | - } |
|
8 | + public function saving($model) |
|
9 | + { |
|
10 | + // |
|
11 | + } |
|
12 | 12 | |
13 | - public function saved($model) |
|
14 | - { |
|
15 | - // |
|
16 | - } |
|
13 | + public function saved($model) |
|
14 | + { |
|
15 | + // |
|
16 | + } |
|
17 | 17 | |
18 | - public function creating($model) |
|
19 | - { |
|
20 | - // |
|
21 | - } |
|
18 | + public function creating($model) |
|
19 | + { |
|
20 | + // |
|
21 | + } |
|
22 | 22 | |
23 | - public function created($model) |
|
24 | - { |
|
25 | - // |
|
26 | - } |
|
23 | + public function created($model) |
|
24 | + { |
|
25 | + // |
|
26 | + } |
|
27 | 27 | |
28 | - public function updating($model) |
|
29 | - { |
|
30 | - if ($model->password) |
|
31 | - { |
|
32 | - $model->last_change_password = \Carbon\Carbon::now()->toDateTimeString(); |
|
33 | - } |
|
34 | - } |
|
28 | + public function updating($model) |
|
29 | + { |
|
30 | + if ($model->password) |
|
31 | + { |
|
32 | + $model->last_change_password = \Carbon\Carbon::now()->toDateTimeString(); |
|
33 | + } |
|
34 | + } |
|
35 | 35 | |
36 | - public function updated($model) |
|
37 | - { |
|
38 | - // |
|
39 | - } |
|
36 | + public function updated($model) |
|
37 | + { |
|
38 | + // |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Soft delete user logs. |
|
43 | - * |
|
44 | - * @param object $model the delted model. |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function deleting($model) |
|
48 | - { |
|
49 | - if ($model->getOriginal('id') == \JWTAuth::parseToken()->authenticate()->id) |
|
50 | - { |
|
51 | - \ErrorHandler::noPermissions(); |
|
52 | - } |
|
53 | - $model->logs()->delete(); |
|
54 | - } |
|
41 | + /** |
|
42 | + * Soft delete user logs. |
|
43 | + * |
|
44 | + * @param object $model the delted model. |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function deleting($model) |
|
48 | + { |
|
49 | + if ($model->getOriginal('id') == \JWTAuth::parseToken()->authenticate()->id) |
|
50 | + { |
|
51 | + \ErrorHandler::noPermissions(); |
|
52 | + } |
|
53 | + $model->logs()->delete(); |
|
54 | + } |
|
55 | 55 | |
56 | - public function deleted($model) |
|
57 | - { |
|
58 | - // |
|
59 | - } |
|
56 | + public function deleted($model) |
|
57 | + { |
|
58 | + // |
|
59 | + } |
|
60 | 60 | |
61 | - public function restoring($model) |
|
62 | - { |
|
63 | - $model->logs()->restore(); |
|
64 | - } |
|
61 | + public function restoring($model) |
|
62 | + { |
|
63 | + $model->logs()->restore(); |
|
64 | + } |
|
65 | 65 | |
66 | - public function restored($model) |
|
67 | - { |
|
68 | - // |
|
69 | - } |
|
66 | + public function restored($model) |
|
67 | + { |
|
68 | + // |
|
69 | + } |
|
70 | 70 | } |
71 | 71 | \ No newline at end of file |
@@ -5,71 +5,71 @@ |
||
5 | 5 | */ |
6 | 6 | class AclGroupObserver { |
7 | 7 | |
8 | - public function saving($model) |
|
9 | - { |
|
10 | - // |
|
11 | - } |
|
8 | + public function saving($model) |
|
9 | + { |
|
10 | + // |
|
11 | + } |
|
12 | 12 | |
13 | - public function saved($model) |
|
14 | - { |
|
15 | - // |
|
16 | - } |
|
13 | + public function saved($model) |
|
14 | + { |
|
15 | + // |
|
16 | + } |
|
17 | 17 | |
18 | - public function creating($model) |
|
19 | - { |
|
20 | - // |
|
21 | - } |
|
18 | + public function creating($model) |
|
19 | + { |
|
20 | + // |
|
21 | + } |
|
22 | 22 | |
23 | - public function created($model) |
|
24 | - { |
|
25 | - // |
|
26 | - } |
|
23 | + public function created($model) |
|
24 | + { |
|
25 | + // |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Prevent updating of the admin group. |
|
30 | - * |
|
31 | - * @param object $model the model beign updated. |
|
32 | - * @return void |
|
33 | - */ |
|
34 | - public function updating($model) |
|
35 | - { |
|
36 | - if ($model->getOriginal('name') == 'Admin') |
|
37 | - { |
|
38 | - \ErrorHandler::noPermissions(); |
|
39 | - } |
|
40 | - } |
|
28 | + /** |
|
29 | + * Prevent updating of the admin group. |
|
30 | + * |
|
31 | + * @param object $model the model beign updated. |
|
32 | + * @return void |
|
33 | + */ |
|
34 | + public function updating($model) |
|
35 | + { |
|
36 | + if ($model->getOriginal('name') == 'Admin') |
|
37 | + { |
|
38 | + \ErrorHandler::noPermissions(); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - public function updated($model) |
|
43 | - { |
|
44 | - // |
|
45 | - } |
|
42 | + public function updated($model) |
|
43 | + { |
|
44 | + // |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Prevent deleting the admin group. |
|
49 | - * |
|
50 | - * @param object $model the delted model. |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function deleting($model) |
|
54 | - { |
|
55 | - if ($model->getOriginal('name') == 'Admin') |
|
56 | - { |
|
57 | - \ErrorHandler::noPermissions(); |
|
58 | - } |
|
59 | - } |
|
47 | + /** |
|
48 | + * Prevent deleting the admin group. |
|
49 | + * |
|
50 | + * @param object $model the delted model. |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function deleting($model) |
|
54 | + { |
|
55 | + if ($model->getOriginal('name') == 'Admin') |
|
56 | + { |
|
57 | + \ErrorHandler::noPermissions(); |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - public function deleted($model) |
|
62 | - { |
|
63 | - // |
|
64 | - } |
|
61 | + public function deleted($model) |
|
62 | + { |
|
63 | + // |
|
64 | + } |
|
65 | 65 | |
66 | - public function restoring($model) |
|
67 | - { |
|
68 | - // |
|
69 | - } |
|
66 | + public function restoring($model) |
|
67 | + { |
|
68 | + // |
|
69 | + } |
|
70 | 70 | |
71 | - public function restored($model) |
|
72 | - { |
|
73 | - // |
|
74 | - } |
|
71 | + public function restored($model) |
|
72 | + { |
|
73 | + // |
|
74 | + } |
|
75 | 75 | } |
76 | 76 | \ No newline at end of file |
@@ -1,10 +1,10 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\V1\Reporting\Report::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
4 | + return [ |
|
5 | 5 | 'report_name' => $faker->randomElement(['Users Count', 'Low Stock Products', 'Active Users']), |
6 | 6 | 'view_name' => $faker->word(), |
7 | 7 | 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
8 | 8 | 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
9 | - ]; |
|
9 | + ]; |
|
10 | 10 | }); |
11 | 11 | \ No newline at end of file |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\V1\Reporting\Report::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\V1\Reporting\Report::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'report_name' => $faker->randomElement(['Users Count', 'Low Stock Products', 'Active Users']), |
6 | 6 | 'view_name' => $faker->word(), |