Skip to content

Commit

Permalink
Merge pull request Zizaco#733 from Archcry/master
Browse files Browse the repository at this point in the history
Fixing issues introduced by Laravel 5.4
  • Loading branch information
Zizaco authored Apr 10, 2017
2 parents 992e484 + fac7dd7 commit 2a05acd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Entrust/Traits/EntrustPermissionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait EntrustPermissionTrait
*/
public function roles()
{
return $this->belongsToMany(Config::get('entrust.role'), Config::get('entrust.permission_role_table'));
return $this->belongsToMany(Config::get('entrust.role'), Config::get('entrust.permission_role_table'), Config::get('entrust.permission_foreign_key'), Config::get('entrust.role_foreign_key'));
}

/**
Expand Down
37 changes: 19 additions & 18 deletions src/Entrust/Traits/EntrustRoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,42 @@ trait EntrustRoleTrait
public function cachedPermissions()
{
$rolePrimaryKey = $this->primaryKey;
$cacheKey = 'entrust_permissions_for_role_'.$this->$rolePrimaryKey;
if(Cache::getStore() instanceof TaggableStore) {
$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 () {
return $this->perms()->get();
});
}
else return $this->perms()->get();
} else return $this->perms()->get();
}

public function save(array $options = [])
{ //both inserts and updates
if(!parent::save($options)){
if (!parent::save($options)) {
return false;
}
if(Cache::getStore() instanceof TaggableStore) {
if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}

public function delete(array $options = [])
{ //soft or hard
if(!parent::delete($options)){
if (!parent::delete($options)) {
return false;
}
if(Cache::getStore() instanceof TaggableStore) {
if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}

public function restore()
{ //soft delete undo's
if(!parent::restore()){
if (!parent::restore()) {
return false;
}
if(Cache::getStore() instanceof TaggableStore) {
if (Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
Expand All @@ -64,8 +66,7 @@ public function restore()
*/
public function users()
{
return $this->belongsToMany(Config::get('auth.providers.users.model'), Config::get('entrust.role_user_table'),Config::get('entrust.role_foreign_key'),Config::get('entrust.user_foreign_key'));
// return $this->belongsToMany(Config::get('auth.model'), Config::get('entrust.role_user_table'));
return $this->belongsToMany(Config::get('auth.providers.users.model'), Config::get('entrust.role_user_table'), Config::get('entrust.role_foreign_key'), Config::get('entrust.user_foreign_key'));
}

/**
Expand All @@ -90,7 +91,7 @@ public static function boot()
{
parent::boot();

static::deleting(function($role) {
static::deleting(function ($role) {
if (!method_exists(Config::get('entrust.role'), 'bootSoftDeletes')) {
$role->users()->sync([]);
$role->perms()->sync([]);
Expand All @@ -103,8 +104,8 @@ public static function boot()
/**
* Checks if the role has a permission by its name.
*
* @param string|array $name Permission name or array of permission names.
* @param bool $requireAll All permissions in the array are required.
* @param string|array $name Permission name or array of permission names.
* @param bool $requireAll All permissions in the array are required.
*
* @return bool
*/
Expand Down Expand Up @@ -151,9 +152,9 @@ public function savePermissions($inputPermissions)
$this->perms()->detach();
}

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

/**
Expand Down
43 changes: 37 additions & 6 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
*/
'roles_table' => 'roles',

/*
|--------------------------------------------------------------------------
| Entrust role foreign key
|--------------------------------------------------------------------------
|
| This is the role foreign key used by Entrust to make a proper
| relation between permissions and roles & roles and users
|
*/
'role_foreign_key' => 'role_id',

/*
|--------------------------------------------------------------------------
| Application User Model
Expand All @@ -53,6 +64,28 @@
*/
'users_table' => 'users',

/*
|--------------------------------------------------------------------------
| Entrust role_user Table
|--------------------------------------------------------------------------
|
| This is the role_user table used by Entrust to save assigned roles to the
| database.
|
*/
'role_user_table' => 'role_user',

/*
|--------------------------------------------------------------------------
| Entrust user foreign key
|--------------------------------------------------------------------------
|
| This is the user foreign key used by Entrust to make a proper
| relation between roles and users
|
*/
'user_foreign_key' => 'user_id',

/*
|--------------------------------------------------------------------------
| Entrust Permission Model
Expand Down Expand Up @@ -88,14 +121,12 @@

/*
|--------------------------------------------------------------------------
| Entrust role_user Table
| Entrust permission foreign key
|--------------------------------------------------------------------------
|
| This is the role_user table used by Entrust to save assigned roles to the
| database.
| This is the permission foreign key used by Entrust to make a proper
| relation between permissions and roles
|
*/
'role_user_table' => 'role_user',


'permission_foreign_key' => 'permission_id',
];

0 comments on commit 2a05acd

Please sign in to comment.