| | |
| | | <?php |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\Instance; |
| | | use JVBase\integrations\ServiceMap; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | | use WP_REST_Request; |
| | |
| | | |
| | | class IntegrationsRoutes extends Rest |
| | | { |
| | | |
| | | use Instance; |
| | | use ServiceMap; |
| | | /** |
| | | * Register REST routes |
| | | */ |
| | |
| | | Route::for('integrations') |
| | | ->post([$this, 'handleAction']) |
| | | ->args([ |
| | | 'service' => 'string|required|enum:'.implode(',',JVB()->getAvailableServices()), |
| | | 'service' => 'string|required|enum:'.implode(',',$this->getAvailableServices()), |
| | | 'action' => 'string|required', |
| | | 'user_id' => 'int', |
| | | 'context' => 'string|enum:admin,user', |
| | |
| | | if ($theUserID === false) { |
| | | return $this->validationError(['message' => 'Not logged in']); |
| | | } |
| | | $integration = JVB()->connect($service, $theUserID); |
| | | $integration = $this->getService($service); |
| | | if (!method_exists($integration, 'switchUser')) { |
| | | return $this->validationError(['message' => 'Cannot get user integration']); |
| | | } |
| | | |
| | | if (!$integration) { |
| | | if (!$integration || !method_exists($integration, 'processAction')) { |
| | | return $this->validationError(['message'=>'Invalid service']); |
| | | } |
| | | |
| | | $integration->switchUser($theUserID); |
| | | |
| | | $integration->getCredentials(); |
| | | |
| | | // Handle the action |
| | |
| | | $user_id = $request->get_param('user_id'); |
| | | $return_url = $request->get_param('return_url'); |
| | | |
| | | $integration = JVB()->connect($service, $user_id); |
| | | $theUserID = current_user_can('manage_options') ? null : get_current_user_id(); |
| | | if ($theUserID === false) { |
| | | return $this->validationError(['message' => 'Not logged in']); |
| | | } |
| | | $integration = $this->getService($service); |
| | | if (!method_exists($integration, 'switchUser')) { |
| | | return $this->validationError(['message' => 'Cannot get user integration']); |
| | | } |
| | | |
| | | if (!$integration || !$integration->isOAuthService) { |
| | | if (!$integration || !method_exists($integration, 'processAction')) { |
| | | return $this->validationError(['message'=>'Invalid service']); |
| | | } |
| | | |
| | | $auth_url = $integration->getOAuthUrl($return_url); |
| | | $integration->switchUser($theUserID); |
| | | |
| | | if (!$integration || !method_exists($integration, 'getOAuthBaseURL') || is_null($integration->getOAuthBaseURL())) { |
| | | return $this->validationError(['message'=>'Invalid service']); |
| | | } |
| | | |
| | | $auth_url = $integration->getOAuthBaseURL($return_url); |
| | | |
| | | if ($auth_url) { |
| | | return $this->success($auth_url); |