/** * Return status description based on id * * @param integer $statusId * @return string Status */ public function getOlUserStatus($userName) { $status = ""; $userId = 0; $userName = trim(strtolower($userName)); try { $users = new OneLogin_Api_Users(); $response = $users->getUser($userName); if ($response->isSuccessful()) { //echo $xmlResult; $xmlResult = simplexml_load_string($response->getBody()); $elementsRequired = array('status', 'id'); foreach ($xmlResult as $child) { if (in_array(strtolower($child->getName()), $elementsRequired)) { $tagName = trim(str_replace('-', ' ', $child->getName())); $className = trim(str_replace('-', '', $child->getName())); // Get status description if needed if (strtolower($className) == 'status') { $status = self::_getUserStatus(trim($child)); } if (strtolower($className) == 'id') { $userId = trim($child); } } // End if not in array } // end foreach } else { //var_dump($response);exit(); throw new Exception($response->getMessage() . ' ' . $response->getBody()); } } catch (Exception $e) { $msg = $e->getMessage(); //var_dump($e->getMessage()); exit(); // DEBUG ONLY $status = ""; } return array($status, $userId); }
/** * 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 }