Skip to content

Commit

Permalink
The Deadbolt JavaScript class constructor change.
Browse files Browse the repository at this point in the history
  • Loading branch information
warrickbayman committed Nov 3, 2021
1 parent c5df062 commit 77280d9
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions dist/Deadbolt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Deadbolt.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions dist/User.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/User.js.map

This file was deleted.

11 changes: 5 additions & 6 deletions src/js/Deadbolt.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/js/User.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/js/deadbolt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 77280d9

Please sign in to comment.