From 519339230cc3feff30520707b0c83ed69821c793 Mon Sep 17 00:00:00 2001 From: Kalyan H Date: Tue, 26 Oct 2021 13:27:03 +0600 Subject: [PATCH] added new API --- README.md | 103 ++++++++ src/Console/MakeIdpDriverCommand.php | 4 +- src/Console/stubs/sdk.stub | 215 +++++++++++++++++ src/Facades/IdpUser.php | 9 +- src/IdpUser.php | 131 ++-------- src/Idps/Wso2idp.php | 151 ++++++++---- src/Interfaces/IDPInterface.php | 22 -- src/SDK/Wso2Idp/IdpGlobal.php | 277 ++++++++++++++++++++++ src/SDK/Wso2Idp/Traits/RequestHandler.php | 39 +++ src/SDK/Wso2Idp/Wso2IdpUsers.php | 157 ++++++++++++ 10 files changed, 915 insertions(+), 193 deletions(-) create mode 100644 src/Console/stubs/sdk.stub create mode 100644 src/SDK/Wso2Idp/IdpGlobal.php create mode 100644 src/SDK/Wso2Idp/Traits/RequestHandler.php create mode 100644 src/SDK/Wso2Idp/Wso2IdpUsers.php diff --git a/README.md b/README.md index 8cdebb8..aa64aa8 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,109 @@ or using helper IdpUser()->create(array()); ``` +# Create you SDK +Run this command to create your own sdk class. +``` + php artisan make:idpdriver YourSDKName +``` +Now add the class in config idpUser.php config file. + +# API references +### Here all covered API references + +>1. Get Wso2 IDP User by ID + +``` +IdpUser()->setPayload('userID')->userInfo()->get(); +``` +or to get only response body + +``` +IdpUser() +->use('wso2idp') +->setPayload('userID') +->userInfo() +->onlyBody() +->get(); +``` +Here - + +----- + +```use('yourSDK')``` `optional` set your custom SDK. + +```onlyBody()``` `optional` return only response from IDP server/end API + +---- + + +```get()``` return response as `array` + +```asObject()``` return response as `object` + +```asJson()``` return response as `json` + +---- + +>2. Create IDP user and get created user info + +``` + $response = IdpUser()->setPayload([ + 'first_name' => 'Kalyan', + 'last_name' => 'Kalyan', + 'username' => 'Kalyan4', + 'email' => 'Kalyan4@gmail.com', + 'mobile' => '01945602071', + 'user_type' => '2', + 'active' => true, + 'department' => 'Kalyan', + ])->create()->get(); +``` + +>3. Update User By User ID + +_you can provide single field or multiple field at the same time_ + +``` + $response = IdpUser()->setPayload([ + 'id' =>'UserID', + 'username' => 'Kalyan3', + 'account_status' => 1, + 'mobile' => '01945602071' + ])->update()->get(); +``` +here `id` and `username` is mendatory. You can provide following field to update & create - + +| Key | Details | +| ------ | ------ | +| first_name | Update `givenName` | +| last_name | [Update `familyName` | +| email | Update `emails` | +| mobile | Update `phoneNumbers` | +| user_type | Update `userType`| +| account_status | Update `accountStatus` | +| department | Update `department` | +| organization | Update `organization` | +| country | Update `country` | +| password | Update `password` | + + +>4. Delete single/bulk IDP User + +``` + $userID = 'ID'; + $response = IdpUser() + ->use('wso2idp') + ->setPayload($userID) + ->delete() + ->get(); + +``` +here - `$userID` can be single user ID or array of user ID. + + + + ## Adding new Gateway ## .env Config diff --git a/src/Console/MakeIdpDriverCommand.php b/src/Console/MakeIdpDriverCommand.php index b4e7d0c..fc49def 100644 --- a/src/Console/MakeIdpDriverCommand.php +++ b/src/Console/MakeIdpDriverCommand.php @@ -34,7 +34,7 @@ class MakeIdpDriverCommand extends GeneratorCommand */ protected function getStub() { - return __DIR__.'/stubs/gateway.stub'; + return __DIR__.'/stubs/sdk.stub'; } /** @@ -46,6 +46,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace.'\Idps'; + return $rootNamespace.'\SDK'; } } diff --git a/src/Console/stubs/sdk.stub b/src/Console/stubs/sdk.stub new file mode 100644 index 0000000..c6b51b0 --- /dev/null +++ b/src/Console/stubs/sdk.stub @@ -0,0 +1,215 @@ +settings = (object) $settings; + } + + public function userInfo($userID) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->userInfo($userID); + return $this; + } + + public function findUsers($userID) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->findUsers($userID); + return $this; + } + + /** + * @param $recipient + * @param $message + * @param null $params + * + * @return object + */ + public function create($userInfo) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->create($userInfo); + return $this; + } + + + /** + * Update user + * @param array $userInformation + * @return mixed|void + */ + public function update(array $userInformation) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->update($userInformation); + return $this; + } + + + public function delete( $userInformation = null) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->delete($userInformation); + return $this; + } + + + /** + * set response type + * @return $this + */ + public function get() + { + if($this->onlyBody){ + return $this->response['data']; + } + return $this->response; + } + /** + * set response type + * @return $this + */ + public function asObject() + { + if($this->onlyBody){ + return (object) $this->response['data']; + } + return (object) $this->response; + } + + /** + * set response type + * @return $this + */ + public function asJson() + { + if($this->onlyBody){ + return json_encode ($this->response['data']); + } + return (object) $this->response; + } + + /** + * set pertial response + * @return $this + */ + public function onlyBody() + { + $this->onlyBody = true; + return $this; + } + + /** + * initialize the is_success parameter. + * + * @return bool + */ + public function isSuccessful(): bool + { + return $this->response['status']; + } + + /** + * assign the message ID as received on the response,auto generate if not available. + * + * @return string + */ + public function getResponseMessage() + { + return $this->response['message']; + } + + /** + * @return int + */ + public function getResponseCode() + { + return $this->response['code']; + } + + /** + * @return mixed|string + */ + public function getUserID() + { + return $this->user_id; + } + + public function fixNumber($number){ + $validCheckPattern = "/^(?:\+88|01)?(?:\d{11}|\d{13})$/"; + if(preg_match($validCheckPattern, $number)){ + if(preg_match('/^(?:01)\d+$/', $number)){ + $number = '+88' . $number; + } + + return $number; + } + + return false; + } + } diff --git a/src/Facades/IdpUser.php b/src/Facades/IdpUser.php index 931a001..d7de70d 100644 --- a/src/Facades/IdpUser.php +++ b/src/Facades/IdpUser.php @@ -3,14 +3,11 @@ namespace Khbd\LaravelWso2IdentityApiUser\Facades; use Illuminate\Support\Facades\Facade; +use phpDocumentor\Reflection\Types\Mixed_; /** - * @method static gateway(string $gateway) - * @method static send(string $recipient, string $message, $params = null) - * @method static bool is_successful() - * @method static getMessageID() - * @method static getBalance() - * @method static object getDeliveryReports(\Illuminate\Http\Request $request) + * @method static use(string $sdk) + * @method static setPayload($payload) * * @see \Khbd\LaravelWso2IdentityApiUser\IdpUser */ diff --git a/src/IdpUser.php b/src/IdpUser.php index c668f41..7b2849e 100644 --- a/src/IdpUser.php +++ b/src/IdpUser.php @@ -4,8 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; -use Khbd\LaravelWso2IdentityApiUser\Models\SmsHistory; - +use Khbd\LaravelWso2IdentityApiUser\SDK\Wso2Idp\Wso2Idp; class IdpUser @@ -36,7 +35,6 @@ class IdpUser */ protected $object = null; - /** * @var array */ @@ -55,16 +53,22 @@ public function __construct() $this->mapGateway(); } + public function __call($function, $args) + { + $payload = empty($args) ? [$this->payload]: $args; + return call_user_func_array([$this->object, $function], $payload); + } + /** - * Change the gateway on the fly. + * Change the sdk on the fly. * - * @param $gateway + * @param $sdk * * @return $this */ - public function gateway($gateway) + public function use($sdk) { - $this->gateway = $gateway; + $this->gateway = $sdk; $this->mapGateway(); return $this; @@ -83,119 +87,24 @@ public function setPayload($payload) return $this; } + /**************** + * Private functions + */ + /** *map the gateway that will be used to send. */ private function mapGateway() { $this->settings = $this->config['gateways'][$this->gateway]; + $this->settings['idp_log'] = $this->config['idp_log']; $class = $this->config['map'][$this->gateway]; - $this->object = new $class($this->settings); - } - /** - * @param $recipient - * @param $message - * @param null $params - * - * @return mixed - */ - public function request($recipient, $message, $params = null) - { - if($this->config['sms_activate'] == false) { - return false; - } - if($this->config['sms_log']) { - $this->beforeSend($recipient, $message, $params = null); + if(is_callable([$class, '__construct'], true, $callable_name)){ + $this->object = new $class($this->settings); + }else{ + throw new \Exception("Unknown SDK. Make sure you have defined the sdk in the config file.", 422); } - if(method_exists($this->object, 'fixNumber') && !$recipient = $this->object->fixNumber($recipient)){ - return false; - } - $object = $this->object->send($recipient, $message, $params); - if($this->config['sms_log']) { - $this->afterSend(); - } - - return $object; - } - - /** - * define when the a message is successfully sent. - * - * @return bool - */ - public function is_successful() - { - return $this->object->is_successful(); - } - /** - * return api response getResponseBody - * - * @return object | array - */ - public function getResponseBody() - { - return $this->object->getResponseBody(); - } - - /** - * the message ID as received on the response. - * - * @return mixed - */ - public function getMessageID() - { - return $this->object->getMessageID(); - } - - /** - * @return mixed - */ - public function getBalance() - { - return $this->object->getBalance(); - } - - /** - * @param Request $request - * - * @return mixed - */ - public function getDeliveryReports(Request $request) - { - return $this->object->getDeliveryReports($request); - } - - private function beforeSend($recipient, $message, $params = null){ - try{ - $history = new SmsHistory(); - $history->mobile_number = $recipient; - $history->message = $message; - $history->gateway = $this->gateway; - $history->created_at = now(); - $history->save(); - $this->smsRecord = $history; - } catch (\Exception $exception){ - Log::debug("Faild to save sms message. " . $exception->getMessage()); - } - } - private function afterSend(){ - try{ - $status = 2; - if($this->is_successful()){ - $status = 1; - } - - if(is_object($this->smsRecord)){ - $this->smsRecord->status = $status; - $this->smsRecord->sms_submitted_id = $this->getMessageID(); - $this->smsRecord->api_response = json_encode($this->getResponseBody()); - $this->smsRecord->save(); - } - - }catch (\Exception $exception){ - $exception->getMessage(); - } } } diff --git a/src/Idps/Wso2idp.php b/src/Idps/Wso2idp.php index c6806d6..038d96a 100644 --- a/src/Idps/Wso2idp.php +++ b/src/Idps/Wso2idp.php @@ -3,7 +3,7 @@ namespace Khbd\LaravelWso2IdentityApiUser\Idps; use Khbd\LaravelWso2IdentityApiUser\Interfaces\IDPInterface; -use Khbd\LaravelWso2IdentityApiUser\SDK\Wso2Idp\Wso2Idp as IDPGateway; +use Khbd\LaravelWso2IdentityApiUser\SDK\Wso2Idp\Wso2IdpUsers; use Illuminate\Http\Request; class Wso2idp implements IDPInterface @@ -32,15 +32,33 @@ class Wso2idp implements IDPInterface */ protected $message; + + /** + * @var mixed + */ + protected $onlyBody; + + /** + * @var object + */ + protected $asObject; + + /** + * @var json + */ + protected $asJson; + /** * @var object */ - public $data; + protected $data; /** * @var object | array */ - public $response; + protected $response; + + /** * @param $settings @@ -49,11 +67,23 @@ class Wso2idp implements IDPInterface */ public function __construct($settings) { - // initiate settings (username, api_key, etc) - $this->settings = (object) $settings; } + public function userInfo($userID) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->userInfo($userID); + return $this; + } + + public function findUsers($userID) + { + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->findUsers($userID); + return $this; + } + /** * @param $recipient * @param $message @@ -61,12 +91,10 @@ public function __construct($settings) * * @return object */ - public function create(array $userInfo) + public function create($userInfo) { - - $AT = new IDPGateway($this->settings->base_url, $this->settings->username, $this->settings->api_key, $this->settings->from); - $this->response = $AT->create(); - + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->create($userInfo); return $this; } @@ -78,80 +106,99 @@ public function create(array $userInfo) */ public function update(array $userInformation) { - + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->update($userInformation); + return $this; } - /** - * initialize the is_success parameter. - * - * @return bool - */ - public function isSuccessful(): bool + + public function delete( $userInformation = null) { - return $this->is_success; + $AT = new Wso2IdpUsers($this->settings->base_url, $this->settings->username, $this->settings->password, $this->settings->idp_log ); + $this->response = $AT->delete($userInformation); + return $this; } + /** - * initialize the getResponseBody parameter. - * - * @return bool + * set response type + * @return $this */ - public function getResponseBody() + public function get() { + if($this->onlyBody){ + return $this->response['data']; + } return $this->response; } - /** - * assign the message ID as received on the response,auto generate if not available. - * - * @return mixed + * set response type + * @return $this */ - public function getResponseMessage() + public function asObject() { - return $this->message; + if($this->onlyBody){ + return (object) $this->response['data']; + } + return (object) $this->response; } - public function getResponseCode() + /** + * set response type + * @return $this + */ + public function asJson() { - $this->response_code; + if($this->onlyBody){ + return json_encode ($this->response['data']); + } + return (object) $this->response; } - public function getUserID() + /** + * set pertial response + * @return $this + */ + public function onlyBody() { - return $this->user_id; + $this->onlyBody = true; + return $this; } /** - * auto generate if not available. + * initialize the is_success parameter. + * + * @return bool */ - public function getBalance() + public function isSuccessful(): bool { - $AT = new IDPGateway($this->settings->base_url, $this->settings->username, $this->settings->api_key, $this->settings->from); - return $AT->balance(); + return $this->response['status']; } /** - * @param Request $request + * assign the message ID as received on the response,auto generate if not available. * - * @return object + * @return string */ - public function getDeliveryReports(Request $request) + public function getResponseMessage() { - $status = $request->status; - - if ($status == 'Failed' || $status == 'Rejected') { - $fs = $request->failureReason; - } else { - $fs = $status; - } + return $this->response['message']; + } - $data = [ - 'status' => $fs, - 'message_id' => $request->id, - 'phone_number' => '', - ]; + /** + * @return int + */ + public function getResponseCode() + { + return $this->response['code']; + } - return (object) $data; + /** + * @return mixed|string + */ + public function getUserID() + { + return $this->user_id; } public function fixNumber($number){ diff --git a/src/Interfaces/IDPInterface.php b/src/Interfaces/IDPInterface.php index b59c257..e8fe2d9 100644 --- a/src/Interfaces/IDPInterface.php +++ b/src/Interfaces/IDPInterface.php @@ -13,21 +13,6 @@ interface IDPInterface */ public function __construct($settings); - /** - * @param array $userInformation - * - * @return mixed - */ - public function create(array $userInformation); - - - /** - * @param array $userInformation - * - * @return mixed - */ - public function update(array $userInformation); - /** * define when the a message is successfully sent. * @@ -42,13 +27,6 @@ public function isSuccessful(); */ public function getUserID(); - /** - * the message API response - * - * @return object - */ - public function getResponseBody(); - /** * the message API response code * diff --git a/src/SDK/Wso2Idp/IdpGlobal.php b/src/SDK/Wso2Idp/IdpGlobal.php new file mode 100644 index 0000000..a83de7b --- /dev/null +++ b/src/SDK/Wso2Idp/IdpGlobal.php @@ -0,0 +1,277 @@ +apiUrl = $apiUrl; + $this->apiUsername = $apiUsername; + $this->apiPassword = $apiPassword; + $this->enabledDebug = $enabledDebug; + } + + public function getAPIUsername(){ + return $this->apiUsername; + } + public function getAPIPassword(){ + return $this->apiPassword; + } + public function isEnabledDebug(){ + return $this->enabledDebug; + } + + public function endpointUserInfo($userID){ + return $this->apiUrl . '/scim2/Users/'.$userID; + } + public function endpointUserCreate(){ + return $this->apiUrl . '/scim2/Users'; + } + public function endpointUserUpdate($userID){ + return $this->apiUrl . '/scim2/Users/' . $userID; + } + + public function endpointUserFiltering($args){ + $pageNo = $args['page'] ?? 1; + $countPerPage = $args['count'] ?? 10; + $filter = null; + if(isset($args['filter'])){ + '&filter='. $args['filter']; + } + $startFrom = ($pageNo -1) * $countPerPage; + + return $this->apiUrl .'/scim2/Users?startIndex=' . $startFrom . '&count=' . $countPerPage.$filter; + } + + /** + * @param $message + * @param array $data + * @param int $code + * @param false $status + * @return array + */ + public function response($message, $data = [], $code = 422, $status = false){ + return [ + 'status' => $status, + 'code' => $code, + 'message' => $message, + 'data' => $data + ]; + } + /** + * @param $message + * @param array $data + * @param int $code + * @param false $status + * @return array + */ + public function prepareResponse(Response $response, array $data = [], $customMessage = null){ + $message = 'Operation Successful.'; + $responseData = $data; + + if($response->serverError() || $response->clientError()){ + $message = 'Operation Not Successful.'; + } + if($response->serverError()){ + $message = 'Internal Idp Server Error, Please chaeck your payload and request carefully.'; + } + if($response->serverError() || $response->clientError()){ + + $responseArray = $response->json(); + + if(isset($responseArray['detail']) && !empty($responseArray['detail'])){ + $message = $responseArray['detail']; + }else if(isset($responseArray['scimType']) && !empty($responseArray['scimType'])){ + $message = $responseArray['scimType']; + } else { + if(isset($responseArray['schemas']) && is_array($responseArray['schemas'])){ + $message = implode(", ", $responseArray['schemas'] ); + } else if (isset($responseArray['schemas']) && !is_array($responseArray['schemas'])){ + $message = $responseArray['schemas']; + } + } + $this->logInfo($message, [ + 'body' => $response->body(), + 'collect' => $response->collect(), + 'status_code' => $response->status(), + 'is_response_200' => $response->ok(), + 'is_successful' => $response->successful(), + 'is_failed' => $response->failed() , + 'is_serverError' => $response->serverError() , + 'is_clientErro' => $response->clientError(), + 'headers' => $response->headers(), + ]); + } + + if(empty($data) && !empty($response->json())){ + $responseData = $response->json(); + } else if(!empty($data) && !empty($response->json())){ + $responseData = array_merge($data, $response->json()); + } + + return [ + 'status' => $response->successful(), + 'code' => $response->status(), + 'message' => $message, + 'data' => $responseData + ]; + } + + public function logInfo($message, $array = []){ + if($this->isEnabledDebug()) + Log::debug("IDP Log:: ".$message, (array) $array); + } + + public function prepareUserInfoToBeCreated($userinfo) + { + $first_name = $userinfo['first_name'] ?? null; + $last_name = $userinfo['last_name'] ?? null; + $username = $userinfo['username'] ?? null; + $email = $userinfo['email'] ?? null; + $mobile = $userinfo['mobile'] ?? null; + $user_type = $userinfo['user_type'] ?? null; + $active = $userinfo['active'] ?? true; + $accountState = $userinfo['account_status'] ?? 'UNLOCKED'; + $department = $userinfo['department'] ?? null; + $organization = $userinfo['organization'] ?? null; + $country = $userinfo['country'] ?? 'Bangladesh'; + $password = $userinfo['password'] ?? '12345678'; + + if(empty($first_name) || empty($last_name) || empty($username) || empty($email) || empty($mobile) || empty($user_type) || empty($active)){ + $this->logInfo("missing necessary user property. Provided user Info - ", (array) $userinfo); + throw new \Exception("missing necessary user property", 422); + } + + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + $this->logInfo("Invalid provided email. Provided user Info - ", (array) $userinfo); + throw new \Exception("Invalid provided email", 422); + } + + return [ + 'schemas' => [ + "urn:ietf:params:scim:schemas:core:2.0:User", + "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" + ], + 'name' => [ + 'familyName' => $first_name, + 'givenName' => $last_name, + ], + 'organization' => $organization, + 'userName' => $username, + 'active' => $active, + 'password' => $password, + 'userType' => $user_type, + 'country' => $country, + 'accountState' => $accountState, + 'emails' => [ + 0 => $email + ], + 'phoneNumbers' => [ + [ + "value" => $mobile, + 'type' => 'mobile', + "primary" => "false" + ] + ] + ]; + } + + public function prepareUserInfoToBeUpdated($userinfo) + { + $ID = $userinfo['id'] ?? null; + $first_name = $userinfo['first_name'] ?? null; + $last_name = $userinfo['last_name'] ?? null; + $username = $userinfo['username'] ?? null; + $email = $userinfo['email'] ?? null; + $mobile = $userinfo['mobile'] ?? null; + $user_type = $userinfo['user_type'] ?? null; + $account_status = $userinfo['account_status'] ?? 'UNLOCKED'; + $department = $userinfo['department'] ?? null; + $organization = $userinfo['organization'] ?? null; + $country = $userinfo['country'] ?? 'Bangladesh'; + $password = $userinfo['password'] ?? null; + + if(empty($ID)){ + $this->logInfo("missing user ID. Provided user Info - ", (array) $userinfo); + throw new \Exception("ID is mendatory.", 422); + } + if(empty($username)){ + $this->logInfo("missing username. Provided user Info - ", (array) $userinfo); + throw new \Exception("username is mendatory.", 422); + } + + if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { + $this->logInfo("Invalid provided email. Provided user Info - ", (array) $userinfo); + throw new \Exception("Invalid provided email", 422); + } + + // "add" and "replace" + $payload = []; + + $payload['schemas'] = [ + "urn:ietf:params:scim:api:messages:2.0:PatchOp" + ]; + $values = []; + + if(!empty($first_name)){ + $values['name']['givenName'] = $first_name; + } + + if(!empty($last_name)){ + $values['name']['familyName'] = $last_name; + } + + if(in_array($account_status, ['0', '1', '2', '3', '4', '5'])){ + # more about account state https://is.docs.wso2.com/en/latest/learn/pending-account-status/ + // $values['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['accountState'] = '1'; + } + if(!empty($organization)){ + $values['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['organization'] = $organization; + } + if(!empty($country)){ + $values['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['country']= $country; + } + + + if(!empty($password)){ + $values['password'] = $password; + } + + if(!empty($user_type)){ + $values['userType'] = $user_type; + } + + if(!empty($email)){ + $values['emails'] = [ + 0 => $email + ]; + } + if(!empty($mobile)){ + $values['phoneNumbers'] = [ + [ + "value" => $mobile, + 'type' => 'mobile', + "primary" => "false" + ] + ]; + } + $payload['Operations'][] = [ + "op" => "replace", + "value" => $values + ]; + + Log::debug(json_encode( $payload)); + + return $payload; + } + +} diff --git a/src/SDK/Wso2Idp/Traits/RequestHandler.php b/src/SDK/Wso2Idp/Traits/RequestHandler.php new file mode 100644 index 0000000..7c99180 --- /dev/null +++ b/src/SDK/Wso2Idp/Traits/RequestHandler.php @@ -0,0 +1,39 @@ +config = config('IdpUser'); + $this->isDebugEnabled = $this->config['idp_log']; + } + public function post(){ + + try{ + $response = Http::get('http://exa9mples.com'); + }catch(\exception $exception){ + dd($exception->getMessage()); + } + + dd([ + $response->body(), + $response->json() , + $response->object() , + $response->collect(), + $response->status(), + $response->ok(), + $response->successful(), + $response->failed() , + $response->serverError() , + $response->clientError(), + $response->headers() , + ]); + } + +} diff --git a/src/SDK/Wso2Idp/Wso2IdpUsers.php b/src/SDK/Wso2Idp/Wso2IdpUsers.php new file mode 100644 index 0000000..4a0cb19 --- /dev/null +++ b/src/SDK/Wso2Idp/Wso2IdpUsers.php @@ -0,0 +1,157 @@ +prepareUserInfoToBeCreated($userData); + + $response = Http::withBasicAuth($this->getApiUsername(), $this->getAPIPassword()) + ->withHeaders([ + 'Content-Type' => 'application/json' + ]) + ->withOptions([ + 'verify' => false + ]) + ->post($this->endpointUserCreate(), $payload); + } catch (\Exception $exception){ + $exceptionInfo = $this->response($exception->getMessage(), $userData, $exception->getCode(), false); + $this->logInfo($exception->getMessage(), $exceptionInfo); + return $exceptionInfo; + } + + return $this->prepareResponse($response); + } + + public function update($userData){ + try { + $payload = $this->prepareUserInfoToBeUpdated($userData); + + $response = Http::withBasicAuth($this->getApiUsername(), $this->getAPIPassword()) + ->withHeaders([ + 'Content-Type' => 'application/json' + ]) + ->withOptions([ + 'verify' => false + ]) + ->patch($this->endpointUserUpdate($userData['id']), $payload); + } catch (\Exception $exception){ + $exceptionInfo = $this->response($exception->getMessage(), $userData, $exception->getCode(), false); + $this->logInfo($exception->getMessage(), $exceptionInfo); + return $exceptionInfo; + } + + return $this->prepareResponse($response); + } + + + public function userInfo($userID){ + try { + if(empty($userID)){ + $this->logInfo("missing user ID."); + throw new \Exception("ID is mendatory.", 422); + } + + $response = Http::withBasicAuth($this->getApiUsername(), $this->getAPIPassword()) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Accept' => 'application/scim+json' + ]) + ->withOptions([ + 'verify' => false + ]) + ->get($this->endpointUserInfo($userID)); + return $this->prepareResponse($response); + + } catch (\Exception $exception){ + $exceptionInfo = $this->response($exception->getMessage(), $userInfo, $exception->getCode(), false); + $this->logInfo($exception->getMessage(), $exceptionInfo); + return $exceptionInfo; + } + } + + public function findUsers($filter){ + try { + if(empty($filter)){ + $filter = [ + 'page' => 1, + 'count' => 10, + 'filter' => '' + ]; + } + if(isset($filter['filter']) && empty($filter['filter'])){ + unset($filter['filter']); + } + + $response = Http::withBasicAuth($this->getApiUsername(), $this->getAPIPassword()) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Accept' => 'application/scim+json' + ]) + ->withOptions([ + 'verify' => false + ]) + ->get($this->endpointUserFiltering($filter)); + return $this->prepareResponse($response); + + } catch (\Exception $exception){ + $exceptionInfo = $this->response($exception->getMessage(), $filter, $exception->getCode(), false); + $this->logInfo($exception->getMessage(), $exceptionInfo); + return $exceptionInfo; + } + } + + public function delete($userInfo){ + try { + if(empty($userInfo)){ + $this->logInfo("missing user ID."); + throw new \Exception("ID is mendatory.", 422); + } + if(is_array($userInfo)){ + $responseArr = []; + $hasFailedResponse = false; + $hasSuccessResponse = false; + foreach ($userInfo as $ID){ + $response = $this->deleteSingleUser($ID); + if(!$response->successful()){ + $hasFailedResponse = true; + }else{ + $hasSuccessResponse = true; + } + $tmpResponse = $this->prepareResponse($response); + $tmpResponse['data']['userID'] = $ID; + $responseArr[] = $tmpResponse; + } + return $this->response($hasFailedResponse ? "Action pertiaally or complaitly failed.":"Action successfully completed.", $responseArr, $hasSuccessResponse ? 200 : 206, $hasSuccessResponse ? true : false); + }else{ + $response = $this->deleteSingleUser($userInfo); + return $this->prepareResponse($response); + } + } catch (\Exception $exception){ + $exceptionInfo = $this->response($exception->getMessage(), $userInfo, $exception->getCode(), false); + $this->logInfo($exception->getMessage(), $exceptionInfo); + return $exceptionInfo; + } + } + + private function deleteSingleUser($ID){ + $response = Http::withBasicAuth($this->getApiUsername(), $this->getAPIPassword()) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'Accept' => 'application/scim+json' + ]) + ->withOptions([ + 'verify' => false + ]) + ->delete($this->endpointUserUpdate($ID)); + return $response; + } +}