How do I use an external library in my project?

Hello everybody, I am using this library for control panel access. https://github.com/chiiya/filament-access-control I like the user policies roles and permissions but I don't know how to extend those same permissions to my resources, example my contact resources I need some advice what I want to do is extend the admin policies to my resources but I have no idea how to do it.
GitHub
GitHub - chiiya/filament-access-control: Admin user, role and permi...
Admin user, role and permission management for Laravel Filament - GitHub - chiiya/filament-access-control: Admin user, role and permission management for Laravel Filament
No description
Solution:
And register policy ```php <?php namespace App\Providers;...
Jump to solution
16 Replies
toeknee
toeknee13mo ago
You just need to build out policies to your models and the resources take into account the models policy
Yurikaso
YurikasoOP13mo ago
php artisan make:policy ContactPolicy --model=Contacts
php artisan make:policy ContactPolicy --model=Contacts
so I have to create something like this right? for each resource I have
toeknee
toeknee13mo ago
Yes
toeknee
toeknee13mo ago
Twilio Blog
What Are Laravel Policies and How to Use Them to Control Access
In this tutorial, you'll learn about how to implement access control in Laravel applications using Laravel Policies
Yurikaso
YurikasoOP13mo ago
ho thank you very much for the article I am going to read it and keep trying, if I get the solution I will mark it here thanks hello I still do not get solution, create the policy of ContactoPolicy.php and add these policies similar to the administrators of the chiiya that is what I want
<?php

declare(strict_types=1);

namespace App\Policies;

namespace Chiiya\FilamentAccessControl\Policies;

use App\Models\Contactos;
use App\Models\User;
use Illuminate\Auth\Access\Response;

use Chiiya\FilamentAccessControl\Enumerators\PermissionName;
use Illuminate\Contracts\Auth\Access\Authorizable;

class ContactoPolicy
{
public function viewAny(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function view(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function create(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function update(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function delete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function restore(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function forceDelete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}
}
<?php

declare(strict_types=1);

namespace App\Policies;

namespace Chiiya\FilamentAccessControl\Policies;

use App\Models\Contactos;
use App\Models\User;
use Illuminate\Auth\Access\Response;

use Chiiya\FilamentAccessControl\Enumerators\PermissionName;
use Illuminate\Contracts\Auth\Access\Authorizable;

class ContactoPolicy
{
public function viewAny(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function view(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function create(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function update(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function delete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function restore(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}

public function forceDelete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_ADMIN_USERS);
}
}
I've been trying for 5 hours T_T, I don't understand what else I have to do.
toeknee
toeknee13mo ago
Have you registered the policy? have you tested the policy is being used?
Yurikaso
YurikasoOP13mo ago
<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;

use App\Filament\Resources\ContactosResource;
use App\Policies\ContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
ContactosResource::class => ContactoPolicy::class
];

/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;

use App\Filament\Resources\ContactosResource;
use App\Policies\ContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
ContactosResource::class => ContactoPolicy::class
];

/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
I think that's what I'm missing but I don't know if I'm doing it right.
toeknee
toeknee13mo ago
You assigned it to the resource, you need to assign it to the model
Yurikaso
YurikasoOP13mo ago
<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;

use App\Filament\Resources\ContactosResource;
use App\Models\Contactos;
use App\Policies\ContactoPolicy;
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy as PoliciesContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
Contactos::class => PoliciesContactoPolicy::class
];

/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;

use App\Filament\Resources\ContactosResource;
use App\Models\Contactos;
use App\Policies\ContactoPolicy;
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy as PoliciesContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
Contactos::class => PoliciesContactoPolicy::class
];

/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
but now I get this error "Target class [Chiiya\FilamentAccessControl\Policies\ContactoPolicy] does not exist." do i have to create a policy like this ? in chiiya's policy
toeknee
toeknee13mo ago
Your naming convention is bad Change: PoliciesContactoPolicy To ContactoPolicy
Yurikaso
YurikasoOP13mo ago
Target class [App\Policies\ContactoPolicy] does not exist. xD HAHAHAJ now this comes out, later I pass all the codes in an organized way, to see if you can continue helping me, is that if I need it hehehej thank you very much.
toeknee
toeknee13mo ago
Did you change the class name and the file name? to ContactoPolicy
Yurikaso
YurikasoOP13mo ago
<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;


use App\Models\Contactos;
use App\Policies\ContactoPolicy;
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy as PoliciesContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
Contactos::class => ContactoPolicy::class
];

/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;


use App\Models\Contactos;
use App\Policies\ContactoPolicy;
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy as PoliciesContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
Contactos::class => ContactoPolicy::class
];

/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
yes, I changed it but now I have to go to the university I'm late xD but thank you a greeting I'll explain better later
toeknee
toeknee13mo ago
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy as PoliciesContactoPolicy; This is the issue, if you have them in \App\Policies which is where it should be...
Yurikaso
YurikasoOP13mo ago
if that was it, I was doing it backwards, I created the policies in the normal application, and not in the library policies of chiiya, here I leave the solution thank you very much.
<?php

declare(strict_types=1);

namespace Chiiya\FilamentAccessControl\Policies;

use Chiiya\FilamentAccessControl\Enumerators\PermissionName;
use Illuminate\Contracts\Auth\Access\Authorizable;

class ContactoPolicy
{
public function viewAny(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function view(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function create(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function update(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function delete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function restore(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function forceDelete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}
}
<?php

declare(strict_types=1);

namespace Chiiya\FilamentAccessControl\Policies;

use Chiiya\FilamentAccessControl\Enumerators\PermissionName;
use Illuminate\Contracts\Auth\Access\Authorizable;

class ContactoPolicy
{
public function viewAny(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function view(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function create(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function update(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function delete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function restore(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}

public function forceDelete(Authorizable $user): bool
{
return $user->can(PermissionName::UPDATE_CONTACTOS);
}
}
Solution
Yurikaso
Yurikaso13mo ago
And register policy
<?php

namespace App\Providers;

use App\Models\Contactos;
use Illuminate\Support\Facades\Gate;
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
Contactos::class => ContactoPolicy::class
];
/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
<?php

namespace App\Providers;

use App\Models\Contactos;
use Illuminate\Support\Facades\Gate;
use Chiiya\FilamentAccessControl\Policies\ContactoPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
Contactos::class => ContactoPolicy::class
];
/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
//
}
}
I learned a lot thank you very much again 👍
Want results from more Discord servers?
Add your server