Skip to content

Commit a5b2c6a

Browse files
committed
added support for 'Object/perm' style checking.
Also some var to let/const conversions.
1 parent 59d0d1d commit a5b2c6a

File tree

7 files changed

+66
-294
lines changed

7 files changed

+66
-294
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ application.
6666
Defining an individual group is very simple:
6767

6868
```javascript
69-
var tp = require('../dist/trivialperms');
69+
const tp = require('../dist/trivialperms');
7070

7171
// Create an 'Admins' group
72-
var admGroup = tp.defineGroup({ name: 'Admins', permissions: ['*/*'] });
72+
const admGroup = tp.defineGroup({ name: 'Admins', permissions: ['*/*'] });
7373
```
7474

7575
#### Loading Groups
@@ -81,7 +81,7 @@ More usefully, you will want to load multiple groups at once. TrivialPermissions
8181
(Note: You _will_ need to wait until after the loading promise resolves before attempting to use the permissions system.)
8282

8383
```javascript
84-
var tp = require('../dist/trivialperms');
84+
const tp = require('../dist/trivialperms');
8585

8686
// Load from a list
8787
var loading = tp.loadGroups([
@@ -171,9 +171,9 @@ If your user object has it's permissions or groups under a different key name, y
171171
looks for using this method. Simply pass an object with the `permissions` and/or `groups` key(s) to map to your object's structure.
172172

173173
```javascript
174-
var tp = require('../dist/trivialperms');
174+
const tp = require('../dist/trivialperms');
175175

