Политики Laravel для вложенных мелких ресурсовPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Политики Laravel для вложенных мелких ресурсов

Сообщение Anonymous »

У меня определены следующие маршруты:

Код: Выделить всё

//Leases
Route::resource('properties.leases', LeaseController::class)
->only(['show'])
->shallow();

//Invoices
Route::resource('leases.invoices', InvoiceController::class)
->only(['index', 'show'])
->shallow();
Приведенное выше создает следующие URL-адреса:

Код: Выделить всё

| GET|HEAD  | leases/{lease}                                             | App\Http\Controllers\LeaseController@show                                       |
| GET|HEAD  | leases/{lease}/invoices                                    | App\Http\Controllers\InvoiceController@index                                    |
| GET|HEAD  | invoices/{invoice}                                         | App\Http\Controllers\InvoiceController@show                                     |
Отношения следующие:

Код: Выделить всё

Properties hasMany Leases.
Leases hasMany Invoices.
Я пытаюсь авторизовать эти маршруты, поэтому только те пользователи, которые:
  • принадлежат к той же команде, что " «Аренда» и «Счета-фактуры» также принадлежат.
  • В данный момент вошел в эту команду.
В моем AuthServiceProvider Я определил следующие политики:

Код: Выделить всё

protected $policies = [
Lease::class => LeasePolicy::class,
Invoice::class => InvoicePolicy::class,
];
В моем LeaseController я определил проверку авторизации:

Код: Выделить всё

public function __construct()
{
$this->authorizeResource(Lease::class, 'lease');
}
Политика аренды выглядит следующим образом:

Код: Выделить всё

public function view(User $user, Lease $lease)
{
//Does the current user belong to the team that the lease is associated with
//and is the user's current team the same one?
$team = $lease->property->team;
return $user->belongsToTeam($team) && $user->isCurrentTeam($team);
}
И в моем InvoiceController я определил это:

Код: Выделить всё

public function __construct()
{
$this->authorizeResource(Invoice::class, 'invoice');
}
InvoicePolicy выглядит следующим образом:

Код: Выделить всё

/**
* Path: leases/{lease}/{$invoice}
*/
public function viewAny(User $user)
{

//When users go to this path I can only access $user here.
//How to check if the user can even access the $lease.

}

/**
* Path: invoices/{$invoice}
*/
public function view(User $user, Invoice $invoice)
{
//Does the current user belong to the team that the lease is associated with
//and is the user's current team the same one?
$team = $invoice->lease->property->team;
return $user->belongsToTeam($team) && $user->isCurrentTeam($team);
}
В моем приложении есть множество маршрутов, которые находятся «под» маршрутом /lease/{lease}/{model}, например:

Код: Выделить всё

//Files
Route::resource('leases.files', FileController::class)
->only(['index'])
->shallow();
Как я могу определить свои политики, чтобы доступ могли получить только пользователи, которым разрешено просматривать эти ресурсы?

Подробнее здесь: https://stackoverflow.com/questions/710 ... ressources
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»