<commit_before><?php

namespace App\Http\Middleware;

use Carbon\Carbon;

use Closure;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

use App\Models\Collections\Exhibition;
use App\Models\Shop\Product;
use App\Models\Web\Article;
use App\Models\Web\DigitalCatalog;
use App\Models\Web\EducatorResource;
use App\Models\Web\Event;
use App\Models\Web\EventOccurrence;
use App\Models\Web\Exhibition as WebExhibition;
use App\Models\Web\GenericPage;
use App\Models\Web\PressRelease;
use App\Models\Web\PrintedCatalog;
use App\Models\Web\Selection;
use App\Models\Web\Sponsor;
use App\Models\Web\StaticPage;

use App\Scopes\PublishedScope;

class RestrictContent
{
    /**
     * Handle an incoming request.
     *
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle(Request $request, Closure $next, $guard = null)
    {
        // Define what to show to anonymous users
        if (Gate::denies('restricted-access')) {
            Article::addGlobalScope(new PublishedScope);
            DigitalCatalog::addGlobalScope(new PublishedScope);
            EducatorResource::addGlobalScope(new PublishedScope);
            GenericPage::addGlobalScope(new PublishedScope);
            PressRelease::addGlobalScope(new PublishedScope);
            PrintedCatalog::addGlobalScope(new PublishedScope);
            Selection::addGlobalScope(new PublishedScope);
            Sponsor::addGlobalScope(new PublishedScope);
            StaticPage::addGlobalScope(new PublishedScope);
            WebExhibition::addGlobalScope(new PublishedScope);
            Event::addGlobalScope(new PublishedScope);
            EventOccurrence::addGlobalScope(new PublishedScope);
            Product::addGlobalScope(new PublishedScope);

            Exhibition::addGlobalScope('is-web-exhibition-published', function (Builder $builder) {
                $builder->leftJoin('web_exhibitions', 'exhibitions.citi_id', '=', 'web_exhibitions.datahub_id')

                    // Show all past exhibitions, accounting for some of the funky ways we've catalogued exhibitions in the past
                    ->where(function ($query) {
                        $query->where('date_aic_start', '<=', Carbon::today())
                            ->where(function ($query2) {
                                $query2->where('date_aic_end', '<=', Carbon::today())
                                    ->orWhere('date_aic_start', '<', Carbon::createMidnightDate(2011, 1, 1));
                            });
                    })

                    // For present and future exhibitions, only show if they're published on the web
                    ->orWhere('web_exhibitions.is_published', '=', true);
            });
        }
<commit_msg>Fix null fields in exhibitions due to join for restrict [WEB-1419]<commit_after><?php

namespace App\Http\Middleware;

use Carbon\Carbon;

use Closure;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

use App\Models\Collections\Exhibition;
use App\Models\Shop\Product;
use App\Models\Web\Article;
use App\Models\Web\DigitalCatalog;
use App\Models\Web\EducatorResource;
use App\Models\Web\Event;
use App\Models\Web\EventOccurrence;
use App\Models\Web\Exhibition as WebExhibition;
use App\Models\Web\GenericPage;
use App\Models\Web\PressRelease;
use App\Models\Web\PrintedCatalog;
use App\Models\Web\Selection;
use App\Models\Web\Sponsor;
use App\Models\Web\StaticPage;

use App\Scopes\PublishedScope;

class RestrictContent
{
    /**
     * Handle an incoming request.
     *
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle(Request $request, Closure $next, $guard = null)
    {
        // Define what to show to anonymous users
        if (Gate::denies('restricted-access')) {
            Article::addGlobalScope(new PublishedScope);
            DigitalCatalog::addGlobalScope(new PublishedScope);
            EducatorResource::addGlobalScope(new PublishedScope);
            GenericPage::addGlobalScope(new PublishedScope);
            PressRelease::addGlobalScope(new PublishedScope);
            PrintedCatalog::addGlobalScope(new PublishedScope);
            Selection::addGlobalScope(new PublishedScope);
            Sponsor::addGlobalScope(new PublishedScope);
            StaticPage::addGlobalScope(new PublishedScope);
            WebExhibition::addGlobalScope(new PublishedScope);
            Event::addGlobalScope(new PublishedScope);
            EventOccurrence::addGlobalScope(new PublishedScope);
            Product::addGlobalScope(new PublishedScope);

            Exhibition::addGlobalScope('is-web-exhibition-published', function (Builder $builder) {
                // Show all past exhibitions, accounting for some of the funky ways we've catalogued exhibitions in the past
                $builder->where(function ($query) {
                    $query->where('date_aic_start', '<=', Carbon::today())
                        ->where(function ($query2) {
                            $query2->where('date_aic_end', '<=', Carbon::today())
                                ->orWhere('date_aic_start', '<', Carbon::createMidnightDate(2011, 1, 1));
                        });
                });

                // For present and future exhibitions, only show if they're published on the web
                // WEB-1419: Using subquery here instead of join to avoid field overrides
                $builder->orWhereIn('citi_id', function($query) {
                    $query->select('datahub_id')
                          ->from('web_exhibitions')
                          ->where('is_published', '=', true);
                });
            });
        }

{'fraction_non_alphanumeric': 0.1101607937050975, 'fraction_numerical': 0.004105371193978789, 'mean_word_length': 2.598153846153846, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3385111', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1547, 'n_words': 326}