Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.20% covered (warning)
62.20%
51 / 82
63.64% covered (warning)
63.64%
7 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Useraccount
62.20% covered (warning)
62.20%
51 / 82
63.64% covered (warning)
63.64%
7 / 11
44.83
0.00% covered (danger)
0.00%
0 / 1
 getEntityMapping
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
 addConditionLoginName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionUserId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionPassword
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionXauthKey
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionDepartmentAndSearch
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 addConditionRoleLevel
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 addConditionDepartmentId
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 addConditionSearch
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 reverseEntityMapping
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 postProcess
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2
3namespace BO\Zmsdb\Query;
4
5class Useraccount extends Base implements MappingInterface
6{
7    /**
8     * @var String TABLE mysql table reference
9     */
10    const TABLE = 'nutzer';
11    const TABLE_ASSIGNMENT = 'nutzerzuordnung';
12
13    const QUERY_READ_ID_BY_USERNAME = '
14        SELECT user.`NutzerID` AS id
15        FROM ' . self::TABLE . ' user
16        WHERE
17            user.`Name`=?
18    ';
19
20    const QUERY_WRITE_ASSIGNED_DEPARTMENTS = '
21        REPLACE INTO
22            ' . self::TABLE_ASSIGNMENT . '
23        SET
24            nutzerid=?,
25            behoerdenid=?
26    ';
27
28    const QUERY_DELETE_ASSIGNED_DEPARTMENTS = '
29        DELETE FROM
30            ' . self::TABLE_ASSIGNMENT . '
31        WHERE
32            nutzerid=?
33        ORDER BY behoerdenid
34    ';
35
36    const QUERY_READ_SUPERUSER_DEPARTMENTS = '
37        SELECT behoerde.`BehoerdenID` AS id,
38            organisation.Organisationsname as organisation__name
39        FROM ' . Department::TABLE . '
40        LEFT JOIN ' . Organisation::TABLE . ' USING(OrganisationsID)
41        ORDER BY organisation.Organisationsname, behoerde.Name
42    ';
43
44    const QUERY_READ_ASSIGNED_DEPARTMENTS = '
45        SELECT userAssignment.`behoerdenid` AS id,
46            organisation.Organisationsname as organisation__name
47        FROM ' . self::TABLE_ASSIGNMENT . ' userAssignment
48        LEFT JOIN ' . self::TABLE . ' useraccount ON useraccount.Name = :useraccountName
49        LEFT JOIN ' . Department::TABLE . ' ON userAssignment.behoerdenid = behoerde.BehoerdenID
50        LEFT JOIN ' . Organisation::TABLE . ' USING(OrganisationsID)
51        WHERE
52            useraccount.`NutzerID` = userAssignment.`nutzerid`
53        ORDER BY organisation.Organisationsname, behoerde.Name
54    ';
55
56    public function getEntityMapping()
57    {
58        return [
59            'id' => 'useraccount.Name',
60            'password' => 'useraccount.Passworthash',
61            'email' => 'useraccount.email',
62            'lastLogin' => 'useraccount.lastUpdate',
63            'rights__superuser' => self::expression('`useraccount`.`Berechtigung` = 90'),
64            'rights__organisation' => self::expression('`useraccount`.`Berechtigung` >= 70'),
65            'rights__department' => self::expression('`useraccount`.`Berechtigung` >= 50'),
66            'rights__cluster' => self::expression('`useraccount`.`Berechtigung` >= 40'),
67            'rights__useraccount' => self::expression('`useraccount`.`Berechtigung` >= 40'),
68            'rights__scope' => self::expression('`useraccount`.`Berechtigung` >= 30'),
69            'rights__departmentStats' => self::expression('`useraccount`.`Berechtigung` >= 25'),
70            'rights__availability' => self::expression('`useraccount`.`Berechtigung` >= 20'),
71            'rights__ticketprinter' => self::expression('`useraccount`.`Berechtigung` >= 15'),
72            'rights__sms' => self::expression('`useraccount`.`Berechtigung` >= 10'),
73            'rights__audit' => self::expression('`useraccount`.`Berechtigung` = 5 OR `useraccount`.`Berechtigung` = 90'),
74            'rights__basic' => self::expression('`useraccount`.`Berechtigung` >= 0'),
75        ];
76    }
77
78    public function addConditionLoginName($loginName)
79    {
80        $this->query->where('useraccount.Name', '=', $loginName);
81        return $this;
82    }
83
84    public function addConditionUserId($userId)
85    {
86        $this->query->where('useraccount.NutzerID', '=', $userId);
87        return $this;
88    }
89
90    public function addConditionPassword($password)
91    {
92        $this->query->where('useraccount.Passworthash', '=', $password);
93        return $this;
94    }
95
96    public function addConditionXauthKey($xAuthKey)
97    {
98        $this->query->where('useraccount.SessionID', '=', $xAuthKey);
99        return $this;
100    }
101
102    public function addConditionDepartmentAndSearch($departmentId, $queryString = null, $orWhere = false)
103    {
104
105        $this->leftJoin(
106            new Alias(static::TABLE_ASSIGNMENT, 'useraccount_department'),
107            'useraccount.NutzerID',
108            '=',
109            'useraccount_department.nutzerid'
110        );
111
112        $this->query->where('useraccount_department.behoerdenid', '=', $departmentId);
113
114        if ($queryString) {
115            $condition = function (\BO\Zmsdb\Query\Builder\ConditionBuilder $query) use ($queryString) {
116                $queryString = trim($queryString);
117                $query->orWith('useraccount.NutzerID', 'LIKE', "%$queryString%");
118                $query->orWith('useraccount.Name', 'LIKE', "%$queryString%");
119                $query->orWith('useraccount.email', 'LIKE', "%$queryString%");
120            };
121
122            if ($orWhere) {
123                $this->query->orWhere($condition);
124            } else {
125                $this->query->where($condition);
126            }
127        }
128
129        return $this;
130    }
131
132    public function addConditionRoleLevel($roleLevel)
133    {
134        $this->query->where('useraccount.Berechtigung', '=', $roleLevel);
135        return $this;
136    }
137
138    public function addConditionDepartmentId($departmentId)
139    {
140        $this->leftJoin(
141            new Alias(static::TABLE_ASSIGNMENT, 'useraccount_department'),
142            'useraccount.NutzerID',
143            '=',
144            'useraccount_department.nutzerid'
145        );
146        $this->query->where('useraccount_department.behoerdenid', '=', $departmentId);
147        return $this;
148    }
149
150    public function addConditionSearch($queryString, $orWhere = false)
151    {
152        $condition = function (\BO\Zmsdb\Query\Builder\ConditionBuilder $query) use ($queryString) {
153            $queryString = trim($queryString);
154            $query->orWith('useraccount.NutzerID', 'LIKE', "%$queryString%");
155            $query->orWith('useraccount.Name', 'LIKE', "%$queryString%");
156            $query->orWith('useraccount.email', 'LIKE', "%$queryString%");
157        };
158        if ($orWhere) {
159            $this->query->orWhere($condition);
160        } else {
161            $this->query->where($condition);
162        }
163        return $this;
164    }
165
166    public function reverseEntityMapping(\BO\Zmsentities\Useraccount $entity)
167    {
168        $data = array();
169        $data['Name'] = $entity->id;
170        $data['email'] = (isset($entity->email)) ? $entity->email : null;
171        $data['Passworthash'] = (isset($entity->password)) ? $entity->password : null;
172        $data['Berechtigung'] = $entity->getRightsLevel();
173        $data['BehoerdenID'] = 0;
174        if (!$entity->isSuperUser() && isset($entity->departments) && 0 < $entity->departments->count()) {
175            $data['BehoerdenID'] = $entity->departments->getFirst()->id;
176        }
177        //default values because of strict mode
178        $data['notrufinitiierung'] = 0;
179        $data['notrufantwort'] = 0;
180
181        $data = array_filter($data, function ($value) {
182            return ($value !== null && $value !== false);
183        });
184        return $data;
185    }
186
187    public function postProcess($data)
188    {
189        $data[$this->getPrefixed("lastLogin")] = ('0000-00-00' != $data[$this->getPrefixed("lastLogin")]) ?
190            strtotime($data[$this->getPrefixed("lastLogin")]) :
191            null;
192        return $data;
193    }
194}