Reputation: 305
Jetstream profile page does not link to spatie laravel 8 roles and permissions
I have Jetstream configured for the login and user system and Spatie for permissions and roles.
The login system works correctly, and I have several pages that are managed with spatie permissions and roles.
The problem arose with the profile page, which only the administrator user has access even though I have given the access permissions with the spatie role to both the "Administrator" and "User" roles.
Below I put part of the code as an example and the most relevant for this problem:
UserController
use DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Arr;
use Spatie\Permission\Models\Role;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('can:admin.user.index');
}
...
public function destroy($id)
{
if( Auth::user()->hasPermissionTo('admin.user.destroy') ) {
$post = User::whereIn('id', $ids)->delete();
}
}
public function profile()
{
return view('admin.profile');
}
}
Route
Route::resource('users', 'App\Http\Controllers\Admin\\UserController')->names('admin.users');
Route::group(['middleware' => ['role:Administrador|Usuario']], function () {
Route::get('/perfil','App\Http\Controllers\Admin\\UserController@profile')->name('admin.profile');
Route::get('/user/profile', function () {return redirect('/admin/perfil');});
});
In the spatie documentation it says that you have to add in the kernel:
protected $routeMiddleware = [
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
]
I have created RoleSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
class RoleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$role_admin = Role::create(['name' => 'Administrador']);
$role_user = Role::create(['name' => 'Usuario']);
Permission::create(['name' => 'admin.user.index', 'description' => 'Ver Listado Usuarios'])->assignRole($role_admin);
Permission::create(['name' => 'admin.user.destroy', 'description' => 'Eliminar Usuarios'])->assignRole($role_admin);
Permission::create(['name' => 'profile.show', 'description' => 'Ver Perfil'])->syncRoles([$role_admin, $role_user]);
}
}
admin.profile.blade
@section('content')
<x-app-layout>
<x-slot name="header">
<h2 class="h4 font-weight-bold">
{{ __('Perfil') }}
</h2>
</x-slot>
<div>
@if (Laravel\Fortify\Features::canUpdateProfileInformation())
@livewire('profile.update-profile-information-form')
<x-jet-section-border />
@endif
@if (Laravel\Fortify\Features::enabled(Laravel\Fortify\Features::updatePasswords()))
@livewire('profile.update-password-form')
<x-jet-section-border />
@endif
@if (Laravel\Fortify\Features::canManageTwoFactorAuthentication())
@livewire('profile.two-factor-authentication-form')
<x-jet-section-border />
@endif
@livewire('profile.logout-other-browser-sessions-form')
@if (Laravel\Jetstream\Jetstream::hasAccountDeletionFeatures())
<x-jet-section-border />
@livewire('profile.delete-user-form')
@endif
</div>
</x-app-layout>
@stop
Upvotes: 1
Views: 1042
Reputation: 305
I have found the solution and explain it below.
I have been able to verify that the default path of the profile is: / user / profile / in my case what I have done is not associate it with any user profile, since it is understood that any user can edit their profile.
So it is not necessary to create any role or permissions, in the path part it is not necessary either, so the only thing I had to modify is this file:
myproject\vendor\laravel\jetstream\routes\livewire.php
Before
Route::group(['middleware' => ['auth', 'verified']], function () {
// User & Profile...
Route::get('/user/profile', [UserProfileController::class, 'show'])
->name('profile.show');
After
Route::group(['middleware' => ['auth', 'verified']], function () {
// User & Profile...
Route::get('/admin/perfil', [UserProfileController::class, 'show'])
->name('profile.show');
Upvotes: 0