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 |
|
44 | Users.prototype.create = function create(username, email, password, fn) {
|
45 | password = (password || '').toString().trim();
|
46 | username = (username || '').toString().trim();
|
47 | email = (email || '').toString().trim();
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | if (!password) return fn(new Error('Missing password'));
|
53 | if (!~password.indexOf(':')) return fn(new Error('Password cannot contain a `:`'));
|
54 | if (!email) return fn(new Error('Missing email'));
|
55 | if (!~email.indexOf('@')) return fn(new Error('Invalid email address'));
|
56 | if (!username) return fn(new Error('Missing username'));
|
57 |
|
58 | return this.send('/-/user/org.couchdb.user:'+ encodeURIComponent(username), {
|
59 | method: 'PUT',
|
60 | json: {
|
61 | _id: 'org.couchdb.user:'+ username
|
62 | , date: (new Date()).toISOString()
|
63 | , email: email
|
64 | , name: username
|
65 | , password: password
|
66 | , roles: []
|
67 | , type: 'user'
|
68 | }
|
69 | }, fn);
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Users.prototype.update = function update(username, fields, fn) {
|
82 | username = '/-/user/org.couchdb.user:'+ encodeURIComponent(username);
|
83 |
|
84 | var users = this;
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function rev(err, _rev, data) {
|
95 | if (err) return fn(err);
|
96 |
|
97 | return users.send(username +'/-rev/'+ _rev, {
|
98 | method: 'PUT',
|
99 | json: users.api.merge(data || {}, fields)
|
100 | }, fn);
|
101 | }
|
102 |
|
103 | if ('_rev' in fields) return rev(undefined, fields._rev);
|
104 | return this.send(username, rev);
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | Users.prototype.list = function list(name, fn) {
|
116 | return this.view('browseAuthors', {
|
117 | key: name
|
118 | }, fn)
|
119 | .map(this.api.map.simple)
|
120 | .filter(Boolean);
|
121 | };
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | Users.prototype.starred = function starred(name, fn) {
|
132 | return this.view('browseStarUser', {
|
133 | key: name
|
134 | }, fn)
|
135 | .map(this.api.map.simple)
|
136 | .filter(Boolean);
|
137 | };
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | Users.prototype.get = function get(username, fn) {
|
148 | username = '/-/user/org.couchdb.user:'+ encodeURIComponent(username);
|
149 |
|
150 | return this.send(username, fn).map(normalize.users);
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | Users.prototype.sync = function sync(source, target, options, fn) {
|
165 | if ('function' === typeof options) {
|
166 | fn = options;
|
167 | options = null;
|
168 | }
|
169 |
|
170 | options = options || {};
|
171 | options.add = 'add' in options ? options.add : true;
|
172 | options.packages = 'packages' in options ? options.packages : false;
|
173 |
|
174 | var user = this;
|
175 |
|
176 | this.list(source, function (err, packages) {
|
177 | user.api.async.each(packages, function (name, next) {
|
178 | user.add(target, name, next);
|
179 | }, fn);
|
180 | });
|
181 | };
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | module.exports = Users;
|