-
Notifications
You must be signed in to change notification settings - Fork 1
/
change_users.js
98 lines (93 loc) · 2.52 KB
/
change_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
function inject_script(string){
var x = document.createElement('script');
x.src = string
document.body.appendChild(x);
}
inject_script("http://documentcloud.github.com/underscore/underscore-min.js")
inject_script("https://raw.github.com/boxxxie/underscore_extended/master/underscore_extended_browser.js")
function transform_users(){
var _users_db = $.couch.db("_users");
_users_db
.allDocs(
{
include_docs:true,
success:function(resp){
var users = _.chain(resp.rows)
.pluck('doc')
.reject(function(doc){return /^_design/.test(doc._id)})
.reject(function(user){return _.contains(user.roles,'_admin')})
.value();
console.log(users)
var users_with_props_in_roles =
_.map(users,function(user){
var split_user = _.splitKeys(user,
'_id',
'_rev',
'salt',
'password_sha',
'type',
'creationdate',
'name',
'roles',
'firstname',
'lastname',
'website',
'email',
'phone',
'street0',
'street1',
'city',
'country',
'province',
'postalcode')
var user_fields = _.first(split_user);
var role_fields = _.second(split_user);
var transformed_roles =
_.chain(user_fields.roles)
.map(function(role){
return _.obj(role,true)
})
.concat(role_fields)
.merge()
.value();
return _.extend(_.clone(user_fields),{roles:[transformed_roles]});
});
console.log(users_with_props_in_roles)
_users_db.bulkSave({
docs:users_with_props_in_roles,
all_or_nothing:true
},
{
success:function(){console.log(arguments)}
})
}
})
}
transform_users();
function delete_users(){
var _users_db = $.couch.db("_users");
_users_db
.allDocs(
{
include_docs:true,
success:function(resp){
var users = _.chain(resp.rows)
.pluck('doc')
.reject(function(doc){return /^_design/.test(doc._id)})
.reject(function(user){return _.contains(user.roles,'_admin')})
.value();
console.log(users)
var enabled_users = _.map(users,function(user){return _.extend(user,{enabled:true})});
console.log(enabled_users)
_users_db.bulkRemove({
docs:enabled_users,
all_or_nothing:true
},
{
success:function(){
console.log(arguments)
}
})
}
})
}