/** * Constrain the given roles query to those that were assigned to the given authorities. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection $model * @param array $keys * @return void */ public function constrainWhereAssignedTo($query, $model, array $keys = null) { list($model, $keys) = Helper::extractModelAndKeys($model, $keys); $query->whereExists(function ($query) use($model, $keys) { $table = $model->getTable(); $key = "{$table}.{$model->getKeyName()}"; $pivot = Models::table('assigned_roles'); $roles = Models::table('roles'); $prefix = Models::prefix(); $query->from($table)->join($pivot, $key, '=', $pivot . '.entity_id')->whereRaw("{$prefix}{$pivot}.role_id = {$prefix}{$roles}.id")->where("{$pivot}.entity_type", $model->getMorphClass())->whereIn($key, $keys); }); }
/** * Get a constraint for abilities that have been granted to the given authority. * * @param \Illuminate\Database\Eloquent\Model $authority * @param bool $allowed * @return \Closure */ protected function getAuthorityConstraint(Model $authority, $allowed) { return function ($query) use($authority, $allowed) { $permissions = Models::table('permissions'); $abilities = Models::table('abilities'); $table = $authority->getTable(); $prefix = Models::prefix(); $query->from($table)->join($permissions, $table . '.id', '=', $permissions . '.entity_id')->whereRaw("{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id")->where("{$permissions}.entity_type", $authority->getMorphClass())->where("{$permissions}.forbidden", !$allowed)->where("{$table}.{$authority->getKeyName()}", $authority->getKey()); }; }