Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.73% covered (warning)
88.73%
63 / 71
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Index
88.73% covered (warning)
88.73%
63 / 71
33.33% covered (danger)
33.33%
1 / 3
16.37
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
5
 testLogin
74.07% covered (warning)
74.07%
20 / 27
0.00% covered (danger)
0.00%
0 / 1
6.63
 getProviderList
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
1<?php
2
3/**
4 * @package Zmsstatistic
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic;
9
10use BO\Zmsentities\Workstation;
11
12class Index extends BaseController
13{
14    protected $withAccess = false;
15
16    /**
17     * @SuppressWarnings(Param)
18     * @return String
19     */
20    public function readResponse(
21        \Psr\Http\Message\RequestInterface $request,
22        \Psr\Http\Message\ResponseInterface $response,
23        array $args
24    ) {
25        try {
26            $workstation = \App::$http->readGetResult('/workstation/')->getEntity();
27        } catch (\Exception $workstationexception) {
28            $workstation = null;
29        }
30
31        $config = \App::$http->readGetResult('/config/', [], \App::CONFIG_SECURE_TOKEN)->getEntity();
32        $input = $request->getParsedBody();
33        $oidclogin = $request->getAttribute('validator')->getParameter('oidclogin')->isString()->getValue();
34        if ($request->getMethod() === 'POST') {
35            $loginData = $this->testLogin($input);
36            if ($loginData instanceof Workstation && $loginData->offsetExists('authkey')) {
37                \BO\Zmsclient\Auth::setKey($loginData->authkey);
38                return \BO\Slim\Render::redirect('workstationSelect', array(), array());
39            }
40
41            return \BO\Slim\Render::withHtml(
42                $response,
43                'page/index.twig',
44                array(
45                    'title' => 'Anmeldung gescheitert',
46                    'loginfailed' => true,
47                    'workstation' => null,
48                    'exception' => $loginData,
49                    'oidcproviderlist' => $this->getProviderList($config),
50                    'oidclogin' => $oidclogin,
51                    'showloginform' => (! $oidclogin)
52                )
53            );
54        } else {
55            return \BO\Slim\Render::withHtml(
56                $response,
57                'page/index.twig',
58                array(
59                    'title' => 'Anmeldung',
60                    'config' => $config,
61                    'workstation' => $workstation,
62                    'oidcproviderlist' => $this->getProviderList($config),
63                    'oidclogin' => $oidclogin,
64                    'showloginform' => (! $oidclogin)
65                )
66            );
67        }
68    }
69
70    protected function testLogin($input)
71    {
72        $userAccount = new \BO\Zmsentities\Useraccount(array(
73            'id' => $input['loginName'],
74            'password' => $input['password'],
75            'departments' => array('id' => 0) // required in schema validation
76        ));
77        try {
78            /** @var \BO\Zmsentities\Workstation $workstation */
79            $workstation = \App::$http->readPostResult('/workstation/login/', $userAccount)->getEntity();
80            return $workstation;
81        } catch (\BO\Zmsclient\Exception $exception) {
82            $template = Helper\TwigExceptionHandler::getExceptionTemplate($exception);
83            if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) {
84                $exceptionData = [
85                  'template' => 'exception/bo/zmsapi/exception/useraccount/invalidcredentials.twig'
86                ];
87                $exceptionData['data']['password']['messages'] = [
88                    'Der Nutzername oder das Passwort wurden falsch eingegeben'
89                ];
90            } elseif ('BO\Zmsapi\Exception\Useraccount\UserAlreadyLoggedIn' == $exception->template) {
91                \BO\Zmsclient\Auth::setKey($exception->data['authkey'], time() + \App::SESSION_DURATION);
92                throw $exception;
93            } elseif (
94                '' != $exception->template
95                && \App::$slim->getContainer()->get('view')->getLoader()->exists($template)
96            ) {
97                $exceptionData = [
98                  'template' => $template,
99                  'data' => $exception->data
100                ];
101            } else {
102                throw $exception;
103            }
104        }
105        return $exceptionData;
106    }
107    protected function getProviderList($config)
108    {
109        $allowedProviderList = explode(',', $config->getPreference('oidc', 'provider'));
110        $oidcproviderlist = [];
111        foreach (\BO\Slim\Middleware\OAuthMiddleware::$authInstances as $provider => $authInstance) {
112            if (
113                0 < count($allowedProviderList) &&
114                class_exists($authInstance) &&
115                in_array($provider, $allowedProviderList)
116            ) {
117                $oidcproviderlist[] = $provider;
118            }
119        }
120        return $oidcproviderlist;
121    }
122}