/**
  * Create a user with OneLogin API
  * @throws Exception
  */
 public function olCreateAsyncAction()
 {
     // No need to render
     $this->_helper->viewRenderer->setNoRender();
     // Create json object
     $json = new stdClass();
     $json->error = 0;
     $json->message = "";
     // The entire process
     try {
         // User must be an array
         $user = array();
         // Get parameters and set values in array
         $user['email'] = $this->_getParam("email");
         $user['firstname'] = $this->_getParam("firstname");
         $user['lastname'] = $this->_getParam("lastname");
         $user['password'] = $this->_getParam("password");
         // Create API instance and create the user
         $users = new OneLogin_Api_Users();
         $response = $users->createUser($user);
         //var_dump($response); exit();
         if ($response->isSuccessful()) {
             $message = 'User ' . str_replace("'", '', trim($response->getMessage()));
             $json->message = $message;
             // User is created, can we set the password ?
             $responseCreate = $users->setUserPasswordByUserName($user);
             if ($responseCreate->isSuccessful()) {
                 $json->message .= ' password has been set. ' . str_replace("'", '', trim($responseCreate->getMessage()));
             } else {
                 $json->message .= ' password could not be set. ' . str_replace("'", '', trim($responseCreate->getMessage()));
             }
         } else {
             throw new Exception(str_replace("'", '', trim($response)));
         }
     } catch (Exception $e) {
         $json->error = 1;
         $json->message = str_replace("'", '', trim($e->getMessage()));
     }
     echo $this->_helper->json($json);
     // Send response
 }
 /**
  * Add user to Application defined
  * 
  */
 public function addUsersAsyncAction()
 {
     ini_set('max_execution_time', 300);
     //300 seconds = 5 minutes
     // No need to render
     $this->_helper->viewRenderer->setNoRender();
     // Create json object
     $json = new stdClass();
     $json->error = 0;
     $json->message = "";
     // The entire process
     try {
         // Get users to add from database
         $usersToImport = new OneLogin_Support_UsersToImport();
         $users = $usersToImport->listForExport();
         // Create API instance and create the user
         $usersApi = new OneLogin_Api_Users();
         // Proceed to create the user
         foreach ($users as $user) {
             $data = $this->_prepareUser($user);
             // Prepare Log params
             $logParams = $kv = null;
             foreach ($data as $key => $value) {
                 $kv[] = "{$key}={$value}";
             }
             $logParams = join("&", $kv);
             $response = $usersApi->createUser($data);
             //var_dump($response); exit();
             if ($response->isSuccessful()) {
                 $message = 'User ' . str_replace("'", '', trim($response->getMessage()));
                 $json->message = $message;
                 $this->_addToLog('AddUsersAsync', $message, $logParams);
                 // User is created, can we set the password ?
                 $responseCreate = $usersApi->setUserPasswordByUserName($data);
                 if ($responseCreate->isSuccessful()) {
                     $json->message .= ' password has been set. ' . str_replace("'", '', trim($responseCreate->getMessage()));
                 } else {
                     $json->message .= ' password could not be set. ' . str_replace("'", '', trim($responseCreate->getMessage()));
                     $logMessage = 'Password for ' . $user->email . ' could not be set.';
                     $this->_addToLog('AddUsersAsync', $logMessage, $logParams . '<br/>-HTTPResponse-' . $responseCreate->getRawBody());
                 }
                 // Remove user added
                 $usersToImport->delete(array("email=?" => $user->email));
             } else {
                 $message = 'User could not be created.' . $user->email . '<br/>' . $response->getRawBody();
                 $this->_addToLog('AddUsersAsync', $message . $response->getRawBody(), $logParams);
                 throw new Exception($message);
             }
         }
         $json->message = '<div class="successMessage">Process complete</div>';
     } catch (Exception $e) {
         $message = str_replace("'", '', trim($e->getMessage()));
         $json->error = 1;
         $json->message = '<div class="errorMessage">Error while processing your request<br/>' . $message . '<br/>Please contact your system administrator.</div>';
     }
     echo $this->_helper->json($json);
     // Send response
 }