|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
4
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace midgard\portable\api; |
|
9
|
|
|
|
|
10
|
|
|
use midgard\portable\storage\connection; |
|
11
|
|
|
use midgard\portable\storage\objectmanager; |
|
12
|
|
|
use midgard\portable\api\error\exception; |
|
13
|
|
|
use midgard_connection; |
|
14
|
|
|
|
|
15
|
|
|
class user extends dbobject |
|
16
|
|
|
{ |
|
17
|
|
|
private $person_object; |
|
18
|
|
|
|
|
19
|
|
|
protected $id; |
|
20
|
|
|
|
|
21
|
|
|
protected $properties = []; |
|
22
|
|
|
|
|
23
|
|
|
protected $person; |
|
24
|
|
|
|
|
25
|
|
|
protected $guid = ''; |
|
26
|
|
|
|
|
27
|
|
|
protected $login = ''; |
|
28
|
|
|
|
|
29
|
|
|
protected $password = ''; |
|
30
|
|
|
|
|
31
|
|
|
protected $active = false; |
|
32
|
|
|
|
|
33
|
|
|
protected $authtype = ''; |
|
34
|
|
|
|
|
35
|
|
|
protected $authtypeid = 0; |
|
36
|
|
|
|
|
37
|
|
|
protected $usertype = 0; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(array $properties = []) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!empty($properties)) { |
|
42
|
|
|
$this->load_by_properties($properties); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function __set($field, $value) |
|
47
|
22 |
|
{ |
|
48
|
|
|
if ($field == 'guid') { |
|
49
|
22 |
|
return; |
|
50
|
5 |
|
} |
|
51
|
3 |
|
parent::__set($field, $value); |
|
52
|
22 |
|
} |
|
53
|
|
|
|
|
54
|
20 |
|
private function load_by_properties(array $properties) |
|
55
|
|
|
{ |
|
56
|
20 |
|
if ( !array_key_exists('authtype', $properties) |
|
57
|
1 |
|
|| !array_key_exists('login', $properties)) { |
|
58
|
|
|
throw exception::invalid_property_value(); |
|
59
|
20 |
|
} |
|
60
|
20 |
|
$entity = connection::get_em()->getRepository('midgard:midgard_user')->findOneBy($properties); |
|
61
|
|
|
|
|
62
|
5 |
|
if ($entity === null) { |
|
63
|
|
|
throw exception::not_exists(); |
|
64
|
5 |
|
} |
|
65
|
5 |
|
$this->populate_from_entity($entity); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
public function login() |
|
69
|
|
|
{ |
|
70
|
4 |
|
if (empty($this->id)) { |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
3 |
|
connection::set_user($this); |
|
74
|
3 |
|
return true; |
|
75
|
|
|
} |
|
76
|
8 |
|
|
|
77
|
|
|
public function logout() |
|
78
|
8 |
|
{ |
|
79
|
1 |
|
if (empty($this->id)) { |
|
80
|
|
|
return false; |
|
81
|
8 |
|
} |
|
82
|
8 |
|
connection::set_user(null); |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
1 |
|
|
|
86
|
|
|
public function is_admin() |
|
87
|
1 |
|
{ |
|
88
|
1 |
|
return $this->usertype == 2; |
|
89
|
|
|
} |
|
90
|
1 |
|
|
|
91
|
1 |
|
public function set_person(person $person) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->person_object = $person; |
|
94
|
|
|
$this->person = $person->guid; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function &get_person() |
|
98
|
|
|
{ |
|
99
|
1 |
|
if ( $this->person_object === null |
|
100
|
|
|
&& $this->person !== null) { |
|
101
|
1 |
|
$this->person_object = connection::get_em()->getRepository('midgard:midgard_person')->findOneBy(['guid' => $this->person]); |
|
102
|
|
|
} |
|
103
|
|
|
return $this->person_object; |
|
104
|
9 |
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
public function create() |
|
107
|
9 |
|
{ |
|
108
|
9 |
|
if ( empty($this->authtype) |
|
109
|
|
|
|| !empty($this->id)) { |
|
110
|
1 |
|
exception::invalid_property_value(); |
|
111
|
|
|
return false; |
|
112
|
1 |
|
} |
|
113
|
1 |
|
if (!$this->is_unique()) { |
|
114
|
1 |
|
exception::duplicate(); |
|
115
|
1 |
|
return false; |
|
116
|
1 |
|
} |
|
117
|
|
|
$this->guid = connection::generate_guid(); |
|
118
|
|
|
try { |
|
119
|
19 |
|
$om = new objectmanager(connection::get_em()); |
|
120
|
|
|
$om->create($this); |
|
121
|
19 |
|
} catch (\Exception $e) { |
|
122
|
19 |
|
exception::internal($e); |
|
123
|
1 |
|
return false; |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
19 |
|
midgard_connection::get_instance()->set_error(MGD_ERR_OK); |
|
127
|
1 |
|
return !empty($this->id); |
|
128
|
1 |
|
} |
|
129
|
|
|
|
|
130
|
19 |
|
public function update() |
|
131
|
|
|
{ |
|
132
|
19 |
|
if (empty($this->id) || !mgd_is_guid($this->guid)) { |
|
133
|
19 |
|
exception::invalid_property_value(); |
|
134
|
19 |
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
if (!$this->is_unique()) { |
|
137
|
|
|
exception::duplicate(); |
|
138
|
|
|
return false; |
|
139
|
19 |
|
} |
|
140
|
19 |
|
try { |
|
141
|
|
|
$om = new objectmanager(connection::get_em()); |
|
142
|
|
|
$om->update($this); |
|
143
|
1 |
|
} catch (\Exception $e) { |
|
144
|
|
|
exception::internal($e); |
|
145
|
1 |
|
return false; |
|
146
|
1 |
|
} |
|
147
|
1 |
|
midgard_connection::get_instance()->set_error(MGD_ERR_OK); |
|
148
|
|
|
return true; |
|
149
|
1 |
|
} |
|
150
|
1 |
|
|
|
151
|
1 |
|
public function delete() |
|
152
|
|
|
{ |
|
153
|
|
|
if (!mgd_is_guid($this->guid)) { |
|
154
|
1 |
|
exception::invalid_property_value(); |
|
155
|
1 |
|
return false; |
|
156
|
1 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
try { |
|
159
|
|
|
$om = new objectmanager(connection::get_em()); |
|
160
|
1 |
|
$om->purge($this); |
|
161
|
1 |
|
} catch (\Exception $e) { |
|
162
|
|
|
exception::internal($e); |
|
163
|
|
|
return false; |
|
164
|
1 |
|
} |
|
165
|
|
|
$this->guid = ''; |
|
166
|
1 |
|
midgard_connection::get_instance()->set_error(MGD_ERR_OK); |
|
167
|
1 |
|
return true; |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function is_unique() |
|
171
|
|
|
{ |
|
172
|
1 |
|
if ( empty($this->login) |
|
173
|
1 |
|
|| empty($this->authtype)) { |
|
174
|
1 |
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$qb = connection::get_em()->createQueryBuilder(); |
|
178
|
1 |
|
$qb->from(get_class($this), 'c'); |
|
179
|
1 |
|
$conditions = $qb->expr()->andX(); |
|
180
|
1 |
|
$parameters = [ |
|
181
|
|
|
'login' => $this->login, |
|
182
|
|
|
'authtype' => $this->authtype |
|
183
|
19 |
|
]; |
|
184
|
|
|
|
|
185
|
19 |
|
if ($this->id) { |
|
186
|
19 |
|
$parameters['id'] = $this->id; |
|
187
|
13 |
|
$conditions->add($qb->expr()->neq('c.id', ':id')); |
|
188
|
|
|
} |
|
189
|
|
|
$conditions->add($qb->expr()->eq('c.login', ':login')); |
|
190
|
8 |
|
$conditions->add($qb->expr()->eq('c.authtype', ':authtype')); |
|
191
|
8 |
|
|
|
192
|
8 |
|
$qb->where($conditions) |
|
193
|
|
|
->setParameters($parameters); |
|
194
|
8 |
|
|
|
195
|
8 |
|
$qb->select("count(c)"); |
|
196
|
8 |
|
$count = (int) $qb->getQuery()->getSingleScalarResult(); |
|
197
|
|
|
|
|
198
|
8 |
|
return $count === 0; |
|
199
|
1 |
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|