Giter VIP home page Giter VIP logo

Comments (7)

nWidart avatar nWidart commented on May 18, 2024 9

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.

from laravel-modules.

simar88 avatar simar88 commented on May 18, 2024 2

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

from laravel-modules.

 avatar commented on May 18, 2024

Thanks again for the advice

from laravel-modules.

simar88 avatar simar88 commented on May 18, 2024

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

from laravel-modules.

yarwalker avatar yarwalker commented on May 18, 2024

The same problem, can't solve

from laravel-modules.

Fredericld avatar Fredericld commented on May 18, 2024

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?

from laravel-modules.

Doclassif avatar Doclassif commented on May 18, 2024

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

from laravel-modules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.