Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
namespace User\Entity;

use Doctrine\ORM\Mapping as ORM;



/**
 * A system user.
 *
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="profile", uniqueConstraints={@ORM\UniqueConstraint(name="unique_username", columns={"email"})})
 */
class Profile
{

	/**
	 * Can archive records.
	 */
	const ARCHIVE = true;
	/**
	 * Pull Synchronization Strategy for this table.
	 */
	const PULL_SYNCH_STRATEGY = false;
	/**
	 * Push Synchronization Strategy for this table.
	 */
	const PUSH_SYNCH_STRATEGY = false;
	/**
	 * Post update action must be called after existing entity is flushed to database.
	 */
	const HAVE_POST_UPDATE = true;

	/**
	 * Valid position values.
	 */
	const POSITION_MANAGER  = 'Manager';
	const POSITION_VALUATOR = 'Valuator';
	const POSITION_SALES    = 'Sales';

	/**
	 * Valid usertype values.
	 */
	const USERTYPE_ADMIN    = 'Administrator';
	const USERTYPE_B4C      = 'B4C User';
	const USERTYPE_GROUP    = 'Group User';
	const USERTYPE_DIVISION = 'Dealer Principle';
	const USERTYPE_USER     = 'Dealership User';

	/**
	 * @ORM\Id
	 * @ORM\Column(type="integer");
	 * @ORM\GeneratedValue(strategy="AUTO")
	 */
	protected $id;

	/**
	 * @ORM\Column(type="string", length=255, unique=true)
	 */
	protected $email;

	/**
	 * @ORM\Column(type="string", length=42)
	 */
	protected $password;

	/**
	 * @ORM\Column(type="string", length=42, name="password_salt")
	 */
	protected $salt;

	/**
	 * @ORM\Column(type="string", length=100, name="first_name")
	 */
	protected $firstName;

	/**
	 * @ORM\Column(type="string", length=100, name="family_name")
	 */
	protected $familyName;

	/**
	 * @ORM\Column(type="string", length=13, name="id_number")
	 */
	protected $idNumber;

	/**
	 * @ORM\Column(type="date", name="date_of_birth")
	 */
	protected $dateOfBirth;

	/**
	 * @ORM\Column(type="string", length=20)
	 */
	protected $mobile;

	/**
	 * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
	 * @ORM\JoinColumn(nullable=true, name="company_id")
	 **/
	protected $company;

	/**
	 * @ORM\Column(type="integer", nullable=true, name="manager_id")
	 **/
	protected $sudo;

	/**
	 * @ORM\ManyToOne(targetEntity="Profile")
	 * @ORM\JoinColumn(nullable=true, name="manager_profile_id")
	 **/
	protected $manager;

	/**
	 * @ORM\ManyToOne(targetEntity="\Location\Entity\Region")
	 * @ORM\JoinColumn(nullable=true, name="lib_region_id")
	 **/
	protected $region;

	/**
	 * @ORM\ManyToOne(targetEntity="\Company\Entity\Group")
	 * @ORM\JoinColumn(nullable=true, name="group_id")
	 **/
	protected $group;

	/**
	 * @ORM\ManyToOne(targetEntity="\Company\Entity\GroupDivision")
	 * @ORM\JoinColumn(nullable=true, name="group_division_id")
	 **/
	protected $groupDivision;

	/**
	 * @ORM\Column(type="string", length=25, name="user_type")
	 **/
	protected $userType = 'Dealership User';

	/**
	 * @ORM\Column(type="string", length=25)
	 **/
	protected $position = 'Manager';

	/**
	 * @ORM\ManyToOne(targetEntity="Permissions")
	 * @ORM\JoinColumn(name="permission_id")
	 **/
	protected $permissions;

	/**
	 * @ORM\ManyToOne(targetEntity="Override", cascade={"all"})
	 * @ORM\JoinColumn(name="permission_override_id")
	 **/
	protected $override;

	/**
	 * @ORM\Column(type="boolean", name="subscribe_newsletter");
	 */
	protected $subscribeNewsletter = true;

