Example #1
0
 /**
  * @param Builder $builder
  * @param Model $model
  */
 public function apply(Builder $builder, Model $model)
 {
     if ($this->enabled === false) {
         return;
     }
     foreach ($this->getModelTenants($model) as $tenantColumn => $tenantId) {
         $builder->whereRaw($model->getTenantWhereClause($tenantColumn, $tenantId));
     }
 }
 /**
  * Check if jsonb field has all or any of the keys/elements specified in $keys array
  * @param Builder $query
  * @param string $column
  * @param array $keys
  * @param string $has all|any
  * @return Builder
  */
 public function scopeWherePgJsonbKeysExist(Builder $query, $column, array $keys, $has = 'all')
 {
     $mark = '&';
     if ($has === 'any') {
         $mark = '|';
     }
     $keys = Helper::phpArrayToPostgresArray($keys);
     $column = Helper::nestedJsonColumn($column);
     return $query->whereRaw("{$column} \\?{$mark} ?", [$keys]);
 }
 /**
  * Apply the scope to a given Eloquent query builder.
  *
  * @param Builder|\Illuminate\Database\Query\Builder $builder
  * @param Model|\Illuminate\Database\Query\Model     $model
  *
  * @return void
  */
 public function apply(Builder $builder, Model $model)
 {
     if (!$this->enabled) {
         return;
     }
     // Use whereRaw instead of where to avoid issues with bindings when removing
     foreach ($this->getModelTenants($model) as $tenantColumn => $tenantId) {
         $builder->whereRaw($model->getTenantWhereClause($tenantColumn, $tenantId));
     }
 }
 /**
  * @param \Illuminate\Database\Eloquent\Builder $builder
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function createWhere(EloquentBuilder $builder, Eloquent $model)
 {
     if ($model->getOnlyTranslated() && $model->shouldFallback()) {
         $key = $model->getForeignKey();
         $primary = "{$this->i18nTable}.{$key}";
         $fallback = "{$this->i18nTable}_fallback.{$key}";
         $ifNull = $builder->getQuery()->compileIfNull($primary, $fallback);
         $builder->whereRaw("{$ifNull} is not null");
     }
 }
Example #5
0
 /**
  * Apply the scope to a given Eloquent query builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  * @throws TenantNotSetError
  * @return void
  */
 public function apply(Builder $builder, Model $model)
 {
     $this->refreshTenant();
     if ($this->disabled) {
         return;
     }
     if ($this->tenant_id === null) {
         throw new TenantNotSetError();
     }
     $builder->whereRaw("{$builder->getQuery()->from}.{$this->tenant_column} = {$this->tenant_id}");
 }
 /**
  * @param User $actor
  * @param Builder $query
  */
 public function find(User $actor, Builder $query)
 {
     if ($actor->cannot('viewDiscussions')) {
         $query->whereRaw('FALSE');
     } elseif (!$actor->hasPermission('discussion.hide')) {
         $query->where(function ($query) use($actor) {
             $query->whereNull('discussions.hide_time')->where('comments_count', '>', 0)->orWhere('start_user_id', $actor->id);
             $this->events->fire(new ScopeHiddenDiscussionVisibility($query, $actor, 'discussion.hide'));
         });
     }
 }
Example #7
0
 /**
  * Scope a query to only include records that are visible to a user.
  *
  * @param Builder $query
  * @param User $actor
  */
 public function scopeWhereVisibleTo(Builder $query, User $actor)
 {
     if (!app('flarum.forum')->can($actor, 'view')) {
         if ($this instanceof User) {
             $query->where('id', $actor->id);
         } else {
             $query->whereRaw('FALSE');
         }
     } else {
         event(new ScopeModelVisibility($this, $query, $actor));
     }
 }
 /**
  * @param EloquentBuilder $query
  * @param EloquentBuilder $parent
  * @param array $columns
  *
  * @return mixed
  */
 public function getRelationQuery(EloquentBuilder $query, EloquentBuilder $parent, $columns = ['*'])
 {
     $query->select($columns);
     $table = $query->getModel()->getTable();
     $query->from($table . ' as ' . ($hash = $this->getRelationCountHash()));
     $grammar = $query->getQuery()->getGrammar();
     $table = $grammar->wrapTable($table);
     $hash = $grammar->wrapTable($hash);
     $lft = $grammar->wrap($this->parent->getLftName());
     $rgt = $grammar->wrap($this->parent->getRgtName());
     return $query->whereRaw("{$hash}.{$lft} between {$table}.{$lft} + 1 and {$table}.{$rgt}");
 }
 /**
  * Apply the scope to a given Eloquent query builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  *
  * @throws Exceptions\TenantIdNotSetException
  * @return void
  */
 public function apply(Builder $builder)
 {
     if (is_null(self::getTenantId())) {
         if (self::$enabled) {
             throw new TenantIdNotSetException();
         }
         return;
     }
     /** @var \Illuminate\Database\Eloquent\Model|ScopedByTenant $model */
     $model = $builder->getModel();
     // Use whereRaw instead of where to avoid issues with bindings when removing
     $builder->whereRaw($model->getTenantWhereClause());
 }
