Giter Club home page Giter Club logo

Comments (4)

gyson avatar gyson commented on May 3, 2024

Thanks your suggestion but I think this (your example) is a bad use case of koa middleware.

  1. It creates and converts koa middleware for every requests, which is unnecessary.
  2. It's error-prone. There might be a bug in your example code.

In rbac.allow at https://github.com/yanickrochon/koa-rbac/blob/master/rbac.js#L115, it restricts permission by return and not calling yield next. In your example, next is just noop and user without permission will continue go through downstream.

from convert.

azhang avatar azhang commented on May 3, 2024

You are right - I forgot to update this snippet when I encountered that bug. Here is how i'm handling this now:

export async function organizationIdParam (id, ctx, next) {
  await convert(rbac.allow('read:organization', { organization: id }))(ctx, noop);
  if (ctx.status === 403) return;

  ctx.organization = await Organization.findById(id);
  await next();
}

from convert.

gyson avatar gyson commented on May 3, 2024

I would not recommend you to create middleware dynamically like this. It seems that your use case (inject id dynamically) has beyond the scope of that middleware. (I am not familiar with that middleware and I might be wrong).

Also, ctx.status === 403 is hacking. I would suggest you to do it in this way:

export function organizationIdParam (id, ctx, next) { // or `async function` if you want
  return convert.compose(
    rbac.allow('read:organization', { organization: id }),
    async function (ctx, next) {
      ctx.organization = await Organization.findById(id);
      await next();
    }
  )(ctx, next);
}

from convert.

azhang avatar azhang commented on May 3, 2024

Great, that makes sense. Thank you!

from convert.

Related Issues (8)

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.