<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

/**
 * Requires a logged-in event organizer. Its own filter (not
 * App\Filters\Auth or AdminAuth) with its own session keys
 * (organizer* prefix) for the same reason AdminAuth's admin* prefix
 * exists: every realm on this app shares one PHP session (same
 * ci_session cookie, no subdomain partitioning — organizer/* is just a
 * URL prefix on the same host, same as admin/*), so a staff member and
 * an organizer logged in on two tabs of the same browser must never be
 * able to collide. See App\Controllers\EventOrganizerAuthController.
 */
class OrganizerAuth implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $session = session();

        if (! $session->get('organizerIsLoggedIn')) {
            $session->setFlashdata('error', 'Please log in to continue.');

            return redirect()->to(site_url('organizer/login'))->withCookies();
        }

        return null;
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        // no-op
    }
}
