forked from OptimalBits/node_acl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb-backend.js
280 lines (249 loc) · 7.96 KB
/
mongodb-backend.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
MongoDB Backend.
Implementation of the storage backend using MongoDB
*/
"use strict";
var contract = require('./contract');
var async = require('async');
var _ = require('lodash');
// Name of the collection where meta and allowsXXX are stored.
// If prefix is specified, it will be prepended to this name, like acl_resources
var aclCollectionName = 'resources';
function MongoDBBackend(db, prefix, useSingle, useRawCollectionNames){
this.db = db;
this.prefix = typeof prefix !== 'undefined' ? prefix : '';
this.useSingle = (typeof useSingle !== 'undefined') ? useSingle : false;
this.useRawCollectionNames = useRawCollectionNames === false; // requires explicit boolean false value
}
MongoDBBackend.prototype = {
/**
Begins a transaction.
*/
begin : function(){
// returns a transaction object(just an array of functions will do here.)
return [];
},
/**
Ends a transaction (and executes it)
*/
end : function(transaction, cb){
contract(arguments).params('array', 'function').end();
async.series(transaction,function(err){
cb(err instanceof Error? err : undefined);
});
},
/**
Cleans the whole storage.
*/
clean : function(cb){
contract(arguments).params('function').end();
this.db.collections(function(err, collections) {
if (err instanceof Error) return cb(err);
async.forEach(collections,function(coll,innercb){
coll.drop(function(){innercb()}); // ignores errors
},cb);
});
},
/**
Gets the contents at the bucket's key.
*/
get : function(bucket, key, cb){
contract(arguments)
.params('string', 'string|number', 'function')
.end();
key = encodeText(key);
var searchParams = (this.useSingle? {_bucketname: bucket, key:key} : {key:key});
var collName = (this.useSingle? aclCollectionName : bucket);
this.db.collection(this.prefix + this.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
// Excluding bucket field from search result
collection.findOne(searchParams, {_bucketname: 0},function(err, doc){
if(err) return cb(err);
if(! _.isObject(doc) ) return cb(undefined,[]);
doc = fixKeys(doc);
cb(undefined,_.without(_.keys(doc),"key","_id"));
});
});
},
/**
Returns the union of the values in the given keys.
*/
union : function(bucket, keys, cb){
contract(arguments)
.params('string', 'array', 'function')
.end();
keys = encodeAll(keys);
var searchParams = (this.useSingle? {_bucketname: bucket, key: { $in: keys }} : {key: { $in: keys }});
var collName = (this.useSingle? aclCollectionName : bucket);
this.db.collection(this.prefix + this.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
// Excluding bucket field from search result
collection.find(searchParams, {_bucketname: 0}).toArray(function(err,docs){
if(err instanceof Error) return cb(err);
if( ! docs.length ) return cb(undefined, []);
var keyArrays = [];
docs = fixAllKeys(docs);
docs.forEach(function(doc){
keyArrays.push.apply(keyArrays, _.keys(doc));
});
cb(undefined, _.without(_.union(keyArrays),"key","_id"));
});
});
},
/**
Adds values to a given key inside a bucket.
*/
add : function(transaction, bucket, key, values){
contract(arguments)
.params('array', 'string', 'string|number','string|array|number')
.end();
if(key=="key") throw new Error("Key name 'key' is not allowed.");
key = encodeText(key);
var self=this;
var updateParams = (self.useSingle? {_bucketname: bucket, key:key} : {key:key});
var collName = (self.useSingle? aclCollectionName : bucket);
transaction.push(function(cb){
values = makeArray(values);
self.db.collection(self.prefix + self.removeUnsupportedChar(collName), function(err,collection){
if(err instanceof Error) return cb(err);
// build doc from array values
var doc = {};
values.forEach(function(value){doc[value]=true;});
// update document
collection.update(updateParams,{$set:doc},{safe:true,upsert:true},function(err){
if(err instanceof Error) return cb(err);
cb(undefined);
});
});
});
transaction.push(function(cb) {
self.db.collection(self.prefix + self.removeUnsupportedChar(collName), function(err,collection){
// Create index
collection.ensureIndex({_bucketname: 1, key: 1}, function(err){
if (err instanceof Error) {
return cb(err);
} else{
cb(undefined);
}
});
});
})
},
/**
Delete the given key(s) at the bucket
*/
del : function(transaction, bucket, keys){
contract(arguments)
.params('array', 'string', 'string|array')
.end();
keys = makeArray(keys);
var self= this;
var updateParams = (self.useSingle? {_bucketname: bucket, key:{$in:keys}} : {key:{$in:keys}});
var collName = (self.useSingle? aclCollectionName : bucket);
transaction.push(function(cb){
self.db.collection(self.prefix + self.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
collection.remove(updateParams,{safe:true},function(err){
if(err instanceof Error) return cb(err);
cb(undefined);
});
});
});
},
/**
Removes values from a given key inside a bucket.
*/
remove : function(transaction, bucket, key, values){
contract(arguments)
.params('array', 'string', 'string|number','string|array|number')
.end();
key = encodeText(key);
var self=this;
var updateParams = (self.useSingle? {_bucketname: bucket, key:key} : {key:key});
var collName = (self.useSingle? aclCollectionName : bucket);
values = makeArray(values);
transaction.push(function(cb){
self.db.collection(self.prefix + self.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
// build doc from array values
var doc = {};
values.forEach(function(value){doc[value]=true;});
// update document
collection.update(updateParams,{$unset:doc},{safe:true,upsert:true},function(err){
if(err instanceof Error) return cb(err);
cb(undefined);
});
});
});
},
removeUnsupportedChar: function(text) {
if (!this.useRawCollectionNames && (typeof text === 'string' || text instanceof String)) {
text = decodeURIComponent(text);
text = text.replace(/[/\s]/g, '_'); // replaces slashes and spaces
}
return text;
}
}
function encodeText(text) {
if (typeof text == 'string' || text instanceof String) {
text = encodeURIComponent(text);
text = text.replace(/\./g, '%2E');
}
return text;
}
function decodeText(text) {
if (typeof text == 'string' || text instanceof String) {
text = decodeURIComponent(text);
}
return text;
}
function encodeAll(arr) {
if (Array.isArray(arr)) {
var ret = [];
arr.forEach(function(aval) {
ret.push(encodeText(aval));
});
return ret;
} else {
return arr;
}
}
function decodeAll(arr) {
if (Array.isArray(arr)) {
var ret = [];
arr.forEach(function(aval) {
ret.push(decodeText(aval));
});
return ret;
} else {
return arr;
}
}
function fixKeys(doc) {
if (doc) {
var ret = {};
for (var key in doc) {
if (doc.hasOwnProperty(key)) {
ret[decodeText(key)] = doc[key];
}
}
return ret;
} else {
return doc;
}
}
function fixAllKeys(docs) {
if (docs && docs.length) {
var ret = [];
docs.forEach(function(adoc) {
ret.push(fixKeys(adoc));
});
return ret;
} else {
return docs;
}
}
function makeArray(arr){
return Array.isArray(arr) ? encodeAll(arr) : [encodeText(arr)];
}
exports = module.exports = MongoDBBackend;