У цій частині ми розглянемо, як реалізувати моделі для нашої системи управління правами доступу та створити відповідні міграції.
Міграції
Спочатку створимо необхідні міграції для наших таблиць:
|
1 2 3 4 5 6 |
php artisan make:migration create_projects_table php artisan make:migration create_project_users_table php artisan make:migration create_project_roles_table php artisan make:migration create_project_permissions_table php artisan make:migration create_project_role_permissions_table php artisan make:migration create_project_user_roles_table |
Ось приклад міграції для таблиці projects:
|
1 2 3 4 5 6 7 8 |
public function up() { Schema::create('projects', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } |
Для project_users:
|
1 2 3 4 5 6 7 8 9 |
public function up() { Schema::create('project_users', function (Blueprint $table) { $table->id(); $table->foreignId('project_id')->constrained()->onDelete('cascade'); $table->foreignId('user_id')->constrained()->onDelete('cascade'); $table->timestamps(); }); } |
Аналогічно створіть міграції для інших таблиць, враховуючи зовнішні ключі та обмеження.
Моделі
Тепер створимо моделі для наших сутностей:
|
1 2 3 4 |
php artisan make:model Project php artisan make:model ProjectUser php artisan make:model ProjectRole php artisan make:model ProjectPermission |
Ось приклад моделі Project:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Project extends Model { protected $fillable = ['name']; public function users() { return $this->hasMany(ProjectUser::class); } public function roles() { return $this->hasMany(ProjectRole::class); } } |
Модель ProjectUser:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class ProjectUser extends Model { protected $fillable = ['project_id', 'user_id']; public function project() { return $this->belongsTo(Project::class); } public function user() { return $this->belongsTo(User::class); } public function roles() { return $this->belongsToMany(ProjectRole::class, 'project_user_roles'); } public function hasPermission($permissionName) { return $this->roles()->whereHas('permissions', function ($query) use ($permissionName) { $query->where('name', $permissionName); })->exists(); } } |
Модель ProjectRole:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class ProjectRole extends Model { protected $fillable = ['project_id', 'name', 'description']; public function project() { return $this->belongsTo(Project::class); } public function permissions() { return $this->belongsToMany(ProjectPermission::class, 'project_role_permissions'); } public function users() { return $this->belongsToMany(ProjectUser::class, 'project_user_roles'); } } |
Модель ProjectPermission:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class ProjectPermission extends Model { protected $fillable = ['name', 'description']; public function roles() { return $this->belongsToMany(ProjectRole::class, 'project_role_permissions'); } } |
Ці моделі забезпечують основні взаємозв’язки між нашими сутностями та надають методи для зручної роботи з ними.