diff --git a/src/Entrust/Traits/EntrustRoleTrait.php b/src/Entrust/Traits/EntrustRoleTrait.php index cd32d19d..4ff0cecb 100644 --- a/src/Entrust/Traits/EntrustRoleTrait.php +++ b/src/Entrust/Traits/EntrustRoleTrait.php @@ -8,6 +8,7 @@ * @package Zizaco\Entrust */ +use Illuminate\Cache\TaggableStore; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Cache; @@ -18,16 +19,21 @@ public function cachedPermissions() { $rolePrimaryKey = $this->primaryKey; $cacheKey = 'entrust_permissions_for_role_'.$this->$rolePrimaryKey; - return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () { - return $this->perms()->get(); - }); + if(Cache::getStore() instanceof TaggableStore) { + return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () { + return $this->perms()->get(); + }); + } + else return $this->perms()->get(); } public function save(array $options = []) { //both inserts and updates if(!parent::save($options)){ return false; } - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + if(Cache::getStore() instanceof TaggableStore) { + Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + } return true; } public function delete(array $options = []) @@ -35,7 +41,9 @@ public function delete(array $options = []) if(!parent::delete($options)){ return false; } - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + if(Cache::getStore() instanceof TaggableStore) { + Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + } return true; } public function restore() @@ -43,10 +51,12 @@ public function restore() if(!parent::restore()){ return false; } - Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + if(Cache::getStore() instanceof TaggableStore) { + Cache::tags(Config::get('entrust.permission_role_table'))->flush(); + } return true; } - + /** * Many-to-Many relations with the user model. * diff --git a/src/Entrust/Traits/EntrustUserTrait.php b/src/Entrust/Traits/EntrustUserTrait.php index 420634e5..cd355f5e 100644 --- a/src/Entrust/Traits/EntrustUserTrait.php +++ b/src/Entrust/Traits/EntrustUserTrait.php @@ -8,6 +8,7 @@ * @package Zizaco\Entrust */ +use Illuminate\Cache\TaggableStore; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Config; use InvalidArgumentException; @@ -19,26 +20,35 @@ public function cachedRoles() { $userPrimaryKey = $this->primaryKey; $cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey; - return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () { - return $this->roles()->get(); - }); + if(Cache::getStore() instanceof TaggableStore) { + return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () { + return $this->roles()->get(); + }); + } + else return $this->roles()->get(); } public function save(array $options = []) { //both inserts and updates parent::save($options); - Cache::tags(Config::get('entrust.role_user_table'))->flush(); + if(Cache::getStore() instanceof TaggableStore) { + Cache::tags(Config::get('entrust.role_user_table'))->flush(); + } } public function delete(array $options = []) { //soft or hard parent::delete($options); - Cache::tags(Config::get('entrust.role_user_table'))->flush(); + if(Cache::getStore() instanceof TaggableStore) { + Cache::tags(Config::get('entrust.role_user_table'))->flush(); + } } public function restore() { //soft delete undo's parent::restore(); - Cache::tags(Config::get('entrust.role_user_table'))->flush(); + if(Cache::getStore() instanceof TaggableStore) { + Cache::tags(Config::get('entrust.role_user_table'))->flush(); + } } - + /** * Many-to-Many relations with Role. * diff --git a/tests/EntrustUserTest.php b/tests/EntrustUserTest.php index efd2de5a..8ea135b5 100644 --- a/tests/EntrustUserTest.php +++ b/tests/EntrustUserTest.php @@ -2,6 +2,7 @@ use Zizaco\Entrust\Contracts\EntrustUserInterface; use Zizaco\Entrust\Traits\EntrustUserTrait; +use Illuminate\Cache\ArrayStore; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Cache; use Zizaco\Entrust\Permission; @@ -11,23 +12,23 @@ class EntrustUserTest extends PHPUnit_Framework_TestCase { private $facadeMocks = array(); - + public function setUp() { parent::setUp(); - + $app = m::mock('app')->shouldReceive('instance')->getMock(); - + $this->facadeMocks['config'] = m::mock('config'); $this->facadeMocks['cache'] = m::mock('cache'); - + Config::setFacadeApplication($app); Config::swap($this->facadeMocks['config']); - + Cache::setFacadeApplication($app); Cache::swap($this->facadeMocks['cache']); } - + public function tearDown() { m::close(); @@ -91,6 +92,7 @@ public function testHasRole() Config::shouldReceive('get')->with('entrust.role_user_table')->times(9)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(9)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(9)->andReturn($user->roles); + Cache::shouldReceive('getStore')->times(9)->andReturn(new ArrayStore); /* |------------------------------------------------------------ @@ -137,6 +139,7 @@ public function testCan() Config::shouldReceive('get')->with('entrust.role_user_table')->times(11)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(11)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(11)->andReturn($user->roles); + Cache::shouldReceive('getStore')->times(11)->andReturn(new ArrayStore); /* |------------------------------------------------------------ @@ -182,6 +185,7 @@ public function testCanWithPlaceholderSupport () Config::shouldReceive('get')->with('entrust.role_user_table')->times(6)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(6)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(6)->andReturn($user->roles); + Cache::shouldReceive('getStore')->times(6)->andReturn(new ArrayStore); /* |------------------------------------------------------------ @@ -196,8 +200,8 @@ public function testCanWithPlaceholderSupport () $this->assertTrue($user->can(['admin.*'])); $this->assertFalse($user->can(['site.*'])); } - - + + public function testAbilityShouldReturnBoolean() { /* @@ -240,7 +244,8 @@ public function testAbilityShouldReturnBoolean() Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles); - + Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore); + $user->shouldReceive('hasRole') ->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false)) ->andReturn(true); @@ -363,7 +368,8 @@ public function testAbilityShouldReturnArray() Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles); - + Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore); + $user->shouldReceive('hasRole') ->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false)) ->andReturn(true); @@ -524,7 +530,8 @@ public function testAbilityShouldReturnBoth() Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles); - + Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore); + $user->shouldReceive('hasRole') ->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false)) ->andReturn(true); @@ -699,7 +706,8 @@ public function testAbilityShouldAcceptStrings() Config::shouldReceive('get')->with('entrust.role_user_table')->times(8)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(8)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(8)->andReturn($user->roles); - + Cache::shouldReceive('getStore')->times(8)->andReturn(new ArrayStore); + $user->shouldReceive('hasRole') ->with(m::anyOf('UserRoleA', 'UserRoleB'), m::anyOf(true, false)) ->andReturn(true); @@ -774,7 +782,8 @@ public function testAbilityDefaultOptions() Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user'); Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440'); Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles); - + Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore); + $user->shouldReceive('hasRole') ->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false)) ->andReturn(true); @@ -1124,7 +1133,7 @@ class HasRoleUser implements EntrustUserInterface public $roles; public $primaryKey; public $id; - + public function __construct() { $this->primaryKey = 'id'; $this->id = 4;