예제 #1
0
 /**
  * on-enter callback for the "submit" state.
  * Should validate the data and then submit the data to AG
  *
  * @param $action
  * @param $flowScope
  */
 public function validateFormAndSubmit($action, &$flowScope)
 {
     $registry = Zend_Registry::getInstance();
     $translate = $registry->get("Zend_Translate");
     $validationErrors = array();
     $isNew = $flowScope['isNew'];
     /**
      * @var Auth $auth
      */
     $auth = $flowScope['auth'];
     $howMany = $flowScope['howMany'];
     if ($isNew && empty($howMany)) {
         $validationErrors['howMany'] = "Please choose how many Auths you would like";
     }
     $shouldValidateCreds = $isNew && $howMany === "1" || !$isNew;
     if ($shouldValidateCreds && $auth->type === AuthType::$AUTHKEY && empty($auth->authKeyAuth->keyValue)) {
         $validationErrors['authKey'] = $translate->translate("For authKey auth, you must specify a key");
     }
     if ($shouldValidateCreds && ($auth->type === AuthType::$BASIC || $auth->type === AuthType::$WSSE) && empty($auth->basicAuth->username) && empty($auth->wsseAuth->username)) {
         $validationErrors['username'] = $translate->translate("Username is required");
     }
     if ($shouldValidateCreds && ($auth->type === AuthType::$BASIC || $auth->type === AuthType::$WSSE) && empty($auth->basicAuth->password) && empty($auth->wsseAuth->password)) {
         $validationErrors['password'] = $translate->translate("Password is required");
     }
     if ($shouldValidateCreds && $auth->type === AuthType::$IPWHITELIST && empty($auth->ipWhiteListAuth->ips)) {
         $validationErrors['ipWhiteList'] = $translate->translate("IP list is required");
     }
     SharedViewUtility::validateHeaderTransformations($auth->headerTransformations, $validationErrors);
     SharedViewUtility::validateProperties($auth->properties, $validationErrors);
     SharedViewUtility::validateTdrRules($auth->tdrData, $validationErrors);
     if (count($validationErrors) > 0) {
         $flowScope['validationErrors'] = $validationErrors;
         return "invalid";
     }
     /**
      * Submit to AG
      */
     $authManager = new AuthManager();
     $creds = array();
     for ($i = 0; $i < $howMany; $i++) {
         // If we are doing a batch create, then generate the credentials
         if ($howMany > 1) {
             switch ($auth->type) {
                 case AuthType::$AUTHKEY:
                     $auth->authKeyAuth->keyValue = uniqid();
                     break;
                 case AuthType::$BASIC:
                     $auth->basicAuth->username = uniqid();
                     $auth->basicAuth->password = uniqid();
                     break;
                 case AuthType::$WSSE:
                     $auth->wsseAuth->username = uniqid();
                     $auth->wsseAuth->password = uniqid();
                     break;
             }
             $auth->id = "";
         }
         $result = $authManager->setAuth($auth, $isNew);
         if ($result->getHTTPCode() !== "200") {
             $xml = simplexml_load_string($result->getPayload());
             $validationErrors['default'] = (string) $xml->error->errorText;
             $flowScope['validationErrors'] = $validationErrors;
             return "invalid";
         }
         // Pull the credentials out for display
         switch ($auth->type) {
             case AuthType::$AUTHKEY:
                 $creds[$auth->id] = "key: " . $auth->authKeyAuth->keyValue;
                 break;
             case AuthType::$BASIC:
                 $creds[$auth->id] = "u/p: " . $auth->basicAuth->username . " / " . $auth->basicAuth->password;
                 break;
             case AuthType::$WSSE:
                 $creds[$auth->id] = "u/p: " . $auth->wsseAuth->username . " / " . $auth->wsseAuth->password;
                 break;
             case AuthType::$IPWHITELIST:
                 $creds[$auth->id] = implode("; ", $auth->ipWhiteListAuth->ips);
                 break;
         }
     }
     if ($isNew) {
         $this->_helper->FlashMessenger("Successfully Created Auth(s)");
         foreach ($creds as $key => $value) {
             $this->_helper->FlashMessenger("{$key} ({$value})");
         }
     } else {
         $this->_helper->FlashMessenger("Successfully Updated Auth");
     }
     $flowScope['authIds'] = array_keys($creds);
     return "valid";
 }