176-
var user = {
176+
const user = {
177177
name: "John Snow",
178178
allowed: ["Foo/canView", "Bar/canView"],
179179
roles: ["Posters"]
@@ -190,10 +190,10 @@ tp.setUserMapping({ permissions: 'allowed', groups: 'roles' });
190190
This is the heart of the system: checking permissions. It's very simply; you pass the user object, the permission descriptor (string), and the object descriptor (string). Here are a few examples:
191191

192192
```javascript
193-
var tp = require('../dist/trivialperms');
193+
const tp = require('../dist/trivialperms');
194194

195195
// Setup
196-
var loading = tp.loadGroups([
196+
const loading = tp.loadGroups([
197197
{
198198
name: "Administrators",
199199
permissions: [
@@ -217,18 +217,18 @@ var loading = tp.loadGroups([
217217
]);
218218

219219
// Define Users
220-
var batman = {
220+
const batman = {
221221
name: 'batman',
222222
groups: ['Administrators']
223223
};
224224

225-
var stark = {
225+
const stark = {
226226
name: 'tstark',
227227
permissions: ['*/*'],
228228
groups: ['Users']
229229
};
230230

231-
var leo = {
231+
const leo = {
232232
name: 'lblume',
233233
groups: ['Users']
234234
};
@@ -260,10 +260,10 @@ about simply seeing if `groupName` is in the list of groups on the user: `hasGro
260260
not been defined, regardless of is the user has that group name in their list of groups.
261261

262262
```javascript
263-
var tp = require('../dist/trivialperms');
263+
const tp = require('../dist/trivialperms');
264264

265265
// Setup
266-
var loading = tp.loadGroups([
266+
const loading = tp.loadGroups([
267267
{
268268
name: "Administrators",
269269
permissions: [
@@ -287,18 +287,18 @@ var loading = tp.loadGroups([
287287
]);
288288

289289
// Define Users
290-
var batman = {
290+
const batman = {
291291
name: 'batman',
292292
groups: ['Administrators']
293293
};
294294

295-
var stark = {
295+
const stark = {
296296
name: 'tstark',
297297
permissions: ['*/*'],
298298
groups: ['Users']
299299
};
300300

301-
var leo = {
301+
const leo = {
302302
name: 'lblume',
303303
groups: ['Users']
304304
};

dist/trivialperms.min.js

Lines changed: 0 additions & 255 deletions
This file was deleted.

examples/simple.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
/// @module
55
//----------------------------------------------------------------------------------------------------------------------
66

7-
var tp = require('../dist/trivialperms');
7+
const tp = require('../dist/trivialperms');
88

99
//----------------------------------------------------------------------------------------------------------------------
1010
// Setup
1111
//----------------------------------------------------------------------------------------------------------------------
1212

13-
var loading = tp.loadGroups([
13+
const loading = tp.loadGroups([
1414
{
1515
name: "Administrators",
1616
permissions: [
@@ -35,18 +35,18 @@ var loading = tp.loadGroups([
3535

3636
// Define Users
3737

38-
var batman = {
38+
const batman = {
3939
name: 'batman',
4040
groups: ['Administrators']
4141
};
4242

43-
var stark = {
43+
const stark = {
4444
name: 'tstark',
4545
permissions: ['*/*'],
4646
groups: ['Users']
4747
};
4848

49-
var leo = {
49+
const leo = {
5050
name: 'lblume',
5151
groups: ['Users']
5252
};

src/group.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import find from 'lodash/find';
88
import uniq from 'lodash/uniq';
99
import remove from 'lodash/remove';
1010

11-
var _ = {
11+
const _ = {
1212
find,
1313
uniq,
1414
remove
@@ -29,9 +29,16 @@ class TPGroup {
2929

3030
hasPerm(perm, obj)
3131
{
32+
if(arguments.length === 1)
33+
{
34+
const parts = perm.split('/');
35+
obj = parts[0];
36+
perm = parts[1];
37+
} // end if
38+
3239
return !!_.find(this.permissions, (permission) =>
3340
{
34-
if(permission == '*/*')
41+
if(permission === '*/*')
3542
{
3643
return true;
3744
}
@@ -60,7 +67,7 @@ class TPGroup {
6067

6168
removePerm(perm, obj)
6269
{
63-
return _.remove(this.permissions, (item) => item == `${ obj }/${ perm }`);
70+
return _.remove(this.permissions, (item) => item === `${ obj }/${ perm }`);
6471
} // end removePerm
6572
} // end TPGroup
6673

src/trivialperms.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import TPGroup from './group';
1515

1616
//----------------------------------------------------------------------------------------------------------------------
1717

18-
var _ = {
18+
const _ = {
1919
map,
2020
find,
2121
some,
2222
includes,
2323
isFunction
2424
};
2525

26-
var mapping = { permissions: 'permissions', groups: 'groups' };
26+
const mapping = {permissions: 'permissions', groups: 'groups'};
2727

2828
//----------------------------------------------------------------------------------------------------------------------
2929

@@ -38,7 +38,7 @@ class TPManager
3838
{
3939
return !!_.find(user[mapping.permissions], (permission) =>
4040
{
41-
if(permission == '*/*')
41+
if(permission === '*/*')
4242
{
4343
return true;
4444
}
@@ -74,15 +74,15 @@ class TPManager
7474
throw new Error("Must specify a 'name' property.");
7575
} // end if
7676

77-
var group = new TPGroup(groupDef.name, groupDef.permissions, this);
77+
const group = new TPGroup(groupDef.name, groupDef.permissions, this);
7878
this.groups[groupDef.name] = group;
7979

8080
return group;
8181
} // end defineGroup
8282

8383
loadGroups(groupsOrFunc)
8484
{
85-
var loadPromise = Promise.resolve(groupsOrFunc);
85+
let loadPromise = Promise.resolve(groupsOrFunc);
8686
if(_.isFunction(groupsOrFunc))
8787
{
8888
loadPromise = groupsOrFunc();
@@ -97,10 +97,17 @@ class TPManager
9797

9898
hasPerm(user, perm, obj)
9999
{
100-
var found = this._userHasPerm(user, perm, obj);
100+
if(arguments.length === 2)
101+
{
102+
const parts = perm.split('/');
103+
obj = parts[0];
104+
perm = parts[1];
105+
} // end if
106+
107+
let found = this._userHasPerm(user, perm, obj);
101108
if(!found)
102109
{
103-
var groups = _.map(user[mapping.groups], (name) => this.groups[name]);
110+
const groups = _.map(user[mapping.groups], (name) => this.groups[name]);
104111
found = _.some(groups, (group) => group.hasPerm(perm, obj));
105112
} // end if
106113

test/group.spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
// @module base.spec.js
55
// ---------------------------------------------------------------------------------------------------------------------
66

7-
var expect = require('chai').expect;
7+
const expect = require('chai').expect;
88

9-
var TPGroup = require('../src/group').default;
9+
const TPGroup = require('../src/group').default;
1010

1111
// ---------------------------------------------------------------------------------------------------------------------
1212

1313
describe('Groups', () =>
1414
{
15-
var groupInst, god;
15+
let groupInst, god;
1616
beforeEach(() =>
1717
{
1818
groupInst = new TPGroup('test', [
@@ -38,6 +38,11 @@ describe('Groups', () =>
3838

3939
describe('#hasPerm()', () =>
4040
{
41+
it('can take the `Object/perm` form', () =>
42+
{
43+
expect(groupInst.hasPerm('Other/canEdit')).to.be.true;
44+
});
45+
4146
it('matches exact permissions', () =>
4247
{
4348
expect(groupInst.hasPerm('canEdit', 'Other')).to.be.true;

test/trivialperms.spec.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
// @module trivialperms.spec.js
55
// ---------------------------------------------------------------------------------------------------------------------
66

7-
var Promise = require('bluebird');
8-
var expect = require('chai').expect;
7+
const Promise = require('bluebird');
8+
const expect = require('chai').expect;
99

10-
var tp = require('../src/trivialperms');
10+
const tp = require('../src/trivialperms');
1111

1212
// ---------------------------------------------------------------------------------------------------------------------
1313

1414
describe('Permissions', () =>
1515
{
16-
var batman, stark, leo;
16+
let batman, stark, leo;
1717
beforeEach(() =>
1818
{
1919
batman = {
@@ -156,7 +156,7 @@ describe('Permissions', () =>
156156
it('supports setting the mapping of properties on the user object', () =>
157157
{
158158
tp.setUserMapping({ groups: 'roles' });
159-
var user = {
159+
const user = {
160160
roles: ['Users']
161161
};
162162

@@ -177,12 +177,20 @@ describe('Permissions', () =>
177177

178178
it('returns false when checking the groups of a user if the group is not registered', () =>
179179
{
180-
var user = {
180+
const user = {
181181
groups: ['Users', 'Fakes']
182182
};
183183

184184
expect(tp.hasGroup(user, 'Fakes')).to.equal(false);
185185
});
186+
187+
describe('#hasPerm()', () =>
188+
{
189+
it('can take the `Object/perm` form', () =>
190+
{
191+
expect(tp.hasPerm(stark, 'Posts/canEdit')).to.equal(true);
192+
});
193+
});
186194
});
187195
});
188196

0 commit comments

Comments
 (0)