-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
173 lines (161 loc) · 5.05 KB
/
index.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
#!/usr/bin/env node
require('dotenv').load({ silent: true });
var path = require('path');
var program = require('commander');
var crypto = require('crypto');
var users = require(path.normalize(__dirname + '/../modules/ept-users')).db;
var categories = require(path.normalize(__dirname + '/../modules/ept-categories')).db;
var boards = require(path.normalize(__dirname + '/../modules/ept-boards')).db;
var config = require(path.join(__dirname, '..', 'config'));
var roles = require(path.join(__dirname, '..', 'modules', 'ept-roles', 'plugins', 'acls'));
var emailerOptions = config.emailerEnv;
var emailer = require(path.normalize(__dirname + '/../server/plugins/emailer')).expose(emailerOptions);
var dbName = process.env.PGDATABASE;
var testConnection = require(path.normalize(__dirname + '/../db.js')).db.testConnection;
program
.version('0.0.1');
// test connection command
program
.command('connect')
.description('Check that database exists and connection is possible.')
.action(function() {
return testConnection()
.then(function() {
console.log(`Succesfully connected to database: ${dbName}`);
process.exit(0);
})
.catch(function() {
console.log(`Unable to connect to database: ${dbName}`);
process.exit(1);
});
});
// the seed command
program
.command('seed')
.description('Seed database. Populates with initial user/board.')
.action(function() {
roles.verifyRoles()
.then(function() {
return seed();
})
.then(function() {
process.exit(0);
})
.catch(console.log);
});
// the create-user command
program
.command('create-user <username> <email>')
.description('Seed a user in the database')
.option('--password <password>', 'Password for user')
.option('--admin', 'Create the user as an admin account (default: false)')
.action(function(username, email, options) {
var options = {
username,
email,
password: options.password,
admin: options.admin || false
};
createUser(options)
.then(function(result) {
console.log('Created user', result);
process.exit(0);
})
.catch(function(error) {
console.log('Error creating user:', error);
process.exit(1);
});
});
program.on('--help', function() {
console.log(' Help on specific command: ' + program.name() + ' [command] --help');
});
program.parse(process.argv);
if (!program.args.length) {
program.help();
}
function seed() {
var adminUser = {
username: 'admin',
email: 'admin@epochtalk.com',
password: 'admin1234',
confirmation: 'admin1234'
};
var createdCategory;
return users.create(adminUser, true)
.then(function() {
var generalCategory = { name: 'General' };
return categories.create(generalCategory);
})
.then(function(category) {
console.log('Created category: ' + category.name);
createdCategory = category;
var generalBoard = {
name: 'General Discussion',
description: 'The art of discussing, generally.'
};
return boards.create(generalBoard);
})
.then(function(board) {
console.log('Created board: ' + board.name);
var boardMapping = [
{
id: createdCategory.id,
name: createdCategory.name,
type: 'category',
view_order: 0
},
{
id: board.id,
name: board.name,
type: 'board',
category_id: createdCategory.id,
view_order: 0
}
];
return boards.updateCategories(boardMapping);
})
.then(function() {
console.log('Added board to category');
})
.catch(function(err) {
console.log(err);
});
}
function createUser(options) { // use email for password reset link
var isAdmin = options.admin;
// if both password and email are not provided,
// don't create an account
if (!options.password && !options.email) {
return Promise.error('Email required for seed without password!');
}
// if no password is provided, but there is an email,
// send the user an email with their reset link
else if (!options.password && options.email) {
console.log('Creating user and emailing password reset link to:', options.email);
return users.create(options, isAdmin)
.then(function(user) {
var updateUser = {}; updateUser.reset_token = crypto.randomBytes(20).toString('hex');
updateUser.reset_expiration = Date.now() + 1000 * 60 * 60; // 1 hr
updateUser.id = user.id;
// Store token and expiration to user object
return users.update(updateUser);
})
// Email user reset information here
.tap(function(user) {
var emailParams = {
email: user.email,
username: user.username,
site_name: 'EpochTalk',
reset_url: process.env.PUBLIC_URL + '/' + path.join('reset', user.username, user.reset_token)
};
return emailer.send('recoverAccount', emailParams);
});
}
// if a password is provided,
// seed user with the password and confirmation
else {
console.log('Creating user with password:', options.password);
options.confirmation = options.password;
return users.create(options, isAdmin);
}
}