initial commit
[namibia] / module / User / src / User / Entity / Profile.php
1 <?php
2 namespace User\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * A system user.
10  *
11  * @ORM\Entity
12  * @ORM\HasLifecycleCallbacks
13  * @ORM\Table(name="profile", uniqueConstraints={@ORM\UniqueConstraint(name="unique_username", columns={"email"})})
14  */
15 class Profile
16 {
17
18         /**
19          * Can archive records.
20          */
21         const ARCHIVE = true;
22         /**
23          * Pull Synchronization Strategy for this table.
24          */
25         const PULL_SYNCH_STRATEGY = false;
26         /**
27          * Push Synchronization Strategy for this table.
28          */
29         const PUSH_SYNCH_STRATEGY = false;
30         /**
31          * Post update action must be called after existing entity is flushed to database.
32          */
33         const HAVE_POST_UPDATE = true;
34
35         /**
36          * Valid position values.
37          */
38         const POSITION_MANAGER  = 'Manager';
39         const POSITION_VALUATOR = 'Valuator';
40         const POSITION_SALES    = 'Sales';
41
42         /**
43          * Valid usertype values.
44          */
45         const USERTYPE_ADMIN    = 'Administrator';
46         const USERTYPE_B4C      = 'B4C User';
47         const USERTYPE_GROUP    = 'Group User';
48         const USERTYPE_DIVISION = 'Dealer Principle';
49         const USERTYPE_USER     = 'Dealership User';
50
51         /**
52          * @ORM\Id
53          * @ORM\Column(type="integer");
54          * @ORM\GeneratedValue(strategy="AUTO")
55          */
56         protected $id;
57
58         /**
59          * @ORM\Column(type="string", length=255, unique=true)
60          */
61         protected $email;
62
63         /**
64          * @ORM\Column(type="string", length=42)
65          */
66         protected $password;
67
68         /**
69          * @ORM\Column(type="string", length=42, name="password_salt")
70          */
71         protected $salt;
72
73         /**
74          * @ORM\Column(type="string", length=100, name="first_name")
75          */
76         protected $firstName;
77
78         /**
79          * @ORM\Column(type="string", length=100, name="family_name")
80          */
81         protected $familyName;
82
83         /**
84          * @ORM\Column(type="string", length=13, name="id_number")
85          */
86         protected $idNumber;
87
88         /**
89          * @ORM\Column(type="date", name="date_of_birth")
90          */
91         protected $dateOfBirth;
92
93         /**
94          * @ORM\Column(type="string", length=20)
95          */
96         protected $mobile;
97
98         /**
99          * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
100          * @ORM\JoinColumn(nullable=true, name="company_id")
101          **/
102         protected $company;
103
104         /**
105          * @ORM\Column(type="integer", nullable=true, name="manager_id")
106          **/
107         protected $sudo;
108
109         /**
110          * @ORM\ManyToOne(targetEntity="Profile")
111          * @ORM\JoinColumn(nullable=true, name="manager_profile_id")
112          **/
113         protected $manager;
114
115         /**
116          * @ORM\ManyToOne(targetEntity="\Location\Entity\Region")
117          * @ORM\JoinColumn(nullable=true, name="lib_region_id")
118          **/
119         protected $region;
120
121         /**
122          * @ORM\ManyToOne(targetEntity="\Company\Entity\Group")
123          * @ORM\JoinColumn(nullable=true, name="group_id")
124          **/
125         protected $group;
126
127         /**
128          * @ORM\ManyToOne(targetEntity="\Company\Entity\GroupDivision")
129          * @ORM\JoinColumn(nullable=true, name="group_division_id")
130          **/
131         protected $groupDivision;
132
133         /**
134          * @ORM\Column(type="string", length=25, name="user_type")
135          **/
136         protected $userType = 'Dealership User';
137
138         /**
139          * @ORM\Column(type="string", length=25)
140          **/
141         protected $position = 'Manager';
142
143         /**
144          * @ORM\ManyToOne(targetEntity="Permissions")
145          * @ORM\JoinColumn(name="permission_id")
146          **/
147         protected $permissions;
148
149         /**
150          * @ORM\ManyToOne(targetEntity="Override", cascade={"all"})
151          * @ORM\JoinColumn(name="permission_override_id")
152          **/
153         protected $override;
154
155         /**
156          * @ORM\Column(type="boolean", name="subscribe_newsletter");
157          */
158         protected $subscribeNewsletter = true;
159
160         /**
161          * @ORM\Column(type="boolean", name="subscribe_reminders");
162          */
163         protected $subscribeReminders = true;
164
165         /**
166          * @ORM\Column(type="datetime", nullable=true, name="last_login")
167          */
168         protected $lastLogin;
169
170         /**
171          * @ORM\Column(type="string", nullable=true, length=100, name="ip_address")
172          */
173         protected $ipAddress;
174
175         /**
176          * @ORM\OneToMany(targetEntity="Session", mappedBy="profile", fetch="EXTRA_LAZY")
177          * @ORM\JoinColumn(name="profile_session_id")
178          **/
179         private $sessions;
180   
181   /**
182          * @ORM\OneToMany(targetEntity="AuthenticationLog", mappedBy="profile", cascade={"all"}, fetch="EXTRA_LAZY")
183          **/
184         private $logins;
185
186         /**
187          * @ORM\Column(type="string", length=10, name="status")
188          */
189         protected $jobState = 'Active';
190
191         /**
192          * @ORM\Column(type="boolean",  name="mm_pcentre");
193          */
194         protected $mainMemberPublicCentre = false;
195
196         /**
197          * @ORM\Column(type="datetime");
198          */
199         protected $created;
200
201         /**
202          * @ORM\Column(type="datetime", nullable=true);
203          */
204         protected $updated;
205
206         /**
207          * @ORM\Column(type="boolean");
208          */
209         protected $archived = false;
210
211
212
213         /**
214          * Initialize collections.
215          */
216         public function __construct()
217         {
218                 $this->sessions = new \Doctrine\Common\Collections\ArrayCollection();
219     $this->logins = new \Doctrine\Common\Collections\ArrayCollection();
220         }
221
222         /**
223          * Add a new Session to this Profile.
224          * @param Region $region
225          * @return \User\Entity\Profile
226          */
227         public function addSession(Session $session)
228         {
229                 $this->sessions[] = $session;
230                 return $this;
231         }
232
233         public function getId()
234         {
235                 return $this->id;
236         }
237
238         /**
239          * Magic getter to expose protected properties.
240          *
241          * @param string $property
242          * @return mixed
243          */
244         public function __get($property)
245         {
246                 return $this->$property;
247         }
248
249         /**
250          * Magic setter to save protected properties.
251          * @param string $property
252          * @param mixed $value
253          */
254         public function __set($property, $value)
255         {
256                 if ('password' == $property)
257                 {
258                         $this->salt = sha1(mt_rand(1000000000, 9999999999));
259                         $this->password = sha1(sha1($value) . 'Salt' . $this->salt);
260                         return;
261                 }
262                 $this->$property = $value;
263         }
264
265         /**
266          * @ORM\PrePersist
267          */
268         public function setCreateTime()
269         {
270                 $company = \Utility\Registry::resolveCompanyContext(
271                         !is_null($this->company)
272                                 ? $this->company->id
273                                 : null
274                 );
275                 $this->company = is_object($company)
276                         ? $company
277                         : $this->company;
278                 if (is_null($this->password))
279                 {
280                         $password = mt_rand(1000, 9999);
281                         \Utility\Registry::setOnce('NewUser.Pin', $password);
282                         $this->salt = sha1(mt_rand(1000000000, 9999999999));
283                         $this->password = sha1(sha1($password) . 'Salt' . $this->salt);
284                 }
285                 $this->override = new Override();
286                 $this->override->fromArray($this->permissions->toArray());
287                 $this->override->permissions = $this->permissions;
288                 $this->override->profile = $this;
289                 $this->created = new \DateTime("now");
290         }
291
292         /**
293          * @ORM\PreUpdate
294          */
295         public function setUpdateTime()
296         {
297                 $this->updated = new \DateTime("now");
298         }
299
300         public function postUpdate()
301         {
302                 if ($this->override->permissions->id != $this->permissions->id)
303                 {
304                         $this->override->fromArray($this->permissions->toArray());
305                         $this->override->permissions = $this->permissions;
306                 }
307         }
308
309         /**
310          * Convert the object to an array.
311          * @param array $expand
312          * @param array $intersect
313          * @param boolean $showIdentifiers
314          * @param integer $expandAll
315          * @return array
316          */
317         public function toArray(
318                         array $expand = array(), array $intersect = array(),
319                         $showIdentifiers = false, $expandAll = 0
320                         )
321         {
322                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
323                 $includeAll = empty($intersect);
324                 $data = array();
325                 ($includeAll || isset($intersect['id']))
326                         && $data['id'] = $this->id;
327                 ($includeAll || isset($intersect['email']))
328                         && $data['email'] = $this->email;
329                 ($includeAll || isset($intersect['firstName']))
330                         && $data['firstName'] = $this->firstName;
331                 ($includeAll || isset($intersect['familyName']))
332                         && $data['familyName'] = $this->familyName;
333                 ($includeAll || isset($intersect['fullName']))
334                         && $data['fullName'] = $this->firstName . ' ' . $this->familyName;
335                 ($includeAll || isset($intersect['idNumber']))
336                         && $data['idNumber'] = $this->idNumber;
337                 ($includeAll || isset($intersect['dateOfBirth']))
338                         && $data['dateOfBirth'] = !is_null($this->dateOfBirth)
339                                 ? $this->dateOfBirth->format(\Utility\Registry::getConfigParam('DateFormat'))
340                                 : null;
341                 ($includeAll || isset($intersect['mobile']))
342                         && $data['mobile'] = $this->mobile;
343                 ($includeAll || isset($intersect['company']))
344                         && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
345                                                                                 && !is_null($this->company)
346                                 ? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray(
347                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
348                                                 ) : $this->company->id)
349                                 : null;
350                 ($includeAll || isset($intersect['manager']))
351                         && $data['manager'] = (in_array('manager', $expand) || $expandAll || $showIdentifiers)
352                                                                                 && !is_null($this->manager)
353                                 ? (!$showIdentifiers || in_array('manager', $expand)
354                                                 ? \Utility\Registry::getEntityManager()
355                                                         ->getRepository('\User\Entity\Profile')
356                                                         ->find($this->sudo)
357                                                         ->toArray(
358                                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
359                                                         )
360                                                 : $this->sudo)
361                                 : null;
362                 ($includeAll || isset($intersect['region']))
363                         && $data['region'] = (in_array('region', $expand) || $expandAll || $showIdentifiers)
364                                                                                 && !is_null($this->region)
365                                 ? (!$showIdentifiers || in_array('region', $expand) ? $this->region->toArray(
366                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
367                                                 ) : $this->region->id)
368                                 : null;
369                 ($includeAll || isset($intersect['group']))
370                         && $data['group'] = (in_array('group', $expand) || $expandAll || $showIdentifiers)
371                                                                                 && !is_null($this->group)
372                                 ? (!$showIdentifiers || in_array('group', $expand) ? $this->group->toArray(
373                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
374                                                 ) : $this->group->id)
375                                 : null;
376                 ($includeAll || isset($intersect['groupDivision']))
377                         && $data['groupDivision'] = (in_array('groupDivision', $expand) || $expandAll || $showIdentifiers)
378                                                                                 && !is_null($this->groupDivision)
379                                 ? (!$showIdentifiers || in_array('groupDivision', $expand) ? $this->groupDivision->toArray(
380                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
381                                                 ) : $this->groupDivision->id)
382                                 : null;
383                 ($includeAll || isset($intersect['override']))
384                         && $showIdentifiers
385                         && $data['override'] = !is_null($this->override)
386                                                         ? $this->override->id
387                                                         : null;
388                 ($includeAll || isset($intersect['userType']))
389                         && $data['userType'] = $this->userType;
390                 ($includeAll || isset($intersect['position']))
391                         && $data['position'] = $this->position;
392                 ($includeAll || isset($intersect['permissions']))
393                         && $data['permissions'] = (in_array('permissions', $expand) || $expandAll || $showIdentifiers)
394                                                                                 && !is_null($this->permissions)
395                                 ? (!$showIdentifiers || in_array('permissions', $expand)
396                                                 ? array_merge($this->permissions->toArray(), $this->override->toArray())
397                                                 : $this->permissions->id)
398                                 : null;
399                 ($includeAll || isset($intersect['subscribeNewsletter']))
400                         && $data['subscribeNewsletter'] = $this->subscribeNewsletter;
401
402                 ($includeAll || isset($intersect['mainMemberPublicCentre']))
403                         && $data['mainMemberPublicCentre'] = $this->mainMemberPublicCentre;
404
405                 ($includeAll || isset($intersect['subscribeReminders']))
406                         && $data['subscribeReminders'] = $this->subscribeReminders;
407                 ($includeAll || isset($intersect['lastLogin']))
408                         && $data['lastLogin'] = !is_null($this->lastLogin)
409                                         ? $this->lastLogin->format($dateTimeFormat)
410                                         : null;
411                 ($includeAll || isset($intersect['jobState']))
412                         && $data['jobState'] = $this->jobState;
413                 ($includeAll || isset($intersect['created']))
414                         && $data['created'] = !is_null($this->created)
415                                 ? $this->created->format($dateTimeFormat)
416                                 : null;
417                 ($includeAll || isset($intersect['updated']))
418                         && $data['updated'] = !is_null($this->updated)
419                                 ? $this->updated->format($dateTimeFormat)
420                                 : null;
421                 return $data;
422         }
423
424         /**
425          * Populate from an array.
426          * @param array $data
427          */
428         public function fromArray($data = array())
429         {
430                 isset($data['id'])
431                         && $this->id = $data['id'];
432                 isset($data['email'])
433                         && $this->email = $data['email'];
434                 isset($data['firstName'])
435                         && $this->firstName = $data['firstName'];
436                 isset($data['familyName'])
437                         && $this->familyName = $data['familyName'];
438                 isset($data['idNumber'])
439                         && $this->idNumber = $data['idNumber'];
440                 isset($data['dateOfBirth'])
441                         && $this->dateOfBirth = !is_object($data['dateOfBirth'])
442                                 ? new \DateTime($data['dateOfBirth'])
443                                 : $data['dateOfBirth'];
444                 isset($data['mobile'])
445                         && $this->mobile = $data['mobile'];
446                 if (array_key_exists('manager', $data))
447                 {
448                         if (null == $data['manager'])
449                         {
450                                 $this->manager = null;
451                                 $this->sudo = null;
452                         }
453                         else if ($this->id != $data['manager']->getId())
454                         {
455                                 $this->manager = $data['manager'];
456                                 $this->sudo = $data['manager']->getId();
457                         }
458                 }
459                 if (isset($data['company']))
460                 {
461                         if (!is_null($this->company) && $this->company->id != $data['company']->id
462                                 && !is_null($this->manager))
463                         {
464                                 $this->manager = null;
465                                 $this->sudo    = null;
466                         }
467                         $this->company = $data['company'];
468                 }
469                 isset($data['region'])
470                         && $this->region = $data['region'];
471                 isset($data['group'])
472                         && $this->group = $data['group'];
473                 isset($data['groupDivision'])
474                         && $this->groupDivision = $data['groupDivision'];
475                 isset($data['userType'])
476                         && $this->userType = $data['userType'];
477                 isset($data['position'])
478                         && $this->position = $data['position'];
479                 isset($data['permissions'])
480                         && $this->permissions = $data['permissions'];
481                 isset($data['override'])
482                         && $this->override = $data['override'];
483                 isset($data['subscribeNewsletter'])
484                         && $this->subscribeNewsletter = $data['subscribeNewsletter'];
485
486                 isset($data['mainMemberPublicCentre'])
487                         && $this->mainMemberPublicCentre = $data['mainMemberPublicCentre'];
488
489                 isset($data['subscribeReminders'])
490                         && $this->subscribeReminders = $data['subscribeReminders'];
491                 isset($data['lastLogin'])
492                         && $this->lastLogin = $data['lastLogin'];
493                 if (isset($data['password']))
494                 {
495                         if (is_null($this->id))
496                         {
497                                 \Utility\Registry::setOnce('NewUser.Pin', $data['password']);
498                         }
499                         $this->salt = sha1(mt_rand(1000000000, 9999999999));
500                         $this->password = sha1(sha1($data['password']) . 'Salt' . $this->salt);
501                 }
502                 if (isset($data['oldPassword']) && isset($data['newPassword']))
503                 {
504                         #-> Change password.
505                         if (sha1(sha1($data['oldPassword']) . 'Salt' . $this->salt) == $this->password)
506                         {
507                                 $this->salt = sha1(mt_rand(1000000000, 9999999999));
508                                 $this->password = sha1(sha1($data['newPassword']) . 'Salt' . $this->salt);
509                         }
510                         else
511                         {
512                                 throw new \Exception('Current password does not match.');
513                         }
514                 }
515         }
516
517         /**
518          * Check if provided password is valid.
519          * @param string $password
520          * @return boolean
521          */
522         public function passwordValid($password)
523         {
524                 return sha1(sha1($password) . 'Salt' . $this->salt) == $this->password;
525         }
526
527 }