Skip to content

Commit 47df35b

Browse files
committed
mostly implemented.
1 parent d5511ae commit 47df35b

File tree

14 files changed

+632
-0
lines changed

14 files changed

+632
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

.editorconfig

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This project uses EditorConfig for setting code formatting options: http://EditorConfig.org
2+
3+
root = true
4+
5+
# Default settings for all files
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
indent_style = tab
12+
indent_size = tab
13+
tab_width = 4
14+
max_line_length = 120
15+
16+
spaces_around_operators = true
17+
spaces_around_brackets = none
18+
curly_bracket_next_line = true
19+
indent_brace_style = Allman
20+
continuation_indent_size = 4
21+
22+
[*.{css,less}]
23+
curly_bracket_next_line = false
24+
continuation_indent_size = 0
25+
26+
# Formats likely to be pasted into an REPL on a terminal should not use tabs to avoid triggering tab-completion.
27+
[*.{sh,bash,sql,psql,pgsql}]
28+
indent_style = space
29+
indent_size = 4
30+
31+
[{*.yml,.eslintrc}]
32+
indent_style = space
33+
indent_size = 2
34+
35+
[*.{js,json,vue}]
36+
indent_style = space
37+
indent_size = 4
38+
39+
# Special overrides for automatically-generated files
40+
[{package.json,.jshintrc}]
41+
indent_size = 2
42+
43+
# For some formats, trailing whitespace is significant; don't strip it.
44+
[*.{md,diff,patch}]
45+
trim_trailing_whitespace = false

.eslintrc.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
env:
2+
es6: true
3+
node: true
4+
5+
parserOptions:
6+
ecmaVersion: 6
7+
sourceType: module
8+
9+
# Note: Rules are modified using a number that defines what the rule's behavior should be:
10+
# 0 - turn the rule off
11+
# 1 - turn the rule on as a warning (doesn't affect exit code)
12+
# 2 - turn the rule on as an error (exit code is 1 when triggered)
13+
rules:
14+
semi: [2, always]
15+
no-extra-semi: 2
16+
semi-spacing: 2
17+
comma-spacing: 2
18+
no-shadow: 1
19+
no-unused-vars: 1
20+
no-use-before-define: [2, nofunc]
21+
22+
# Disabled:
23+
no-underscore-dangle: 0
24+
quotes: 0
25+
strict: 0

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Editor artifacts
2+
**.swp
3+
4+
# NPM cruft
5+
node_modules
6+
**.log
7+
8+
# Jetbrains IDE project files
9+
.idea
10+
11+
# Build files
12+
dist

.npmignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Editor artifacts
2+
**.swp
3+
4+
# NPM cruft
5+
node_modules
6+
**.log
7+
8+
# Jetbrains IDE project files
9+
.idea
10+
11+
# Ignore source files
12+
.babelrc
13+
.eslintrc
14+
.gitignore
15+
*.yml
16+
Gruntfile.js
17+
src
18+
examples
19+
tests

Gruntfile.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
// TrivialPermissions Gruntfile
3+
//----------------------------------------------------------------------------------------------------------------------
4+
5+
module.exports = function(grunt)
6+
{
7+
grunt.initConfig({
8+
clean: ['dist'],
9+
babel: {
10+
options: {
11+
sourceMap: "inline",
12+
presets: ['es2015']
13+
},
14+
dev: {
15+
options: {
16+
compact: false,
17+
comments: true
18+
},
19+
files: [{
20+
expand: true,
21+
cwd:"src",
22+
src: ['**/*.js'],
23+
dest: 'dist'
24+
}]
25+
},
26+
prod: {
27+
options: {
28+
compact: true,
29+
comments: false
30+
},
31+
files: [{
32+
expand: true,
33+
cwd:"src",
34+
src: ['**/*.js'],
35+
dest: 'dist'
36+
}]
37+
}
38+
},
39+
watch: {
40+
index: {
41+
files: ["src/**/*.js"],
42+
tasks: ["babel:dev"]
43+
}
44+
},
45+
eslint: {
46+
src: {
47+
src: ['Gruntfile.js', 'src/**/*.js'],
48+
options: { configFile: '.eslintrc' }
49+
},
50+
test: {
51+
src: ['test/**/*.js'],
52+
options: { configFile: 'test/.eslintrc' }
53+
}
54+
}
55+
});
56+
57+
//------------------------------------------------------------------------------------------------------------------
58+
59+
grunt.loadNpmTasks("grunt-babel");
60+
grunt.loadNpmTasks('grunt-contrib-clean');
61+
grunt.loadNpmTasks('grunt-contrib-watch');
62+
grunt.loadNpmTasks("gruntify-eslint");
63+
64+
//------------------------------------------------------------------------------------------------------------------
65+
66+
grunt.registerTask("build-dev", ["eslint", "clean", "babel:dev"]);
67+
grunt.registerTask("build", ["eslint", "clean", "babel:prod"]);
68+
grunt.registerTask("default", ["build-dev", 'watch']);
69+
70+
//------------------------------------------------------------------------------------------------------------------
71+
};
72+
73+
//----------------------------------------------------------------------------------------------------------------------

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "trivialperms",
3+
"version": "1.0.0-alpha.1",
4+
"description": "A simple RBAC implementation that is datasource agnostic.",
5+
"main": "./dist/trivialperms.js",
6+
"scripts": {
7+
"test": "./node_modules/.bin/mocha --reporter spec --recursive --compilers js:babel-core/register",
8+
"compile": "./node_modules/.bin/grunt build",
9+
"prepublish": "npm run compile"
10+
},
11+
"keywords": [
12+
"json",
13+
"rbac",
14+
"permissions",
15+
"roles"
16+
],
17+
"author": "Christopher S. Case <[email protected]>",
18+
"bugs": "https://github.com/trivialsoftware/TrivialPermissions/issues",
19+
"homepage": "http://trivialsoftware.github.io/TrvialPermissions/",
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/trivialsoftware/TrivialPermissions.git"
23+
},
24+
"license": "MIT",
25+
"dependencies": {
26+
"bluebird": "^3.0.5",
27+
"lodash": "^3.10.1",
28+
"make-error": "^1.0.4"
29+
},
30+
"devDependencies": {
31+
"babel-cli": "^6.2.0",
32+
"babel-core": "^6.2.1",
33+
"babel-preset-es2015": "^6.1.18",
34+
"chai": "^3.5.0",
35+
"grunt": "^0.4.5",
36+
"grunt-babel": "^6.0.0",
37+
"grunt-cli": "^0.1.13",
38+
"grunt-contrib-clean": "^0.7.0",
39+
"grunt-contrib-watch": "^0.6.1",
40+
"gruntify-eslint": "^1.3.0",
41+
"mocha": "~1.18.2"
42+
}
43+
}

