Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
62.65% |
52 / 83 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
Useraccount | |
62.65% |
52 / 83 |
|
63.64% |
7 / 11 |
43.98 | |
0.00% |
0 / 1 |
getEntityMapping | |
100.00% |
18 / 18 |
|
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 / 18 |
|
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 / 10 |
|
0.00% |
0 / 1 |
6 | |||
reverseEntityMapping | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
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 | 'email' => 'useraccount.email', |
64 | 'lastLogin' => 'useraccount.lastUpdate', |
65 | 'rights__superuser' => self::expression('`useraccount`.`Berechtigung` = 90'), |
66 | 'rights__organisation' => self::expression('`useraccount`.`Berechtigung` >= 70'), |
67 | 'rights__department' => self::expression('`useraccount`.`Berechtigung` >= 50'), |
68 | 'rights__cluster' => self::expression('`useraccount`.`Berechtigung` >= 40'), |
69 | 'rights__useraccount' => self::expression('`useraccount`.`Berechtigung` >= 40'), |
70 | 'rights__scope' => self::expression('`useraccount`.`Berechtigung` >= 30'), |
71 | 'rights__departmentStats' => self::expression('`useraccount`.`Berechtigung` >= 25'), |
72 | 'rights__availability' => self::expression('`useraccount`.`Berechtigung` >= 20'), |
73 | 'rights__ticketprinter' => self::expression('`useraccount`.`Berechtigung` >= 15'), |
74 | 'rights__sms' => self::expression('`useraccount`.`Berechtigung` >= 10'), |
75 | 'rights__audit' => self::expression('`useraccount`.`Berechtigung` = 5 OR `useraccount`.`Berechtigung` = 90'), |
76 | 'rights__basic' => self::expression('`useraccount`.`Berechtigung` >= 0'), |
77 | ]; |
78 | } |
79 | |
80 | public function addConditionLoginName($loginName) |
81 | { |
82 | $this->query->where('useraccount.Name', '=', $loginName); |
83 | return $this; |
84 | } |
85 | |
86 | public function addConditionUserId($userId) |
87 | { |
88 | $this->query->where('useraccount.NutzerID', '=', $userId); |
89 | return $this; |
90 | } |
91 | |
92 | public function addConditionPassword($password) |
93 | { |
94 | $this->query->where('useraccount.Passworthash', '=', $password); |
95 | return $this; |
96 | } |
97 | |
98 | public function addConditionXauthKey($xAuthKey) |
99 | { |
100 | $this->query->where('useraccount.SessionID', '=', $xAuthKey); |
101 | $this->query->where('useraccount.SessionExpiry', '>', date('Y-m-d H:i:s', time() - App::SESSION_DURATION)); |
102 | return $this; |
103 | } |
104 | |
105 | public function addConditionDepartmentAndSearch($departmentId, $queryString = null, $orWhere = false) |
106 | { |
107 | |
108 | $this->leftJoin( |
109 | new Alias(static::TABLE_ASSIGNMENT, 'useraccount_department'), |
110 | 'useraccount.NutzerID', |
111 | '=', |
112 | 'useraccount_department.nutzerid' |
113 | ); |
114 | |
115 | $this->query->where('useraccount_department.behoerdenid', '=', $departmentId); |
116 | |
117 | if ($queryString) { |
118 | $condition = function (\BO\Zmsdb\Query\Builder\ConditionBuilder $query) use ($queryString) { |
119 | $queryString = trim($queryString); |
120 | $query->orWith('useraccount.NutzerID', 'LIKE', "%$queryString%"); |
121 | $query->orWith('useraccount.Name', 'LIKE', "%$queryString%"); |
122 | $query->orWith('useraccount.email', 'LIKE', "%$queryString%"); |
123 | }; |
124 | |
125 | if ($orWhere) { |
126 | $this->query->orWhere($condition); |
127 | } else { |
128 | $this->query->where($condition); |
129 | } |
130 | } |
131 | |
132 | return $this; |
133 | } |
134 | |
135 | public function addConditionRoleLevel($roleLevel) |
136 | { |
137 | $this->query->where('useraccount.Berechtigung', '=', $roleLevel); |
138 | return $this; |
139 | } |
140 | |
141 | public function addConditionDepartmentId($departmentId) |
142 | { |
143 | $this->leftJoin( |
144 | new Alias(static::TABLE_ASSIGNMENT, 'useraccount_department'), |
145 | 'useraccount.NutzerID', |
146 | '=', |
147 | 'useraccount_department.nutzerid' |
148 | ); |
149 | $this->query->where('useraccount_department.behoerdenid', '=', $departmentId); |
150 | return $this; |
151 | } |
152 | |
153 | public function addConditionSearch($queryString, $orWhere = false) |
154 | { |
155 | $condition = function (\BO\Zmsdb\Query\Builder\ConditionBuilder $query) use ($queryString) { |
156 | $queryString = trim($queryString); |
157 | $query->orWith('useraccount.NutzerID', 'LIKE', "%$queryString%"); |
158 | $query->orWith('useraccount.Name', 'LIKE', "%$queryString%"); |
159 | $query->orWith('useraccount.email', 'LIKE', "%$queryString%"); |
160 | }; |
161 | if ($orWhere) { |
162 | $this->query->orWhere($condition); |
163 | } else { |
164 | $this->query->where($condition); |
165 | } |
166 | return $this; |
167 | } |
168 | |
169 | public function reverseEntityMapping(\BO\Zmsentities\Useraccount $entity) |
170 | { |
171 | $data = array(); |
172 | $data['Name'] = $entity->id; |
173 | $data['email'] = (isset($entity->email)) ? $entity->email : null; |
174 | $data['Passworthash'] = (isset($entity->password)) ? $entity->password : null; |
175 | $data['Berechtigung'] = $entity->getRightsLevel(); |
176 | $data['BehoerdenID'] = 0; |
177 | if (!$entity->isSuperUser() && isset($entity->departments) && 0 < $entity->departments->count()) { |
178 | $data['BehoerdenID'] = $entity->departments->getFirst()->id; |
179 | } |
180 | //default values because of strict mode |
181 | $data['notrufinitiierung'] = 0; |
182 | $data['notrufantwort'] = 0; |
183 | |
184 | $data = array_filter($data, function ($value) { |
185 | return ($value !== null && $value !== false); |
186 | }); |
187 | return $data; |
188 | } |
189 | |
190 | public function postProcess($data) |
191 | { |
192 | $data[$this->getPrefixed("lastLogin")] = ('0000-00-00' != $data[$this->getPrefixed("lastLogin")]) ? |
193 | strtotime($data[$this->getPrefixed("lastLogin")]) : |
194 | null; |
195 | return $data; |
196 | } |
197 | } |