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