diff --git a/src/Entrust/Traits/EntrustPermissionTrait.php b/src/Entrust/Traits/EntrustPermissionTrait.php index 8c06c261..d2a460f6 100644 --- a/src/Entrust/Traits/EntrustPermissionTrait.php +++ b/src/Entrust/Traits/EntrustPermissionTrait.php @@ -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')); } /** diff --git a/src/Entrust/Traits/EntrustRoleTrait.php b/src/Entrust/Traits/EntrustRoleTrait.php index e7b37189..4e39439a 100644 --- a/src/Entrust/Traits/EntrustRoleTrait.php +++ b/src/Entrust/Traits/EntrustRoleTrait.php @@ -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; @@ -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')); } /** @@ -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([]); @@ -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 */ @@ -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(); + } } /** diff --git a/src/config/config.php b/src/config/config.php index 3fff5920..07da1773 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -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 @@ -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 @@ -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', ];