CASLogout.php 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace Drupal\casost\Controller;

use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\user\Entity\User;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
14
use phpCAS;
15 16 17

class CASLogout extends ControllerBase
{
18 19 20 21 22
    protected $serverVersion;
    protected $serverHostname;
    protected $serverPort;
    protected $serverUri;
    protected $changeSessionId;
23
    protected $logoutRedirectUrl;
24 25 26 27 28 29 30 31 32 33 34
    protected $CASServerCACert;
    protected $CASServerCNValidate;
    protected $noCASServerValidation;
    protected $proxy;
    protected $handleLogoutRequests;
    protected $CASLang;
    protected $allowed1;
    protected $allowed1Value;
    protected $allowed2;
    protected $allowed2Value;

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    protected $entity_query;
    protected $entityTypeManager;
    protected $logger;
    protected $connection;

    public function __construct(
    EntityTypeManagerInterface $entityTypeManager,
    QueryFactory $entity_query,
    Connection $connection,
    LoggerChannelFactoryInterface $loggerChannel)
    {
        $this->entityTypeManager = $entityTypeManager;
        $this->entity_query = $entity_query;
        $this->connection = $connection;
        $this->logger = $loggerChannel->get('casost');
    }

    public static function create(ContainerInterface $container)
    {
        return new static(
          $container->get('entity.manager'),
          $container->get('entity.query'),
          $container->get('database'),
          $container->get('logger.factory')
      );
    }

    public function logoutGo(Request $request)
    {
64
        $configRowName = 'casost_sch_sso_config';
65
        try {
66

67 68 69 70 71 72 73 74 75 76 77 78 79
            $configRowId = $request->query->get('config');
            if ($configRowId) {
                $configRowName = $configRowName.'_'.$configRowId;
            }
            $CASOSTConfigs = $this->entityTypeManager->getStorage('casost_config')->loadByProperties(array('name' => $configRowName));
            $CASOSTConfig = reset($CASOSTConfigs);
            if ($CASOSTConfig) {
                $this->serverVersion = $CASOSTConfig->serverversion->value;
                $this->serverHostname = $CASOSTConfig->serverhostname->value;
                $this->serverPort = $CASOSTConfig->serverport->value;
                $this->serverUri = $CASOSTConfig->serveruri->value === null ? '' : $CASOSTConfig->serveruri->value;
                $this->redirectUrl = $CASOSTConfig->redirecturl->value;
                $this->changeSessionId = $CASOSTConfig->changesessionid->value;
80
                $this->logoutRedirectUrl = $CASOSTConfig->logoutredirecturl->value;
81 82 83 84 85 86 87 88 89 90 91
                $this->CASServerCACert = $CASOSTConfig->casservercacert->value;
                $this->CASServerCNValidate = $CASOSTConfig->casservercnvalidate->value;
                $this->noCASServerValidation = $CASOSTConfig->nocasservervalidation->value;
                $this->proxy = $CASOSTConfig->proxy->value;
                $this->handleLogoutRequests = $CASOSTConfig->handlelogoutrequests->value;
                $this->CASLang = $CASOSTConfig->caslang->value;
                $this->allowed1 = $CASOSTConfig->allowed1->value;
                $this->allowed1Value = $CASOSTConfig->allowed1value->value;
                $this->allowed2 = $CASOSTConfig->allowed2->value;
                $this->allowed2Value = $CASOSTConfig->allowed2value->value;
            } else {
92
                return $this->redirectForbidden($configRowName, '7001');
93
            }
94 95

            // Enable debugging
96
            phpCAS::setDebug("phpcas.log");
97
            // Enable verbose error messages. Disable in production!
Open Source Developer's avatar
trans  
Open Source Developer committed
98
           // phpCAS::setVerbose(true);
99 100

            // Initialize phpCAS
101 102
            phpCAS::client(
                $this->serverVersion,
103 104 105
                $this->serverHostname,
                intval($this->serverPort),
                $this->serverUri,
106 107
                boolval($this->changeSessionId)
            );
108 109 110 111 112 113

            $authToken = $request->headers->get('PHP_AUTH_USER');
            $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
            $user = reset($users);

            if (!$user) {
114
                return $this->redirectForbidden($configRowName, '7002');
115 116 117
            }
            $user->setPassword(uniqid('pw'));
            $user->save();
118

119
            $response = new Response();
120
            $response->setContent("{\"message\": \"Server logout successful\",\"next\": \"{$this->logoutRedirectUrl}\"}");
121 122
            $response->setStatusCode(Response::HTTP_OK);
            $response->headers->set('Content-Type', 'application/json');
123

124 125
            session_unset();
            session_destroy();
126
            \Drupal::service('page_cache_kill_switch')->trigger();
127 128 129 130 131
            // phpCAS::logout(array('service' => 'http://eduslim2.minedu.gov.gr/dist/#/school'));
            // phpCAS::logoutWithRedirectService('http://eduslim2.minedu.gov.gr/dist/#/school');
            // phpCAS::handleLogoutRequests();

            session_start();
132
            return $response;
133 134
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
135 136 137
            return $this->redirectForbidden($configRowName, '8000');
        }
    }
138

139 140 141 142 143 144 145 146
    private function redirectForbidden($configRowName, $errorCode) {
        session_unset();
        session_destroy();
        \Drupal::service('page_cache_kill_switch')->trigger();
        if ('casost_sch_sso_config' === $configRowName) {
            return new RedirectResponse($this->redirectUrl.'&error_code=' . $errorCode, 302, []);
        } else {
            return new RedirectResponseWithCookieExt($this->redirectUrl .'&error_code=' . $errorCode, 302, []);
147
        }
148
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
149
}