forked from mantrajs/meteor-mantra-kickstarter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccess_control.js
173 lines (145 loc) · 4.49 KB
/
access_control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Class, Enum } from 'meteor/jagi:astronomy';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import App from './app.js';
export const accessControl = new Mongo.Collection('access_control');
const levelsH2L = [];
levelsH2L.push( 'Owner' );
levelsH2L.push( 'Administrator' );
levelsH2L.push( 'Staff' );
levelsH2L.push( 'Member' );
levelsH2L.push( 'Customer' );
levelsH2L.push( 'Registered' );
const HASBARS = '||||||';
const HASNOTBARS = '::::::';
export const TrustLevel = Enum.create({
name: 'TrustLevel',
identifiers: levelsH2L
});
export const AccessControl = Class.create({
name: 'AccessControl',
collection: accessControl,
fields: {
module: String,
action: String,
level: TrustLevel,
group: String,
roles: {
type: [ String ],
transient: true
}
},
indexes: {
moduleActionGroup: {
fields: {
module: 1,
action: 1,
group: 1
},
options: {
unique: true
}
}
},
helpers: {
initialize(_module, _action, _level, _group) {
AccessControl.upsert(
{ module: _module, action: _action, group: _group },
{
module: _module,
action: _action,
level: _level,
group: _group,
removed: false
},
{ upsert: true }
);
},
getTrustedRoles() {
return AccessControl.getGreaterLevelsFor(levelsH2L[this.level]);
}
},
behaviors: {
softremove: {
// The field name with a flag for marking a document as removed.
removedFieldName: 'removed',
// A flag indicating if a "removedAt" field should be present in a document.
hasRemovedAtField: true,
// The field name storing the removal date.
removedAtFieldName: 'removedAt'
}
}
});
AccessControl.upsertRecord = (_module, _action, _level, _group) => {
// console.log('upsert => ' + _module + ' : ' + _action + ' : ' + _level + ' : ' + _group);
AccessControl.upsert(
{ module: _module, action: _action, group: _group },
{
module: _module,
action: _action,
level: _level,
group: _group,
removed: false
},
{ upsert: true }
);
};
AccessControl.getGreaterLevelsFor = (_levelName) => {
check(_levelName, String);
return levelsH2L.slice(0, TrustLevel[_levelName] + 1);
};
AccessControl.getLesserLevelsFor = (_levelName) => {
check(_levelName, String);
return levelsH2L.slice(TrustLevel[_levelName]);
};
AccessControl.getTrustLevels = () => {
return TrustLevel.getIdentifiers();
};
AccessControl.getIcon = ( level ) => {
let hasnt = TrustLevel[level];
return HASBARS.slice(0, levelsH2L.length - hasnt) +
HASNOTBARS.slice(0, hasnt);
};
AccessControl.getIcons = function () {
return this.getTrustLevels().map( v => this.getIcon(v) );
};
AccessControl.findAccessPoint = (_module, _action, _grp) => {
// console.log('\n\n Finding => module: ' + _module + ', action: ' +
// _action + ' , group:' + _grp);
// console.log('\n\n Finding => all: ', AccessControl.find({ }).fetch());
let ap = AccessControl.findOne({ module: _module, action: _action, group: _grp });
if (ap) {
ap.roles = ap.getTrustedRoles();
}
return ap;
};
AccessControl.canTrust = (context, _idUser, _accessPoint) => {
const { Tracker, LocalState } = context;
const { module, action } = _accessPoint;
// console.log('~~~~~~~~~~~ context : ', context);
// Lgr.info( `got : module = ${module} action = ${action}.` );
// console.log('\n\n canTrust got : ', module, action, Tracker, LocalState);
// console.log('\n\n wait for ready ', _accessPoint, App.group);
// if (Meteor.subscribe('access-points', _accessPoint, App.group).ready()) {
// console.log(' ready == ', _accessPoint);
// // const allowed = Collections.AccessControl.canTrust( Meteor.userId(), ACCESS_POINT );
// let ap = AccessControl.findOne({ key: 'colors.add', group: 'headOffice'});
// console.log(' AP ', ap.level);
// }
// console.log(' past wait for ready ');
Tracker.autorun(function () {
const ap = AccessControl.findAccessPoint( module, action, App.group );
const authorized = ap ?
Roles.userIsInRole(_idUser, ap.roles, ap.group) :
false;
let hook = module + ':' + action + ':' + App.group + ':' + _idUser;
LocalState.set( hook, authorized );
// console.log(`~~~~~~~~~~~ canTrust ${hook}? ${authorized}`);
return authorized;
});
// Lgr.info( 'returning' );
};
export default {
AccessControl,
TrustLevel
};