Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.84% covered (warning)
54.84%
34 / 62
80.00% covered (warning)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Access
54.84% covered (warning)
54.84%
34 / 62
80.00% covered (warning)
80.00%
8 / 10
140.48
0.00% covered (danger)
0.00%
0 / 1
 initAccessRights
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 readWorkstation
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 readDepartment
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readOrganisation
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readOwner
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 validateAccessRights
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validateAccess
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
7
 validateScope
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
4.59
 isPathWithoutScope
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 testLogin
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3/**
4 *
5 * @package zmsstatistic
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsstatistic\Helper;
11
12use BO\Zmsclient\Auth;
13use BO\Zmsentities\Exception\UserAccountAccessRightsFailed;
14use BO\Zmsentities\Exception\WorkstationMissingScope;
15use BO\Zmsentities\Useraccount;
16use BO\Zmsentities\Workstation;
17use Psr\Http\Message\RequestInterface;
18
19class Access extends \BO\Slim\Controller
20{
21    protected $workstation = null;
22
23    protected $organisation = null;
24
25    protected $department = null;
26
27    protected $resolveLevel = 2;
28
29    protected $withAccess = true;
30
31    protected $owner = null;
32
33    protected function initAccessRights(RequestInterface $request): void
34    {
35        $this->workstation = $this->readWorkstation();
36        if ($this->workstation && isset($this->workstation->scope['id']) && $this->workstation->scope['id'] > 0) {
37            $this->department = $this->readDepartment();
38            $this->organisation = $this->readOrganisation();
39            $this->owner = $this->readOwner();
40        }
41        $this->validateAccessRights($request);
42    }
43
44    protected function readWorkstation()
45    {
46        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => $this->resolveLevel]);
47        return ($workstation) ? $workstation->getEntity() : null;
48    }
49
50    protected function readDepartment()
51    {
52        if ($this->workstation->getUseraccount()->hasPermissions(['statistic'])) {
53            return \App::$http
54                ->readGetResult('/scope/' . $this->workstation->scope['id'] . '/department/')
55                ->getEntity();
56        }
57    }
58
59    protected function readOrganisation()
60    {
61        if ($this->workstation->getUseraccount()->isSuperUser()) {
62            return \App::$http
63                ->readGetResult('/department/' . $this->department->getId() . '/organisation/')
64                ->getEntity();
65        }
66    }
67
68    protected function readOwner()
69    {
70        if ($this->workstation->getUseraccount()->isSuperUser()) {
71            return \App::$http
72                ->readGetResult('/organisation/' . $this->organisation->getId() . '/owner/')
73                ->getEntity();
74        }
75    }
76
77    protected function validateAccessRights(RequestInterface $request): void
78    {
79        $path = $request->getUri()->getPath();
80        $this->validateAccess($path);
81        $this->validateScope($path);
82    }
83
84    protected function validateAccess(string $path): void
85    {
86        if (
87            (false !== strpos($path, 'owner') && ! $this->owner) ||
88            (false !== strpos($path, 'organisation') && ! $this->organisation) ||
89            (false !== strpos($path, 'department') && ! $this->department)
90        ) {
91            throw new UserAccountAccessRightsFailed();
92        }
93    }
94
95    protected function validateScope(string $path): void
96    {
97        if (
98            $this->isPathWithoutScope($path)
99            && (! isset($this->workstation['scope']) || ! isset($this->workstation['scope']['id']))
100        ) {
101            throw new WorkstationMissingScope();
102        }
103    }
104
105    protected function isPathWithoutScope(string $path): bool
106    {
107        // TODO: refactor to integrate these access rules in the controller to make them visible
108        return (false === strpos($path, 'select')
109            && false === strpos($path, 'warehouse')
110            && false === strpos($path, 'logout')
111            && false === strpos($path, 'report')
112        );
113    }
114
115    /**
116     * @return (mixed|string|string[][][])[]|Workstation
117     *
118     */
119    protected function testLogin($input)
120    {
121        $userAccount = new Useraccount(array(
122            'id' => $input['loginName'],
123            'password' => $input['password'],
124            'departments' => array('id' => 0) // required in schema validation
125        ));
126        try {
127            /** @var Workstation $workstation */
128            $workstation = \App::$http->readPostResult('/workstation/login/', $userAccount)->getEntity();
129            return $workstation;
130        } catch (\BO\Zmsclient\Exception $exception) {
131            $template = TwigExceptionHandler::getExceptionTemplate($exception);
132            if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) {
133                $exceptionData = [
134                  'template' => 'exception/bo/zmsbackend/useraccount/exception/invalidcredentials.twig'
135                ];
136                $exceptionData['data']['password']['messages'] = [
137                    'Der Nutzername oder das Passwort wurden falsch eingegeben'
138                ];
139            } elseif ('BO\Zmsbackend\Useraccount\Exception\UserAlreadyLoggedIn' == $exception->template) {
140                Auth::setKey($exception->data['authkey'], time() + \App::SESSION_DURATION);
141                throw $exception;
142            } elseif (
143                '' != $exception->template
144                && \App::$slim->getContainer()->get('view')->getLoader()->exists($template)
145            ) {
146                $exceptionData = [
147                  'template' => $template,
148                  'data' => $exception->data
149                ];
150            } else {
151                throw $exception;
152            }
153        }
154        return $exceptionData;
155    }
156}