forked from max-mapper/datacouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
executable file
·38 lines (30 loc) · 958 Bytes
/
users.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
var couchapp = require('couchapp')
, path = require('path')
;
ddoc = { _id: '_design/users' };
ddoc.views = {
users: {
map: function(doc) {
emit(doc._id);
}
}
};
ddoc.lists = {
array: function(head, req) {
start({"headers":{"Content-Type" : "application/json; charset=utf-8"}});
if ('callback' in req.query) send(req.query['callback'] + "(");
var headers = [];
while (row = getRow()) {
headers.push(row.key);
}
send(JSON.stringify(headers));
if ('callback' in req.query) send(")");
}
}
ddoc.validate_doc_update = function (newDoc, oldDoc, userCtx) {
if (userCtx.roles.indexOf('_admin') > -1) return;
if (oldDoc && (newDoc._id !== oldDoc._id)) throw "That username is taken!";
if (newDoc._deleted === true && userCtx.roles.indexOf('_admin') === -1) throw "Only admin can delete documents.";
};
couchapp.loadAttachments(ddoc, path.join(__dirname, 'attachments'));
module.exports = ddoc;