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; } }