Skip to content

Commit

Permalink
Merge pull request #1 from apigee-internal/drup-5-module-skeleton
Browse files Browse the repository at this point in the history
Created module skeleton and the authentication form class.
  • Loading branch information
Péter Serfőző authored Nov 24, 2017
2 parents 0852872 + 9ce4b6a commit 2225e32
Show file tree
Hide file tree
Showing 30 changed files with 1,262 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# apigee-drupal-module
A Drupal 8 module that turns a site into a developer portal for Apigee's API management product

# Testing

To run the tests, some environment variables are needed. Creating a simple script is recommended:

```
#!/bin/bash
export EDGE_USERNAME=""
export EDGE_PASSWORD=""
export EDGE_ORGANIZATION=""
php ./core/scripts/run-tests.sh --verbose --color --url "http://edge.test" ApigeeEdge
```

Change the `--url` parameter to the site's url (it defaults to localhost, and sometimes it breaks the tests).
12 changes: 12 additions & 0 deletions apigee_edge/apigee_edge.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Apigee Edge
description: Apigee Edge Drupal integration
package: Apigee

type: module
core: 8.x

dependencies:

configure: apigee_edge.settings

php: 7.1
43 changes: 43 additions & 0 deletions apigee_edge/apigee_edge.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* @file
* Install, update and uninstall functions for Apigee Edge.
*/

/**
* Implements hook_requirements().
*/
function apigee_edge_requirements($phase) {
$requirements = [];

if ($phase == 'runtime') {
$credentials_storage_plugin_manager = \Drupal::service('plugin.manager.apigee_edge.credentials_storage');
foreach ($credentials_storage_plugin_manager->getDefinitions() as $key => $value) {
$credentials_storage_plugin = $credentials_storage_plugin_manager->createInstance($key);
/** @var \Drupal\apigee_edge\CredentialsStoragePluginInterface $credentials_storage_plugin */
if (!empty($credentials_storage_plugin->hasRequirements())) {
$requirements[$credentials_storage_plugin->getId()] = [
'title' => t('Apigee Edge'),
'description' => $credentials_storage_plugin->hasRequirements(),
'severity' => REQUIREMENT_WARNING,
];
}
}
}

return $requirements;
}

/**
* Implements hook_uninstall().
*/
function apigee_edge_uninstall() {
$credentials_storage_plugin_manager = \Drupal::service('plugin.manager.apigee_edge.credentials_storage');

foreach ($credentials_storage_plugin_manager->getDefinitions() as $key => $value) {
$credentials_storage_plugin = $credentials_storage_plugin_manager->createInstance($key);
/** @var \Drupal\apigee_edge\CredentialsStoragePluginInterface $credentials_storage_plugin */
$credentials_storage_plugin->deleteCredentials();
}
}
5 changes: 5 additions & 0 deletions apigee_edge/apigee_edge.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apigee_edge.admin:
version: 1.1
css:
theme:
css/apigee_edge.admin.css: {}
6 changes: 6 additions & 0 deletions apigee_edge/apigee_edge.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apigee_edge.settings:
title: 'Apigee Edge'
description: 'Apigee Edge module settings.'
parent: system.admin_config
route_name: apigee_edge.settings
weight: 100
71 changes: 71 additions & 0 deletions apigee_edge/apigee_edge.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @file
* Main module file for Apigee Edge.
*/

use Apigee\Edge\Api\Management\Entity\Developer;
use Apigee\Edge\Exception\ClientErrorException;
use Drupal\user\UserInterface;

/**
* Implements hook_user_presave().
*/
function apigee_edge_user_presave(UserInterface $account) {
$developer_data = [
'userName' => $account->getAccountName(),
'email' => $account->getEmail(),
'firstName' => $account->getAccountName(),
'lastName' => $account->getAccountName(),
];

try {
/** @var \Apigee\Edge\Entity\EntityControllerFactory $ecf */
$ecf = \Drupal::service('apigee_edge.sdk_connector')->getEntityControllerFactory();
$ec = $ecf->getControllerByEndpoint('developers');
$developer = new Developer($developer_data);
$ec->create($developer);
}
catch (ClientErrorException $exception) {
$context = [
'@developer' => print_r($developer_data, TRUE),
'@message' => (string) $exception,
'@trace' => $exception->getTraceAsString(),
];
if ($exception->getEdgeErrorCode() === 'developer.service.DeveloperAlreadyExists') {
\Drupal::logger('apigee_edge')->notice('Developer is already exists. @developer, @message, @trace', $context);
}
else {
\Drupal::logger('apigee_edge')->error('Could not save developer entity. @developer, @message, @trace', $context);
}
}
catch (\Exception $exception) {
$context = [
'@developer' => print_r($developer_data, TRUE),
'@message' => (string) $exception,
'@trace' => $exception->getTraceAsString(),
];
\Drupal::logger('apigee_edge')->error('Could not save developer entity. @developer, @message, @trace', $context);
}
}