	/**
	 * @ORM\Column(type="boolean", name="subscribe_reminders");
	 */
	protected $subscribeReminders = true;

	/**
	 * @ORM\Column(type="datetime", nullable=true, name="last_login")
	 */
	protected $lastLogin;

	/**
	 * @ORM\Column(type="string", nullable=true, length=100, name="ip_address")
	 */
	protected $ipAddress;

	/**
	 * @ORM\OneToMany(targetEntity="Session", mappedBy="profile", fetch="EXTRA_LAZY")
	 * @ORM\JoinColumn(name="profile_session_id")
	 **/
	private $sessions;
  
  /**
	 * @ORM\OneToMany(targetEntity="AuthenticationLog", mappedBy="profile", cascade={"all"}, fetch="EXTRA_LAZY")
	 **/
	private $logins;

	/**
	 * @ORM\Column(type="string", length=10, name="status")
	 */
	protected $jobState = 'Active';

	/**
	 * @ORM\Column(type="boolean",  name="mm_pcentre");
	 */
	protected $mainMemberPublicCentre = false;

	/**
	 * @ORM\Column(type="datetime");
	 */
	protected $created;

	/**
	 * @ORM\Column(type="datetime", nullable=true);
	 */
	protected $updated;

	/**
	 * @ORM\Column(type="boolean");
	 */
	protected $archived = false;



	/**
	 * Initialize collections.
	 */
	public function __construct()
	{
		$this->sessions = new \Doctrine\Common\Collections\ArrayCollection();
    $this->logins = new \Doctrine\Common\Collections\ArrayCollection();
	}

	/**
	 * Add a new Session to this Profile.
	 * @param Region $region
	 * @return \User\Entity\Profile
	 */
	public function addSession(Session $session)
	{
		$this->sessions[] = $session;
		return $this;
	}

	public function getId()
	{
		return $this->id;
	}

	/**
	 * Magic getter to expose protected properties.
	 *
	 * @param string $property
	 * @return mixed
	 */
	public function __get($property)
	{
		return $this->$property;
	}

	/**
	 * Magic setter to save protected properties.
	 * @param string $property
	 * @param mixed $value
	 */
	public function __set($property, $value)
	{
		if ('password' == $property)
		{
			$this->salt = sha1(mt_rand(1000000000, 9999999999));
			$this->password = sha1(sha1($value) . 'Salt' . $this->salt);
			return;
		}
		$this->$property = $value;
	}

	/**
	 * @ORM\PrePersist
	 */
	public function setCreateTime()
	{
		$company = \Utility\Registry::resolveCompanyContext(
			!is_null($this->company)
				? $this->company->id
				: null
		);
		$this->company = is_object($company)
			? $company
			: $this->company;
		if (is_null($this->password))
		{
			$password = mt_rand(1000, 9999);
			\Utility\Registry::setOnce('NewUser.Pin', $password);
			$this->salt = sha1(mt_rand(1000000000, 9999999999));
			$this->password = sha1(sha1($password) . 'Salt' . $this->salt);
		}
		$this->override = new Override();
		$this->override->fromArray($this->permissions->toArray());
		$this->override->permissions = $this->permissions;
		$this->override->profile = $this;
		$this->created = new \DateTime("now");
	}

	/**
	 * @ORM\PreUpdate
	 */
	public function setUpdateTime()
	{
		$this->updated = new \DateTime("now");
	}

	public function postUpdate()
	{
		if ($this->override->permissions->id != $this->permissions->id)
		{
			$this->override->fromArray($this->permissions->toArray());
			$this->override->permissions = $this->permissions;
		}
	}

