/** * Returns an HTML anchor tag that links to user lock/unlock action * * @param User $user User * @param string $label Link label * @return string HMTL anchor tag */ public function userLockLink($user, $label = null) { if (!Zend_Registry::get('acl')->isUserAllowed('mvc:admin:users:edit', 'view')) { return ''; } if (null === $label) { $label = $user->getLocked() ? 'Unlock' : 'Lock'; } $class = ' class="userLockLink ss_sprite ss_lock' . ($user->getLocked() ? '_delete' : '') . '"'; return '<a' . $class . ' id="userLockLink-' . $user->getId() . '" href="#">' . $label . '</a>'; }
/** * Returns an HTML anchor tag that links to user activate action * * @param User $user User * @param string $label Link label * @return string HMTL anchor tag */ public function userActivateLink(User $user, $label = null) { if (!Zend_Registry::get('acl')->isUserAllowed('mvc:admin:users:edit', 'view')) { return ''; } if ($user->getActive() === true) { return ''; } if (null === $label) { $label = 'Activate'; } $class = ' class="userActivateLink ss_sprite ss_check"'; return '<a' . $class . ' id="userActivateLink-' . $user->getId() . '" href="javascript:;">' . $label . '</a>'; }
/** * Add social network account fields * * @return void */ private function _addSocial() { $names = array(); $socialNetworkIdentities = array_values($this->_user->getProfile()->getSocialNetworkIdentities()->toArray()); $c = count($socialNetworkIdentities); if (0 === $c) { $names[0] = 'social1'; $this->addElement(new SocialNetworkIdentity($names[0], array('filters' => array(), 'validators' => array(), 'required' => false, 'label' => 'Social Identities:', 'decorators' => array(array('SocialNetworkIdentity', array('link' => true)), array('Label', array('optionalSuffix' => '<span class="optional"> </span>', 'requiredSuffix' => '<span class="required"> *</span>', 'escape' => false)), array('HtmlTag', array('tag' => 'li')))))); } else { for ($i = 0; $i < $c; ++$i) { $socialNetworkIdentity = $socialNetworkIdentities[$i]; #$names[$i] = 'social'.($i > 0 ? ($i+1) : ''); $names[$i] = 'social' . ($i + 1); $social = new SocialNetworkIdentity($names[$i], array('filters' => array(), 'validators' => array(), 'required' => false, 'label' => 'Social Identities:', 'value' => array('name' => $socialNetworkIdentity->getName(), 'network' => $socialNetworkIdentity->getSocialNetwork()->getId()), 'decorators' => array('SocialNetworkIdentity', array('Label', array('optionalSuffix' => '<span class="optional"> </span>', 'requiredSuffix' => '<span class="required"> *</span>', 'escape' => false)), array('HtmlTag', array('tag' => 'li'))))); $this->addElement($social); if ($i > 0) { $social->getDecorator('Label')->setOption('class', 'invisible'); } if ($i + 1 === $c) { $social->getDecorator('SocialNetworkIdentity')->setOption('link', true); } } } return $names; }
/** * Initializes user * * @param User $user * @return void */ private function _initUser(User $user = null) { if ($user === null) { // Attempt to retrieve user from registry (may have been placed there by Auth controller plugin) try { $user = Zend_Registry::get('user'); } catch (Zend_Exception $e) { } if (null !== $user) { $this->_user = $user; } else { // No user found so default to 'guest' $this->_user = new User(array('id' => 0, 'role' => $this->_guestRole, 'username' => 'Anonymous')); } } else { $this->_user = $user; } $this->addRole(new Zend_Acl_Role($this->_user->getUsername()), $this->_user->getRole()->getName()); }
/** * Set form field default values * * @param User $user * @return void */ public function setDefaults(User $user) { $profile = $user->getProfile(); parent::setDefaults(array('userId' => $user->getId(), 'username' => $user->getUsername(), 'role' => $user->getRole()->getId(), 'firstName' => $profile->getFirstName(), 'lastName' => $profile->getLastName(), 'email' => $user->getEmail(), 'phone' => $profile->getPhone(), 'active' => $user->getActive(), 'locked' => $user->getLocked())); }
/** * Send email address verification email to user * * @param User $user * @param Zend_Mail_Transport_Abstract $transport [Optional] Zend mail transport class * @return void */ public static function sendVerificationEmail(User $user, Zend_Mail_Transport_Abstract $transport = null) { $serverUrlHelper = new Zend_View_Helper_ServerUrl(); $urlHelper = HelperBroker::getStaticHelper('url'); $siteDomain = preg_replace('/^https?:\\/\\//', '', $serverUrlHelper->serverUrl()); $siteName = Zend_Registry::get('siteName'); $config = Zend_Registry::get('config'); $from = 'noreply@' . $siteDomain; if (!empty($config->mail) && !empty($config->mail->from)) { $from = $config->mail->from; } if (null === $transport) { if (Zend_Session::$_unitTestEnabled) { $transport = new MockMailTransport(); } else { if (!empty($config->mail) && !empty($config->mail->smtp) && !empty($config->mail->smtp->host)) { $options = $config->mail->smtp->toArray(); unset($options['host']); $transport = new Zend_Mail_Transport_Smtp($config->mail->smtp->host, $options); } } } UserEmailVerificationService::collectGarbage(); // @todo cronjob?; should also remove any unverified user accounts $verificationToken = sha1(mt_rand() . $user->getEmail() . mt_rand()); if (APPLICATION_ENV === 'testing') { $verificationLink = $serverUrlHelper->serverUrl() . '/verifyEmail/' . $verificationToken; } else { // @codeCoverageIgnoreStart $verificationLink = $serverUrlHelper->serverUrl() . $urlHelper->url(array('token' => $verificationToken), 'verifyEmail'); } // @codeCoverageIgnoreEnd UserEmailVerificationService::create(new UserEmailVerification(array('user' => $user, 'token' => $verificationToken, 'requestDate' => new DateTime()))); $text = 'Hello ' . $user->getUsername() . ', Thank you for registering with ' . $siteName . '. To activate your account and complete the registration process, please click the following link: ' . $verificationLink . '. You are receiving this email because someone recently registered on our site and provided <' . $user->getEmail() . '> as their ema il address. If you did not recently register at ' . $siteDomain . ', then please ignore this email. Your information will be remov ed from our system within 24 hours. Thank you, The ' . $siteName . ' Team '; $html = '<p>Hello ' . $user->getUsername() . ',</p> <p>Thank you for registering with ' . $siteName . '. To activate your account and complete the registration process, please click the following link: <a href="' . $verificationLink . '" title="Verify your email address">' . $verificationLink . '</a>.</p> <p>You are receiving this email because someone recently registered on our site and provided <' . $user->getEmail() . '> as their email address. If you did not recently register at ' . $siteDomain . ', then please ignore this email. Your information will be removed from our system within 24 hours.</p> <p>Thank you,<br> The ' . $siteName . ' Team</p> '; try { Logger::info('Attempting to send email to \'' . $user->getEmail() . '\'.'); $mail = new Zend_Mail('utf-8'); $mail->setFrom($from, $siteName)->setSubject('[' . $siteName . '] Email Verification')->setBodyText($text)->setBodyHtml($html)->addTo($user->getEmail()); $mail->send($transport); } catch (Exception $e) { Logger::crit($e->getMessage()); throw $e; } }
/** * Remove all records associated with the given user * * @param User $user * @return void */ public static function clearTokensForUser(User $user) { $queryBuilder = self::getEntityManager()->createQueryBuilder(); $queryBuilder->delete(self::getEntityClass(), 'e')->where('e.user = ?1')->setParameter(1, $user->getId())->getQuery()->execute(); }
/** * Update user * * @param User $user User to be updated * @param array $data User data to be updated * @throws Exception * @return bool True if changes were made */ private function _updateUser(User $user, array $data) { Logger::debug(__METHOD__ . '::' . var_export($data, true)); #if(isset($data['email']) && '' != $data['email'] && $data['email'] != $user->getEmail()) { # $user->setEmail($data['email']); #} $profile = $user->getProfile(); $social = $profile->getSocialNetworkIdentities(); // Track changes $changes = array(PROFILE_EDIT => array(), SOCIAL_EDIT => array(), USER_EDIT => array()); foreach ($data as $key => $newValue) { Logger::debug(__METHOD__ . ":: {$key}"); if (in_array($key, array('firstName', 'lastName', 'phone'))) { Logger::debug(__METHOD__ . ':: Profile key'); $type = PROFILE_EDIT; $oldValue = $profile->{'get' . ucfirst($key)}(); } elseif (preg_match('/^social(\\d+)$/', $key, $matches)) { Logger::debug(__METHOD__ . ':: Social key: social' . $matches[1]); $type = SOCIAL_EDIT; $oldValue = $social[$matches[1] - 1]; } else { Logger::debug(__METHOD__ . ':: User key'); $type = USER_EDIT; $oldValue = $user->{'get' . ucfirst($key)}(); } Logger::debug(__METHOD__ . ":: OLD => " . (is_object($oldValue) ? get_class($oldValue) : var_export($oldValue, true))); Logger::debug(__METHOD__ . ":: NEW => " . (is_object($newValue) ? get_class($newValue) : var_export($newValue, true))); // Only update changed properties, and keep track of the changes as well if ($this->_valueChanged($oldValue, $newValue)) { Logger::debug(__METHOD__ . ":: {$key} has changed"); Logger::debug(__METHOD__ . ":: OLD => " . (is_object($oldValue) ? get_class($oldValue) : var_export($oldValue, true))); Logger::debug(__METHOD__ . ":: NEW => " . (is_object($newValue) ? get_class($newValue) : var_export($newValue, true))); $oldVal = $oldValue; $newVal = $newValue; if ($newValue instanceof Rexmac\Zyndax\Form\Element\SocialNetworkIdentity && $oldValue instanceof Rexmac\Zyndax\Entity\UserSocialNetworkIdentity) { $newVal = $newValue->getIdentityName() . '@' . SocialNetworkService::findOneById($newValue->getNetwork())->getName(); $oldVal = $oldValue->getName() . '@' . $oldValue->getSocialNetwork()->getName(); } elseif (is_object($newValue)) { if (isset($oldValue)) { $oldVal = $oldValue->getName(); } else { $oldVal = ''; } $newVal = $newValue->getName(); } elseif (is_object($oldValue)) { $oldVal = $oldValue->getName(); } $changes[$type][] = array('item' => $key, 'oldValue' => $oldVal, 'newValue' => $newVal); // Set new value if ($type === SOCIAL_EDIT) { if ('' === $newValue->getIdentityName()) { $removed = $profile->removeSocialNetworkIdentity($oldValue); Logger::debug(__METHOD__ . ':: Removed? ' . var_export($removed, true)); UserSocialNetworkIdentityService::delete($oldValue); #$profile->setSocialNetworkIdentities(UserSocialNetworkIdentityService::findBy(array('userProfile', $profile->getId()))); } else { $oldValue->setSocialNetwork(SocialNetworkService::findOneById($newValue->getNetwork())); $oldValue->setName($newValue->getIdentityName()); } } elseif ($type === PROFILE_EDIT) { $profile->{'set' . ucfirst($key)}($newValue); } else { $user->{'set' . ucfirst($key)}($newValue); } } } UserService::update(); UserProfileService::update(); UserSocialNetworkIdentityService::update(); // Any changes to record? $changed = false; foreach (array(PROFILE_EDIT, SOCIAL_EDIT, USER_EDIT) as $type) { Logger::debug(__METHOD__ . ':: Examining ' . $type . ' changes...'); if (count($changes[$type]) > 0) { Logger::debug(__METHOD__ . ':: changes[\'' . $type . '\'] = ' . var_export($changes[$type], true)); $description = ''; foreach ($changes[$type] as $change) { Logger::debug(__METHOD__ . ':: change = ' . var_export($change, true)); $description .= sprintf('%s changed from "%s" to "%s".', $change['item'], $change['oldValue'] === 0 ? '0' : $change['oldValue'], $change['newValue']) . PHP_EOL; Logger::debug(__METHOD__ . ':: description = ' . $description); } UserEditEventService::create(array('user' => $user, 'editor' => $this->_user, 'ip' => $this->getRequest()->getServer('REMOTE_ADDR'), 'date' => new DateTime(), 'description' => rtrim($description))); $changed = true; } } return $changed; }
/** * Update User entity * * @param User $user * @param array $data * @return void */ private function _updateUser(User $user, array $data) { if (isset($data['newPassword']) && '' != $data['newPassword']) { // Verify old password #if(!UserService::verifyPassword($this->_user, $data['password'])) { # throw new Exception('Current password is invalid'); #} $data['password'] = UserService::encryptPassword($data['newPassword']); } else { $data['password'] = $user->getPassword(); } unset($data['newPassword']); unset($data['newPasswordConfirm']); if (isset($data['role'])) { $data['role'] = AclRoleService::findOneById($data['role']); } if (isset($data['timeZone'])) { $data['timeZone'] = TimeZoneService::findOneById($data['timeZone']); } // Track changes $changes = array(); foreach ($data as $key => $newValue) { if ($key === 'userId') { continue; } $oldValue = $user->{'get' . ucfirst($key)}(); Logger::debug(__METHOD__ . ":: {$key}"); Logger::debug(__METHOD__ . ":: OLD => " . (is_object($oldValue) ? get_class($oldValue) : var_export($oldValue, true))); Logger::debug(__METHOD__ . ":: NEW => " . (is_object($newValue) ? get_class($newValue) : var_export($newValue, true))); // Only update changed properties, and keep track of the changes as well if ($this->_valueChanged($oldValue, $newValue)) { Logger::debug(__METHOD__ . ":: {$key} has changed"); Logger::debug(__METHOD__ . ":: OLD => " . (is_object($oldValue) ? get_class($oldValue) : var_export($oldValue, true))); Logger::debug(__METHOD__ . ":: NEW => " . (is_object($newValue) ? get_class($newValue) : var_export($newValue, true))); $oldVal = $oldValue; $newVal = $newValue; if (is_object($newValue)) { if (isset($oldValue)) { $oldVal = $oldValue->getName(); } else { $oldVal = ''; } $newVal = $newValue->getName(); } elseif (is_object($oldValue)) { $oldVal = $oldValue->getName(); } $changes[] = array('item' => $key, 'oldValue' => $oldVal, 'newValue' => $newVal); // Set new value $user->{'set' . ucfirst($key)}($newValue); } } UserService::update(); // Any changes to record? if (count($changes) > 0) { $description = ''; foreach ($changes as $change) { $description .= sprintf('%s changed from "%s" to "%s".', $change['item'], $change['oldValue'] === 0 ? '0' : $change['oldValue'], $change['newValue']) . PHP_EOL; } UserEditEventService::create(array('user' => $user, 'editor' => $this->_user, 'ip' => $this->getRequest()->getServer('REMOTE_ADDR'), 'date' => new DateTime(), 'description' => rtrim($description))); return true; } return false; }
public function testSetters() { $user = new User(); $user->setId(self::$testData['id']); $user->setRole(self::$testData['role']); #$user->setProfile(self::$testData['profile']); $user->setUsername(self::$testData['username']); $user->setPassword(self::$testData['password']); $user->setEmail(self::$testData['email']); $user->setDateCreated(self::$testData['dateCreated']); $user->setLastConnect(self::$testData['lastConnect']); $user->setActive(self::$testData['active']); $user->setLocked(self::$testData['locked']); $this->testGetters($user); }