<?php

namespace App\Filters;

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

/**
 * Requires a logged-in platform operator. Deliberately its own filter
 * (not App\Filters\Auth) with its own session keys (admin* prefix) —
 * super-admin sessions must never be confused with a tenant staff
 * session, since they authenticate against a completely different table
 * (admin_users, control DB) with no tenant scoping at all. See
 * App\Controllers\Admin\AdminAuthController.
 */
class AdminAuth implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $session = session();

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

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

        return null;
    }

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