Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SWP-2261] feat(multi-domain-tenant): Added implementation for multidomain tenants #1261

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace SWP\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240614144029 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SEQUENCE swp_tenant_domain_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE swp_tenant_domain (id INT NOT NULL, tenant_id INT DEFAULT NULL, subdomain VARCHAR(255) DEFAULT NULL, domain_name VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_22EBB6319033212A ON swp_tenant_domain (tenant_id)');
$this->addSql('ALTER TABLE swp_tenant_domain ADD CONSTRAINT FK_22EBB6319033212A FOREIGN KEY (tenant_id) REFERENCES swp_tenant (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE swp_tenant_domain');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace SWP\Bundle\MultiTenancyBundle\DependencyInjection;

use SWP\Bundle\MultiTenancyBundle\Doctrine\ORM\OrganizationRepository;
use SWP\Bundle\MultiTenancyBundle\Doctrine\ORM\TenantDomainRepository;
use SWP\Bundle\MultiTenancyBundle\Doctrine\ORM\TenantRepository;
use SWP\Bundle\MultiTenancyBundle\Doctrine\PHPCR\OrganizationRepository as PHPCROrganizationRepository;
use SWP\Bundle\MultiTenancyBundle\Doctrine\PHPCR\TenantRepository as PHPCRTenantRepository;
Expand All @@ -24,6 +25,8 @@
use SWP\Component\MultiTenancy\Model\Organization;
use SWP\Component\MultiTenancy\Model\OrganizationInterface;
use SWP\Component\MultiTenancy\Model\Tenant;
use SWP\Component\MultiTenancy\Model\TenantDomain;
use SWP\Component\MultiTenancy\Model\TenantDomainInterface;
use SWP\Component\MultiTenancy\Model\TenantInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -128,6 +131,15 @@ public function getConfigTreeBuilder()
->scalarNode('object_manager_name')->defaultValue(null)->end()
->end()
->end()
->arrayNode('tenantDomain')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->cannotBeEmpty()->defaultValue(TenantDomain::class)->end()
->scalarNode('interface')->cannotBeEmpty()->defaultValue(TenantDomainInterface::class)->end()
->scalarNode('repository')->defaultValue(TenantDomainRepository::class)->end()
->scalarNode('object_manager_name')->defaultValue(null)->end()
->end()
->end()
->arrayNode('organization')
->addDefaultsIfNotSet()
->children()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
*
* Copyright 2015 Sourcefabric z.u. and contributors.
*
* For the full copyright and license information, please see the
* AUTHORS and LICENSE files distributed with this source code.
*
* @copyright 2015 Sourcefabric z.ú
* @license http://www.superdesk.org/license
*/

namespace SWP\Bundle\MultiTenancyBundle\Doctrine\ORM;

use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository;
use SWP\Component\MultiTenancy\Model\TenantDomainInterface;
use SWP\Component\MultiTenancy\Repository\TenantDomainRepositoryInterface;

/**
* Repository interface for tenants.
*/
class TenantDomainRepository extends EntityRepository implements TenantDomainRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function findOneBySubdomainAndDomain(string $subdomain, string $domain): ?TenantDomainInterface
{
return $this
->createQueryBuilder('td')
->select('td', 't',)
->leftJoin('td.tenant', 't')
->where('td.subdomain = :subdomain')
->andWhere('td.domainName = :domainName')
->setParameters([
'subdomain' => $subdomain,
'domainName' => $domain,
])
->getQuery()
->getOneOrNullResult();
}

/**
* {@inheritdoc}
*/
public function findOneByDomain(string $domain): ?TenantDomainInterface
{
return $this
->createQueryBuilder('td')
->select('td', 't')
->leftJoin('td.tenant', 't')
->where('td.domainName = :domainName')
->andWhere('td.subdomain IS NULL')
->setParameter('domainName', $domain)
->getQuery()
->getOneOrNullResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
*
* Copyright 2016 Sourcefabric z.ú. and contributors.
*
* For the full copyright and license information, please see the
* AUTHORS and LICENSE files distributed with this source code.
*
* @copyright 2016 Sourcefabric z.ú
* @license http://www.superdesk.org/license
*/

namespace SWP\Bundle\MultiTenancyBundle\Doctrine\PHPCR;

use SWP\Bundle\StorageBundle\Doctrine\ODM\PHPCR\DocumentRepository;
use SWP\Component\MultiTenancy\Repository\TenantDomainRepositoryInterface;

class TenantDomainRepository extends DocumentRepository implements TenantDomainRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function findOneBySubdomainAndDomain($subdomain, $domain)
{
$qb = $this->createQueryBuilder('t');
$qb->where()->eq()->field('t.subdomain')->literal($subdomain);
$qb->andWhere()->eq()->field('t.domainName')->literal($domain);

return $qb->getQuery()->getOneOrNullResult();
}

/**
* {@inheritdoc}
*/
public function findOneByDomain($domain)
{
$qb = $this->createQueryBuilder('t');
$qb->where()->eq()->field('t.domainName')->literal($domain);
$qb->andWhere()->eq()->field('t.subdomain')->literal(null);

return $qb->getQuery()->getOneOrNullResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SWP\Component\MultiTenancy\Model\TenantDomain:
type: entity
table: swp_tenant_domain
repositoryClass: SWP\Bundle\MultiTenancyBundle\Doctrine\ORM\TenantDomainRepository
gedmo:
soft_deleteable:
field_name: deletedAt
time_aware: false
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
subdomain:
type: string
length: 255
nullable: true
domainName:
type: string
nullable: true

createdAt:
type: datetime
gedmo:
timestampable:
on: create
updatedAt:
type: datetime
nullable: true
gedmo:
timestampable:
on: update
deletedAt:
type: datetime
nullable: true
manyToOne:
tenant:
targetEntity: SWP\Component\MultiTenancy\Model\TenantInterface
joinColumn:
name: tenant_id
referencedColumnName: id
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SWP\Component\MultiTenancy\Model\TenantDomain:
repositoryClass: SWP\Bundle\MultiTenancyBundle\Doctrine\PHPCR\TenantDomainRepository
referenceable: true
parentdocument: tenant
nodename: code
id: id
fields:
subdomain:
type: string
length: 255
nullable: true
domainName:
type: string
length: 255
createdAt:
type: date
updatedAt:
type: date
nullable: true
deletedAt:
type: date
nullable: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SWP\Component\MultiTenancy\Model\TenantDomain:
exclusion_policy: ALL
properties:
id:
expose: true
groups: [api, api_tenant_list]
type: integer
subdomain:
expose: true
groups: [api, api_tenant_list]
type: string
domainName:
expose: true
groups: [api, api_tenant_list]
type: string
createdAt:
expose: true
groups: [api]
type: DateTime
updatedAt:
expose: true
groups: [api]
type: DateTime
tenant:
expose: true
groups: [api]
type: SWP\Component\MultiTenancy\Model\Tenant
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
$suffixListFilename: '%env(PUBLIC_SUFFIX_LIST_FILENAME)%'
arguments:
- '@swp.repository.tenant'
- '@swp.repository.tenantDomain'
- '@doctrine.system_cache_pool'

swp_multi_tenancy.tenant_context:
Expand Down
118 changes: 118 additions & 0 deletions src/SWP/Component/MultiTenancy/Model/TenantDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/*
* This file is part of the Superdesk Web Publisher MultiTenancy Component.
*
* Copyright 2016 Sourcefabric z.ú. and contributors.
*
* For the full copyright and license information, please see the
* AUTHORS and LICENSE files distributed with this source code.
*
* @copyright 2016 Sourcefabric z.ú
* @license http://www.superdesk.org/license
*/

namespace SWP\Component\MultiTenancy\Model;

use SWP\Component\Common\Model\EnableableTrait;
use SWP\Component\Common\Model\SoftDeletableTrait;
use SWP\Component\Common\Model\TimestampableTrait;

class TenantDomain implements TenantDomainInterface
{
use SoftDeletableTrait;
use TimestampableTrait;
use EnableableTrait;

/**
* @var mixed
*/
protected $id;

/**
* @var string
*/
protected $domainName;

/**
* @var string
*/
protected $subdomain;

/**
* @var TenantInterface
*/
protected $tenant;

/**
* TenantDomain constructor.
*/
public function __construct()
{
$this->createdAt = new \DateTime();
}

/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}

/**
* {@inheritdoc}
*/
public function setId($id)
{
$this->id = $id;
}

/**
* {@inheritdoc}
*/
public function getDomainName(): string
{
return $this->domainName;
}

/**
* {@inheritdoc}
*/
public function setDomainName(string $domainName)
{
$this->domainName = $domainName;
}

/**
* {@inheritdoc}
*/
public function getSubdomain(): string
{
return $this->subdomain;
}

/**
* {@inheritdoc}
*/
public function setSubdomain(string $subdomain)
{
$this->subdomain = $subdomain;
}

/**
* {@inheritdoc}
*/
public function setTenant(TenantInterface $tenant)
{
$this->tenant = $tenant;
}

/**
* {@inheritdoc}
*/
public function getTenant(): TenantInterface
{
return $this->tenant;
}
}
Loading
Loading