Example #10
0
 public function apply(Builder $builder, Model $model)
 {
     if (!$this->isEnabled($model)) {
         return;
     }
     $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
     foreach ($traces as $trace) {
         // Find the first non-vendor-dir file in the backtrace
         if (isset($trace['file']) && !str_contains($trace['file'], DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR)) {
             $file = '"query.file" <> "' . $trace['file'] . '"';
             $line = '"query.line" <> "' . $trace['line'] . '"';
             return $builder->whereRaw($file)->whereRaw($line);
         }
     }
 }
 /**
  * @param Builder $builder
  * @param Model   $model
  */
 public function apply(Builder $builder, Model $model)
 {
     if (!Auth::guest() && Auth::user()->hasCountryRole()) {
         $country = Auth::user()->country;
         if ($builder->getModel()->getTable() == "activity_logs") {
             $builder->whereHas('contract', function ($q) use($country) {
                 $q->whereRaw("contracts.metadata->'country'->>'code' in (?)", $country);
             });
         } elseif ($builder->getModel()->getTable() == "contract_annotations") {
             $builder->whereHas('contract', function ($q) use($country) {
                 $q->whereRaw("contracts.metadata->'country'->>'code' in (?)", $country);
             });
         } else {
             $builder->whereRaw("contracts.metadata->'country'->>'code' in (?)", $country);
         }
     }
 }
Example #12
0
 /**
  * Apply the scope to a given Eloquent query builder.
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @param  \Illuminate\Database\Eloquent\Model    $builder
  * @return void
  */
 public function apply(Builder $builder, Model $model)
 {
     $builder->whereRaw("level = {$this->level}");
 }
Example #13
0
 /**
  * Only retrieve files whose slots are integers.
  *
  * @param Builder $query
  * @return Builder
  */
 public function scopeInIntegerSlot(Builder $query)
 {
     return $query->whereRaw(sprintf('%s.slot REGEXP \'^[[:digit:]]+$\'', $query->getQuery()->from));
 }
Example #14
0
 /**
  * @param User $actor
  * @param Builder $query
  */
 public function find(User $actor, Builder $query)
 {
     if ($actor->cannot('viewDiscussions')) {
         $query->whereRaw('FALSE');
     }
 }
 /**
  * Where database array $column has any of the elements in $value
  * @param Builder $query
  * @param string $column
  * @param mixed $value
  * @return Builder
  */
 public function scopeWherePgArrayOverlap(Builder $query, $column, $value)
 {
     $value = self::mutateToPgArray((array) $value);
     return $query->whereRaw("{$column} && ?", [$value]);
 }
 public function apply(Builder $builder, Model $model)
 {
     $this->tenant_id = Session::get('tenant_id');
     $builder->whereRaw($model->getTenantWhereClause($this->tenant_col, $this->tenant_id));
 }
Example #17
0
 /**
  * Scope of received messages.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  * @param string                                $query
  *
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeReceived($query)
 {
     return $query->whereRaw('recipient_user_id = user_id');
 }
Example #18
0
 /**
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  string  $slug
  * @return \Illuminate\Database\Eloquent\Builder
  */
 public function scopeWhereSlug($query, $slug)
 {
     return $query->whereRaw('BINARY `slug` = ?', [$slug]);
 }
Example #19
0
 /**
  * Query scope to detect the presence of one or more attached media that is bound to all of the specified tags simultaneously.
  * @param  \Illuminate\Database\Eloquent\Builder $q
  * @param  array                                 $tags
  * @return void
  */
 public function scopeWhereHasMediaMatchAll(Builder $q, array $tags)
 {
     $grammar = $q->getConnection()->getQueryGrammar();
     $subquery = $this->newMatchAllQuery($tags)->selectRaw('count(*)')->whereRaw($grammar->wrap($this->media()->getForeignKey()) . ' = ' . $grammar->wrap($this->getQualifiedKeyName()));
     $q->whereRaw('(' . $subquery->toSql() . ') >= 1', $subquery->getBindings());
 }