Skip to content

Commit

Permalink
make api permission configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Oct 16, 2020
1 parent 18e6b98 commit 48fc8a3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ Otherwise, you'll receive an error message. You may then need to repeat the conn
| `Page Id` | Defines which page entries should be imported |
| `Limit` | Define a limit to restrict the amount of social posts to import (Default: 50) |

## Extended Connector Configuration
Normally you don't need to modify connector (`connector_config`) configuration, so most of the time you can skip this step.
However, if you need to change some core setting of a connector, you're able to change them of course.

```yaml
# app/config/config.yml
social_data:
available_connectors:
- connector_name: facebook
connector_config:
api_connect_permission: ['pages_show_list'] # default value
```

***

## Copyright and license
Copyright: [DACHCOM.DIGITAL](http://dachcom-digital.ch)
For licensing details please visit [LICENSE.md](LICENSE.md)
Expand Down
30 changes: 20 additions & 10 deletions src/Controller/Admin/FacebookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
use SocialData\Connector\Facebook\Client\FacebookClient;
use SocialData\Connector\Facebook\Model\EngineConfiguration;
use SocialDataBundle\Connector\ConnectorDefinitionInterface;
use SocialDataBundle\Controller\Admin\Traits\ConnectResponseTrait;
use SocialDataBundle\Service\ConnectorServiceInterface;
use SocialDataBundle\Service\EnvironmentServiceInterface;
Expand Down Expand Up @@ -64,20 +65,19 @@ public function __construct(
public function connectAction(Request $request)
{
try {
$connectorEngineConfig = $this->getConnectorEngineConfig();
$connectorDefinition = $this->getConnectorDefinition();
$connectorEngineConfig = $this->getConnectorEngineConfig($connectorDefinition);
} catch (\Throwable $e) {
return $this->buildConnectErrorResponse(500, 'general_error', 'connector engine configuration error', $e->getMessage());
}

$fb = $this->facebookClient->getClient($connectorEngineConfig);

$helper = $fb->getRedirectLoginHelper();
$definitionConfiguration = $connectorDefinition->getDefinitionConfiguration();

$callbackUrl = $this->generateUrl('social_data_connector_facebook_connect_check', [], UrlGeneratorInterface::ABSOLUTE_URL);

// @todo: make this configurable (e.g. via connector config?)
$permissions = ['pages_show_list'];

$loginUrl = $helper->getLoginUrl($callbackUrl, $permissions);
$loginUrl = $helper->getLoginUrl($callbackUrl, $definitionConfiguration['api_connect_permission']);

return $this->redirect($loginUrl);
}
Expand All @@ -92,7 +92,7 @@ public function connectAction(Request $request)
public function checkAction(Request $request)
{
try {
$connectorEngineConfig = $this->getConnectorEngineConfig();
$connectorEngineConfig = $this->getConnectorEngineConfig($this->getConnectorDefinition());
} catch (\Throwable $e) {
return $this->buildConnectErrorResponse(500, 'general_error', 'connector engine configuration error', $e->getMessage());
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public function checkAction(Request $request)
public function debugTokenAction(Request $request)
{
try {
$connectorEngineConfig = $this->getConnectorEngineConfig();
$connectorEngineConfig = $this->getConnectorEngineConfig($this->getConnectorDefinition());
} catch (\Throwable $e) {
return $this->adminJson(['error' => true, 'message' => $e->getMessage()]);
}
Expand Down Expand Up @@ -197,16 +197,26 @@ public function debugTokenAction(Request $request)
}

/**
* @return EngineConfiguration
* @return ConnectorDefinitionInterface
*/
protected function getConnectorEngineConfig()
protected function getConnectorDefinition()
{
$connectorDefinition = $this->connectorService->getConnectorDefinition('facebook', true);

if (!$connectorDefinition->engineIsLoaded()) {
throw new HttpException(400, 'Engine is not loaded.');
}

return $connectorDefinition;
}

/**
* @param ConnectorDefinitionInterface $connectorDefinition
*
* @return EngineConfiguration
*/
protected function getConnectorEngineConfig(ConnectorDefinitionInterface $connectorDefinition)
{
$connectorEngineConfig = $connectorDefinition->getEngineConfiguration();
if (!$connectorEngineConfig instanceof EngineConfiguration) {
throw new HttpException(400, 'Invalid facebook configuration. Please configure your connector "facebook" in backend first.');
Expand Down
16 changes: 2 additions & 14 deletions src/Definition/ConnectorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public function setDefinitionConfiguration(array $definitionConfiguration)
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'core_disabled' => false
'api_connect_permission' => ['pages_show_list']
]);

$resolver->setAllowedTypes('core_disabled', 'bool');
$resolver->setAllowedTypes('api_connect_permission', 'string[]');

try {
$this->definitionConfiguration = $resolver->resolve($definitionConfiguration);
Expand Down Expand Up @@ -99,10 +99,6 @@ public function engineIsLoaded()
*/
public function isOnline()
{
if ($this->definitionConfiguration['core_disabled'] === true) {
return false;
}

if (!$this->engineIsLoaded()) {
return false;
}
Expand Down Expand Up @@ -190,14 +186,6 @@ public function needsEngineConfiguration()
return true;
}

/**
* {@inheritdoc}
*/
public function hasLogPanel()
{
return false;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 48fc8a3

Please sign in to comment.