1 | 'use strict';
|
2 |
|
3 | var normalize = require('../normalize');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function Users(api) {
|
13 | this.api = api;
|
14 | this.send = api.send.bind(api);
|
15 | this.view = api.view.bind(api);
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | Users.prototype.add = function add(name, pkg, fn) {
|
28 | return this.send(name, {
|
29 | method: 'PUT',
|
30 | json: {}
|
31 | }, fn);
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | Users.prototype.create = function create(username, email, password, fn) {
|
44 | password = (password || '').toString().trim();
|
45 | username = (username || '').toString().trim();
|
46 | email = (email || '').toString().trim();
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | if (!password) return fn(new Error('Missing password'));
|
52 | if (!~password.indexOf(':')) return fn(new Error('Password cannot contain a `:`'));
|
53 | if (!email) return fn(new Error('Missing email'));
|
54 | if (!~email.indexOf('@')) return fn(new Error('Invalid email address'));
|
55 | if (!username) return fn(new Error('Missing username'));
|
56 |
|
57 | return this.send('/-/user/org.couchdb.user:'+ encodeURIComponent(username), {
|
58 | method: 'PUT',
|
59 | json: {
|
60 | _id: 'org.couchdb.user:'+ username
|
61 | , date: (new Date()).toISOString()
|
62 | , email: email
|
63 | , name: username
|
64 | , password: password
|
65 | , roles: []
|
66 | , type: 'user'
|
67 | }
|
68 | }, fn);
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | Users.prototype.update = function update(username, fields, fn) {
|
79 | username = '/-/user/org.couchdb.user:'+ encodeURIComponent(username);
|
80 |
|
81 | var users = this;
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function rev(err, _rev, data) {
|
92 | if (err) return fn(err);
|
93 |
|
94 | return users.send(username +'/-rev/'+ _rev, {
|
95 | method: 'PUT',
|
96 | json: users.api.merge(data || {}, fields)
|
97 | }, fn);
|
98 | }
|
99 |
|
100 | if ('_rev' in fields) return rev(undefined, fields._rev);
|
101 | return this.send(username, rev);
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | Users.prototype.list = function list(name, fn) {
|
113 | return this.view('browseAuthors', {
|
114 | key: name
|
115 | }, fn).map(this.api.map.simple);
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | Users.prototype.starred = function starred(name, fn) {
|
127 | return this.view('browseStarUser', {
|
128 | key: name
|
129 | }, fn).map(this.api.map.simple);
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | Users.prototype.get = function get(username, fn) {
|
141 | username = '/-/user/org.couchdb.user:'+ encodeURIComponent(username);
|
142 |
|
143 | return this.send(name, fn).map(normalize.users);
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | Users.prototype.sync = function sync(source, target, options, fn) {
|
157 | if ('function' === typeof options) {
|
158 | fn = options;
|
159 | options = null;
|
160 | }
|
161 |
|
162 | options = options || {};
|
163 | options.add = 'add' in options ? options.add : true;
|
164 | options.packages = 'packages' in options ? options.packages : false;
|
165 |
|
166 | var user = this;
|
167 |
|
168 | this.list(source, function (err, packages) {
|
169 | user.api.async.each(packages, function (name, next) {
|
170 | user.add(target, name, next);
|
171 | }, fn);
|
172 | });
|
173 | };
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | module.exports = Users;
|