diff --git a/.env b/.env index 98ae7afe..701dac4c 100644 --- a/.env +++ b/.env @@ -70,10 +70,7 @@ APP_SUBDOMAIN=pc # he domains on wich you want to provide this component, the first wil be used as primary (or common in cert-manger terms) APP_DOMAINS=["conduction.nl","zaakonline.nl","larping.eu","common-ground.dev","trouwplanner.online","huwelijksplanner.online"] - -# The demo enviroment for this component @depracticed -APP_DEMO=pc.zaakonline.nl -# he Repository for this component +APP_DEMO=test.conduction.nl APP_REPRO=https://github.com/ConductionNL/Proto-component-commonground ################################################## @@ -137,4 +134,4 @@ NLX_ORGANIZATION_NAME=Conduction NLX_ORGANIZATION_UNIT_NAME=Common-Ground NLX_COMMON_NAME=zaakonline.nl NLX_EMAIL_ADDRESS=info@conduction.nl -NLX_CHALLANGE_PASSWORD= +NLX_CHALLANGE_PASSWORD= \ No newline at end of file diff --git a/INSTALLATION.md b/INSTALLATION.md index 5ed0a969..7ba8cd2c 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -104,23 +104,23 @@ $ helm dependency update ./api/helm ``` If you want to create a new instance ```CLI -$ helm install --name pc-dev ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=dev --set settings.env=dev,settings.debug=1 -$ helm install --name pc-stag ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=stag --set settings.env=stag,settings.debug=0 -$ helm install --name pc-prod ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=prod --set settings.env=prod,settings.debug=0 +$ helm install --name pa-dev ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=dev --set settings.env=dev,settings.debug=1 +$ helm install --name pa-stag ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=stag --set settings.env=stag,settings.debug=0 +$ helm install --name pa-prod ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=prod --set settings.env=prod,settings.debug=0 ``` Or update if you want to update an existing one ```CLI -$ helm upgrade pc-dev ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=dev --set settings.env=dev,settings.debug=1 -$ helm upgrade pc-stag ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=stag --set settings.env=stag,settings.debug=0 -$ helm upgrade pc-prod ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=prod --set settings.env=prod,settings.debug=0 +$ helm upgrade pa-dev ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=dev --set settings.env=dev,settings.debug=1 +$ helm upgrade pa-stag ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=stag --set settings.env=stag,settings.debug=0 +$ helm upgrade pa-prod ./api/helm --kubeconfig="api/helm/kubeconfig.yaml" --namespace=prod --set settings.env=prod,settings.debug=0 ``` Or del if you want to delete an existing one ```CLI -$ helm del pc-dev --purge --kubeconfig="api/helm/kubeconfig.yaml" -$ helm del pc-stag --purge --kubeconfig="api/helm/kubeconfig.yaml" -$ helm del pc-prod --purge --kubeconfig="api/helm/kubeconfig.yaml" +$ helm del pa-dev --purge --kubeconfig="api/helm/kubeconfig.yaml --namespace=dev" +$ helm del pa-stag --purge --kubeconfig="api/helm/kubeconfig.yaml --namespace=stag" +$ helm del pa-prod --purge --kubeconfig="api/helm/kubeconfig.yaml --namespace=prod" ``` Note that you can replace common ground with the namespace that you want to use (normally the name of your component). diff --git a/TUTORIAL.md b/TUTORIAL.md index d05815fc..0a956fdd 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -18,7 +18,7 @@ You can install docker-desktop from [the docker website](https://hub.docker.com/ ## Generating your component (repository/codebase) Starting up your first Common Ground component is extremely easy, al you need is a GitHub account and go the link below and fill in the form, press create and press to we have a component! -[https://github.com/ConductionNL/Proto-component-commonground/generate](https://github.com/ConductionNL/Proto-component-commonground/generate) +[https://github.com/ConductionNL/proto-application-commonground/generate](https://github.com/ConductionNL/proto-application-commonground/generate) After that you should be redirected to your own brand new repository. @@ -84,8 +84,19 @@ Best practice is to fetch the Conduction Common Ground component into a local up __Please make sure the you have committed al your changes to your current codebase and pushed a backup copy to your Git repo before continuing__ + ```CLI git remote add upstream https://github.com/ConductionNL/Proto-component-commonground.git +``` + +If you are running the proto application use the following line instead + +```CLI +git remote add upstream https://github.com/ConductionNL/proto-application-commonground.git +``` +And then we need to fetch this new code + +```CLI git fetch upstream git branch upstream upstream/master ``` diff --git a/api/config/packages/security.yaml b/api/config/packages/security.yaml index 7063c995..91ded2d1 100644 --- a/api/config/packages/security.yaml +++ b/api/config/packages/security.yaml @@ -1,7 +1,13 @@ security: # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers providers: - in_memory: { memory: ~ } + chain_provider: + chain: + providers: [CommongroundApplicationProvider, CommongroundUserProvider] + commongroundUserProvider: + id: App\Security\User\CommongroundUserProvider + commongroundApplicationProvider: + id: App\Security\User\CommongroundApplicationProvider firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ @@ -17,6 +23,7 @@ security: pattern: ^/payments/mollie_webhook methods: [POST] main: + provider: chain_provider guard: authenticators: - App\Security\CommongroundApplicationAuthenticator diff --git a/api/config/routes.yaml b/api/config/routes.yaml index c3283aa2..1cd78329 100644 --- a/api/config/routes.yaml +++ b/api/config/routes.yaml @@ -1,3 +1,3 @@ -#index: -# path: / -# controller: App\Controller\DefaultController::index +index: + path: / + controller: App\Controller\DefaultController::index diff --git a/api/helm/values.yaml b/api/helm/values.yaml index 3274af71..5485907c 100644 --- a/api/helm/values.yaml +++ b/api/helm/values.yaml @@ -25,7 +25,7 @@ settings: state: Noord-Holland locality: Amsterdam unit: Common-Ground - demo: pc.zaakonline.nl + demo: test.conduction.nl env: dev debug: 1 replicaCount: 1 diff --git a/api/public/schema/publiccode.yaml b/api/public/schema/publiccode.yaml index a1034346..0cc18318 100644 --- a/api/public/schema/publiccode.yaml +++ b/api/public/schema/publiccode.yaml @@ -3,10 +3,10 @@ publiccodeYmlVersion: "0.2" name: bc applicationSuite: commonground url: "https://github.com/ConductionNL/Proto-component-commonground" -landingURL: "pc.zaakonline.nl" +landingURL: "test.conduction.nl" isBasedOn: "https://github.com/ConductionNL/Proto-component-commonground.git" softwareVersion: "V.0.1" -releaseDate: "2020-20-02" +releaseDate: "2020-21-02" logo: pc.zaakonline.nl monochromeLogo: img/logo-mono.svg @@ -67,7 +67,7 @@ description: # is and why one should need it. documentation: "https://github.com/ConductionNL/Proto-component-commonground/blob/master/README.md" - apiDocumentation: "pc.zaakonline.nl" + apiDocumentation: "test.conduction.nl" # # features: # - Very important feature diff --git a/api/src/Security/User/CommongroundApplication.php b/api/src/Security/User/CommongroundApplication.php index d2b6396b..91eb89b4 100644 --- a/api/src/Security/User/CommongroundApplication.php +++ b/api/src/Security/User/CommongroundApplication.php @@ -13,7 +13,7 @@ class CommongroundApplication implements UserInterface, EquatableInterface private $salt; private $roles; - public function __construct($username, $password, $salt, array $roles) + public function __construct(string $username = '', string $password = '', string $salt = null, array $roles = []) { $this->username = $username; $this->password = $password; diff --git a/api/src/Security/User/CommongroundApplicationProvider.php b/api/src/Security/User/CommongroundApplicationProvider.php index dcd60d88..9d116d19 100644 --- a/api/src/Security/User/CommongroundApplicationProvider.php +++ b/api/src/Security/User/CommongroundApplicationProvider.php @@ -40,13 +40,13 @@ private function fetchUser($uuid) // $userData = ... // pretend it returns an array on success, false if there is no user - if ($userData) { - $password = '...'; + //if ($userData) { + // $password = '...'; // ... - return new CommongroundApplication($username, $password, $salt, $roles); - } + return new CommongroundApplication('Default Application', $uuid, null, ['user']); + //} throw new UsernameNotFoundException( sprintf('Application "%s" does not exist.', $uuid) diff --git a/api/src/Security/User/CommongroundUser.php b/api/src/Security/User/CommongroundUser.php index 2a0ddea4..444805aa 100644 --- a/api/src/Security/User/CommongroundUser.php +++ b/api/src/Security/User/CommongroundUser.php @@ -13,7 +13,7 @@ class CommongroundUser implements UserInterface, EquatableInterface private $salt; private $roles; - public function __construct($username, $password, $salt, array $roles) + public function __construct(string $username = '', string $password = '', string $salt = null, array $roles = []) { $this->username = $username; $this->password = $password; diff --git a/api/src/Security/User/CommongroundUserProvider.php b/api/src/Security/User/CommongroundUserProvider.php index 0ae0eeba..9c9419ef 100644 --- a/api/src/Security/User/CommongroundUserProvider.php +++ b/api/src/Security/User/CommongroundUserProvider.php @@ -40,13 +40,13 @@ private function fetchUser($uuid) //$userData = ... // pretend it returns an array on success, false if there is no user - if ($userData) { - $password = '...'; + //if ($userData) { + // $password = '...'; // ... - return new CommongroundUser($username, $password, $salt, $roles); - } + return new CommongroundUser('Default User', $uuid, null, ['user']); + //} throw new UsernameNotFoundException( sprintf('User "%s" does not exist.', $uuid) diff --git a/api/src/Service/CommonGroundService.php b/api/src/Service/CommonGroundService.php index 12393254..ecd9a02e 100644 --- a/api/src/Service/CommonGroundService.php +++ b/api/src/Service/CommonGroundService.php @@ -11,331 +11,334 @@ class CommonGroundService { - private $params; - private $cache; - private $session; - private $headers; - - public function __construct(ParameterBagInterface $params, SessionInterface $session, CacheInterface $cache) - { - $this->params = $params; - $this->session = $session; - $this->cash = $cache; - $this->session= $session; - - // To work with NLX we need a couple of default headers - $this->headers = [ - 'Accept' => 'application/ld+json', - 'Content-Type' => 'application/json', - 'Authorization' => $this->params->get('app_commonground_key'), - 'X-NLX-Request-Application-Id' => $this->params->get('app_commonground_id')// the id of the application performing the request - ]; - - if($session->get('user') === $user){ - $headers['X-NLX-Request-User-Id'] = $user['@id']; - } - - if($session->get('process') === $process){ - $headers[] = $process['@id']; - } - - - // We might want to overwrite the guzle config, so we declare it as a separate array that we can then later adjust, merge or otherwise influence - $this->guzzleConfig = [ - // Base URI is used with relative requests - 'http_errors' => false, - //'base_uri' => 'https://wrc.zaakonline.nl/applications/536bfb73-63a5-4719-b535-d835607b88b2/', - // You can set any number of default request options. - 'timeout' => 4000.0, - // To work with NLX we need a couple of default headers - 'headers' => $this->headers, - ]; - - // Lets start up a default client - $this->client = new Client($this->guzzleConfig); - } - - /* - * Get a single resource from a common ground componant - */ - public function getResourceList($url, $query = [], $force = false) - { - if (!$url) { - return false; - } - $parsedUrl = $parse_url($url); - - $elementList = []; - foreach($query as $element){ - $elementList[] = implode("=",$element); - } - $elementList = implode(",", $elementList); - - // To work with NLX we need a couple of default headers - $headers = $this->headers; - $headers['X-NLX-Request-Data-Elements'] = $elementList; - $headers['X-NLX-Request-Data-Subject'] = $elementList; - - $item = $this->cash->getItem('commonground_'.md5($url)); - if ($item->isHit() && !$force) { - //return $item->get(); - } - - $response = $this->client->request('GET', $url, [ - 'query' => $query, - 'headers' => $headers, - ] - ); - - $response = json_decode($response->getBody(), true); - - /* @todo this should look to al @id keus not just the main root */ - foreach($response['_embedded'] as $key => $embedded){ - if($embedded['@id']){ - $response['_embedded'][$key]['@id'] = $parsedUrl["host"].$embedded['@id']; - } - } - - $item->set($response); - $item->expiresAt(new \DateTime('tomorrow')); - $this->cash->save($item); - - return $response; - } - - /* - * Get a single resource from a common ground componant - */ - public function getResource($url, $query = [], $force = false) - { - - if (!$url) { - //return false; - } - $parsedUrl = $parse_url($url); - - // To work with NLX we need a couple of default headers - $headers = $this->headers; - $headers['X-NLX-Request-Subject-Identifier'] = $url; - - $item = $this->cash->getItem('commonground_'.md5($url)); - if ($item->isHit() && !$force) { - //return $item->get(); - } - - $response = $this->client->request('GET', $url, [ - 'query' => $query, - 'headers' => $headers, - ] - ); - - $response = json_decode($response->getBody(), true); - - if($response['@id']){ - $response['@id'] = $parsedUrl["host"].$response['@id']; - } - - $item->set($response); - $item->expiresAt(new \DateTime('tomorrow')); - $this->cash->save($item); - - return $response; - } - - /* - * Get a single resource from a common ground componant - */ - public function updateResource($resource, $url = null) - { - if (!$url) { - return false; - } - $parsedUrl = $parse_url($url); - - unset($resource['@context']); - unset($resource['@id']); - unset($resource['@type']); - unset($resource['id']); - unset($resource['_links']); - unset($resource['_embedded']); - - $response = $this->client->request('PUT', $url, [ - 'body' => json_encode($resource), - ] - ); - - if($response->getStatusCode() != 200){ - var_dump(json_encode($resource)); - var_dump(json_encode($url)); - var_dump(json_encode($response->getBody())); - die; - } - - $response = json_decode($response->getBody(), true); - - if($response['@id']){ - $response['@id'] = $parsedUrl["host"].$response['@id']; - } - - // Lets cash this item for speed purposes - $item = $this->cash->getItem('commonground_'.md5($url)); - $item->set($response); - $item->expiresAt(new \DateTime('tomorrow')); - $this->cash->save($item); - - return $response; - } - - /* - * Get a single resource from a common ground componant - */ - public function createResource($resource, $url = null) - { - if (!$url) { - return false; - } - $parsedUrl = $parse_url($url); - - $response = $this->client->request('POST', $url, [ - 'body' => json_encode($resource), - ] - ); - - - if($response->getStatusCode() != 201){ - var_dump(json_encode($resource)); - var_dump(json_encode($url)); - var_dump($response->getBody()); - die; - } - - - $response = json_decode($response->getBody(), true); - - if($response['@id']){ - $response['@id'] = $parsedUrl["host"].$response['@id']; - } - - // Lets cash this item for speed purposes - $item = $this->cash->getItem('commonground_'.md5($url.'/'.$response['id'])); - $item->set($response); - $item->expiresAt(new \DateTime('tomorrow')); - $this->cash->save($item); - - return $response; - } - - /* - * Get a single resource from a common ground componant - */ - public function clearCash($url) - { - } - - /* - * Get a list of available commonground components - */ - public function getComponentList() - { - $components = [ - 'cc' => ['href'=>'http://cc.zaakonline.nl', 'authorization'=>''], - 'lc' => ['href'=>'http://lc.zaakonline.nl', 'authorization'=>''], - 'ltc' => ['href'=>'http://ltc.zaakonline.nl', 'authorization'=>''], - 'brp' => ['href'=>'http://brp.zaakonline.nl', 'authorization'=>''], - 'irc' => ['href'=>'http://irc.zaakonline.nl', 'authorization'=>''], - 'ptc' => ['href'=>'http://ptc.zaakonline.nl', 'authorization'=>''], - 'mrc' => ['href'=>'http://mrc.zaakonline.nl', 'authorization'=>''], - 'arc' => ['href'=>'http://arc.zaakonline.nl', 'authorization'=>''], - 'vtc' => ['href'=>'http://vtc.zaakonline.nl', 'authorization'=>''], - 'vrc' => ['href'=>'http://vrc.zaakonline.nl', 'authorization'=>''], - 'pdc' => ['href'=>'http://pdc.zaakonline.nl', 'authorization'=>''], - 'wrc' => ['href'=>'http://wrc.zaakonline.nl', 'authorization'=>''], - 'orc' => ['href'=>'http://orc.zaakonline.nl', 'authorization'=>''], - 'bc' => ['href'=>'http://orc.zaakonline.nl', 'authorization'=>''], - ]; - - return $components; - } - - /* - * Get the health of a commonground componant - */ - public function getComponentHealth(string $component, $force = false) - { - $componentList = $this->getComponentList(); - - $item = $this->cash->getItem('componentHealth_'.md5($component)); - if ($item->isHit() && !$force) { - //return $item->get(); - } - - //@todo trhow symfony error - if (!array_key_exists($component, $componentList)) { - return false; - } else { - // Lets swap the component for a - - // Then we like to know al the component endpoints - $component = $this->getComponentResources($component); - } - - // Lets loop trough the endoints and get health (the self endpoint is included) - foreach ($component['endpoints'] as $key=>$endpoint) { - - //var_dump($component['endpoints']); - //var_dump($endpoint); - - $response = $this->client->request('GET', $component['href'].$endpoint['href'], ['Headers' =>['Authorization' => $component['authorization'], 'Accept' => 'application/health+json']]); - if ($response->getStatusCode() == 200) { - //$component['endpoints'][$key]['health'] = json_decode($response->getBody(), true); - $component['endpoints'][$key]['health'] = false; - } - } - - $item->set($component); - $item->expiresAt(new \DateTime('tomorrow')); - $this->cash->save($item); - - return $component; - } - - /* - * Get a list of available resources on a commonground componant - */ - public function getComponentResources(string $component, $force = false) - { - $componentList = $this->getComponentList(); - - $item = $this->cash->getItem('componentResources_'.md5($component)); - if ($item->isHit() && !$force) { - //return $item->get(); - } - - //@todo trhow symfony error - if (!array_key_exists($component, $componentList)) { - return false; - } else { - // Lets swap the component for a version that has an endpoint and authorization - $component = $componentList[$component]; - } - - $response = $this->client->request('GET', $component['href'], ['Headers' =>['Authorization' => $component['authorization'], 'Accept' => 'application/ld+json']]); - - $component['status'] = $response->getStatusCode(); - if ($response->getStatusCode() == 200) { - $component['endpoints'] = json_decode($response->getBody(), true); - // Lets pull any json-ld values - if (array_key_exists('_links', $component['endpoints'])) { - $component['endpoints'] = $component['endpoints']['_links']; - } - } else { - $component['endpoints'] = []; - } - - $item->set($component); - $item->expiresAt(new \DateTime('tomorrow')); - $this->cash->save($item); - - return $component; - } + private $params; + private $cache; + private $session; + private $headers; + + public function __construct(ParameterBagInterface $params, SessionInterface $session, CacheInterface $cache) + { + $this->params = $params; + $this->session = $session; + $this->cash = $cache; + $this->session= $session; + + // To work with NLX we need a couple of default headers + $this->headers = [ + 'Accept' => 'application/ld+json', + 'Content-Type' => 'application/json', + 'Authorization' => $this->params->get('app_commonground_key'), + 'X-NLX-Request-Application-Id' => $this->params->get('app_commonground_id')// the id of the application performing the request + ]; + + if($session->get('user')){ + $headers['X-NLX-Request-User-Id'] = $session->get('user')['@id']; + } + + if($session->get('process')){ + $headers[] = $session->get('process')['@id']; + } + + + // We might want to overwrite the guzle config, so we declare it as a separate array that we can then later adjust, merge or otherwise influence + $this->guzzleConfig = [ + // Base URI is used with relative requests + 'http_errors' => false, + //'base_uri' => 'https://wrc.zaakonline.nl/applications/536bfb73-63a5-4719-b535-d835607b88b2/', + // You can set any number of default request options. + 'timeout' => 4000.0, + // To work with NLX we need a couple of default headers + 'headers' => $this->headers, + ]; + + // Lets start up a default client + $this->client = new Client($this->guzzleConfig); + } + + /* + * Get a single resource from a common ground componant + */ + public function getResourceList($url, $query = [], $force = false) + { + if (!$url) { + return false; + } + $parsedUrl = parse_url($url); + + $elementList = []; + foreach($query as $element){ + if(!is_array($element)){ + return; + } + $elementList[] = implode("=",$element); + } + $elementList = implode(",", $elementList); + + // To work with NLX we need a couple of default headers + $headers = $this->headers; + $headers['X-NLX-Request-Data-Elements'] = $elementList; + $headers['X-NLX-Request-Data-Subject'] = $elementList; + + $item = $this->cash->getItem('commonground_'.md5($url)); + if ($item->isHit() && !$force) { + //return $item->get(); + } + + $response = $this->client->request('GET', $url, [ + 'query' => $query, + 'headers' => $headers, + ] + ); + + $response = json_decode($response->getBody(), true); + + /* @todo this should look to al @id keus not just the main root */ + foreach($response['hydra:member'] as $key => $embedded){ + if($embedded['@id']){ + $response['hydra:member'][$key]['@id'] = $parsedUrl["host"].$embedded['@id']; + } + } + + $item->set($response); + $item->expiresAt(new \DateTime('tomorrow')); + $this->cash->save($item); + + return $response; + } + + /* + * Get a single resource from a common ground componant + */ + public function getResource($url, $query = [], $force = false) + { + + if (!$url) { + //return false; + } + $parsedUrl = parse_url($url); + + // To work with NLX we need a couple of default headers + $headers = $this->headers; + $headers['X-NLX-Request-Subject-Identifier'] = $url; + + $item = $this->cash->getItem('commonground_'.md5($url)); + if ($item->isHit() && !$force) { + //return $item->get(); + } + + $response = $this->client->request('GET', $url, [ + 'query' => $query, + 'headers' => $headers, + ] + ); + + $response = json_decode($response->getBody(), true); + + if($response['@id']){ + $response['@id'] = $parsedUrl["host"].$response['@id']; + } + + $item->set($response); + $item->expiresAt(new \DateTime('tomorrow')); + $this->cash->save($item); + + return $response; + } + + /* + * Get a single resource from a common ground componant + */ + public function updateResource($resource, $url = null) + { + if (!$url) { + return false; + } + $parsedUrl = parse_url($url); + + unset($resource['@context']); + unset($resource['@id']); + unset($resource['@type']); + unset($resource['id']); + unset($resource['_links']); + unset($resource['_embedded']); + + $response = $this->client->request('PUT', $url, [ + 'body' => json_encode($resource), + ] + ); + + if($response->getStatusCode() != 200){ + var_dump(json_encode($resource)); + var_dump(json_encode($url)); + var_dump(json_encode($response->getBody())); + die; + } + + $response = json_decode($response->getBody(), true); + + if($response['@id']){ + $response['@id'] = $parsedUrl["host"].$response['@id']; + } + + // Lets cash this item for speed purposes + $item = $this->cash->getItem('commonground_'.md5($url)); + $item->set($response); + $item->expiresAt(new \DateTime('tomorrow')); + $this->cash->save($item); + + return $response; + } + + /* + * Get a single resource from a common ground componant + */ + public function createResource($resource, $url = null) + { + if (!$url) { + return false; + } + $parsedUrl = parse_url($url); + + $response = $this->client->request('POST', $url, [ + 'body' => json_encode($resource), + ] + ); + + + if($response->getStatusCode() != 201){ + var_dump(json_encode($resource)); + var_dump(json_encode($url)); + var_dump($response->getBody()); + die; + } + + + $response = json_decode($response->getBody(), true); + + if($response['@id']){ + $response['@id'] = $parsedUrl["host"].$response['@id']; + } + + // Lets cash this item for speed purposes + $item = $this->cash->getItem('commonground_'.md5($url.'/'.$response['id'])); + $item->set($response); + $item->expiresAt(new \DateTime('tomorrow')); + $this->cash->save($item); + + return $response; + } + + /* + * Get a single resource from a common ground componant + */ + public function clearCash($url) + { + } + + /* + * Get a list of available commonground components + */ + public function getComponentList() + { + $components = [ + 'cc' => ['href'=>'http://cc.zaakonline.nl', 'authorization'=>''], + 'lc' => ['href'=>'http://lc.zaakonline.nl', 'authorization'=>''], + 'ltc' => ['href'=>'http://ltc.zaakonline.nl', 'authorization'=>''], + 'brp' => ['href'=>'http://brp.zaakonline.nl', 'authorization'=>''], + 'irc' => ['href'=>'http://irc.zaakonline.nl', 'authorization'=>''], + 'ptc' => ['href'=>'http://ptc.zaakonline.nl', 'authorization'=>''], + 'mrc' => ['href'=>'http://mrc.zaakonline.nl', 'authorization'=>''], + 'arc' => ['href'=>'http://arc.zaakonline.nl', 'authorization'=>''], + 'vtc' => ['href'=>'http://vtc.zaakonline.nl', 'authorization'=>''], + 'vrc' => ['href'=>'http://vrc.zaakonline.nl', 'authorization'=>''], + 'pdc' => ['href'=>'http://pdc.zaakonline.nl', 'authorization'=>''], + 'wrc' => ['href'=>'http://wrc.zaakonline.nl', 'authorization'=>''], + 'orc' => ['href'=>'http://orc.zaakonline.nl', 'authorization'=>''], + 'bc' => ['href'=>'http://orc.zaakonline.nl', 'authorization'=>''], + ]; + + return $components; + } + + /* + * Get the health of a commonground componant + */ + public function getComponentHealth(string $component, $force = false) + { + $componentList = $this->getComponentList(); + + $item = $this->cash->getItem('componentHealth_'.md5($component)); + if ($item->isHit() && !$force) { + //return $item->get(); + } + + //@todo trhow symfony error + if (!array_key_exists($component, $componentList)) { + return false; + } else { + // Lets swap the component for a + + // Then we like to know al the component endpoints + $component = $this->getComponentResources($component); + } + + // Lets loop trough the endoints and get health (the self endpoint is included) + foreach ($component['endpoints'] as $key=>$endpoint) { + + //var_dump($component['endpoints']); + //var_dump($endpoint); + + $response = $this->client->request('GET', $component['href'].$endpoint['href'], ['Headers' =>['Authorization' => $component['authorization'], 'Accept' => 'application/health+json']]); + if ($response->getStatusCode() == 200) { + //$component['endpoints'][$key]['health'] = json_decode($response->getBody(), true); + $component['endpoints'][$key]['health'] = false; + } + } + + $item->set($component); + $item->expiresAt(new \DateTime('tomorrow')); + $this->cash->save($item); + + return $component; + } + + /* + * Get a list of available resources on a commonground componant + */ + public function getComponentResources(string $component, $force = false) + { + $componentList = $this->getComponentList(); + + $item = $this->cash->getItem('componentResources_'.md5($component)); + if ($item->isHit() && !$force) { + //return $item->get(); + } + + //@todo trhow symfony error + if (!array_key_exists($component, $componentList)) { + return false; + } else { + // Lets swap the component for a version that has an endpoint and authorization + $component = $componentList[$component]; + } + + $response = $this->client->request('GET', $component['href'], ['Headers' =>['Authorization' => $component['authorization'], 'Accept' => 'application/ld+json']]); + + $component['status'] = $response->getStatusCode(); + if ($response->getStatusCode() == 200) { + $component['endpoints'] = json_decode($response->getBody(), true); + // Lets pull any json-ld values + if (array_key_exists('_links', $component['endpoints'])) { + $component['endpoints'] = $component['endpoints']['_links']; + } + } else { + $component['endpoints'] = []; + } + + $item->set($component); + $item->expiresAt(new \DateTime('tomorrow')); + $this->cash->save($item); + + return $component; + } } diff --git a/api/src/Service/MollieService.php b/api/src/Service/MollieService.php index c80f95bc..5f595039 100644 --- a/api/src/Service/MollieService.php +++ b/api/src/Service/MollieService.php @@ -56,7 +56,7 @@ public function createPayment(Invoice $invoice, Request $request):string{ "order_id" => $invoice->getReference(), ], ]); - var_dump($molliePayment); + var_dump($molliePayment->id); return $molliePayment->getCheckoutUrl(); } catch (ApiException $e) @@ -66,24 +66,34 @@ public function createPayment(Invoice $invoice, Request $request):string{ } } - public function updatePayment(string $paymentId, EntityManagerInterface $manager):Payment + public function updatePayment(string $paymentId, Service $service, EntityManagerInterface $manager):?Payment { $molliePayment = $this->mollie->payments->get($paymentId); $payment = $manager->getRepository('App:Payment')->findOneBy(['paymentId'=> $paymentId]); if($payment instanceof Payment) { $payment->setStatus($molliePayment->status); - //return $payment; + return $payment; } else{ - $invoiceReference = $molliePayment->metadata['order_id']; + $invoiceReference = $molliePayment->metadata->order_id; + //var_dump($invoiceReference); $invoice = $manager->getRepository('App:Invoice')->findBy(['reference'=>$invoiceReference]); - $payment = new Payment(); - $payment->setPaymentId($molliePayment->id); - $payment->setStatus($molliePayment->status); - $payment->setInvoice(); - $manager->persist($payment); - $manager->flush(); + //var_dump(count($invoice)); + if(is_array($invoice)) + $invoice = end($invoice); + if($invoice instanceof Invoice) + { + $payment = new Payment(); + $payment->setPaymentId($molliePayment->id); + $payment->setPaymentProvider($service); + $payment->setStatus($molliePayment->status); + $payment->setInvoice($invoice); + $manager->persist($payment); + $manager->flush(); + + return $payment; + } } - return $payment; + return null; } } diff --git a/api/src/Subscriber/InvoiceSubscriber.php b/api/src/Subscriber/InvoiceSubscriber.php index 5adcf5bc..e72d088b 100644 --- a/api/src/Subscriber/InvoiceSubscriber.php +++ b/api/src/Subscriber/InvoiceSubscriber.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use PhpParser\Error; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Serializer\SerializerInterface; @@ -40,16 +41,16 @@ public function __construct(ParameterBagInterface $params, EntityManagerInterfac public static function getSubscribedEvents() { return [ - KernelEvents::VIEW => ['invoice', EventPriorities::PRE_VALIDATE], + KernelEvents::REQUEST => ['invoice', EventPriorities::PRE_DESERIALIZE], ]; } - public function invoice(ViewEvent $event) + public function invoice(RequestEvent $event) { // $result = $event->getControllerResult(); // $method = $event->getRequest()->getMethod(); $route = $event->getRequest()->attributes->get('_route'); - var_dump($route); + //var_dump($route); $data = json_decode($event->getRequest()->getContent()); if ($route != 'api_invoices_post_order_collection' || $data == null) @@ -62,14 +63,13 @@ public function invoice(ViewEvent $event) $invoice = new Invoice(); // var_dump($order); $invoice->setName($order->name); - $invoice->setDescription($order->description); + if(isset($order->description)) + $invoice->setDescription($order->description); $invoice->setReference($order->reference); $invoice->setPrice($order->price); $invoice->setPriceCurrency($order->priceCurrency); $invoice->setTax($order->tax); $invoice->setCustomer($order->customer); - $invoice->setOrder($order->url); - $invoice->setRemark($order->remark); $organization = $this->em->getRepository('App:Organization')->findOrCreateByRsin($order->organization->rsin); if ($organization instanceof Organization) { diff --git a/api/src/Subscriber/PaymentCreationSubscriber.php b/api/src/Subscriber/PaymentCreationSubscriber.php index 8550fbe8..b95eb383 100644 --- a/api/src/Subscriber/PaymentCreationSubscriber.php +++ b/api/src/Subscriber/PaymentCreationSubscriber.php @@ -49,7 +49,7 @@ public function payment(ViewEvent $event) $result = $event->getControllerResult(); $method = $event->getRequest()->getMethod(); $route = $event->getRequest()->attributes->get('_route'); - var_dump($route); + //var_dump($route); if($result instanceof Invoice && $method != 'DELETE') { @@ -83,62 +83,4 @@ public function payment(ViewEvent $event) return; } } -// public function payment(ViewEvent $event) -// { -// $result = $event->getControllerResult(); -// $method = $event->getRequest()->getMethod(); -// $route = $event->getRequest()->attributes->get('_route'); -// -// //var_dump($route); -// if(!$result instanceof Payment || ($route != 'api_payments_post_webhook_collection' && $route != 'api_payments_post_collection')){ -// //var_dump('a'); -// return; -// } -// elseif($route=='api_payment_post_webhook_collection'){ -// $requestData = json_decode($event->getRequest()->getContent(),true); -// $paymentProvider = $this->em->getRepository('App\Entity\Service')->find($requestData['serviceId']); -// if($paymentProvider instanceof Service && $paymentProvider->getType() == 'mollie'){ -// $mollieService = new MollieService($requestData['paymentProvider']); -// $payment = $mollieService->updatePayment($event->getRequest(), $this->em); -// } -// else{ -// return; -// } -// }else{ -// $requestData = json_decode($event->getRequest()->getContent(),true); -// $paymentProvider = $this->em->getRepository('App\Entity\Service')->find($requestData['serviceId']); -// -// if($paymentProvider instanceof Service) { -// switch ($paymentProvider->getType()) { -// case 'mollie': -// $mollieService = new MollieService($paymentProvider); -// $payment = $mollieService->createPayment($event->getRequest()); -// break; -// case 'sumup': -// $sumupService = new SumUpService($paymentProvider); -// $payment = $sumupService->createPayment($event->getRequest()); -// break; -// default: -// return; -// } -// } -// else -// return; -// } -// $this->em->persist($payment); -// $this->em->flush(); -// -// -// $json = $this->serializer->serialize( -// $payment, -// 'jsonhal', ['enable_max_depth'=>true] -// ); -// -// $response = new Response( -// $json, -// Response::HTTP_OK, -// ['content-type' => 'application/json+hal'] -// ); -// $event->setResponse($response); -// } } diff --git a/api/src/Subscriber/PaymentSubscriber.php b/api/src/Subscriber/PaymentSubscriber.php index 257d990f..1b0f6b9e 100644 --- a/api/src/Subscriber/PaymentSubscriber.php +++ b/api/src/Subscriber/PaymentSubscriber.php @@ -55,7 +55,7 @@ public function payment(RequestEvent $event) if($route=='api_payments_post_webhook_collection'){ //var_dump('a'); $providerId = $event->getRequest()->query->get('provider'); - //var_dump($providerId); + //var_dump($providerId); $provider = $this->em->getRepository('App\Entity\Service')->find($providerId); $paymentId = $event->getRequest()->request->get('id'); @@ -64,7 +64,7 @@ public function payment(RequestEvent $event) if($provider instanceof Service && $provider->getType() == 'mollie'){ $mollieService = new MollieService($provider); - $payment = $mollieService->updatePayment($paymentId, $this->em); + $payment = $mollieService->updatePayment($paymentId, $provider, $this->em); } else{ return; @@ -73,14 +73,21 @@ public function payment(RequestEvent $event) else{ return; } - $this->em->persist($payment); - $this->em->flush(); - $json = $this->serializer->serialize( - $payment, - 'jsonhal', ['enable_max_depth'=>true] - ); + if($payment){ + $this->em->persist($payment); + $this->em->flush(); + $json = $this->serializer->serialize( + $payment, + 'jsonhal', ['enable_max_depth'=>true] + ); + }else{ + $json = $this->serializer->serialize( + ["Error"=>"The payment is not related to an invoice in our database"], 'jsonhal', ['enable_max_depth'=>true] + ); + } + $response = new Response( $json, diff --git a/api/templates/default/index.html.twig b/api/templates/default/index.html.twig new file mode 100644 index 00000000..27739acb --- /dev/null +++ b/api/templates/default/index.html.twig @@ -0,0 +1,5 @@ +{% extends "base.html.twig" %} + +{% block body %} + Test +{% endblock %} \ No newline at end of file