Example #1
0
 /**
  * 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;
 }
Example #2
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;
     }
 }