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
|
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) and others /
// /
// All rights reserved. This program and the accompanying materials /
// are made available under the terms of the Apache License, Version 2.0 /
// which accompanies this distribution, and is available at /
// http://www.apache.org/licenses/LICENSE-2.0 /
/////////////////////////////////////////////////////////////////////////////////////////
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import * as R from 'ramda';
import { Roles } from 'meteor/alanning:roles';
import { Environments } from '/imports/api/environments/environments';
let userSchema = new SimpleSchema({
_id: { type: String },
username: { type: String },
password: { type: String },
viewEnvs: { type: [ String ] },
editEnvs: { type: [ String ] },
});
export const insert = new ValidatedMethod({
name: 'accounts.insert',
validate: userSchema
.pick([
'username',
'password',
'viewEnvs',
'viewEnvs.$',
'editEnvs',
'editEnvs.$',
]).validator({ clean: true, filter: false }),
run({
username,
password,
viewEnvs,
editEnvs,
}) {
if (! Roles.userIsInRole(Meteor.userId(), 'manage-users', Roles.GLOBAL_GROUP)) {
throw new Meteor.Error('unauthorized for removing users');
}
let userId = Accounts.createUser({
username: username,
password: password
});
addRole(viewEnvs, 'view-env', userId);
addRole(editEnvs, 'edit-env', userId);
}
});
export const update = new ValidatedMethod({
name: 'accounts.update',
validate: userSchema
.pick([
'_id',
// 'password',
'viewEnvs',
'viewEnvs.$',
'editEnvs',
'editEnvs.$',
]).validator({ clean: true, filter: false }),
run({
_id,
//_password,
viewEnvs,
editEnvs,
}) {
console.log('accounts - methods - update - start');
//throw new Meteor.Error('unimplemented');
if (! Roles.userIsInRole(Meteor.userId(), 'manage-users', Roles.GLOBAL_GROUP)) {
throw new Meteor.Error('unauthorized for updating users');
}
/*
let item = Meteor.users.findOne({ _id: _id });
console.log('user for update: ', item);
item = R.merge(R.pick([
'password',
], item), {
password
});
*/
/*
let item = {
//password
};
Meteor.users.update({ _id: _id }, { $set: item });
*/
let currentViewEnvs = R.map((env) => {
return env.name;
}, Environments.find({ 'auth.view-env': { $in: [ _id ] }}).fetch());
let viewEnvsForDelete = R.difference(currentViewEnvs, viewEnvs);
let viewEnvsForAdd = R.difference(viewEnvs, currentViewEnvs);
removeRole(viewEnvsForDelete, 'view-env', _id);
addRole(viewEnvsForAdd, 'view-env', _id);
//
let currentEditEnvs = R.map((env) => {
return env.name;
}, Environments.find({ 'auth.edit-env': { $in: [ _id ] }}).fetch());
let editEnvsForDelete = R.difference(currentEditEnvs, editEnvs);
let editEnvsForAdd = R.difference(editEnvs, currentEditEnvs);
removeRole(editEnvsForDelete, 'edit-env', _id);
addRole(editEnvsForAdd, 'edit-env', _id);
console.log('accounts - methods - update - end');
}
});
export const remove = new ValidatedMethod({
name: 'accounts.remove',
validate: userSchema
.pick([
'_id',
]).validator({ clean: true, filter: false }),
run({
_id
}) {
if (! Roles.userIsInRole(Meteor.userId(), 'manage-users', Roles.GLOBAL_GROUP)) {
throw new Meteor.Error('unauthorized for removing users');
}
let user = Meteor.users.findOne({ _id: _id });
console.log('user for remove: ', user);
Meteor.users.remove({ _id: _id });
}
});
function removeRole(rolesForRemoval, roleName, userId) {
R.forEach((envName) => {
let env = Environments.findOne({ name: envName });
let auth = env.auth;
if (R.isNil(auth)) { auth = { }; }
if (R.isNil(R.path([roleName], auth))) {
auth = R.assoc(roleName, [], auth);
}
auth = R.assoc(roleName, R.reject(R.equals(userId), auth[roleName]), auth);
updateEnv(auth, env);
//let newEnv = R.merge(env, { auth: auth });
}, rolesForRemoval);
}
function addRole(rolesForAdd, roleName, userId) {
R.forEach((envName) => {
let env = Environments.findOne({ name: envName });
let auth = env.auth;
if (R.isNil(auth)) { auth = { }; }
if (R.isNil(R.path([roleName], auth))) {
auth = R.assoc(roleName, [], auth);
}
auth = R.assoc(roleName, R.append(userId, auth[roleName]), auth);
updateEnv(auth, env);
//let newEnv = R.merge(env, { auth: auth });
}, rolesForAdd);
}
function updateEnv(auth, env) {
console.log('update env. set: ' + R.toString(auth));
try {
Environments.update(env._id, {
$set: {
auth: auth,
configuration: env.configuration,
//distribution: distribution,
//name: name,
type_drivers: env.type_drivers,
mechanism_drivers: env.mechanism_drivers,
listen: env.listen,
enable_monitoring: env.enable_monitoring,
}
});
} catch(e) {
console.error('error in update: ' + R.toString(e));
throw new Meteor.Error('enviornment update error',
`unable to update ACL for environment - ${env.name}. Please check envrironment info. ${e.message}`);
}
}
|