/**
* Implements hook_user_delete().
*/
function apigee_edge_user_delete(UserInterface $account) {
try {
/** @var \Apigee\Edge\Entity\EntityControllerFactory $ecf */
$ecf = \Drupal::service('apigee_edge.sdk_connector')->getEntityControllerFactory();
$ec = $ecf->getControllerByEndpoint('developers');
$ec->delete($account->getEmail());
}
catch (\Exception $exception) {
$context = [
'@developer' => $account->getEmail(),
'@message' => (string) $exception,
'@trace' => $exception->getTraceAsString(),
];
\Drupal::logger('apigee_edge')->error('Could not delete developer entity. @developer, @message, @trace', $context);
}
}
2 changes: 2 additions & 0 deletions apigee_edge/apigee_edge.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
administer apigee edge:
title: 'Administer Apigee Edge'
7 changes: 7 additions & 0 deletions apigee_edge/apigee_edge.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apigee_edge.settings:
path: '/admin/config/apigee_edge'
defaults:
_form: '\Drupal\apigee_edge\Form\AuthenticationForm'
_title: 'Apigee Edge Configuration'
requirements:
_permission: 'administer apigee edge'
11 changes: 11 additions & 0 deletions apigee_edge/apigee_edge.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
plugin.manager.apigee_edge.credentials_storage:
class: Drupal\apigee_edge\CredentialsStorageManager
parent: default_plugin_manager

plugin.manager.apigee_edge.authentication_method:
class: Drupal\apigee_edge\AuthenticationMethodManager
parent: default_plugin_manager

apigee_edge.sdk_connector:
class: Drupal\apigee_edge\SDKConnector
9 changes: 9 additions & 0 deletions apigee_edge/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Apigee Edge",
"type": "drupal-module",
"require": {
"php": ">=7.1",
"php-http/guzzle6-adapter": "^1.1",
"apigee/edge": "dev-2.x-dev#be0754b7088fa386a8306577d841b63e080f2d5f"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
authentication_method: authentication_method_basic_auth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
credentials_storage_type: credentials_storage_private_file
11 changes: 11 additions & 0 deletions apigee_edge/config/schema/apigee_edge.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apigee_edge.authentication_method:
type: config_object
mapping:
authentication_method:
type: string

apigee_edge.credentials_storage:
type: config_object
mapping:
credentials_storage_type:
type: string
13 changes: 13 additions & 0 deletions apigee_edge/css/apigee_edge.admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#edit-test-connection-submit {
margin-left: 0;
}

.test-connection-response-error {
color: red;
font-weight: bold;
}

.test-connection-response-success {
color: green;
font-weight: bold;
}
30 changes: 30 additions & 0 deletions apigee_edge/src/Annotation/AuthenticationMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\apigee_edge\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines an authentication method plugin annotation object.
*
* @Annotation
*/
class AuthenticationMethod extends Plugin {

/**
* The plugin ID.
*
* @var string
*/
public $id;

/**
* The name of the storage plugin.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $name;

}
30 changes: 30 additions & 0 deletions apigee_edge/src/Annotation/CredentialsStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\apigee_edge\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines a credentials storage plugin annotation object.
*
* @Annotation
*/
class CredentialsStorage extends Plugin {

/**
* The plugin ID.
*
* @var string
*/
public $id;

/**
* The name of the storage plugin.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $name;

}
45 changes: 45 additions & 0 deletions apigee_edge/src/AuthenticationMethodManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drupal\apigee_edge;

use Drupal\apigee_edge\Annotation\AuthenticationMethod;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

/**
* Provides an authentication method plugin manager.
*
* @see \Drupal\apigee_edge\Annotation\AuthetnicationMethod
* @see \Drupal\apigee_edge\AuthetnicationMethodPluginInterface
* @see plugin_api
*/
class AuthenticationMethodManager extends DefaultPluginManager {

/**
* Constructs a AuthenticationMethodManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
'Plugin/AuthenticationMethod',
$namespaces,
$module_handler,
AuthenticationMethodPluginInterface::class,
AuthenticationMethod::class
);

$this->alterInfo('authetnication_method_info');
$this->setCacheBackend($cache_backend, 'authentication_method');
$this->factory = new DefaultFactory($this->getDiscovery());
}

}
26 changes: 26 additions & 0 deletions apigee_edge/src/AuthenticationMethodPluginBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Drupal\apigee_edge;

use Drupal\Component\Plugin\PluginBase;

/**
* Defines the AuthenticationMethodPluginBase abstract class.
*/
abstract class AuthenticationMethodPluginBase extends PluginBase implements AuthenticationMethodPluginInterface {

/**
* {@inheritdoc}
*/
public function getId() : string {
return $this->pluginDefinition['id'];
}

/**
* {@inheritdoc}
*/
public function getName() : string {
return $this->pluginDefinition['name'];
}

}
41 changes: 41 additions & 0 deletions apigee_edge/src/AuthenticationMethodPluginInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\apigee_edge;

use Drupal\Component\Plugin\PluginInspectionInterface;
use Http\Message\Authentication as AuthenticationInterface;

/**
* Defines an interface for authentication method plugins.
*/
interface AuthenticationMethodPluginInterface extends PluginInspectionInterface {

/**
* Returns the ID of the authentication method plugin.
*
* @return string
* The ID of the authentication method plugin.
*/
public function getId() : string;

/**
* Returns the name of the authentication method plugin.
*
* @return string
* The name of the authentication method plugin.
*/
public function getName() : string;

/**
* Creates an authentication object.
*
* @param \Drupal\apigee_edge\CredentialsInterface $credentials
* An object that implements \Drupal\apigee_edge\CredentialsInterface
* which contains the API credentials.
*
* @return \Http\Message\Authentication
* An object that implements \Http\Message\Authentication.
*/
public function createAuthenticationObject(CredentialsInterface $credentials) : AuthenticationInterface;

}
Loading

0 comments on commit 2225e32

Please sign in to comment.