Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
ignore fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JaneJeon committed Oct 18, 2021
1 parent 9265017 commit 38788aa
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const opts = {
unauthenticatedErrorCode: 401,
unauthorizedErrorCode: 403,
castDiffToModelClass: true,
ignoreFields: [],
casl: {
useInputItemAsResourceForRelation: false
}
Expand Down Expand Up @@ -190,6 +191,15 @@ If you want to disable it, just set `opts.castDiffToModelClass` to false and the

</details>

<details>
<summary>ignoreFields</summary>

When you automatically modify/include some fields (e.g. automatic timestamps) in your Objection models, as objection-authorize is typically the "last" hook to run before execution, the policies will check for those fields as well.

These allow you to ignore those fields in authorization decisions. Note that you can specify the fields in dot notation as well (e.g. `timestamp.updatedAt`).

</details>

<details>
<summary>casl.useInputItemAsResourceForRelation</summary>

Expand Down
6 changes: 6 additions & 0 deletions src/adapters/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const httpError = require('http-errors')
const objectDiff = require('../utils/object-diff')
const merge = require('lodash/merge')
const unset = require('lodash/unset')

class ACLInterface {
constructor(acl, args, defaultAction) {
Expand Down Expand Up @@ -67,6 +68,11 @@ class ACLInterface {
})
}

// Remove fields that we want to exclude - `delete` allows us to preserve the class information!
this.opts.ignoreFields.forEach(field => {
unset(inputItem, field)
})

if (!this._checkIndividualAccess(item, inputItem))
throw httpError(
this.user.role === this.opts.defaultRole
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = (acl, library = 'role-acl', opts) => {
castDiffToModelClass: true,
casl: {
useInputItemAsResourceForRelation: false
}
},
ignoreFields: []
}
opts = merge({}, defaultOpts, opts)

Expand Down
4 changes: 3 additions & 1 deletion tests/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const BaseUser = require('./models/user')
const authorizePlugin = require('../src')

describe.each(ACLs)('Insert queries (%s)', (library, acl) => {
class User extends authorizePlugin(acl, library)(BaseUser) {}
class User extends authorizePlugin(acl, library, {
ignoreFields: ['created_at', 'updated_at']
})(BaseUser) {}

test('restrict insert query based on their create access', async () => {
// create user while anonymous
Expand Down
2 changes: 2 additions & 0 deletions tests/migrations/20200807001632_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ exports.up = function (knex) {
table.text('password')
table.text('role')
table.jsonb('metadata')

table.timestamps()
})
}

Expand Down
8 changes: 8 additions & 0 deletions tests/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class User extends BaseModel {
}
}
}

$beforeInsert() {
this.created_at = new Date().toISOString()
}

$beforeUpdate() {
this.updated_at = new Date().toISOString()
}
}

module.exports = User
4 changes: 3 additions & 1 deletion tests/patch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const BaseUser = require('./models/user')
const authorizePlugin = require('../src')

describe.each(ACLs)('Patch queries (%s)', (library, acl) => {
class User extends authorizePlugin(acl, library)(BaseUser) {}
class User extends authorizePlugin(acl, library, {
ignoreFields: ['created_at', 'updated_at']
})(BaseUser) {}

test('restrict access with automatically fetched context', async () => {
// you shouldn't be able to delete a user as someone else...
Expand Down
4 changes: 3 additions & 1 deletion tests/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const BaseUser = require('./models/user')
const authorizePlugin = require('../src')

describe.each(ACLs)('Update queries (%s)', (library, acl) => {
class User extends authorizePlugin(acl, library)(BaseUser) {}
class User extends authorizePlugin(acl, library, {
ignoreFields: ['created_at', 'updated_at']
})(BaseUser) {}

test('restrict access with automatically fetched context', async () => {
// you shouldn't be able to change a user as someone else...
Expand Down

0 comments on commit 38788aa

Please sign in to comment.