CASLogout.php 5.46 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
15
16
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use phpCAS;
17
18
19

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

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
64
    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)
    {
65
66
67
68
69
70
71
72
        try {
        $CASOSTConfigs = $this->entityTypeManager->getStorage('casost_config')->loadByProperties(array('name' => 'casost_sch_sso_config'));
        $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;
73
            $this->redirectUrl = $CASOSTConfig->redirecturl->value;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
            $this->changeSessionId = $CASOSTConfig->changesessionid->value;
            $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 {
            $response = new Response();
            $response->setContent('forbidden. No config');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }



            // Enable debugging
//            phpCAS::setDebug("/home/haris/devel/eepal/drupal/modules/casost/phpcas.log");
            // Enable verbose error messages. Disable in production!
Open Source Developer's avatar
paging    
Open Source Developer committed
98
   //         phpCAS::setVerbose(true);
99
100
101
102
103
104
105
106
107
108
109
110
111
112

            // Initialize phpCAS
            phpCAS::client($this->serverVersion,
                $this->serverHostname,
                intval($this->serverPort),
                $this->serverUri,
                boolval($this->changeSessionId));

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

            if (!$user) {
                $this->logger->warning("user not found");
113

114
115
116
117
118
119
120
                $response = new Response();
                $response->setContent('forbidden');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
//            phpCAS::handleLogoutRequests();
121
122
123
//            phpCAS::logoutWithRedirectService('http://eduslim2.minedu.gov.gr/dist/#/school');
            session_unset();
            session_destroy();
124
125
            $user->setPassword(uniqid('pw'));
            $user->save();
126
            $response = new Response();
127
            $response->setContent('logout successful');
128
129
            $response->setStatusCode(Response::HTTP_OK);
            $response->headers->set('Content-Type', 'application/json');
130
131
132
133
134
135
136
            return $response;
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
            $response = new Response();
            $response->setContent('forbidden');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
137
            return $response;
138
        }
139
140
141
    }

}