/** * Get or create the role. * * @return \Silber\Bouncer\Database\Role */ protected function role() { if ($this->role instanceof Role) { return $this->role; } return Models::role()->firstOrCreate(['name' => $this->role]); }
/** * Get the model or create a role. * * @return \Illuminate\Database\Eloquent\Model */ protected function getModel() { if ($this->model instanceof Model) { return $this->model; } return Models::role()->firstOrCreate(['name' => $this->model]); }
/** * Get the authority, creating a role authority if necessary. * * @return \Illuminate\Database\Eloquent\Model */ protected function getAuthority() { if ($this->authority instanceof Model) { return $this->authority; } return Models::role()->firstOrCreate(['name' => $this->authority]); }
/** * Get the model from which to disassociate the abilities. * * @return \Illuminate\Database\Eloquent\Model|null */ protected function getModel() { if ($this->model instanceof Model) { return $this->model; } return Models::role()->where('name', $this->model)->first(); }
/** * Get the role. * * @return \Silber\Bouncer\Database\Role|null */ protected function role() { if ($this->role instanceof Role) { return $this->role; } return Models::role()->where('name', $this->role)->first(); }
/** * Get a constraint for abilities that have been granted to the given authority through a role. * * @param \Illuminate\Database\Eloquent\Model $authority * @param bool $allowed * @return \Closure */ protected function getRoleConstraint(Model $authority, $allowed) { return function ($query) use($authority, $allowed) { $permissions = Models::table('permissions'); $abilities = Models::table('abilities'); $roles = Models::table('roles'); $prefix = Models::prefix(); $query->from($roles)->join($permissions, $roles . '.id', '=', $permissions . '.entity_id')->whereRaw("{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id")->where($permissions . ".forbidden", !$allowed)->where($permissions . ".entity_type", Models::role()->getMorphClass()); $query->where(function ($query) use($roles, $authority, $allowed) { $query->whereExists($this->getAuthorityRoleConstraint($authority)); if ($allowed) { $this->addRoleInheritCondition($query, $authority, $roles); } }); }; }
/** * Migrate the data from the role_abilities table. * * @return void */ protected function migrateRoleAbilities() { $pivots = DB::table('role_abilities')->get(); $type = Models::role()->getMorphClass(); $records = array_map(function ($pivot) use($type) { return ['ability_id' => $pivot->ability_id, 'entity_type' => $type, 'entity_id' => $pivot->role_id]; }, $this->toArray($pivots)); DB::table('permissions')->insert($records); }
/** * Get the table name for the role model. * * @return string */ protected function roles() { return Models::role()->getTable(); }
/** * Get an instance of the role model. * * @param array $attributes * @return \Silber\Bouncer\Database\Role */ public function role(array $attributes = []) { return Models::role($attributes); }
/** * Refresh the cache for all roles and users, iteratively. * * @return void */ protected function refreshAllIteratively() { foreach (Models::user()->all() as $user) { $this->refreshFor($user); } foreach (Models::role()->all() as $role) { $this->refreshFor($role); } }