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