fratzinger / feathers-casl

feathers.js + casl: hooks & channels
https://feathers-casl.netlify.app/
MIT License
40 stars 18 forks source link

feat: mask method #101

Closed fratzinger closed 1 year ago

fratzinger commented 1 year ago

This PR adds a first implementation for custom methods.

As of now, my custom methods behave like other regular method. I have a sum method for which I need the find permissions to apply. So I use options.method to 'overwrite' context.method.

Example:

class CustomService extends MemoryService {
  sum(data: any, params: any) {
    return this.find(params);
  }
}

app.use("tests", new CustomService({ ... }), {
  methods: ["find", "get", "create", "update", "patch", "remove", "sum"],
})

const service = app.service("tests");

service.hooks({
  before: {
    sum: [
      authorize({
        method: "find",
        adapter: "@feathersjs/memory",
      }),
    ],
  },
  after: {
    sum: [
      authorize({
        method: "find",
        adapter: "@feathersjs/memory",
      }),
    ],
  },
});

const items = (await service.sum(null, {
  ability: defineAbility(
    (can) => {
      can("read", "tests", { userId: 1 });
    },
    { resolveAction }
  ),
  paginate: false,
}));