Repository navigation
Expand file tree
/
Copy pathAbout.js
More file actions
208 lines (206 loc) · 7.56 KB
/
Copy pathAbout.js
File metadata and controls
208 lines (206 loc) · 7.56 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
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
var config = require('config');
var debug = require('debug')('lib:about');
var exec = function () {};
var emit = function () {};
module.exports.FieldDB = function FieldDB() {
this.getApiDocs = function getApiDocs() {
return {
apiVersion: '1.72.0',
swaggerVersion: '1.2',
basePath: 'https://localhost:3181/v2',
resourcePath: '/users',
produces: ['application/json', 'application/xml', 'text/plain', 'text/html', 'application/x-latex', 'application/tar', 'application/zip', 'application/pdf', 'text/srt', 'text/websrt', 'text/x-textgrid', 'text/csv'],
apis: [],
models: {
User: {
username: 'User',
required: ['username', 'password'],
properties: {
username: {
type: 'string',
required: true,
description: 'lowercase username, must be file safe limited to a-z0-9'
},
name: {
type: 'string'
}
}
},
Corpus: {
corpusidentifier: 'Corpus',
properties: {
dbname: {
type: 'string',
description: 'username-filesafeversionofcorpustitleinlowercase'
}
}
}
},
info: {
title: 'FieldDB AuthService API',
description: "This is a public service permitting any client side app (must be known to the system) to contact FieldDB servers on the user's behalf to register, login, create new databases and share databases. You can find out more about FieldDB \n on <a target=\"_blank\" href=\"https://github.com/FieldDB/FieldDB/issues/milestones?state=closed\"> our GitHub organization</a>. For this sample,\n you can use the api key \"special-key\" to test the authorization filters",
termsOfServiceUrl: 'https://github.com/FieldDB/FieldDBWebServer/blob/master/public/privacy.html',
contact: 'issuetracker',
license: 'Apache 2.0',
licenseUrl: 'http://www.apache.org/licenses/LICENSE-2.0.html'
}
};
};
this.getVersion = function getVersion(req, res) {
res.send('getVersion');
};
/*
____ _ _ _ _
| _ \(_)___ __ _| |__ | | ___ __| |
| | | | / __|/ _` | '_ \| |/ _ \/ _` |
| |_| | \__ \ (_| | |_) | | __/ (_| |
|____/|_|___/\__,_|_.__/|_|\___|\__,_|
* */
this.installFieldDB = function installFieldDB(req, res) {
if (process.env.INSTALABLE !== 'true') {
res.send({
error: 'Method disabled'
});
return;
}
var installStatus = '';
var connectionUrl = config.usersDbConnection.protocol + config.couchKeys.username + ':' + config.couchKeys.password + '@' + config.usersDbConnection.domain + ':' + config.usersDbConnection.port;
var couchServer = require('nano')(connectionUrl);
var respond = function respond(info, result, error) {
if (error) {
res.send(info);
} else {
res.send(result);
}
};
var pushTemplateDatabases = function pushTemplateDatabases(err, body) {
if (err) {
debug(err);
return respond({
info: installStatus
}, false, err);
}
var command = 'sh ../FieldDB/scripts/build_template_databases_using_fielddb.sh ' + connectionUrl;
debug(command);
var child = exec(command, function afterExec(err, stdout, stderr) {
if (err) {
return respond({
info: installStatus
}, false, err);
}
debug('stdout', stdout);
debug('stderr', stderr);
installStatus += ' ::: build_template_databases_using_fielddb';
debug('all done ' + installStatus);
return respond(null, {
ok: 'sucess',
info: installStatus,
stdout: stdout
}, null);
});
};
var betatesters = function betatesters(doc) {
if (!doc.roles || doc.roles.length === 0) {
return;
}
var username = doc._id.replace(/org.couchdb.user:/, '');
if (username.indexOf('test') > -1 || username.indexOf('anonymous') > -1 || username.indexOf('acra') > -1) {
emit(username, doc.roles);
} else {
// this is not a beta tester
}
};
var normalusers = function normalusers(doc) {
if (!doc.roles || doc.roles.length === 0) {
return;
}
var username = doc._id.replace(/org.couchdb.user:/, '');
if (username.indexOf('test') > -1 || username.indexOf('anonymous') > -1 || username.indexOf('acra') > -1) {
// this is a beta tester
} else {
emit(username, doc.roles);
}
};
var createUsersViewInFielddbUsersDB = function createUsersViewInFielddbUsersDB(err, body) {
if (err) {
debug(err);
return respond({
info: installStatus
}, false, err);
}
installStatus += ' ::: _users/_design/users';
var usersView = {
_id: '_design/users',
language: 'javascript',
views: {
usermasks: {
map: 'function(doc) {\n emit(doc.username, {username: doc.username, gravatar: doc.gravatar, gravatar_email: doc.email});\n}'
}
}
};
var userWhoHaveTroubleLoggingIn = {
_id: '_design/users',
language: 'javascript',
views: {
usermasks: {
map: 'function(doc) {\n if (doc.email && doc.serverlogs && doc.serverlogs.incorrectPasswordAttempts && doc.serverlogs.incorrectPasswordAttempts.length > 0) {\n emit(doc.email, doc);\n }\n}'
}
}
};
var usersByEmail = {
_id: '_design/users',
language: 'javascript',
views: {
usermasks: {
map: 'function(doc) {\n if (doc.email) {\n emit(doc.email, doc);\n }\n}'
}
}
};
couchServer.db.use(config.usersDbConnection.dbname).insert(usersView, '_design/users', pushTemplateDatabases);
couchServer.db.use(config.usersDbConnection.dbname).insert(userWhoHaveTroubleLoggingIn, '_design/users', pushTemplateDatabases);
couchServer.db.use(config.usersDbConnection.dbname).insert(usersByEmail, '_design/users', pushTemplateDatabases);
};
var createUsersView = function createUsersView(err, body) {
if (err) {
debug(err);
return respond({
info: installStatus
}, false, err);
}
installStatus += ' ::: new_user_activity_feed';
var usersView = {
_id: '_design/users',
language: 'javascript',
views: {
userroles: {
map: 'function(doc) {\n var username = doc._id.replace(/org.couchdb.user:/,"");\n if(doc.password_sha || doc.password_scheme)\n emit(username,doc.roles);\n}'
}
}
};
couchServer.db.use('_users').insert(usersView, '_design/users', createUsersViewInFielddbUsersDB);
};
var createUserActivityFeed = function createUserActivityFeed(err, body) {
if (err) {
debug(err);
return respond({
info: installStatus
}, false, err);
}
installStatus += ' ::: new_corpus_activity_feed';
couchServer.db.create('new_user_activity_feed', createUsersView);
};
var createCorpusActivityFeed = function createCorpusActivityFeed(err, body) {
if (err) {
debug(err);
return respond({
info: installStatus
}, false, err);
}
installStatus += ' ::: new_corpus';
couchServer.db.create('new_corpus_activity_feed', createUserActivityFeed);
};
couchServer.db.create(config.usersDbConnection.dbname, createCorpusActivityFeed);
// res.send("installFieldDB");
};
return this;
};