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)); }
/** * @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; }