	/**
	 * Convert the object to an array.
	 * @param array $expand
	 * @param array $intersect
	 * @param boolean $showIdentifiers
	 * @param integer $expandAll
	 * @return array
	 */
	public function toArray(
			array $expand = array(), array $intersect = array(),
			$showIdentifiers = false, $expandAll = 0
			)
	{
		$dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
		$includeAll = empty($intersect);
		$data = array();
		($includeAll || isset($intersect['id']))
			&& $data['id'] = $this->id;
		($includeAll || isset($intersect['email']))
			&& $data['email'] = $this->email;
		($includeAll || isset($intersect['firstName']))
			&& $data['firstName'] = $this->firstName;
		($includeAll || isset($intersect['familyName']))
			&& $data['familyName'] = $this->familyName;
		($includeAll || isset($intersect['fullName']))
			&& $data['fullName'] = $this->firstName . ' ' . $this->familyName;
		($includeAll || isset($intersect['idNumber']))
			&& $data['idNumber'] = $this->idNumber;
		($includeAll || isset($intersect['dateOfBirth']))
			&& $data['dateOfBirth'] = !is_null($this->dateOfBirth)
				? $this->dateOfBirth->format(\Utility\Registry::getConfigParam('DateFormat'))
				: null;
		($includeAll || isset($intersect['mobile']))
			&& $data['mobile'] = $this->mobile;
		($includeAll || isset($intersect['company']))
			&& $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
										&& !is_null($this->company)
				? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray(
						$expand, $intersect, $showIdentifiers, ($expandAll - 1)
						) : $this->company->id)
				: null;
		($includeAll || isset($intersect['manager']))
			&& $data['manager'] = (in_array('manager', $expand) || $expandAll || $showIdentifiers)
										&& !is_null($this->manager)
				? (!$showIdentifiers || in_array('manager', $expand)
						? \Utility\Registry::getEntityManager()
							->getRepository('\User\Entity\Profile')
							->find($this->sudo)
							->toArray(
								$expand, $intersect, $showIdentifiers, ($expandAll - 1)
							)
						: $this->sudo)
				: null;
		($includeAll || isset($intersect['region']))
			&& $data['region'] = (in_array('region', $expand) || $expandAll || $showIdentifiers)
										&& !is_null($this->region)
				? (!$showIdentifiers || in_array('region', $expand) ? $this->region->toArray(
						$expand, $intersect, $showIdentifiers, ($expandAll - 1)
						) : $this->region->id)
				: null;
		($includeAll || isset($intersect['group']))
			&& $data['group'] = (in_array('group', $expand) || $expandAll || $showIdentifiers)
										&& !is_null($this->group)
				? (!$showIdentifiers || in_array('group', $expand) ? $this->group->toArray(
						$expand, $intersect, $showIdentifiers, ($expandAll - 1)
						) : $this->group->id)
				: null;
		($includeAll || isset($intersect['groupDivision']))
			&& $data['groupDivision'] = (in_array('groupDivision', $expand) || $expandAll || $showIdentifiers)
										&& !is_null($this->groupDivision)
				? (!$showIdentifiers || in_array('groupDivision', $expand) ? $this->groupDivision->toArray(
						$expand, $intersect, $showIdentifiers, ($expandAll - 1)
						) : $this->groupDivision->id)
				: null;
		($includeAll || isset($intersect['override']))
			&& $showIdentifiers
			&& $data['override'] = !is_null($this->override)
							? $this->override->id
							: null;
		($includeAll || isset($intersect['userType']))
			&& $data['userType'] = $this->userType;
		($includeAll || isset($intersect['position']))
			&& $data['position'] = $this->position;
		($includeAll || isset($intersect['permissions']))
			&& $data['permissions'] = (in_array('permissions', $expand) || $expandAll || $showIdentifiers)
										&& !is_null($this->permissions)
				? (!$showIdentifiers || in_array('permissions', $expand)
						? array_merge($this->permissions->toArray(), $this->override->toArray())
						: $this->permissions->id)
				: null;
		($includeAll || isset($intersect['subscribeNewsletter']))
			&& $data['subscribeNewsletter'] = $this->subscribeNewsletter;

		($includeAll || isset($intersect['mainMemberPublicCentre']))
			&& $data['mainMemberPublicCentre'] = $this->mainMemberPublicCentre;

