stalniy / casl

CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access
https://casl.js.org/
MIT License
6.07k stars 272 forks source link

Create pages from question issues and gitter questions #204

Open stalniy opened 5 years ago

stalniy commented 5 years ago

Tasks:

Advanced:

Security:

Persisted permissions (i.e., roles)

Use cases:

casl don't manage references on related documents. You need to do it yourself.

So, I see few ways:

  1. Retrieve ids of all vehicles when you define rules. This works good in case if amount of vehicles not big (<= 1000)
const vehicleIds = await getVehicleIds(user)

can(['read', 'update'], 'document', { vehicle: { $in: vehicleIds } })
  1. Denormalize your scheme. For example, add additional user_id field to vehicle document
  2. Think whether you can embed document as subdocument to vechicle, something like this:
    vehicle {
    documents: [Document],
    users: [ {type: objectId, ref: 'user'} ]
    }
  3. Just don't define rule per documents and enforce them in routes (REST or GraphQL doesn't matter).
// app - express app

app.get('/vehicle/:id/documents', async (req, res) => {
   const vehicle = await Vehicle.findById(req.params.id)

   req.ability.throwUnlessCan('read', vehicle)
   const documents = Document.find({ vehicle: vehicle.id })

   res.send({ documents })
})
  • user has multiple roles
  • e.g a user can update an item created by another user in the same group. (#199)
  • share permissions with UI (#227)

frontend

Mongoose:

Tests

backend

stalniy commented 5 years ago

Need to check what is in this course https://codecourse.com/watch/vue-roles-and-permissions?part=using-casl-with-vue

stalniy commented 4 years ago

FAQ will be represented as a cookbook in the new docs

stalniy commented 3 years ago

empty conditions object behavior:

{
    "subject": "Financial",
    "action": "read",
    "fields": [
        "salary",
        "taxcode"
    ], 
   conditions: {}
},
{
    "subject": "Financial",
    "action": "read",
    "inverted": true,
    "fields": [
        “salary"
    ],
   conditions: {}
}

Conditions are sometimes populated but in this case they are not. After retrieving from the array of Abilities from the database I pass them in to Ability. When empty conditions objects are there the can/cannot don’t work as expected. If I remove empty conditions objects before pushing them into Ability they work exactly as expected. This is using the latest version of CASL. Cleaning up my data solves the problem so I’m happy but I thought you might like to know in case that is unexpected behaviour.