Skip to content

Commit 80d4771

Browse files
committedOct 13, 2019
piw
1 parent 27a6e87 commit 80d4771

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed
 

‎src/Models/Permission.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function ($model) use ($relationModel) {
6060
public function roles() : MorphToMany
6161
{
6262
return $this
63-
->morphedByMany(config('admission.models.roles'), 'permissionable')
63+
->morphedByMany(config('admission.models.role'), 'permissionable')
6464
->withPivot('from', 'until')
6565
->withTimestamps();
6666
}

‎src/Traits/HasPermissions.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ public function hasPermissionViaRole(PermissionContract $permission)
223223
return $this->hasRole($permission->roles);
224224
}
225225

226+
/**
227+
* General action matches with all other actions
228+
*
229+
* @return string
230+
*/
231+
protected function generalAction()
232+
{
233+
return '*';
234+
}
235+
226236
/**
227237
* Determine if the model has permission to run an action over given entity.
228238
*
@@ -234,10 +244,13 @@ public function hasPermissionViaRole(PermissionContract $permission)
234244
public function hasPermissionTo($action, $entity = null) : bool
235245
{
236246
$entity = array_pop($entity);
237-
238247
$permission = $this
239248
->getPermissionModel()
240-
->where('action', '=', $action)
249+
->where(function($query) use ($action) {
250+
$query
251+
->where('action', '=', $action)
252+
->orWhere('action', '=', $this->generalAction());
253+
})
241254
->when(
242255
!empty($entity),
243256
function ($query) use ($entity) {
@@ -246,8 +259,8 @@ function ($query) use ($entity) {
246259
$entityName = array_search($entityClassName, Relation::$morphMap);
247260

248261
$query
249-
->where('name', '=', $entityClassName)
250-
->orWhere('name', '=', $entityName);
262+
->where('entity_type', '=', $entityClassName)
263+
->orWhere('entity_type', '=', $entityName);
251264
});
252265

253266
if (is_object($entity) && !empty($entity->id)) {

0 commit comments

Comments
 (0)