Skip to content
Draft
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
27 changes: 16 additions & 11 deletions src/Entrust/Traits/EntrustRoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/

use Illuminate\Cache\TaggableStore;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

trait EntrustRoleTrait
{
Expand All @@ -19,8 +19,9 @@ public function cachedPermissions()
{
$rolePrimaryKey = $this->primaryKey;
$cacheKey = 'entrust_permissions_for_role_' . $this->$rolePrimaryKey;
if (Cache::getStore() instanceof TaggableStore) {
return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl', 60), function () {
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
return $cache->tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl', 60), function () {
return $this->perms()->get();
});
} else return $this->perms()->get();
Expand All @@ -31,8 +32,9 @@ public function save(array $options = [])
if (!parent::save($options)) {
return false;
}
if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}
Expand All @@ -42,8 +44,9 @@ public function delete(array $options = [])
if (!parent::delete($options)) {
return false;
}
if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}
Expand All @@ -53,8 +56,9 @@ public function restore()
if (!parent::restore()) {
return false;
}
if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}
Expand Down Expand Up @@ -152,8 +156,9 @@ public function savePermissions($inputPermissions)
$this->perms()->detach();
}

if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.permission_role_table'))->flush();
}
}

Expand Down
23 changes: 13 additions & 10 deletions src/Entrust/Traits/EntrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

use InvalidArgumentException;

trait EntrustUserTrait
Expand All @@ -26,8 +25,9 @@ public function cachedRoles()
{
$userPrimaryKey = $this->primaryKey;
$cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey;
if(Cache::getStore() instanceof TaggableStore) {
return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
return $cache->tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
return $this->roles()->get();
});
}
Expand All @@ -39,8 +39,9 @@ public function cachedRoles()
*/
public function save(array $options = [])
{ //both inserts and updates
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.role_user_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.role_user_table'))->flush();
}
return parent::save($options);
}
Expand All @@ -51,8 +52,9 @@ public function save(array $options = [])
public function delete(array $options = [])
{ //soft or hard
$result = parent::delete($options);
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.role_user_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.role_user_table'))->flush();
}
return $result;
}
Expand All @@ -63,8 +65,9 @@ public function delete(array $options = [])
public function restore()
{ //soft delete undo's
$result = parent::restore();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.role_user_table'))->flush();
$cache = Cache::store(Config::get('entrust.cache_store'));
if ($cache instanceof TaggableStore) {
$cache->tags(Config::get('entrust.role_user_table'))->flush();
}
return $result;
}
Expand Down Expand Up @@ -307,7 +310,7 @@ public function detachRoles($roles=null)
}

/**
*Filtering users according to their role
*Filtering users according to their role
*
*@param string $role
*@return users collection
Expand Down
11 changes: 11 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

return [

/*
|--------------------------------------------------------------------------
| Cache store used by Entrust
|--------------------------------------------------------------------------
|
| The Laravel cache store used to cache permissions and roles. If null, it
| will use the default cache store.
|
*/
'cache_store' => env('ENTRUST_CACHE_STORE', null),

/*
|--------------------------------------------------------------------------
| Entrust Role Model
Expand Down