Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (138 loc) · 3.96 KB
/
Copy pathindex.js
File metadata and controls
155 lines (138 loc) · 3.96 KB
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
/**
* Abstract user permission resolver
*/
class AbstractUserPermission {
constructor({userId, storageService}) {
this.userId = userId;
this.changes = [];
this.storageService = storageService;
}
allow(p) {
this.changes.push(Object.assign({changeType: 'allow'}, p));
return this;
}
deny(p) {
this.changes.push(Object.assign({changeType: 'deny'}, p))
return this;
}
async getPermissions(resourceType = null, resourceId = null) {
throw new Error("not implemented yet");
}
async apply() {
throw new Error("not implemented yet");
}
}
/**
* Mongoose backed user permission resolver
*/
class UserPermissionMongoose extends AbstractUserPermission {
constructor({userId, storageService}) {
super({userId, storageService});
// init models
this.mongoose = this.storageService.mongoose;
this.models = {};
this.models.UserPermissions = this.mongoose.models.authz_user_permissions || this.mongoose.model('authz_user_permissions', {
userId: { type: String, index: true },
resourceType: { type: String, index: true },
resourceId: { type: String, index: true },
permissions: { type: Array, index: true },
groups: { type: Array, index: true }
});
this.models.GroupPermissions = this.mongoose.models.authz_group_permissions || this.mongoose.model('authz_group_permissions', {
groupId: { type: String, index: true },
resourceType: { type: String, index: true },
resourceId: { type: String, index: true },
permissions: { type: Array, index: true },
users: { type: Array, index: true }
});
}
async getPermissions(resourceType = null, resourceId = null) {
const selector = {
userId: this.userId
};
if (resourceType)
selector.resourceType = resourceType;
if (resourceId)
selector.resourceId = resourceId;
try {
const perms = await (this.models
.UserPermissions
.find(selector)
.exec());
return perms;
} catch (err) {
return [];
}
}
async apply() {
for (const ch of this.changes) {
if (ch.changeType === 'allow') {
const existing = await this.models.UserPermissions.findOne({
userId: this.userId,
resourceType: ch.resourceType,
resourceId: ch.resourceId
}).exec();
if (existing && existing.permissions) {
const newPermissions = [...new Set([...existing.permissions,...ch.permissions])];
existing.permissions = newPermissions;
await existing.save();
} else {
await this.models.UserPermissions.create({
userId: this.userId,
resourceType: ch.resourceType,
resourceId: ch.resourceId,
permissions: ch.permissions
});
}
}
}
}
}
/**
* Authorization service
*/
class Authorize {
/**
* Ctor
*
* @param {Object} svcs - DI container services
*/
constructor(svcs) {
this.config = svcs.config.get('authorize');
const storageServiceName = this.config.storageService || 'memory';
switch (storageServiceName) {
// mongoose storage
case 'mongoose':
this.storageService = svcs.mongoose;
this.UserPermissionClass = UserPermissionMongoose;
// TODO: this.GroupPermissionClass = GroupPermissionMongoose;
break;
default:
throw new Error(`authorize requires a supported storage service, ${storageService} is not supported`);
break;
}
}
/**
* Returns user permission resolver
*
* @param {String}
*/
user(userId) {
const tx = new this.UserPermissionClass({userId, storageService: this.storageService});
return tx;
}
/**
* Returns group permission resolver
*
* @param {String}
*/
group(groupId) {
throw new Error(`not implemented yet`);
//const tx = new this.GroupPermissionClass({userId, models: this.models});
//return tx;
}
register() {
return [];
}
}
module.exports = Authorize;