src/base.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
/// TPBase
3+
///
4+
/// @module
5+
//----------------------------------------------------------------------------------------------------------------------
6+
7+
import _ from 'lodash';
8+
9+
//----------------------------------------------------------------------------------------------------------------------
10+
11+
class TPBase {
12+
constructor(name, permissions, manager)
13+
{
14+
this.name = name;
15+
this.permissions = permissions || [];
16+
this.manager = manager;
17+
18+
// Ensure our permissions are unique
19+
this.permissions = _.unique(this.permissions);
20+
} // end constructor
21+
22+
hasPerm(perm, obj)
23+
{
24+
return !!_.find(this.permissions, (permission) =>
25+
{
26+
if(permission.match(/^.*\/\*/))
27+
{
28+
return !!permission.match(new RegExp(`^${ obj }/`));
29+
}
30+
else if(permission.match(/^\*\/.*/))
31+
{
32+
return !!permission.match(new RegExp(`^.*/${ perm }$`));
33+
}
34+
else
35+
{
36+
return !!permission.match(new RegExp(`^${ obj }/${ perm }$`));
37+
} // end if
38+
});
39+
} // end hasPerm
40+
41+
addPerm(perm, obj)
42+
{
43+
this.permissions.push(`${ obj }/${ perm }`);
44+
45+
// Ensure our permissions are unique
46+
this.permissions = _.unique(this.permissions);
47+
} // end addPerm
48+
49+
removePerm(perm, obj)
50+
{
51+
return _.remove(this.permissions, (item) => item == `${ obj }/${ perm }`);
52+
} // end removePerm
53+
} // end TPBase
54+
55+
//----------------------------------------------------------------------------------------------------------------------
56+
57+
export default TPBase;
58+
59+
//----------------------------------------------------------------------------------------------------------------------

src/group.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
/// TPGroup
3+
///
4+
/// @module
5+
//----------------------------------------------------------------------------------------------------------------------
6+
7+
import _ from 'lodash';
8+
9+
import TPBase from './base';
10+
11+
//----------------------------------------------------------------------------------------------------------------------
12+
13+
class TPGroup extends TPBase {
14+
} // end TPGroup
15+
16+
//----------------------------------------------------------------------------------------------------------------------
17+
18+
export default TPGroup;
19+
20+
//----------------------------------------------------------------------------------------------------------------------

src/trivialperms.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//----------------------------------------------------------------------------------------------------------------------
2+
/// TrivialPerms
3+
///
4+
/// @module
5+
//----------------------------------------------------------------------------------------------------------------------
6+
7+
import TPGroup from './group';
8+
import TPUser from './user';
9+
10+
//----------------------------------------------------------------------------------------------------------------------
11+
12+
class TPManager
13+
{
14+
constructor()
15+
{
16+
this.users = {};
17+
this.groups = {};
18+
} // end constructor
19+
20+
defineGroup(groupDef)
21+
{
22+
if(!groupDef.name)
23+
{
24+
throw new Error("Must specify a 'name' property.");
25+
} // end if
26+
27+
var group = new TPGroup(groupDef.name, groupDef.permissions, this);
28+
this.groups[groupDef.name] = group;
29+
30+
return group;
31+
} // end defineGroup
32+
33+
defineUser(userDef)
34+
{
35+
if(!userDef.name)
36+
{
37+
throw new Error("Must specify a 'name' property.");
38+
} // end if
39+
40+
var user = new TPUser(userDef, this);
41+
this.users[userDef.name] = user;
42+
43+
return user;
44+
} // end defineUser
45+
46+
hasPerm(userName, perm, obj)
47+
{
48+
var user = this.users[userName];
49+
if(user)
50+
{
51+
return user.hasPerm(perm, obj);
52+
} // end if
53+
54+
return false;
55+
} // end hasPerm
56+
57+
hasGroup(userName, groupName)
58+
{
59+
var user = this.users[userName];
60+
if(user)
61+
{
62+
return user.hasGroup(groupName);
63+
} // end if
64+
65+
return false;
66+
} // end hasGroup
67+
} // end TPManager
68+
69+
//----------------------------------------------------------------------------------------------------------------------
70+
71+
export default new TPManager();
72+
73+
//----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)