Привет У меня возникли проблемы при попытке создать политику в мультиавторичном приложении Laravel (отдельные таблицы для пользователей и администраторов).
Я хочу создать политику для AdminProfile, чтобы ее мог редактировать только владелец этого профиля. Но теперь, даже если для политики update
задано постоянное возвращение true, я все равно получаю страницу 403 ...
Это мой AdminProfilePolicy:
<?php
namespace App\Policies;
use App\AdminProfile;
use App\Admin;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class AdminProfilePolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any admin profiles.
*
* @param \App\User $user
* @return mixed
*/
public function viewAny(User $user)
{
//
}
/**
* Determine whether the user can view the admin profile.
*
* @param \App\User $user
* @param \App\AdminProfile $adminProfile
* @return mixed
*/
public function view(User $user, AdminProfile $adminProfile)
{
//
}
/**
* Determine whether the user can create admin profiles.
*
* @param \App\User $user
* @return mixed
*/
public function create(User $user)
{
//
}
/**
* Determine whether the user can update the admin profile.
*
* @param \App\User $user
* @param \App\AdminProfile $adminProfile
* @return mixed
*/
public function update(Admin $admin, AdminProfile $adminProfile)
{
return true;
}
/**
* Determine whether the user can delete the admin profile.
*
* @param \App\User $user
* @param \App\AdminProfile $adminProfile
* @return mixed
*/
public function delete(User $user, AdminProfile $adminProfile)
{
//
}
/**
* Determine whether the user can restore the admin profile.
*
* @param \App\User $user
* @param \App\AdminProfile $adminProfile
* @return mixed
*/
public function restore(User $user, AdminProfile $adminProfile)
{
//
}
/**
* Determine whether the user can permanently delete the admin profile.
*
* @param \App\User $user
* @param \App\AdminProfile $adminProfile
* @return mixed
*/
public function forceDelete(User $user, AdminProfile $adminProfile)
{
//
}
}
Мой AdminProfileController:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin;
use App\AdminProfile;
class AdminProfileController extends Controller
{
public function show(Admin $admin)
{
return view('admin.profile.show', compact('admin'));
}
public function edit(Admin $admin)
{
$this->authorize('update', $admin->profile);
return $admin->adminProfile->cv;
}
}
мои маршруты:
<?php
use App\Admin;
/* --------------------- Common/User Routes START -------------------------------- */
Route::get('/', function () {
return view('welcome');
});
Auth::routes([ 'verify' => true ]);
Route::middleware('verified')->prefix('/user')->name('user.')->namespace('User')->group(function() {
Route::get('/home', 'HomeController@index')->name('home');
});
/* --------------------- Common/User Routes END -------------------------------- */
/* ----------------------- Admin Routes START -------------------------------- */
Route::prefix('/admin')->name('admin.')->namespace('Admin')->group(function(){
/**
* Admin Auth Route(s)
*/
Route::namespace('Auth')->group(function(){
//Login Routes
Route::get('/login','LoginController@showLoginForm')->name('login');
Route::post('/login','LoginController@login');
Route::post('/logout','LoginController@logout')->name('logout');
//Register Routes
// Route::get('/register','RegisterController@showRegistrationForm')->name('register');
// Route::post('/register','RegisterController@register');
//Forgot Password Routes
Route::get('/password/reset','ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('/password/email','ForgotPasswordController@sendResetLinkEmail')->name('password.email');
//Reset Password Routes
Route::get('/password/reset/{token}','ResetPasswordController@showResetForm')->name('password.reset');
Route::post('/password/reset','ResetPasswordController@reset')->name('password.update');
// Email Verification Route(s)
Route::get('email/verify','VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}','VerificationController@verify')->name('verification.verify');
Route::get('email/resend','VerificationController@resend')->name('verification.resend');
});
Route::group(['middleware' => 'guard.verified:admin,admin.verification.notice'], function() {
Route::get('/home','HomeController@index')->name('home');
Route::get('/profile/{admin}/edit', 'AdminProfileController@edit')->name('profile.edit');
});
//Put all of your admin routes here...
});
/* ----------------------- Admin Routes END -------------------------------- */
Route::get('/{admin_username}', 'Admin\AdminProfileController@show')->name('admin.profile.show');
в идеале, в конце я хочу, чтобы политика update
выглядела так:
public function update(Admin $admin, AdminProfile $adminProfile)
{
return $admin->id == $adminProfile->admin_id;
}
но не знаю, если это будет работать ...