Skip to content

Commit

Permalink
Not export tenant_id attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Oct 15, 2019
1 parent f0992a8 commit 3e8ce48
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/MultiTenantRecord.php
Expand Up @@ -15,8 +15,8 @@ public function beforeSave($insert)
if (!Yii::$app->user->isGuest) {
$identity = Yii::$app->user->identity;
if ($identity instanceof TenantInterface) {
if ($insert && $this->beforeApplyTenant() && !$this->tenant_id) {
$this->tenant_id = $identity->getTenantId();
if ($insert && $this->beforeApplyTenant() && $this->{TenantInterface::ATTRIBUTE_NAME} === null) {
$this->{TenantInterface::ATTRIBUTE_NAME} = $identity->getTenantId();
$this->afterApplyTenant();
}
} else {
Expand All @@ -34,6 +34,21 @@ public static function find()
return Yii::createObject(MultiTenantQuery::class, [get_called_class()]);
}

/**
* {@inheritdoc}
*/
protected function resolveFields(array $fields, array $expand)
{
$fields = parent::resolveFields($fields, $expand);

if ($this->hasAttribute(TenantInterface::ATTRIBUTE_NAME)) {
unset($fields[TenantInterface::ATTRIBUTE_NAME]);
}

return $fields;
}



/**
* This method is invoked before assign the attribute tenant id
Expand Down
2 changes: 2 additions & 0 deletions src/TenantInterface.php
Expand Up @@ -4,6 +4,8 @@

interface TenantInterface {

const ATTRIBUTE_NAME = 'tenant_id';

/**
* Returns tenant id
* @return integer id of tenant
Expand Down
20 changes: 19 additions & 1 deletion tests/MultiTenantRecordTest.php
Expand Up @@ -3,6 +3,9 @@
namespace solutosoft\multitenant\tests;

use solutosoft\multitenant\tests\fixtures\PersonFixture;
use solutosoft\multitenant\tests\fixtures\PersonTagFixture;
use solutosoft\multitenant\tests\fixtures\PessoaTagFixture;
use solutosoft\multitenant\tests\fixtures\TagFixture;
use solutosoft\multitenant\tests\models\Person;

class MultiTenantRecordTest extends TestCase
Expand All @@ -23,7 +26,9 @@ protected function setUp()
public function fixtures()
{
return [
'people' => PersonFixture::class
'people' => PersonFixture::class,
'tags' => TagFixture::class,
'persontags' => PersonTagFixture::class
];
}

Expand Down Expand Up @@ -59,4 +64,17 @@ public function testFind()
$this->assertCount(count($data), Person::find()->withoutTenant()->all());
$this->assertCount(count($filtered), Person::find()->all());
}

public function testToArray()
{
$person = $this->people('bob');
$data = $person->toArray([], ['tags']);

$this->assertArrayNotHasKey('tenant_id', $data);
$this->assertArrayHasKey('tags', $data);

foreach ($data['tags'] as $tag) {
$this->assertArrayNotHasKey('tenant_id', $tag);
}
}
}
17 changes: 16 additions & 1 deletion tests/TestCase.php
Expand Up @@ -107,7 +107,7 @@ protected function setupDatabase()
$db->createCommand()->createTable('person', [
'id' => 'pk',
'name' => 'string',
'profile_id' => 'integeger',
'profile_id' => 'integer',
'tenant_id' => 'integer'
])->execute();

Expand All @@ -116,6 +116,21 @@ protected function setupDatabase()
'number' => 'string',
'description' => 'string'
])->execute();

$db->createCommand()->createTable('tag', [
'id' => 'pk',
'description' => 'string',
'tenant_id' => 'integer'
])->execute();


$db->createCommand()->createTable('person_tag', [
'person_id' => 'integer',
'tag_id' => 'integer',
'PRIMARY KEY(person_id, tag_id)'
])->execute();


}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/PersonTagFixture.php
@@ -0,0 +1,10 @@
<?php

namespace solutosoft\multitenant\tests\fixtures;

use yii\test\ActiveFixture;

class PersonTagFixture extends ActiveFixture
{
public $tableName = 'person_tag';
}
12 changes: 12 additions & 0 deletions tests/fixtures/data/person_tag.php
@@ -0,0 +1,12 @@
<?php

return [
'person-tag-1' => [
'person_id' => 2,
'tag_id' => 1,
],
'tag-2' => [
'person_id' => 2,
'tag_id' => 2,
]
];
8 changes: 4 additions & 4 deletions tests/fixtures/data/tag.php
Expand Up @@ -3,12 +3,12 @@
return [
'tag-1' => [
'id' => 1,
'link' => 't01',
'description' => 'Tag 2'
'description' => 'Tag 1',
'tenant_id' => 1
],
'tag-2' => [
'id' => 2,
'link' => 't02',
'description' => 'Tag 2'
'description' => 'Tag 2',
'tenant_id' => 1
]
];
19 changes: 19 additions & 0 deletions tests/models/Person.php
Expand Up @@ -42,6 +42,15 @@ public function getProfile()
return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('person_tag', ['person_id' => 'id']);
}

/**
* @inheritdoc
*/
Expand All @@ -53,6 +62,16 @@ public function rules()
];
}

/**
* @inheritdoc
*/
public function extraFields()
{
return [
'tags'
];
}

/**
* @inheritdoc
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/models/Tag.php
Expand Up @@ -2,7 +2,7 @@

namespace solutosoft\multitenant\tests\models;

use soluto\base\db\MultiTenantRecord;
use solutosoft\multitenant\MultiTenantRecord;
use yii\db\ActiveRecord;

/**
Expand All @@ -27,7 +27,7 @@ public static function tableName()
public function rules()
{
return [
[['description', 'link'], 'string'],
[['description'], 'string'],
[['description'], 'required']
];
}
Expand Down

0 comments on commit 3e8ce48

Please sign in to comment.