4 use Doctrine\ORM\Mapping as ORM;
12 * @ORM\HasLifecycleCallbacks
13 * @ORM\Table(name="profile", uniqueConstraints={@ORM\UniqueConstraint(name="unique_username", columns={"email"})})
19 * Can archive records.
23 * Pull Synchronization Strategy for this table.
25 const PULL_SYNCH_STRATEGY = false;
27 * Push Synchronization Strategy for this table.
29 const PUSH_SYNCH_STRATEGY = false;
31 * Post update action must be called after existing entity is flushed to database.
33 const HAVE_POST_UPDATE = true;
36 * Valid position values.
38 const POSITION_MANAGER = 'Manager';
39 const POSITION_VALUATOR = 'Valuator';
40 const POSITION_SALES = 'Sales';
43 * Valid usertype values.
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';
53 * @ORM\Column(type="integer");
54 * @ORM\GeneratedValue(strategy="AUTO")
59 * @ORM\Column(type="string", length=255, unique=true)
64 * @ORM\Column(type="string", length=42)
69 * @ORM\Column(type="string", length=42, name="password_salt")
74 * @ORM\Column(type="string", length=100, name="first_name")
79 * @ORM\Column(type="string", length=100, name="family_name")
81 protected $familyName;
84 * @ORM\Column(type="string", length=13, name="id_number")
89 * @ORM\Column(type="date", name="date_of_birth")
91 protected $dateOfBirth;
94 * @ORM\Column(type="string", length=20)
99 * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
100 * @ORM\JoinColumn(nullable=true, name="company_id")
105 * @ORM\Column(type="integer", nullable=true, name="manager_id")
110 * @ORM\ManyToOne(targetEntity="Profile")
111 * @ORM\JoinColumn(nullable=true, name="manager_profile_id")
116 * @ORM\ManyToOne(targetEntity="\Location\Entity\Region")
117 * @ORM\JoinColumn(nullable=true, name="lib_region_id")
122 * @ORM\ManyToOne(targetEntity="\Company\Entity\Group")
123 * @ORM\JoinColumn(nullable=true, name="group_id")
128 * @ORM\ManyToOne(targetEntity="\Company\Entity\GroupDivision")
129 * @ORM\JoinColumn(nullable=true, name="group_division_id")
131 protected $groupDivision;
134 * @ORM\Column(type="string", length=25, name="user_type")
136 protected $userType = 'Dealership User';
139 * @ORM\Column(type="string", length=25)
141 protected $position = 'Manager';
144 * @ORM\ManyToOne(targetEntity="Permissions")
145 * @ORM\JoinColumn(name="permission_id")
147 protected $permissions;
150 * @ORM\ManyToOne(targetEntity="Override", cascade={"all"})
151 * @ORM\JoinColumn(name="permission_override_id")
156 * @ORM\Column(type="boolean", name="subscribe_newsletter");
158 protected $subscribeNewsletter = true;
161 * @ORM\Column(type="boolean", name="subscribe_reminders");
163 protected $subscribeReminders = true;
166 * @ORM\Column(type="datetime", nullable=true, name="last_login")
168 protected $lastLogin;
171 * @ORM\Column(type="string", nullable=true, length=100, name="ip_address")
173 protected $ipAddress;
176 * @ORM\OneToMany(targetEntity="Session", mappedBy="profile", fetch="EXTRA_LAZY")
177 * @ORM\JoinColumn(name="profile_session_id")
182 * @ORM\OneToMany(targetEntity="AuthenticationLog", mappedBy="profile", cascade={"all"}, fetch="EXTRA_LAZY")
187 * @ORM\Column(type="string", length=10, name="status")
189 protected $jobState = 'Active';
192 * @ORM\Column(type="boolean", name="mm_pcentre");
194 protected $mainMemberPublicCentre = false;
197 * @ORM\Column(type="datetime");
202 * @ORM\Column(type="datetime", nullable=true);
207 * @ORM\Column(type="boolean");
209 protected $archived = false;
214 * Initialize collections.
216 public function __construct()
218 $this->sessions = new \Doctrine\Common\Collections\ArrayCollection();
219 $this->logins = new \Doctrine\Common\Collections\ArrayCollection();
223 * Add a new Session to this Profile.
224 * @param Region $region
225 * @return \User\Entity\Profile
227 public function addSession(Session $session)
229 $this->sessions[] = $session;
233 public function getId()
239 * Magic getter to expose protected properties.
241 * @param string $property
244 public function __get($property)
246 return $this->$property;
250 * Magic setter to save protected properties.
251 * @param string $property
252 * @param mixed $value
254 public function __set($property, $value)
256 if ('password' == $property)
258 $this->salt = sha1(mt_rand(1000000000, 9999999999));
259 $this->password = sha1(sha1($value) . 'Salt' . $this->salt);
262 $this->$property = $value;
268 public function setCreateTime()
270 $company = \Utility\Registry::resolveCompanyContext(
271 !is_null($this->company)
275 $this->company = is_object($company)
278 if (is_null($this->password))
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);
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");
295 public function setUpdateTime()
297 $this->updated = new \DateTime("now");
300 public function postUpdate()
302 if ($this->override->permissions->id != $this->permissions->id)
304 $this->override->fromArray($this->permissions->toArray());
305 $this->override->permissions = $this->permissions;
310 * Convert the object to an array.
311 * @param array $expand
312 * @param array $intersect
313 * @param boolean $showIdentifiers
314 * @param integer $expandAll
317 public function toArray(
318 array $expand = array(), array $intersect = array(),
319 $showIdentifiers = false, $expandAll = 0
322 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
323 $includeAll = empty($intersect);
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'))
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)
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')
358 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
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)
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)
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)
383 ($includeAll || isset($intersect['override']))
385 && $data['override'] = !is_null($this->override)
386 ? $this->override->id
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)
399 ($includeAll || isset($intersect['subscribeNewsletter']))
400 && $data['subscribeNewsletter'] = $this->subscribeNewsletter;
402 ($includeAll || isset($intersect['mainMemberPublicCentre']))
403 && $data['mainMemberPublicCentre'] = $this->mainMemberPublicCentre;
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)
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)
417 ($includeAll || isset($intersect['updated']))
418 && $data['updated'] = !is_null($this->updated)
419 ? $this->updated->format($dateTimeFormat)
425 * Populate from an array.
428 public function fromArray($data = array())
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))
448 if (null == $data['manager'])
450 $this->manager = null;
453 else if ($this->id != $data['manager']->getId())
455 $this->manager = $data['manager'];
456 $this->sudo = $data['manager']->getId();
459 if (isset($data['company']))
461 if (!is_null($this->company) && $this->company->id != $data['company']->id
462 && !is_null($this->manager))
464 $this->manager = null;
467 $this->company = $data['company'];
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'];
486 isset($data['mainMemberPublicCentre'])
487 && $this->mainMemberPublicCentre = $data['mainMemberPublicCentre'];
489 isset($data['subscribeReminders'])
490 && $this->subscribeReminders = $data['subscribeReminders'];
491 isset($data['lastLogin'])
492 && $this->lastLogin = $data['lastLogin'];
493 if (isset($data['password']))
495 if (is_null($this->id))
497 \Utility\Registry::setOnce('NewUser.Pin', $data['password']);
499 $this->salt = sha1(mt_rand(1000000000, 9999999999));
500 $this->password = sha1(sha1($data['password']) . 'Salt' . $this->salt);
502 if (isset($data['oldPassword']) && isset($data['newPassword']))
505 if (sha1(sha1($data['oldPassword']) . 'Salt' . $this->salt) == $this->password)
507 $this->salt = sha1(mt_rand(1000000000, 9999999999));
508 $this->password = sha1(sha1($data['newPassword']) . 'Salt' . $this->salt);
512 throw new \Exception('Current password does not match.');
518 * Check if provided password is valid.
519 * @param string $password
522 public function passwordValid($password)
524 return sha1(sha1($password) . 'Salt' . $this->salt) == $this->password;