131 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace nur\b\authnz;
 | 
						|
 | 
						|
use nur\authz;
 | 
						|
use nur\config;
 | 
						|
use nur\F;
 | 
						|
use nur\func;
 | 
						|
use nur\msg;
 | 
						|
use nur\v\fo;
 | 
						|
use nur\v\icon;
 | 
						|
use nur\v\ly;
 | 
						|
use nur\v\page;
 | 
						|
use nur\v\v;
 | 
						|
use nur\v\vo;
 | 
						|
use nur\v\vp\AInitAuthzPage;
 | 
						|
 | 
						|
/**
 | 
						|
 * Class CasAuthzManager: un utilisateur authentifié par CAS v3
 | 
						|
 */
 | 
						|
class CasAuthzManager extends AuthzManager {
 | 
						|
  const USER_MANAGER_CLASS = CasUserManager::class;
 | 
						|
  const APPCODE = null;
 | 
						|
 | 
						|
  function __construct(?string $appcode=null) {
 | 
						|
    if ($appcode === null) $appcode = static::APPCODE;
 | 
						|
    if ($appcode === null) $appcode = config::get_appcode();
 | 
						|
    $this->appcode = $appcode;
 | 
						|
  }
 | 
						|
 | 
						|
  protected $appcode;
 | 
						|
 | 
						|
  protected $userManager;
 | 
						|
 | 
						|
  protected function getUserManager(): IUserManager {
 | 
						|
    if ($this->userManager === null) {
 | 
						|
      $class = static::USER_MANAGER_CLASS;
 | 
						|
      $this->userManager = func::cons($class, $this->appcode);
 | 
						|
    }
 | 
						|
    return $this->userManager;
 | 
						|
  }
 | 
						|
 | 
						|
  private $destPage = null;
 | 
						|
 | 
						|
  function beforeSetup(AInitAuthzPage $page): void {
 | 
						|
    # initialiser la session avant setup. ainsi, dans les fonction beforeSetup(),
 | 
						|
    # setup() et afterSetup(), la session est disponible
 | 
						|
    $this->destPage = F::get("d", $page->getMainUrl());
 | 
						|
    $this->checkSession($username, $authType);
 | 
						|
 | 
						|
    if ($authType === "cas" && F::get("a")) {
 | 
						|
      # autologin
 | 
						|
      $casauthUrl = config::k("url")."/".$page->getCasauthUrl();
 | 
						|
      page::redirect(page::bu($page->getCasLoginUrl(), [
 | 
						|
        "service" => page::bu($casauthUrl, [
 | 
						|
          "r" => $page->getLoginUrl(),
 | 
						|
          "d" => $this->destPage,
 | 
						|
        ])
 | 
						|
      ]));
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  function print(AInitAuthzPage $page): void {
 | 
						|
    page::no_cache();
 | 
						|
    ly::row();
 | 
						|
    vo::h1(["class" => "text-center", q($page->TITLE())]);
 | 
						|
 | 
						|
    ly::col(["sm" => 6, "sm-push" => 3]);
 | 
						|
    $status = $this->getStatus();
 | 
						|
    switch ($status) {
 | 
						|
    case authz::DISCONNECTED:
 | 
						|
      msg::warning("Vous avez été déconnecté. Veuillez vous reconnecter");
 | 
						|
      break;
 | 
						|
    case authz::UNAUTHORIZED:
 | 
						|
      msg::error(["user" => [
 | 
						|
        "Connecté en tant que ",
 | 
						|
        v::b($this->getAuth()),
 | 
						|
        ", vous n'êtes pas autorisé à accéder à la page que vous avez demandé.",
 | 
						|
      ]]);
 | 
						|
      break;
 | 
						|
    }
 | 
						|
 | 
						|
    ly::panel("Connexion par CAS");
 | 
						|
    if ($page->isDevauthAllowed()) {
 | 
						|
      fo::start([
 | 
						|
        "type" => "basic",
 | 
						|
        "action" => $page->getCasauthUrl(),
 | 
						|
        "method" => "get",
 | 
						|
      ]);
 | 
						|
      fo::hidden("r", $page->getLoginUrl());
 | 
						|
      fo::hidden("d", $this->destPage);
 | 
						|
    } else {
 | 
						|
      fo::start([
 | 
						|
        "type" => "basic",
 | 
						|
        "action" => $page->getCasLoginUrl(),
 | 
						|
        "method" => "get",
 | 
						|
      ]);
 | 
						|
      $casauthUrl = config::k("url")."/".$page->getCasauthUrl();
 | 
						|
      fo::hidden("service", page::bu($casauthUrl, [
 | 
						|
        "r" => $page->getLoginUrl(),
 | 
						|
        "d" => $this->destPage,
 | 
						|
      ]));
 | 
						|
    }
 | 
						|
    #fo::p("Si vous avez un compte à l'université, vous pouvez vous connecter via CAS");
 | 
						|
    vo::p("Si vous avez un compte à l'université, vous pouvez vous connecter via CAS");
 | 
						|
    if ($this->isAuth()) {
 | 
						|
      if ($status != authz::UNAUTHORIZED) {
 | 
						|
        msg::warning(["user" => [
 | 
						|
          "Connecté en tant que ",
 | 
						|
          v::b(authz::get_auth()),
 | 
						|
          ", vous n'êtes pas autorisé à accéder à cette application.",
 | 
						|
        ]]);
 | 
						|
      }
 | 
						|
      fo::submit([
 | 
						|
        icon::logout("Vous déconnecter"),
 | 
						|
        "formaction" => $page->getLogoutUrl(),
 | 
						|
        "accesskey" => "z",
 | 
						|
      ]);
 | 
						|
      fo::hidden("renew", "true");
 | 
						|
      fo::submit([
 | 
						|
        icon::login("Changer de compte"),
 | 
						|
        "accesskey" => "r",
 | 
						|
      ]);
 | 
						|
    } else {
 | 
						|
      fo::submit(["Connexion par CAS", "accesskey" => "r"]);
 | 
						|
    }
 | 
						|
    fo::end();
 | 
						|
 | 
						|
    ly::end();
 | 
						|
  }
 | 
						|
}
 |