		($includeAll || isset($intersect['subscribeReminders']))
			&& $data['subscribeReminders'] = $this->subscribeReminders;
		($includeAll || isset($intersect['lastLogin']))
			&& $data['lastLogin'] = !is_null($this->lastLogin)
					? $this->lastLogin->format($dateTimeFormat)
					: null;
		($includeAll || isset($intersect['jobState']))
			&& $data['jobState'] = $this->jobState;
		($includeAll || isset($intersect['created']))
			&& $data['created'] = !is_null($this->created)
				? $this->created->format($dateTimeFormat)
				: null;
		($includeAll || isset($intersect['updated']))
			&& $data['updated'] = !is_null($this->updated)
				? $this->updated->format($dateTimeFormat)
				: null;
		return $data;
	}

	/**
	 * Populate from an array.
	 * @param array $data
	 */
	public function fromArray($data = array())
	{
		isset($data['id'])
			&& $this->id = $data['id'];
		isset($data['email'])
			&& $this->email = $data['email'];
		isset($data['firstName'])
			&& $this->firstName = $data['firstName'];
		isset($data['familyName'])
			&& $this->familyName = $data['familyName'];
		isset($data['idNumber'])
			&& $this->idNumber = $data['idNumber'];
		isset($data['dateOfBirth'])
			&& $this->dateOfBirth = !is_object($data['dateOfBirth'])
				? new \DateTime($data['dateOfBirth'])
				: $data['dateOfBirth'];
		isset($data['mobile'])
			&& $this->mobile = $data['mobile'];
		if (array_key_exists('manager', $data))
		{
			if (null == $data['manager'])
			{
				$this->manager = null;
				$this->sudo = null;
			}
			else if ($this->id != $data['manager']->getId())
			{
				$this->manager = $data['manager'];
				$this->sudo = $data['manager']->getId();
			}
		}
		if (isset($data['company']))
		{
			if (!is_null($this->company) && $this->company->id != $data['company']->id
				&& !is_null($this->manager))
			{
				$this->manager = null;
				$this->sudo    = null;
			}
			$this->company = $data['company'];
		}
		isset($data['region'])
			&& $this->region = $data['region'];
		isset($data['group'])
			&& $this->group = $data['group'];
		isset($data['groupDivision'])
			&& $this->groupDivision = $data['groupDivision'];
		isset($data['userType'])
			&& $this->userType = $data['userType'];
		isset($data['position'])
			&& $this->position = $data['position'];
		isset($data['permissions'])
			&& $this->permissions = $data['permissions'];
		isset($data['override'])
			&& $this->override = $data['override'];
		isset($data['subscribeNewsletter'])
			&& $this->subscribeNewsletter = $data['subscribeNewsletter'];

		isset($data['mainMemberPublicCentre'])
			&& $this->mainMemberPublicCentre = $data['mainMemberPublicCentre'];

		isset($data['subscribeReminders'])
			&& $this->subscribeReminders = $data['subscribeReminders'];
		isset($data['lastLogin'])
			&& $this->lastLogin = $data['lastLogin'];
		if (isset($data['password']))
		{
			if (is_null($this->id))
			{
				\Utility\Registry::setOnce('NewUser.Pin', $data['password']);
			}
			$this->salt = sha1(mt_rand(1000000000, 9999999999));
			$this->password = sha1(sha1($data['password']) . 'Salt' . $this->salt);
		}
		if (isset($data['oldPassword']) && isset($data['newPassword']))
		{
			#-> Change password.
			if (sha1(sha1($data['oldPassword']) . 'Salt' . $this->salt) == $this->password)
			{
				$this->salt = sha1(mt_rand(1000000000, 9999999999));
				$this->password = sha1(sha1($data['newPassword']) . 'Salt' . $this->salt);
			}
			else
			{
				throw new \Exception('Current password does not match.');
			}
		}
	}

	/**
	 * Check if provided password is valid.
	 * @param string $password
	 * @return boolean
	 */
	public function passwordValid($password)
	{
		return sha1(sha1($password) . 'Salt' . $this->salt) == $this->password;
	}

}

Commits for namibiamodule/User/src/User/Entity/Profile.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit