Ejemplo n.º 1
0
 /**
  * Create action
  *
  * @return void
  */
 public function createAction()
 {
     $request = $this->getRequest();
     $form = new \Admin_Form_UserCreate();
     if ($request->isPost()) {
         if ($form->isValid($request->getParams())) {
             $data = $form->getValues();
             try {
                 $user = UserService::create(array('username' => $data['username'], 'email' => $data['email'], 'password' => UserService::encryptPassword($data['password']), 'role' => AclRoleService::find($data['role']), 'dateCreated' => new DateTime(), 'active' => true, 'locked' => false));
                 UserProfileService::create(array('user' => $user, 'firstName' => $data['firstName'], 'lastName' => $data['lastName'], 'phone' => $data['phone'], 'website' => $data['website'], 'timeZone' => TimeZoneService::findOneByName('America/Los_Angeles')));
                 UserEditEventService::create(array('user' => $user, 'editor' => $this->_user, 'ip' => $this->getRequest()->getServer('REMOTE_ADDR'), 'date' => new DateTime(), 'description' => 'Creation'));
                 $this->view->success = 1;
                 $this->_helper->sessionMessenger('User created successfully.', 'success');
                 return $this->_helper->getHelper('Redirector')->gotoRoute(array(), 'adminUsers');
             } catch (Exception $e) {
                 $this->getResponse()->setHttpResponseCode(500);
                 $this->view->success = 0;
                 $message = 'development' == APPLICATION_ENV ? $e->getMessage() : 'Application error: AUCCA001';
                 $this->view->messages()->addMessage($message, 'error');
                 Logger::err($e->getMessage());
             }
         } else {
             // Submitted form data is invalid
             $this->getResponse()->setHttpResponseCode(500);
             $this->view->success = 0;
         }
     }
     $this->view->form = $form;
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * Insert test data into test DB.
  *
  * @return void
  */
 private static function insertTestData()
 {
     // Insert test data
     $roles = array('admin' => AclRoleService::create(array('name' => 'Administrator', 'description' => 'Site Administrator')), 'user' => AclRoleService::create(array('name' => 'User', 'description' => 'Regular user')), 'guest' => AclRoleService::create(array('name' => 'Guest', 'description' => 'Anonymous guest')));
     $resources = array('default' => AclResourceService::create(array('identifier' => 'mvc:default:all', 'name' => 'Global non-admin access')), 'userLogin' => AclResourceService::create(array('identifier' => 'mvc:default:user:login', 'name' => 'User login')), 'admin' => AclResourceService::create(array('identifier' => 'mvc:admin', 'name' => 'Admin interface')));
     AclPermissionService::create(array('role' => $roles['guest'], 'resource' => $resources['default'], 'name' => 'view'));
     AclPermissionService::create(array('role' => $roles['guest'], 'resource' => $resources['userLogin'], 'name' => 'view'));
     AclPermissionService::create(array('role' => $roles['admin'], 'resource' => $resources['admin'], 'name' => 'view'));
     #AclPermissionService::create(array('role' => $roles['admin'], 'resource' => $resources['adminIndex'], 'name' => 'view'));
     $userData = array(array('username' => 'admin', 'firstName' => 'admin', 'lastName' => 'istrator', 'role' => $roles['admin']), array('username' => 'testuser', 'firstName' => 'test', 'lastName' => 'er', 'role' => $roles['user']));
     $timeZone = TimeZoneService::create(array('name' => 'America/Los_Angeles'));
     $users = array();
     foreach ($userData as $u) {
         $user = UserService::create(array('role' => $u['role'], 'username' => $u['username'], 'password' => $u['username'], 'email' => $u['username'] . '@example.com', 'dateCreated' => new \DateTime(), 'lastConnect' => new \DateTime(), 'active' => 1, 'locked' => 0));
         $user->setPassword(UserService::encryptPassword($user->getPassword()));
         $profile = UserProfileService::create(array('user' => $user, 'firstName' => $u['firstName'], 'lastName' => $u['lastName'], 'phone' => '408-555-5555', 'website' => '', 'timeZone' => $timeZone));
         $user->setProfile($profile);
         #UserService::update();
         #UserProfileService::update();
         $users[$u['username']] = $user;
     }
 }