/**
  * Save our results. Tearing down and unsetting the Wizard is handled by
  * in {@link runWizard()} and does not need to be implemented here.
  * 
  * @param string $cacheName
  * @return boolean TRUE if save was successful and tear-down/cleanup of the
  *		Wizard should ensue.
  * @access public
  * @since 10/24/05
  */
 function saveWizard($cacheName)
 {
     $harmoni = Harmoni::Instance();
     $authN = Services::getService("AuthN");
     $tokenM = Services::getService("AgentTokenMapping");
     $wizard = $this->getWizard($cacheName);
     $properties = $wizard->getAllValues();
     $type = $this->getMethod()->getType();
     $id = $authN->getUserId($type);
     $it = $tokenM->getMappingsForAgentId($id);
     while ($it->hasNext()) {
         $mapping = $it->next();
         if ($mapping->getAuthenticationType() == $type) {
             $tokens = $mapping->getTokens();
         }
     }
     if (isset($tokens)) {
         $method = $this->getMethod();
         $uname = $tokens->getUsername();
         // Validate the old password
         $oldTokens = $method->createTokens(array('username' => $uname, 'password' => $properties['old_password']));
         if (!$method->authenticateTokens($oldTokens)) {
             $error = "Invalid old password";
             $localizedError = dgettext("polyphony", "Invalid old password, please try again.") . "\n<br/>";
         } else {
             if ($properties['new_password'] != '' && $properties['new_password'] == $properties['n_p_again']) {
                 // Log the action
                 if (Services::serviceRunning("Logging")) {
                     $loggingManager = Services::getService("Logging");
                     $log = $loggingManager->getLogForWriting("Authentication");
                     $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes", "A format in which the acting Agent[s] and the target nodes affected are specified.");
                     $priorityType = new Type("logging", "edu.middlebury", "Event_Notice", "Normal events.");
                     $item = new AgentNodeEntryItem("Modify Agent", "Password changed for:\n<br/>&nbsp; &nbsp; &nbsp;" . htmlspecialchars($uname) . "\n<br/>&nbsp; &nbsp; &nbsp;" . htmlspecialchars($type->getKeyword()));
                     $item->addAgentId($id);
                     $log->appendLogWithTypes($item, $formatType, $priorityType);
                 }
                 $t_array = array("username" => $uname, "password" => $properties['new_password']);
                 $authNTokens = $method->createTokens($t_array);
                 // Add it to the system and login with new password
                 if ($method->supportsTokenUpdates()) {
                     $method->updateTokens($tokens, $authNTokens);
                     $harmoni->request->startNamespace("harmoni-authentication");
                     $harmoni->request->set("username", $uname);
                     $harmoni->request->set("password", $properties['new_password']);
                     $harmoni->request->endNamespace();
                     $authN->authenticateUser($type);
                     return TRUE;
                 }
             } else {
                 $error = "Invalid new password";
                 $localizedError = dgettext("polyphony", "Invalid new password, please try again.") . "\n<br/>";
             }
         }
     }
     if (isset($error)) {
         // Log the action
         if (Services::serviceRunning("Logging")) {
             $loggingManager = Services::getService("Logging");
             $log = $loggingManager->getLogForWriting("Authentication");
             $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes", "A format in which the acting Agent[s] and the target nodes affected are specified.");
             $priorityType = new Type("logging", "edu.middlebury", "Error", "Normal events.");
             $item = new AgentNodeEntryItem("Modify Agent", "Password change error:\n<br/>&nbsp; &nbsp; &nbsp;" . htmlspecialchars($error) . "\n<br/>for:\n<br/>&nbsp; &nbsp; &nbsp;" . $uname . "\n<br/>&nbsp; &nbsp; &nbsp;" . htmlspecialchars($type->getKeyword()));
             $item->addAgentId($id);
             $log->appendLogWithTypes($item, $formatType, $priorityType);
         }
         $this->closeWizard($cacheName);
         RequestContext::locationHeader($harmoni->request->quickURL("user", "change_password", array("error" => $localizedError)));
     }
 }
 /**
  * Log in a user if the username matches, but without checking the password,
  * as part of the admin-act-as process
  * 
  * @param <##>
  * @return boolean TRUE if tokens are valid.
  * @access public
  * @since 12/11/06
  */
 function _authenticateAdminActAsUserForType(AuthNTokens $authNTokens, Type $authenticationType)
 {
     $this->_checkType($authenticationType);
     // 		$this->destroyAuthenticationForType($authenticationType);
     if ($authNTokens) {
         $authNMethodManager = Services::getService("AuthNMethods");
         $authNMethod = $authNMethodManager->getAuthNMethodForType($authenticationType);
         // just check if the tokens exist, not if there is a correct password.
         $isValid = $authNMethod->tokensExist($authNTokens);
         // If the authentication was successful, get the AgentId from the mapping
         // system and record the result.
         if ($isValid) {
             $agentId = $this->_getAgentIdForAuthNTokens($authNTokens, $authenticationType);
             $authenticationTypeString = $this->_getTypeString($this->_adminActAsType);
             $_SESSION['__AuthenticatedAgents'][$authenticationTypeString] = $agentId;
             // Update any stale info that was previously loaded
             $properties = $authNMethod->getPropertiesForTokens($authNTokens);
             $displayName = $authNMethod->getDisplayNameForTokens($authNTokens);
             $agentManager = Services::getService("Agent");
             $agent = $agentManager->getAgent($agentId);
             $agent->updateDisplayName($displayName);
             $propertyManager = Services::getService("Property");
             $propertyManager->storeProperties($agentId->getIdString(), $properties);
             // Ensure that the Authorization Cache gets the new users
             $authorizationMgr = Services::getService("AuthZ");
             $isAuthorizedCache = $authorizationMgr->getIsAuthorizedCache();
             $isAuthorizedCache->dirtyUser();
         }
         // Log the success or failure
         if (Services::serviceRunning("Logging")) {
             $loggingManager = Services::getService("Logging");
             $log = $loggingManager->getLogForWriting("Authentication");
             $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes", "A format in which the acting Agent[s] and the target nodes affected are specified.");
             $priorityType = new Type("logging", "edu.middlebury", "Event_Notice", "Normal events.");
             if ($isValid) {
                 $item = new AgentNodeEntryItem("Admin Acting As", "Admin users: <br/>&nbsp;&nbsp;&nbsp;&nbsp;" . implode(", ", $_SESSION['__ADMIN_NAMES_ACTING_AS_OTHER']) . "<br/>Successfully authenticated as: <br/>&nbsp;&nbsp;&nbsp;&nbsp;" . htmlspecialchars($authenticationType->getKeyword()) . " <br/>&nbsp;&nbsp;&nbsp;&nbsp;" . htmlspecialchars($authNTokens->getIdentifier()));
                 $item->addAgentId($agentId);
                 $item->addUserIds();
                 $log->appendLogWithTypes($item, $formatType, $priorityType);
             }
         }
         return $isValid;
     } else {
         return false;
     }
 }