Skip to content

Commit 7aef306

Browse files
committed
Multi-Tenant ok
1 parent 98a944c commit 7aef306

20 files changed

+352
-17
lines changed

app/Http/Kernel.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeFin\Http;
44

5+
use CodeFin\Http\Middleware\AddClientTenant;
56
use Illuminate\Foundation\Http\Kernel as HttpKernel;
67

78
class Kernel extends HttpKernel
@@ -15,6 +16,7 @@ class Kernel extends HttpKernel
1516
*/
1617
protected $middleware = [
1718
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
19+
AddClientTenant::class,
1820
];
1921

2022
/**
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CodeFin\Http\Middleware;
4+
5+
use Closure;
6+
use HipsterJazzbo\Landlord\Facades\Landlord;
7+
use Illuminate\Support\Facades\Auth;
8+
9+
class AddClientTenant
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
* @return mixed
17+
*/
18+
public function handle($request, Closure $next)
19+
{
20+
if($request->is('api/*')){
21+
$user = Auth::guard('api')->user();
22+
if($user){
23+
$client = $user->client;
24+
Landlord::addTenant($client);
25+
}
26+
}
27+
return $next($request);
28+
}
29+
}

app/Models/BankAccount.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace CodeFin\Models;
44

5+
use HipsterJazzbo\Landlord\BelongsToTenants;
56
use Illuminate\Database\Eloquent\Model;
67
use Prettus\Repository\Contracts\Transformable;
78
use Prettus\Repository\Traits\TransformableTrait;
89

910
class BankAccount extends Model implements Transformable
1011
{
1112
use TransformableTrait;
13+
use BelongsToTenants;
1214

1315
protected $fillable = [
1416
'name',

app/Models/Client.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodeFin\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Prettus\Repository\Contracts\Transformable;
7+
use Prettus\Repository\Traits\TransformableTrait;
8+
9+
class Client extends Model implements Transformable
10+
{
11+
use TransformableTrait;
12+
13+
protected $fillable = ['name'];
14+
15+
public function users()
16+
{
17+
return $this->hasMany(User::class);
18+
}
19+
20+
}

app/Models/User.php

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class User extends Authenticatable implements JWTSubject
2828
'password', 'remember_token',
2929
];
3030

31+
public function client()
32+
{
33+
return $this->belongsTo(Client::class);
34+
}
35+
3136
/**
3237
* Get the identifier that will be stored in the subject claim of the JWT.
3338
*

app/Providers/RepositoryServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function register()
2525
{
2626
$this->app->bind(\CodeFin\Repositories\Interfaces\BankRepository::class, \CodeFin\Repositories\BankRepositoryEloquent::class);
2727
$this->app->bind(\CodeFin\Repositories\Interfaces\BankAccountRepository::class, \CodeFin\Repositories\BankAccountRepositoryEloquent::class);
28+
$this->app->bind(\CodeFin\Repositories\Interfaces\ClientRepository::class, \CodeFin\Repositories\ClientRepositoryEloquent::class);
2829
//:end-bindings:
2930
}
3031
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace CodeFin\Repositories;
4+
5+
use Prettus\Repository\Eloquent\BaseRepository;
6+
use Prettus\Repository\Criteria\RequestCriteria;
7+
use CodeFin\Repositories\Interfaces\ClientRepository;
8+
use CodeFin\Models\Client;
9+
use CodeFin\Validators\ClientValidator;
10+
11+
/**
12+
* Class ClientRepositoryEloquent
13+
* @package namespace CodeFin\Repositories;
14+
*/
15+
class ClientRepositoryEloquent extends BaseRepository implements ClientRepository
16+
{
17+
/**
18+
* Specify Model class name
19+
*
20+
* @return string
21+
*/
22+
public function model()
23+
{
24+
return Client::class;
25+
}
26+
27+
28+
29+
/**
30+
* Boot up the repository, pushing criteria
31+
*/
32+
public function boot()
33+
{
34+
$this->pushCriteria(app(RequestCriteria::class));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace CodeFin\Repositories\Interfaces;
4+
5+
use Prettus\Repository\Contracts\RepositoryInterface;
6+
7+
/**
8+
* Interface ClientRepository
9+
* @package namespace CodeFin\Repositories\Interfaces;
10+
*/
11+
interface ClientRepository extends RepositoryInterface
12+
{
13+
//
14+
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"barryvdh/laravel-cors": "^0.8.5",
1212
"prettus/l5-repository": "~2.6.6",
1313
"laravelcollective/html": "^5.3.0",
14-
"league/fractal": "^0.15.0"
14+
"league/fractal": "^0.15.0",
15+
"hipsterjazzbo/landlord": "2.0.4"
1516
},
1617
"require-dev": {
1718
"fzaninotto/faker": "~1.4",

composer.lock

+49-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| any other location as required by the application or its packages.
1313
*/
1414

15-
'name' => 'Laravel',
15+
'name' => 'CodeFinanceiro',
1616

1717
/*
1818
|--------------------------------------------------------------------------
@@ -170,6 +170,7 @@
170170
Barryvdh\Cors\ServiceProvider::class,
171171
Prettus\Repository\Providers\RepositoryServiceProvider::class,
172172
Collective\Html\HtmlServiceProvider::class,
173+
HipsterJazzbo\Landlord\LandlordServiceProvider::class,
173174

174175
//
175176

@@ -232,7 +233,7 @@
232233
'View' => Illuminate\Support\Facades\View::class,
233234
'Form' => Collective\Html\FormFacade::class,
234235
'Html' => Collective\Html\HtmlFacade::class,
235-
236+
'Landlord' => HipsterJazzbo\Landlord\Facades\Landlord::class,
236237
],
237238

238239
];

config/landlord.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Tenant Column
8+
|--------------------------------------------------------------------------
9+
|
10+
| Every model that needs to be scoped by tenant (company, user, etc.)
11+
| should have one or more columns that reference the `id` of a tenant in the tenant
12+
| table.
13+
|
14+
| For example, if you are scoping by company, you should have a
15+
| `companies` table that stores all your companies, and your other tables
16+
| should each have a `company_id` column that references an `id` on the
17+
| `companies` table.
18+
|
19+
*/
20+
21+
'default_tenant_columns' => ['client_id'],
22+
23+
];

database/factories/ModelFactory.php

+7
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@
3838
'account' => rand(70000,260000).'-'.rand(0,9),
3939
];
4040
});
41+
42+
43+
$factory->define(\CodeFin\Models\Client::class, function (Faker\Generator $faker) {
44+
return [
45+
'name' => $faker->name
46+
];
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateClientsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('clients', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('name');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('clients');
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddClientsToUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->integer('client_id')->unsigned()->nullable();
18+
$table->foreign('client_id')->references('id')->on('clients');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('users', function (Blueprint $table) {
30+
$table->dropForeign('users_client_id_foreign');
31+
$table->dropColumn('client_id');
32+
});
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddClientsToBankAccounts extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('bank_accounts', function (Blueprint $table) {
17+
$table->integer('client_id')->unsigned();
18+
$table->foreign('client_id')->references('id')->on('clients');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('bank_accounts', function (Blueprint $table) {
30+
$table->dropForeign('bank_accounts_client_id_foreign');
31+
$table->dropColumn('client_id');
32+
});
33+
}
34+
}

0 commit comments

Comments
 (0)