adonis-guard icon indicating copy to clipboard operation
adonis-guard copied to clipboard

How can I pass an additional parameter to the policy?

Open Agilulfo1820 opened this issue 3 years ago • 0 comments

Hello, I'm using Adonis v4 with adonis-guard. I created a ProductPolicy:

'use strict'
class ProductPolicy {
  index (user) {
    //
  }
  show (user, product) {
    //
  }
  create (user) {
    //
  }
  async update (user, product) {
    console.log(user, product)
  }
  create (edit) {
    //
  }
  destroy (user, product) {
    //
  }
}
module.exports = ProductPolicy

Linked it inside to the gate inside acl.js:

Gate.policy('App/Models/Product', 'App/Policies/ProductPolicy')

And call it inside the authorize function of my validator like this:

async authorize() {
    return await this.ctx.guard.allows(
        'update',
        this.ctx.product
    )
}

I would like to pass an additional parameter to the update policy, which is the brand, but it seems I am unable to do it.

I tried adding the parameter inside the update function like this:

  async update (user, product, brand) {
    console.log(user, product, brand)
  }

And pass it in the validator like this:

async authorize() {
    return await this.ctx.guard.allows(
        'update',
        this.ctx.product,
        this.ctx.brand
    )
}

But in my console log brand is undefined.

How can I pass an additional parameter from my validator?

Agilulfo1820 avatar Mar 19 '21 13:03 Agilulfo1820