/** * 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(); }
/** * 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]); }
/** * 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); } }; }
/** * 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(); }
/** * 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; }
/** * 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(); }
/** * The users relationship. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function users() { return $this->belongsToMany(Models::classname(User::class), 'user_ability'); }
/** * The Abilities relationship. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function abilities() { return $this->belongsToMany(Models::classname(Ability::class), 'user_ability'); }
/** * Setup the database schema. * * @return void */ public function setUp() { Models::setUsersModel(User::class); $this->clipboard = new CachedClipboard(new ArrayStore()); $this->migrate(); }
/** * Set the model to be used for users. * * @param string $model */ public static function useUserModel($model) { Models::setUsersModel($model); }
/** * 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); }
/** * 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); }
/** * Get the table name for the user model. * * @return string */ protected function users() { return Models::user()->getTable(); }