/** * @param string $url * @param string $endpointId * @throws Exception */ public function xSaveAction($url, $endpointId = null) { if (!$endpointId) { $endpoint = new WebhookEndpoint(); $endpoint->setScope($this->request->getScope(), $this->user->getAccountId(), $this->getEnvironmentId(true)); $endpoint->securityKey = Scalr::GenerateRandomKey(64); } else { $endpoint = WebhookEndpoint::findPk($endpointId); if (!$this->canEditEndpoint($endpoint)) { throw new Scalr_Exception_Core('Insufficient permissions to edit endpoint at this scope'); } } $validator = new Validator(); $validator->validate($url, 'url', Validator::URL); //check url unique within current level $criteria = []; $criteria[] = ['url' => $url]; if ($endpoint->endpointId) { $criteria[] = ['endpointId' => ['$ne' => $endpoint->endpointId]]; } switch ($this->request->getScope()) { case WebhookEndpoint::SCOPE_ENVIRONMENT: $criteria[] = ['level' => WebhookEndpoint::LEVEL_ENVIRONMENT]; $criteria[] = ['envId' => $endpoint->envId]; $criteria[] = ['accountId' => $endpoint->accountId]; break; case WebhookEndpoint::SCOPE_ACCOUNT: $criteria[] = ['level' => WebhookEndpoint::LEVEL_ACCOUNT]; $criteria[] = ['envId' => null]; $criteria[] = ['accountId' => $endpoint->accountId]; break; case WebhookEndpoint::SCOPE_SCALR: $criteria[] = ['level' => WebhookEndpoint::LEVEL_SCALR]; $criteria[] = ['envId' => null]; $criteria[] = ['accountId' => null]; break; } if (WebhookEndpoint::findOne($criteria)) { $validator->addError('url', 'Endpoint url must be unique within current scope'); } if (!$validator->isValid($this->response)) { return; } if ($endpoint->url != $url) { $endpoint->isValid = false; $endpoint->url = $url; } ////temporarily disable url validation per Igor`s request(see also webhooks/endpoints/view.js) $endpoint->isValid = true; $endpoint->save(); $this->response->success('Endpoint successfully saved'); $this->response->data(array('endpoint' => array('endpointId' => $endpoint->endpointId, 'url' => $endpoint->url, 'isValid' => $endpoint->isValid, 'validationToken' => $endpoint->validationToken, 'securityKey' => $endpoint->securityKey, 'scope' => $endpoint->getScope()))); }