diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fb3cc4..4b84361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.2.0] 2021-11-04 +### Changed +- Slight breaking change in the JavaScript helper. The constructor will now only accept a permissions array or JSON encoded string. +- You don't need to pass a whole user object into the `Deadbolt()` constructor. + ## [2.1.0] 2021-09-01 ### Added - Added a simple JavaScript library to aid in using permissions on the front-end (ESM). diff --git a/README.md b/README.md index b7bdbc5..73a40e4 100644 --- a/README.md +++ b/README.md @@ -401,7 +401,7 @@ class DatabaseDriver implements DriverInterface How the `permissions` method sources permissions is up to you. It could a database request, or even a remote API request. ## JavaScript Helper -Deadbolt includes a simple JavaScript helper that provides some simple tools for checking permissions. You'll need to pass your User object to your front-end. You can import the `Deadbolt` class into your JavaScript component. If you're using Vue you could use it something like this: +Deadbolt includes a simple JavaScript helper that provides some simple tools for checking permissions. You'll need to pass the user permissions to your front-end. You can import the `Deadbolt` class into your JavaScript component. If you're using Vue you could do something like this: ```javascript import Deadbolt from '../../vendor/thepublicgood/deadbolt/dist/Deadbolt'; @@ -412,7 +412,7 @@ export default { }, setup (props) { - const permissions = new Deadbolt(props.user); + const permissions = new Deadbolt(props.user.permissions); // Check if the user has a permission permissions.has('articles.create'); diff --git a/dist/Deadbolt.js b/dist/Deadbolt.js index 4d31275..a15a6a4 100644 --- a/dist/Deadbolt.js +++ b/dist/Deadbolt.js @@ -1,10 +1,10 @@ export default class Deadbolt { - constructor(user, column = 'permissions') { - if (typeof user.permissions === 'string') { - this._permissions = JSON.parse(user[column]); + constructor(permissions) { + if (typeof permissions === 'string') { + this._permissions = JSON.parse(permissions); } else { - this._permissions = user[column]; + this._permissions = permissions; } } has(permission) { diff --git a/dist/Deadbolt.js.map b/dist/Deadbolt.js.map index ef10e8a..265ecf1 100644 --- a/dist/Deadbolt.js.map +++ b/dist/Deadbolt.js.map @@ -1 +1 @@ -{"version":3,"file":"Deadbolt.js","sourceRoot":"","sources":["../src/js/Deadbolt.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,OAAO,QAAQ;IAIzB,YAAY,IAAU,EAAE,SAAiB,aAAa;QAClD,IAAI,OAAO,IAAI,CAAC,WAAW,KAAK,QAAQ,EAAE;YACtC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;SAChD;aAAM;YACH,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;SACpC;IACL,CAAC;IAED,GAAG,CAAE,UAAkB;QACnB,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAClD,CAAC;IAED,MAAM,CAAE,WAAqB;QACzB,OAAO,WAAW,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IAClG,CAAC;IAED,MAAM,CAAE,WAAqB;QACzB,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IAC/F,CAAC;IAED,OAAO,CAAE,WAAqB;QAC1B,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACrC,CAAC;IAED,GAAG;QACC,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;CACJ"} \ No newline at end of file +{"version":3,"file":"Deadbolt.js","sourceRoot":"","sources":["../src/js/Deadbolt.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,QAAQ;IAIzB,YAAY,WAA4B;QAEpC,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE;YACjC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;SAC/C;aAAM;YACH,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;SACnC;IACL,CAAC;IAED,GAAG,CAAE,UAAkB;QACnB,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAClD,CAAC;IAED,MAAM,CAAE,WAAqB;QACzB,OAAO,WAAW,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IAClG,CAAC;IAED,MAAM,CAAE,WAAqB;QACzB,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IAC/F,CAAC;IAED,OAAO,CAAE,WAAqB;QAC1B,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACrC,CAAC;IAED,GAAG;QACC,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;CACJ"} \ No newline at end of file diff --git a/dist/User.js b/dist/User.js deleted file mode 100644 index 4459182..0000000 --- a/dist/User.js +++ /dev/null @@ -1,2 +0,0 @@ -export {}; -//# sourceMappingURL=User.js.map \ No newline at end of file diff --git a/dist/User.js.map b/dist/User.js.map deleted file mode 100644 index 3141119..0000000 --- a/dist/User.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"User.js","sourceRoot":"","sources":["../src/js/User.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/src/js/Deadbolt.ts b/src/js/Deadbolt.ts index cce54f6..8d56110 100644 --- a/src/js/Deadbolt.ts +++ b/src/js/Deadbolt.ts @@ -1,14 +1,13 @@ -import {User} from '@/User'; - export default class Deadbolt { readonly _permissions: string[]; - constructor(user: User, column: string = 'permissions') { - if (typeof user.permissions === 'string') { - this._permissions = JSON.parse(user[column]); + constructor(permissions: string|string[]) { + + if (typeof permissions === 'string') { + this._permissions = JSON.parse(permissions); } else { - this._permissions = user[column]; + this._permissions = permissions; } } diff --git a/src/js/User.ts b/src/js/User.ts deleted file mode 100644 index b8a79e6..0000000 --- a/src/js/User.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface User { - permissions: string[]|string, -} diff --git a/tests/js/deadbolt.test.js b/tests/js/deadbolt.test.js index 2d5af33..f1e8e56 100644 --- a/tests/js/deadbolt.test.js +++ b/tests/js/deadbolt.test.js @@ -10,7 +10,7 @@ const user = { ], } -const permissions = new Deadbolt(user); +const permissions = new Deadbolt(user.permissions); it('"has" should return true for an existing permission', () => { assert.equal(permissions.has('articles.create'), true);