Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policies/Exceptions within modules #16

Closed
ghost opened this issue Jul 30, 2016 · 7 comments
Closed

Policies/Exceptions within modules #16

ghost opened this issue Jul 30, 2016 · 7 comments

Comments

@ghost
Copy link

ghost commented Jul 30, 2016

Hi There,

Is is already possible, or do you have plans to allow for policies and exceptions to be within the modules created by laravel-modules?

For policies, it looks like it is possible with the registerPolicies function within the AuthServiceProvider. So would each module require an instance of the AuthServiceProvider to register their policies? Or would it be easier to register each module's policies within the global AuthServiceProvider?

I assume that exception classes will be auto loaded by default, so it is just a case of creating a folder within each module and place the exceptions in there?

Thanks in advance for any advice offered.

@nWidart
Copy link
Owner

nWidart commented Jul 30, 2016

Hello,

You can already define you policies inside a module.
For that you need to create a service provider in your module, that extends Illuminate\Foundation\Support\Providers\AuthServiceProvider. In there you can do anything the documentation says.

You could also register the policies in the AuthServiceProvider under the app namespace.

And yes exceptions are just as any other class, meaning they get autoloaded by composer.

@ghost
Copy link
Author

ghost commented Jul 31, 2016

Thanks again for the advice

@simar88
Copy link

simar88 commented Mar 7, 2019

Hi, i reopened the issue for going further with an aspect.
How i can add the policies of the module to the global application?
I've a partial view in the project where I need to check if i can create/view/other on a certain class of object.
I was able to do this adding the policies inside the global AuthServiceProvider, but i would like to make it work without editing the main provider.
I declared an AuthServiceProvider inside a module but the policies inside it, seem to not be loaded/used.
Can you give me some help?
Thanks in advance,
Simone

@simar88
Copy link

simar88 commented Mar 7, 2019

I solved.
You have to add $this->app->register(AuthServiceProvider::class); inside register function of the NAMEMODULEServiceProvider.php

@yarwalker
Copy link

The same problem, can't solve

@Fredericld
Copy link

I have the same problem.

My MODULENAMEServiceProvider has

public function register()
    {
        $this->app->register(RouteServiceProvider::class);
        $this->app->register(TemplateAuthServiceProvider::class);
    }

In TemplateAuthServiceProvider I have

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class TemplateAuthServiceProvider extends ServiceProvider
{

    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'Modules\Template\Entities\Template' => 'Modules\Template\Policies\TemplatePolicy',
        'Modules\Template\Entities\TemplateTab' => 'Modules\Template\Policies\TemplatePolicy',
    ];

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        
    }


    /**
     * Register any application authentication / authorization services.
     *
     * @param  \Illuminate\Contracts\Auth\Access\Gate  $gate
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
    }

My controller which is inside the module has

    public function index(Request $request)
    {

        //checks policy
        $this->authorize('list', Template::class);

The policy contains

public function list(Admin $admin)
{   
dd(567);
}

what could be the reason for the system not to trigger the policy function?

@Doclassif
Copy link

I have the same problem.

My MODULENAMEServiceProvider has

public function register()
    {
        $this->app->register(RouteServiceProvider::class);
        $this->app->register(TemplateAuthServiceProvider::class);
    }

In TemplateAuthServiceProvider I have

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class TemplateAuthServiceProvider extends ServiceProvider
{

    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'Modules\Template\Entities\Template' => 'Modules\Template\Policies\TemplatePolicy',
        'Modules\Template\Entities\TemplateTab' => 'Modules\Template\Policies\TemplatePolicy',
    ];

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        
    }


    /**
     * Register any application authentication / authorization services.
     *
     * @param  \Illuminate\Contracts\Auth\Access\Gate  $gate
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
    }

My controller which is inside the module has

    public function index(Request $request)
    {

        //checks policy
        $this->authorize('list', Template::class);

The policy contains

public function list(Admin $admin)
{   
dd(567);
}

what could be the reason for the system not to trigger the policy function?

I have the same problem

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants