Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dynamic property support #152

Open
wants to merge 7 commits into
base: v6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![ReadMeSupportPalestine](https://raw.githubusercontent.com/Safouene1/support-palestine-banner/master/banner-support.svg)](https://sahem.ksrelief.org/Pages/ProgramDetails/1ca8852b-9e6d-ee11-b83f-005056ac5498)
# laravel-permission-mongodb

[![Latest Version on Packagist][ico-version]][link-releases]
Expand All @@ -9,6 +10,7 @@
[![StyleCI][ico-styleci]][link-styleci]
[![Coverage Status][ico-coveralls]][link-coveralls]
[![Total Downloads][ico-downloads]][link-packagist]
[![StandWithPalestine](https://raw.githubusercontent.com/Safouene1/support-palestine-banner/master/StandWithPalestine.svg)](https://sahem.ksrelief.org/Pages/ProgramDetails/1ca8852b-9e6d-ee11-b83f-005056ac5498)

This package allows you to manage user permissions and roles in a database.
It is inspired from [laravel-permission][link-laravel-permission]. Same code same every thing but it is compatible with [laravel-mongodb][link-laravel-mongodb]
Expand Down Expand Up @@ -156,6 +158,25 @@ return [
'permissions' => 'permissions',
],

'user_props_names' => [

/*
* When using the "HasRoles" trait from this package, we need to know which
*Users table property should be used to retrieve their roles. We chose a basic
* default value, but you can easily change it to any property you want.
*/

'roles' => 'roles_id',

/*
* When using the "HasRoles" trait from this package, we need to know which
*Users table property should be used to retrieve their roles. We chose a basic
* default value, but you can easily change it to any property you want.
*/

'permissions' => 'permissions_id',
],

/*
* By default all permissions will be cached for 24 hours unless a permission or
* role is updated. Then the cache will be flushed immediately.
Expand Down
19 changes: 19 additions & 0 deletions config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@
'permissions' => 'permissions',
],

'user_props_names' => [

/*
* When using the "HasRoles" trait from this package, we need to know which
*Users table property should be used to retrieve their roles. We chose a basic
* default value, but you can easily change it to any property you want.
*/

'roles' => 'roles_id',

/*
* When using the "HasRoles" trait from this package, we need to know which
*Users table property should be used to retrieve their roles. We chose a basic
* default value, but you can easily change it to any property you want.
*/

'permissions' => 'permissions_id',
],

/*
* By default all permissions will be cached for 24 hours unless a permission or
* role is updated. Then the cache will be flushed immediately.
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function findOrCreate(string $name, string $guardName = null): Per
public function rolesQuery(): mixed
{
$roleClass = $this->getRoleClass();
return $roleClass->query()->where('permission_ids', 'all', [$this->_id]);
return $roleClass->query()->where(config('permission.user_props_names.permissions'), 'all', [$this->_id]);
}

/**
Expand All @@ -122,7 +122,7 @@ public function getRolesAttribute(): mixed
public function usersQuery(): mixed
{
$usersClass = app($this->helpers->getModelForGuard($this->attributes['guard_name']));
return $usersClass->query()->where('permission_ids', 'all', [$this->_id]);
return $usersClass->query()->where(config('permission.user_props_names.permissions'), 'all', [$this->_id]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function findByName(string $name, string $guardName = null): RoleI
public function usersQuery(): mixed
{
$usersClass = app($this->helpers->getModelForGuard($this->attributes['guard_name']));
return $usersClass->query()->where('role_ids', 'all', [$this->_id]);
return $usersClass->query()->where(config('permission.user_props_names.roles'), 'all', [$this->_id]);
}

/**
Expand Down Expand Up @@ -164,6 +164,6 @@ public function hasPermissionTo(string|PermissionInterface $permission): bool
throw new GuardDoesNotMatch($this->helpers->getGuardDoesNotMatchMessage($expected, $given));
}

return in_array($permission->_id, $this->permission_ids ?? [], true);
return in_array($permission->_id, $this->getAttribute(config('permission.user_props_names.permissions')) ?? [], true);
}
}
18 changes: 9 additions & 9 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getPermissionClass()
public function permissionsQuery()
{
$permission = $this->getPermissionClass();
return $permission::whereIn('_id', $this->permission_ids ?? []);
return $permission::whereIn('_id', $this->getAttribute(config('permission.user_props_names.permissions')) ?? []);
}

/**
Expand All @@ -59,9 +59,9 @@ public function getPermissionsAttribute()
*/
public function givePermissionTo(...$permissions): self
{
$this->permission_ids = collect($this->permission_ids ?? [])
$this->setAttribute(config('permission.user_props_names.permissions'), collect($this->getAttribute(config('permission.user_props_names.permissions')) ?? [])
->merge($this->getPermissionIds($permissions))
->all();
->all());

$this->save();

Expand All @@ -80,7 +80,7 @@ public function givePermissionTo(...$permissions): self
*/
public function syncPermissions(...$permissions): self
{
$this->permission_ids = $this->getPermissionIds($permissions);
$this->setAttribute(config('permission.user_props_names.permissions'), $this->getPermissionIds($permissions));

$this->save();
return $this->givePermissionTo($permissions);
Expand All @@ -98,11 +98,11 @@ public function revokePermissionTo(...$permissions): self
{
$permissions = $this->getPermissionIds($permissions);

$this->permission_ids = collect($this->permission_ids ?? [])
$this->$this->setAttribute(config('permission.user_props_names.permissions'), collect($this->getAttribute(config('permission.user_props_names.permissions')) ?? [])
->filter(function ($permission) use ($permissions) {
return ! in_array($permission, $permissions, true);
})
->all();
->all());

$this->save();

Expand Down Expand Up @@ -206,7 +206,7 @@ public function getPermissionNames(): Collection
*/
public function getPermissionsViaRoles(): Collection
{
$permissionIds = $this->roles->pluck('permission_ids')->flatten()->unique()->values();
$permissionIds = $this->roles->pluck(config('permission.user_props_names.permissions'))->flatten()->unique()->values();
return $this->getPermissionClass()->query()->whereIn('_id', $permissionIds)->get();
/*return $this->load('roles', 'roles.permissions')
->roles->flatMap(function (Role $role) {
Expand Down Expand Up @@ -345,8 +345,8 @@ public function scopePermission(Builder $query, $permissions): Builder
}
$roles = $roles->unique();

return $query->orWhereIn('permission_ids', $permissions->pluck('_id'))
->orWhereIn('role_ids', $roles->pluck('_id'));
return $query->orWhereIn(config('permission.user_props_names.permissions'), $permissions->pluck('_id'))
->orWhereIn(config('permission.user_props_names.roles'), $roles->pluck('_id'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function scopeRole(Builder $query, $roles): Builder
{
$roles = $this->convertToRoleModels($roles);

return $query->whereIn('role_ids', $roles->pluck('_id'));
return $query->whereIn(config('permission.user_props_names.roles'), $roles->pluck('_id'));
}

/**
Expand Down