Example #1
0
 /**
  * Get the callback to constrain an permissions query to the given permission.
  *
  * @param  string  $permission
  * @param  \Illuminate\Database\Eloquent\Model|string|null  $model
  * @return \Closure
  */
 protected function getPermissionConstraint($permission, $model)
 {
     return function ($query) use($permission, $model) {
         $table = Models::permission()->getTable();
         $query->where("{$table}.name", $permission);
         if (!is_null($model)) {
             $query->forModel($model);
         }
     };
 }
Example #2
0
 /**
  * Deserialize an array of permissions into a collection of models.
  *
  * @param  array  $permissions
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function deserializePermissions(array $permissions)
 {
     return Models::permission()->hydrate($permissions);
 }
Example #3
0
 /**
  * Get an instance of the permission model.
  *
  * @param  array  $attributes
  * @return \Silber\Bouncer\Database\Permission
  */
 public function permission(array $attributes = [])
 {
     return Models::permission($attributes);
 }
Example #4
0
 /**
  * Get the permission IDs from the names present in the given array.
  *
  * @param  array  $permissions
  * @return array
  */
 protected function getPermissionIdsFromStrings(array $permissions)
 {
     $names = array_filter($permissions, 'is_string');
     if (!count($names)) {
         return [];
     }
     return Models::permission()->whereIn('name', $names)->lists('id')->all();
 }
 /**
  * Get the table name for the permission model.
  *
  * @return string
  */
 protected function permissions()
 {
     return Models::permission()->getTable();
 }
Example #6
0
 /**
  * Get a list of the user's permissions.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $user
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function getPermissions(Model $user)
 {
     $query = Models::permission()->whereHas('roles', $this->getRoleUsersConstraint($user));
     return $query->orWhereHas('users', $this->getUserConstraint($user))->get();
 }
Example #7
0
 /**
  * Create permissions whose name is not in the given list.
  *
  * @param  \Illuminate\Database\Eloquent\Collection  $models
  * @param  array  $permissions
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function createMissingPermissions(Collection $models, array $permissions)
 {
     $missing = array_diff($permissions, $models->pluck('name')->all());
     $created = [];
     foreach ($missing as $permission) {
         $created[] = Models::permission()->create(['name' => $permission]);
     }
     return $created;
 }