Ejemplo n.º 1
0
 /**
  * Get the role.
  *
  * @return \JonaGoldman\Bouncer\Database\Role|null
  */
 protected function role()
 {
     if ($this->role instanceof Role) {
         return $this->role;
     }
     return Models::role()->where('name', $this->role)->first();
 }
Ejemplo n.º 2
0
 /**
  * Get or create the role.
  *
  * @return \JonaGoldman\Bouncer\Database\Role
  */
 protected function role()
 {
     if ($this->role instanceof Role) {
         return $this->role;
     }
     return Models::role()->firstOrCreate(['name' => $this->role]);
 }
Ejemplo n.º 3
0
 /**
  * Get the callback to constrain an abilities query to the given ability.
  *
  * @param  string  $ability
  * @param  \Illuminate\Database\Eloquent\Model|string|null  $model
  * @return \Closure
  */
 protected function getAbilityConstraint($ability, $model)
 {
     return function ($query) use($ability, $model) {
         $table = Models::ability()->getTable();
         $query->where("{$table}.name", $ability);
         if (!is_null($model)) {
             $query->forModel($model);
         }
     };
 }
Ejemplo n.º 4
0
 /**
  * Get a list of the user's abilities.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $user
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function getAbilities(Model $user)
 {
     $query = Models::ability()->whereHas('roles', $this->getRoleUsersConstraint($user));
     return $query->orWhereHas('users', $this->getUserConstraint($user))->get();
 }
Ejemplo n.º 5
0
 /**
  * Create abilities whose name is not in the given list.
  *
  * @param  \Illuminate\Database\Eloquent\Collection  $models
  * @param  array  $abilities
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function createMissingAbilities(Collection $models, array $abilities)
 {
     $missing = array_diff($abilities, $models->pluck('name')->all());
     $created = [];
     foreach ($missing as $ability) {
         $created[] = Models::ability()->create(['name' => $ability]);
     }
     return $created;
 }
Ejemplo n.º 6
0
 /**
  * Get the ability IDs from the names present in the given array.
  *
  * @param  array  $abilities
  * @return array
  */
 protected function getAbilityIdsFromStrings(array $abilities)
 {
     $names = array_filter($abilities, 'is_string');
     if (!count($names)) {
         return [];
     }
     return Models::ability()->whereIn('name', $names)->lists('id')->all();
 }
Ejemplo n.º 7
0
 /**
  * The users relationship.
  *
  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  */
 public function users()
 {
     return $this->belongsToMany(Models::classname(User::class), 'user_ability');
 }
Ejemplo n.º 8
0
 /**
  * The Abilities relationship.
  *
  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  */
 public function abilities()
 {
     return $this->belongsToMany(Models::classname(Ability::class), 'user_ability');
 }
Ejemplo n.º 9
0
 /**
  * Setup the database schema.
  *
  * @return void
  */
 public function setUp()
 {
     Models::setUsersModel(User::class);
     $this->clipboard = new CachedClipboard(new ArrayStore());
     $this->migrate();
 }
Ejemplo n.º 10
0
 /**
  * Set the model to be used for users.
  *
  * @param string  $model
  */
 public static function useUserModel($model)
 {
     Models::setUsersModel($model);
 }
Ejemplo n.º 11
0
 /**
  * Deserialize an array of abilities into a collection of models.
  *
  * @param  array  $abilities
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function deserializeAbilities(array $abilities)
 {
     return Models::ability()->hydrate($abilities);
 }
Ejemplo n.º 12
0
 /**
  * Set the classname of the user model to be used by Bouncer.
  *
  * @return void
  */
 protected function setUserModel()
 {
     $config = $this->app->make('config');
     $model = $config->get('auth.providers.users.model', function () use($config) {
         return $config->get('auth.model', \App\User::class);
     });
     Models::setUsersModel($model);
 }
Ejemplo n.º 13
0
 /**
  * Get the table name for the user model.
  *
  * @return string
  */
 protected function users()
 {
     return Models::user()->getTable();
 }