Last active
August 29, 2015 14:20
-
-
Save basz/56aef5773154a5687b77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * This doctrine event subscriber will join a user table to the client table | |
| * thereby freeing the user table from the OAuth2 contraints | |
| */ | |
| namespace ZF\OAuth2\Doctrine\EventListener; | |
| use Doctrine\Common\EventSubscriber; | |
| use Doctrine\ORM\Events; | |
| use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | |
| class DynamicMappingSubscriber implements EventSubscriber | |
| { | |
| protected $config = array(); | |
| public function __construct($config) | |
| { | |
| $this->config = $config; | |
| } | |
| /** | |
| * {@inheritDoc} | |
| */ | |
| public function getSubscribedEvents() | |
| { | |
| return array( | |
| Events::loadClassMetadata, | |
| ); | |
| } | |
| /** | |
| * @param LoadClassMetadataEventArgs $eventArgs | |
| */ | |
| public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) | |
| { | |
| // the $metadata is the whole mapping info for this class | |
| $metadata = $eventArgs->getClassMetadata(); | |
| switch ($metadata->getName()) { | |
| case $this->config['user_entity']['entity']: | |
| $metadata->mapOneToMany(array( | |
| 'targetEntity' => $this->config['client_entity']['entity'], | |
| 'fieldName' => $this->config['client_entity']['field'], | |
| 'mappedBy' => $this->config['user_entity']['field'], | |
| )); | |
| $metadata->mapOneToMany(array( | |
| 'targetEntity' => $this->config['access_token_entity']['entity'], | |
| 'fieldName' => $this->config['access_token_entity']['field'], | |
| 'mappedBy' => $this->config['user_entity']['field'], | |
| )); | |
| $metadata->mapOneToMany(array( | |
| 'targetEntity' => $this->config['authorization_code_entity']['entity'], | |
| 'fieldName' => $this->config['authorization_code_entity']['field'], | |
| 'mappedBy' => $this->config['user_entity']['field'], | |
| )); | |
| $metadata->mapOneToMany(array( | |
| 'targetEntity' => $this->config['refresh_token_entity']['entity'], | |
| 'fieldName' => $this->config['refresh_token_entity']['field'], | |
| 'mappedBy' => $this->config['user_entity']['field'], | |
| )); | |
| break; | |
| case $this->config['client_entity']['entity']: | |
| if (isset($this->config['client_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['client_entity']['table_name']); | |
| } | |
| $joinMap = array( | |
| 'targetEntity' => $this->config['user_entity']['entity'], | |
| 'fieldName' => $this->config['user_entity']['field'], | |
| 'inversedBy' => $this->config['client_entity']['field'], | |
| ); | |
| if (isset($this->config['client_entity']['additional_mapping_data'])) { | |
| $joinMap = array_merge( | |
| $joinMap, | |
| $this->config['client_entity']['additional_mapping_data'] | |
| ); | |
| } | |
| $metadata->mapManyToOne($joinMap); | |
| break; | |
| case $this->config['access_token_entity']['entity']: | |
| if (isset($this->config['access_token_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['access_token_entity']['table_name']); | |
| } | |
| $joinMap = array( | |
| 'targetEntity' => $this->config['user_entity']['entity'], | |
| 'fieldName' => $this->config['user_entity']['field'], | |
| 'inversedBy' => $this->config['access_token_entity']['field'], | |
| ); | |
| if (isset($this->config['access_token_entity']['additional_mapping_data'])) { | |
| $joinMap = array_merge( | |
| $joinMap, | |
| $this->config['access_token_entity']['additional_mapping_data'] | |
| ); | |
| } | |
| $metadata->mapManyToOne($joinMap); | |
| break; | |
| case $this->config['authorization_code_entity']['entity']: | |
| if (isset($this->config['authorization_code_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['authorization_code_entity']['table_name']); | |
| } | |
| $joinMap = array( | |
| 'targetEntity' => $this->config['user_entity']['entity'], | |
| 'fieldName' => $this->config['user_entity']['field'], | |
| 'inversedBy' => $this->config['authorization_code_entity']['field'], | |
| ); | |
| if (isset($this->config['authorization_code_entity']['additional_mapping_data'])) { | |
| $joinMap = array_merge( | |
| $joinMap, | |
| $this->config['authorization_code_entity']['additional_mapping_data'] | |
| ); | |
| } | |
| $metadata->mapManyToOne($joinMap); | |
| break; | |
| case $this->config['refresh_token_entity']['entity']: | |
| if (isset($this->config['refresh_token_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['refresh_token_entity']['table_name']); | |
| } | |
| $joinMap = array( | |
| 'targetEntity' => $this->config['user_entity']['entity'], | |
| 'fieldName' => $this->config['user_entity']['field'], | |
| 'inversedBy' => $this->config['refresh_token_entity']['field'], | |
| ); | |
| if (isset($this->config['refresh_token_entity']['additional_mapping_data'])) { | |
| $joinMap = array_merge( | |
| $joinMap, | |
| $this->config['refresh_token_entity']['additional_mapping_data'] | |
| ); | |
| } | |
| $metadata->mapManyToOne($joinMap); | |
| break; | |
| case $this->config['scope_entity']['entity']: | |
| if (isset($this->config['scope_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['scope_entity']['table_name']); | |
| } | |
| if (isset($this->config['scope_entity']['additional_mapping_data']['associations_override'])) { | |
| $override = $this->config['scope_entity']['additional_mapping_data']['associations_override']; | |
| foreach ($override as $field => $mapping) { | |
| $metadata->setAssociationOverride($field, $mapping); | |
| } | |
| } | |
| break; | |
| case $this->config['jwt_entity']['entity']: | |
| if (isset($this->config['jwt_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['jwt_entity']['table_name']); | |
| } | |
| break; | |
| case $this->config['jti_entity']['entity']: | |
| if (isset($this->config['jti_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['jti_entity']['table_name']); | |
| } | |
| break; | |
| case $this->config['public_key_entity']['entity']: | |
| if (isset($this->config['public_key_entity']['table_name'])) { | |
| $metadata->setTableName($this->config['public_key_entity']['table_name']); | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * The user entity is always stored in another namespace than ZF\OAuth2 | |
| */ | |
| $userEntity = 'HF\Model\Entity\Authentication\User'; | |
| return array( | |
| 'zf-oauth2-doctrine' => array( | |
| 'storage' => 'ZF\OAuth2\Doctrine\Adapter\DoctrineAdapter', | |
| 'storage_settings' => array( | |
| 'object_manager' => 'doctrine.entitymanager.orm_default', | |
| 'event_manager' => 'doctrine.eventmanager.orm_default', | |
| 'driver' => 'doctrine.driver.orm_default', | |
| 'enable_default_entities' => true, | |
| 'bcrypt_cost' => 14, # match zfcuser | |
| // Dynamically map the user_entity to the client_entity | |
| 'dynamic_mapping' => array( | |
| 'user_entity' => array( | |
| 'entity' => $userEntity, | |
| 'field' => 'user', | |
| ), | |
| 'client_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'field' => 'client', | |
| 'table_name' => 'oauth2_client', | |
| 'additional_mapping_data' => array( | |
| 'joinColumns' => array( | |
| array( | |
| 'name' => 'user_id', | |
| 'referencedColumnName' => 'user_id', | |
| ), | |
| ), | |
| ), | |
| ), | |
| 'access_token_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\AccessToken', | |
| 'field' => 'accessToken', | |
| 'table_name' => 'oauth2_access_token', | |
| 'additional_mapping_data' => array( | |
| 'joinColumns' => array( | |
| array( | |
| 'name' => 'user_id', | |
| 'referencedColumnName' => 'user_id', | |
| ), | |
| ), | |
| ), | |
| ), | |
| 'authorization_code_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\AuthorizationCode', | |
| 'table_name' => 'oauth2_authorization_code', | |
| 'field' => 'authorizationCode', | |
| 'additional_mapping_data' => array( | |
| 'joinColumns' => array( | |
| array( | |
| 'name' => 'user_id', | |
| 'referencedColumnName' => 'user_id', | |
| ), | |
| ), | |
| ), | |
| ), | |
| 'refresh_token_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\RefreshToken', | |
| 'field' => 'refreshToken', | |
| 'table_name' => 'oauth2_refresh_token', | |
| 'additional_mapping_data' => array( | |
| 'joinColumns' => array( | |
| array( | |
| 'name' => 'user_id', | |
| 'referencedColumnName' => 'user_id', | |
| ), | |
| ), | |
| ), | |
| ), | |
| 'scope_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Scope', | |
| 'table_name' => 'oauth2_scope', | |
| 'additional_mapping_data' => array( | |
| 'associations_override' => array( | |
| 'client' => array( | |
| 'joinTable' => array( | |
| 'name' => 'oauth2_client_to_scope' | |
| ) | |
| ), | |
| 'authorizationCode' => array( | |
| 'joinTable' => array( | |
| 'name' => 'oauth2_authorizationcode_to_scope' | |
| ) | |
| ), | |
| 'refreshToken' => array( | |
| 'joinTable' => array( | |
| 'name' => 'oauth2_refreshtoken_to_scope' | |
| ) | |
| ), | |
| 'accessToken' => array( | |
| 'joinTable' => array( | |
| 'name' => 'oauth2_accesstoken_to_scope' | |
| ) | |
| ), | |
| ), | |
| ), | |
| ), | |
| 'public_key_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\PublicKey', | |
| 'table_name' => 'oauth2_publickey', | |
| ), | |
| 'jwt_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Jwt', | |
| 'table_name' => 'oauth2_jwt', | |
| ), | |
| 'jti_entity' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Jti', | |
| 'table_name' => 'oauth2_jti', | |
| ), | |
| ), | |
| 'mapping' => array( | |
| 'ZF\OAuth2\Doctrine\Mapper\User' => array( | |
| 'entity' => $userEntity, | |
| 'mapping' => array( | |
| 'user_id' => array( | |
| 'type' => 'field', | |
| 'name' => 'id', | |
| 'datatype' => 'integer', | |
| ), | |
| 'username' => array( | |
| 'type' => 'field', | |
| 'name' => 'username', | |
| 'datatype' => 'string', | |
| ), | |
| 'password' => array( | |
| 'type' => 'field', | |
| 'name' => 'password', | |
| 'datatype' => 'string', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\Client' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'mapping' => array( | |
| 'client_id' => array( | |
| 'type' => 'field', | |
| 'name' => 'clientId', | |
| 'datatype' => 'integer', | |
| ), | |
| 'client_secret' => array( | |
| 'type' => 'field', | |
| 'name' => 'secret', | |
| 'datatype' => 'string', | |
| ), | |
| 'redirect_uri' => array( | |
| 'type' => 'field', | |
| 'name' => 'redirectUri', | |
| 'datatype' => 'text', | |
| ), | |
| 'grant_types' => array( | |
| 'type' => 'field', | |
| 'name' => 'grantType', | |
| 'datatype' => 'array', | |
| ), | |
| 'scope' => array( | |
| 'type' => 'collection', | |
| 'name' => 'scope', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Scope', | |
| 'mapper' => 'ZF\OAuth2\Doctrine\Mapper\Scope', | |
| ), | |
| 'user_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'user', | |
| 'entity_field_name' => 'user_id', | |
| 'entity' => $userEntity, | |
| 'datatype' => 'integer', | |
| 'allow_null' => true, | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\AccessToken' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\AccessToken', | |
| 'mapping' => array( | |
| 'access_token' => array( | |
| 'type' => 'field', | |
| 'name' => 'accessToken', | |
| 'datatype' => 'string', | |
| ), | |
| 'expires' => array( | |
| 'type' => 'field', | |
| 'name' => 'expires', | |
| 'datatype' => 'datetime', | |
| ), | |
| 'scope' => array( | |
| 'type' => 'collection', | |
| 'name' => 'scope', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Scope', | |
| 'mapper' => 'ZF\OAuth2\Doctrine\Mapper\Scope', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| 'user_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'user', | |
| 'entity_field_name' => 'id', | |
| 'entity' => $userEntity, | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\RefreshToken' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\RefreshToken', | |
| 'mapping' => array( | |
| 'refresh_token' => array( | |
| 'type' => 'field', | |
| 'name' => 'refreshToken', | |
| 'datatype' => 'string', | |
| ), | |
| 'expires' => array( | |
| 'type' => 'field', | |
| 'name' => 'expires', | |
| 'datatype' => 'datetime', | |
| ), | |
| 'scope' => array( | |
| 'type' => 'collection', | |
| 'name' => 'scope', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Scope', | |
| 'mapper' => 'ZF\OAuth2\Doctrine\Mapper\Scope', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| 'user_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'user', | |
| 'entity_field_name' => 'id', | |
| 'entity' => $userEntity, | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\AuthorizationCode' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\AuthorizationCode', | |
| 'mapping' => array( | |
| 'authorization_code' => array( | |
| 'type' => 'field', | |
| 'name' => 'authorizationCode', | |
| 'datatype' => 'string', | |
| ), | |
| 'redirect_uri' => array( | |
| 'type' => 'field', | |
| 'name' => 'redirectUri', | |
| 'datatype' => 'text', | |
| ), | |
| 'expires' => array( | |
| 'type' => 'field', | |
| 'name' => 'expires', | |
| 'datatype' => 'datetime', | |
| ), | |
| 'scope' => array( | |
| 'type' => 'collection', | |
| 'name' => 'scope', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Scope', | |
| 'mapper' => 'ZF\OAuth2\Doctrine\Mapper\Scope', | |
| ), | |
| 'id_token' => array( | |
| 'type' => 'field', | |
| 'name' => 'idToken', | |
| 'datatype' => 'text', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| 'user_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'user', | |
| 'entity_field_name' => 'id', | |
| 'entity' => $userEntity, | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\Jwt' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Jwt', | |
| 'mapping' => array( | |
| 'subject' => array( | |
| 'type' => 'field', | |
| 'name' => 'subject', | |
| 'datatype' => 'string', | |
| ), | |
| 'public_key' => array( | |
| 'type' => 'field', | |
| 'name' => 'publicKey', | |
| 'datatype' => 'text', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\Jti' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Jti', | |
| 'mapping' => array( | |
| 'subject' => array( | |
| 'type' => 'field', | |
| 'name' => 'subject', | |
| 'datatype' => 'string', | |
| ), | |
| 'audience' => array( | |
| 'type' => 'field', | |
| 'name' => 'audience', | |
| 'datatype' => 'string', | |
| ), | |
| 'expiration' => array( | |
| 'type' => 'field', | |
| 'name' => 'expires', | |
| 'datatype' => 'datetime', | |
| ), | |
| 'jti' => array( | |
| 'type' => 'field', | |
| 'name' => 'jti', | |
| 'datatype' => 'text', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\Scope' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Scope', | |
| 'mapping' => array( | |
| 'scope' => array( | |
| 'type' => 'field', | |
| 'name' => 'scope', | |
| 'datatype' => 'text', | |
| ), | |
| 'is_default' => array( | |
| 'type' => 'field', | |
| 'name' => 'isDefault', | |
| 'datatype' => 'boolean', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| 'ZF\OAuth2\Doctrine\Mapper\PublicKey' => array( | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\PublicKey', | |
| 'mapping' => array( | |
| 'public_key' => array( | |
| 'type' => 'field', | |
| 'name' => 'publicKey', | |
| 'datatype' => 'text', | |
| ), | |
| 'private_key' => array( | |
| 'type' => 'field', | |
| 'name' => 'privateKey', | |
| 'datatype' => 'text', | |
| ), | |
| 'encryption_algorithm' => array( | |
| 'type' => 'field', | |
| 'name' => 'encryptionAlgorithm', | |
| 'datatype' => 'string', | |
| ), | |
| 'client_id' => array( | |
| 'type' => 'relation', | |
| 'name' => 'client', | |
| 'entity_field_name' => 'clientId', | |
| 'entity' => 'ZF\OAuth2\Doctrine\Entity\Client', | |
| 'datatype' => 'integer', | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment