forked from complex-media/InstaAdminCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder-users.js
105 lines (92 loc) · 2.62 KB
/
seeder-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
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
var User = require('./models/user.js');
var mongoose = require( 'mongoose' );
var config = require('./config/index.js');
var Person = mongoose.model('acl_resources', new mongoose.Schema());
console.log('Seeder starting.');
var userset = config.defaultUsers;
var useridstrings = [];
var i = 0;
//create default users from list above
var addUserfn = function(fin){
console.log(' Adding ' + userset[i].email);
User.findOne({ 'local.email' : userset[i].email }, function(err, user) {
var name = 'defaultfakenamekey';
if(user){
name = (user._id.toString() + '-' + user.local.email).replace(/\./g,'%2E').replace("@",'%40');
useridstrings.push(name);
}
Person.collection.remove({_bucketname:'users',key:name}, function(err,pers) {
if(err){
console.log(err);
}
if(user){
user.remove(function(err,cat){
var newUser = new User();
newUser.local.email = userset[i].email;
newUser.local.password = newUser.generateHash(userset[i].password);
// save the user
newUser.save(function(err) {
if (err)
console.log(err);
i++;
if(typeof userset[i] != 'undefined'){
addUserfn(fin);
}else{
if(typeof fin == 'undefined'){
lastfn();
}else{
removeRoleUsers(fin);
}
}
});
});
} else {
var newUser = new User();
newUser.local.email = userset[i].email;
newUser.local.password = newUser.generateHash(userset[i].password);
// save the user
newUser.save(function(err) {
if (err)
console.log(err);
i++;
if(typeof userset[i] != 'undefined'){
addUserfn(fin);
}else{
if(typeof fin == 'undefined'){
lastfn();
}else{
removeRoleUsers(fin);
}
}
});
}
// Person.collection.findOne({_bucketname:'roles',key:name})
});
});
};
var lastfn = function(){
console.log('Seeder Ending.');
process.exit();
};
//cleans up acl_resource default users
var removeRoleUsers = function(fin){
var build = {};
useridstrings.forEach(function(name){
build[name] = '';
});
Person.collection.update({}, {'$unset':build},{multi:true} , function(err,docs) {
addBaseRoles(fin)
});
}
// gives any users that exist a default role that exist in acl_resource if they have none
var addBaseRoles = function(fin){
Person.collection.update({'$and': [{_bucketname:'users' },{owner:{'$ne':true}},{admin:{'$ne':true}},{user:{'$ne':true}}]},{'$set':{user:true}},{multi:true},function(err,resp){
fin();
});
}
module.exports = {
addUserfn:function(fn){
console.log('Seeding Users');
addUserfn(fn);
}
}