Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Generated tables prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
lookyman committed Aug 19, 2016
1 parent 0ba67c3 commit 79c2c2f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ oauth2:
publicKey: /path/to/public.key
approveDestination: Approve:
loginDestination: Sign:in
tablePrefix: nette_oauth2_server_
```

The `grants` section contains grants that you want to enable. By default they are all disabled, so you just have to enter those you want to use. Each value doesn't have to just be a boolean. You can specify a token TTL like this: `[ttl: PT1H]`. Two of the grants also have additional settings. The `Authorization Code` grant has the `authCodeTtl` option, and the `Implicit` grant has the `accessTokenTtl` option. In each of these cases, the format for specifying the intervals follows the format described [here](https://secure.php.net/manual/en/dateinterval.construct.php).
Expand All @@ -79,6 +80,8 @@ Finally, if you are using either `Authorization Code` or `Implicit` grants, you

You can omit `approveDestination` and `loginDestination` options if you are not using `Authorization Code` or `Implicit` grants.

The `tablePrefix` option lets you set the prefix for generated SQL tables. The default value is `nette_oauth2_server_`.

### 4. Update database schema

```sh
Expand Down
4 changes: 2 additions & 2 deletions src/AccessToken/AccessTokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* @ORM\Entity()
* @ORM\Table(name="nette_oauth2_server_access_token")
* @ORM\Table(name="access_token")
*/
class AccessTokenEntity implements AccessTokenEntityInterface
{
Expand Down Expand Up @@ -55,7 +55,7 @@ class AccessTokenEntity implements AccessTokenEntityInterface

/**
* @ORM\ManyToMany(targetEntity="Lookyman\NetteOAuth2Server\Storage\Doctrine\Scope\ScopeEntity")
* @ORM\JoinTable(name="nette_oauth2_server_access_token_scope")
* @ORM\JoinTable(name="access_token_scope")
* @var Collection of ScopeEntity
*/
private $scopes;
Expand Down
4 changes: 2 additions & 2 deletions src/AuthCode/AuthCodeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* @ORM\Entity()
* @ORM\Table(name="nette_oauth2_server_auth_code")
* @ORM\Table(name="auth_code")
*/
class AuthCodeEntity implements AuthCodeEntityInterface
{
Expand Down Expand Up @@ -64,7 +64,7 @@ class AuthCodeEntity implements AuthCodeEntityInterface

/**
* @ORM\ManyToMany(targetEntity="Lookyman\NetteOAuth2Server\Storage\Doctrine\Scope\ScopeEntity")
* @ORM\JoinTable(name="nette_oauth2_server_auth_code_scope")
* @ORM\JoinTable(name="auth_code_scope")
* @var Collection of ScopeEntity
*/
private $scopes;
Expand Down
2 changes: 1 addition & 1 deletion src/Client/ClientEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* @ORM\Entity()
* @ORM\Table(name="nette_oauth2_server_client")
* @ORM\Table(name="client")
*/
class ClientEntity implements ClientEntityInterface
{
Expand Down
8 changes: 8 additions & 0 deletions src/NetteOAuth2ServerDoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine;

use Kdyby\Doctrine\DI\IEntityProvider;
use Kdyby\Events\DI\EventsExtension;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Grant\AuthCodeGrant;
Expand Down Expand Up @@ -44,13 +45,20 @@ class NetteOAuth2ServerDoctrineExtension extends CompilerExtension implements IE
'publicKey' => null,
'approveDestination' => null,
'loginDestination' => null,
'tablePrefix' => 'nette_oauth2_server_',
];

public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

// Table mapping
Validators::assertField($config, 'tablePrefix', 'string');
$builder->addDefinition($this->prefix('tablePrefixListener'))
->setClass(TablePrefixListener::class, [$config['tablePrefix']])
->addTag(EventsExtension::TAG_SUBSCRIBER);

// Common repositories
$builder->addDefinition($this->prefix('repository.client'))
->setClass(ClientRepository::class);
Expand Down
2 changes: 1 addition & 1 deletion src/RefreshToken/RefreshTokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* @ORM\Entity()
* @ORM\Table(name="nette_oauth2_server_refresh_token")
* @ORM\Table(name="refresh_token")
*/
class RefreshTokenEntity implements RefreshTokenEntityInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Scope/ScopeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* @ORM\Entity()
* @ORM\Table(name="nette_oauth2_server_scope")
* @ORM\Table(name="scope")
*/
class ScopeEntity implements ScopeEntityInterface
{
Expand Down
60 changes: 60 additions & 0 deletions src/TablePrefixListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Lookyman\NetteOAuth2Server\Storage\Doctrine;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Kdyby\Doctrine\Events;
use Kdyby\Doctrine\Mapping\ClassMetadata;
use Kdyby\Events\Subscriber;
use Lookyman\NetteOAuth2Server\Storage\Doctrine\AccessToken\AccessTokenEntity;
use Lookyman\NetteOAuth2Server\Storage\Doctrine\AuthCode\AuthCodeEntity;
use Lookyman\NetteOAuth2Server\Storage\Doctrine\Client\ClientEntity;
use Lookyman\NetteOAuth2Server\Storage\Doctrine\RefreshToken\RefreshTokenEntity;
use Lookyman\NetteOAuth2Server\Storage\Doctrine\Scope\ScopeEntity;

class TablePrefixListener implements Subscriber
{
const ENTITIES = [
AccessTokenEntity::class,
AuthCodeEntity::class,
ClientEntity::class,
RefreshTokenEntity::class,
ScopeEntity::class,
];

/**
* @var string
*/
private $prefix;

public function __construct(string $prefix)
{
$this->prefix = $prefix;
}

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
/** @var ClassMetadata $metadata */
$metadata = $eventArgs->getClassMetadata();
if (in_array($metadata->getName(), self::ENTITIES)) {
$metadata->setPrimaryTable([
'name' => $this->prefix . $metadata->getTableName(),
]);

foreach ($metadata->getAssociationMappings() as $name => $mapping) {
if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && $mapping['isOwningSide']) {
$metadata->associationMappings[$name]['joinTable']['name'] = $this->prefix . $mapping['joinTable']['name'];
}
}
}
}

/**
* @return array
*/
public function getSubscribedEvents()
{
return [Events::loadClassMetadata];
}
}

0 comments on commit 79c2c2f

Please sign in to comment.