public function xGroupActionHandlerAction() { $this->request->defineParams(array('endpointIds' => array('type' => 'json'), 'action')); $processed = array(); $errors = array(); $endpoints = WebhookEndpoint::find(array(array('accountId' => $this->getEnvironment()->clientId), array('envId' => $this->getEnvironmentId()), array('endpointId' => array('$in' => $this->getParam('endpointIds'))))); foreach ($endpoints as $endpoint) { //todo: check is endpoint in use and forbid deleting if (count(WebhookConfigEndpoint::findByEndpointId($endpoint->endpointId)) == 0) { $processed[] = $endpoint->endpointId; $endpoint->delete(); } else { $errors[] = 'Endpoint is used by webhooks and can\'t be removed'; } } $num = count($this->getParam('endpointIds')); if (count($processed) == $num) { $this->response->success('Endpoints successfully processed'); } else { array_walk($errors, function (&$item) { $item = '- ' . $item; }); $this->response->warning(sprintf("Successfully processed only %d from %d endpoints. \nFollowing errors occurred:\n%s", count($processed), $num, join($errors, ''))); } $this->response->data(array('processed' => $processed)); }
private function getEndpoints() { $endpoints = array(); $criteria = []; switch ($this->request->getScope()) { case WebhookEndpoint::SCOPE_ENVIRONMENT: $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => $this->getEnvironmentId()], ['level' => WebhookEndpoint::LEVEL_ENVIRONMENT]]], ['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_SCALR]]]]]; break; case WebhookEndpoint::SCOPE_ACCOUNT: $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_SCALR]]]]]; break; case WebhookEndpoint::SCOPE_SCALR: $criteria[] = ['level' => WebhookEndpoint::LEVEL_SCALR]; $criteria[] = ['envId' => null]; $criteria[] = ['accountId' => null]; break; } foreach (WebhookEndpoint::find($criteria) as $entity) { $endpoints[] = ['id' => $entity->endpointId, 'url' => $entity->url, 'isValid' => $entity->isValid, 'scope' => $entity->getScope()]; } return $endpoints; }
/** * @param string $scope * @return array * @throws Scalr_Exception_Core */ private function getList($scope = '') { $endpoints = array(); $criteria = []; switch ($this->request->getScope()) { case WebhookEndpoint::SCOPE_ENVIRONMENT: $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => $this->getEnvironmentId()], ['level' => WebhookEndpoint::LEVEL_ENVIRONMENT]]], ['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_SCALR]]]]]; break; case WebhookEndpoint::SCOPE_ACCOUNT: $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => WebhookEndpoint::LEVEL_SCALR]]]]]; break; case WebhookEndpoint::SCOPE_SCALR: $criteria[] = ['level' => WebhookEndpoint::LEVEL_SCALR]; $criteria[] = ['envId' => null]; $criteria[] = ['accountId' => null]; break; } $scopeLinking = [ScopeInterface::SCOPE_SCALR => WebhookEndpoint::LEVEL_SCALR, ScopeInterface::SCOPE_ACCOUNT => WebhookEndpoint::LEVEL_ACCOUNT, ScopeInterface::SCOPE_ENVIRONMENT => WebhookEndpoint::LEVEL_ENVIRONMENT]; if ($scope && array_key_exists($scope, $scopeLinking)) { $criteria[] = ['level' => $scopeLinking[$scope]]; } foreach (WebhookEndpoint::find($criteria) as $entity) { $webhooks = array(); foreach (WebhookConfigEndpoint::findByEndpointId($entity->endpointId) as $WebhookConfigEndpoint) { $webhooks[$WebhookConfigEndpoint->webhookId] = WebhookConfig::findPk($WebhookConfigEndpoint->webhookId)->name; } $endpoint = array('endpointId' => $entity->endpointId, 'url' => $entity->url, 'scope' => $entity->getScope()); if ($this->request->getScope() == $entity->getScope()) { $endpoint['isValid'] = $entity->isValid; $endpoint['validationToken'] = $entity->validationToken; $endpoint['securityKey'] = $entity->securityKey; $endpoint['webhooks'] = $webhooks; } $endpoints[] = $endpoint; } return $endpoints; }
public function xSaveAction() { $this->request->defineParams(array('endpoints' => array('type' => 'json'), 'action', 'events' => array('type' => 'json'), 'action', 'farms' => array('type' => 'json'), 'action')); if (!$this->request->getParam('webhookId')) { $webhook = new WebhookConfig(); $webhook->level = WebhookConfig::LEVEL_ENVIRONMENT; $webhook->accountId = $this->getEnvironment()->clientId; $webhook->envId = $this->getEnvironmentId(); } else { $webhook = WebhookConfig::findPk($this->request->getParam('webhookId')); if ($webhook->envId != $this->getEnvironmentId() || $webhook->accountId != $this->getEnvironment()->clientId) { throw new Scalr_Exception_Core('Insufficient permissions to edit webhook'); } } $webhook->name = $this->request->getParam('name'); $webhook->postData = $this->request->getParam('postData'); $webhook->skipPrivateGv = $this->request->getParam('skipPrivateGv') == 'on' ? 1 : 0; $webhook->save(); //save endpoints $endpoints = $this->getParam('endpoints'); foreach (WebhookConfigEndpoint::findByWebhookId($webhook->webhookId) as $endpoint) { $index = array_search($endpoint->endpointId, $endpoints); if ($index === false) { $endpoint->delete(); } else { unset($endpoints[$index]); } } if (!empty($endpoints)) { $endpoints = WebhookEndpoint::find(array(array('accountId' => $this->getEnvironment()->clientId), array('envId' => $this->getEnvironmentId()), array('endpointId' => array('$in' => $endpoints)))); foreach ($endpoints as $endpoint) { $configEndpoint = new WebhookConfigEndpoint(); $configEndpoint->webhookId = $webhook->webhookId; $configEndpoint->setEndpoint($endpoint); $configEndpoint->save(); } } //save events $events = $this->getParam('events'); $allEvents = $this->getEventsList(); foreach (WebhookConfigEvent::findByWebhookId($webhook->webhookId) as $event) { $index = array_search($event->eventType, $events); if ($index === false) { if (isset($allEvents[$event->eventType])) { //20486-rebundlecomplete-emails - we shouldn't remove some events(RebundleComplete...) $event->delete(); } } else { unset($events[$index]); } } foreach ($events as $event) { /*if (!isset(EVENT_TYPE::getScriptingEvents()[$event])) { continue; }*/ $configEvent = new WebhookConfigEvent(); $configEvent->webhookId = $webhook->webhookId; $configEvent->eventType = $event; $configEvent->save(); } //save farms $farms = $this->getParam('farms'); if (empty($farms)) { $farms = array(0); } foreach (WebhookConfigFarm::findByWebhookId($webhook->webhookId) as $farm) { $index = array_search($farm->farmId, $farms); if ($index === false) { $farm->delete(); } else { unset($farms[$index]); } } foreach ($farms as $farmId) { $configFarm = new WebhookConfigFarm(); $configFarm->webhookId = $webhook->webhookId; $configFarm->farmId = $farmId; $configFarm->save(); } $endpoints = array(); foreach ($webhook->getEndpoints() as $endpoint) { $endpoints[] = $endpoint->endpointId; } $events = array(); foreach ($webhook->getEvents() as $event) { $events[] = $event->eventType; } $farms = array(); foreach ($webhook->getFarms() as $farm) { if ($farm->farmId) { $farms[] = $farm->farmId; } } $this->response->data(array('webhook' => array('webhookId' => $webhook->webhookId, 'name' => $webhook->name, 'postData' => $webhook->postData, 'skipPrivateGv' => $webhook->skipPrivateGv, 'endpoints' => $endpoints, 'events' => $events, 'farms' => $farms))); }