Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 33bdfbd

Browse files
authored
Merge pull request #493 from appirio-tech/dev
Merge dev to master (admin updates)
2 parents caa2fb6 + 3f3a1de commit 33bdfbd

File tree

102 files changed

+6038
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+6038
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ test/tmp/design_tmp_submissions/*.zip
1010
test/tmp/memberPhoto/*
1111
.idea
1212
.settings
13+
**/jdk-8u51-linux-x64.gz
14+
*.swp

actions/admins.js

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*jslint nomen: true */
2+
/*
3+
* Copyright (C) 2016 TopCoder Inc., All Rights Reserved.
4+
*
5+
* @version 1.0
6+
* @author TCSCODER
7+
*/
8+
"use strict";
9+
var _ = require('underscore');
10+
var async = require('async');
11+
var DuplicateResourceError = require('../errors/DuplicateResourceError');
12+
13+
/**
14+
* This is the function that will actually get all admins.
15+
*
16+
* @param {Object} api The api object that is used to access the global infrastructure
17+
* @param {Object} connection The connection object for the current request
18+
* @param {Object} dbConnectionMap The database connection map for the current request
19+
* @param {Function} next The callback to be called after this function is done
20+
*/
21+
var getAdmins = function (api, connection, dbConnectionMap, next) {
22+
var helper = api.helper;
23+
async.waterfall([function (cb) {
24+
cb(helper.checkAdmin(connection, "You need to login for this api.", 'You don\'t have access to this api.'));
25+
}, function (cb) {
26+
api.dataAccess.executeQuery("get_admins", {}, dbConnectionMap, cb);
27+
}, function (result, cb) {
28+
var ret = {}, i, entity, type, id;
29+
for (i = 0; i < result.length; i = i + 1) {
30+
type = result[i].type.trim();
31+
id = result[i].user_id;
32+
if (!ret[id]) {
33+
ret[id] = {
34+
id: result[i].user_id,
35+
name: result[i].handle,
36+
adminGroup: false,
37+
adminRole: false,
38+
managerResource: false
39+
};
40+
}
41+
entity = ret[id];
42+
if (type === 'Admin Group') {
43+
entity.adminGroup = true;
44+
} else if (type === 'Admin Role') {
45+
entity.adminRole = true;
46+
} else if (type === 'Manager Resource') {
47+
entity.managerResource = true;
48+
}
49+
}
50+
cb(null, {
51+
allAdmins: _.values(ret)
52+
});
53+
}], function (err, result) {
54+
if (err) {
55+
helper.handleError(api, connection, err);
56+
} else {
57+
connection.response = result;
58+
}
59+
next(connection, true);
60+
});
61+
};
62+
63+
/**
64+
* The API for getting all admins
65+
*/
66+
exports.admins = {
67+
name: "admins",
68+
description: "retrieve all TopCoder admins",
69+
inputs: {
70+
required: [],
71+
optional: []
72+
},
73+
blockedConnectionTypes: [],
74+
outputExample: {},
75+
version: 'v2',
76+
cacheEnabled: false,
77+
transaction: 'read', // this action is read-only
78+
databases: ['tcs_catalog'],
79+
run: function (api, connection, next) {
80+
if (connection.dbConnectionMap) {
81+
api.log("Execute admins#run", 'debug');
82+
getAdmins(api, connection, connection.dbConnectionMap, next);
83+
} else {
84+
api.helper.handleNoConnection(api, connection, next);
85+
}
86+
}
87+
};
88+
89+
/**
90+
* This is the function that will actually create admin.
91+
*
92+
* @param {Object} api The api object that is used to access the global infrastructure
93+
* @param {Object} connection The connection object for the current request
94+
* @param {Object} dbConnectionMap The database connection map for the current request
95+
* @param {Function} next The callback to be called after this function is done
96+
*/
97+
var createAdmin = function (api, connection, dbConnectionMap, next) {
98+
var helper = api.helper, username = connection.params.username, userId, operatorId, parameters,
99+
result = {
100+
success: true
101+
};
102+
async.waterfall([function (cb) {
103+
cb(helper.checkAdmin(connection, "You need to login for this api.", 'You don\'t have access to this api.'));
104+
}, function (cb) {
105+
operatorId = connection.caller.userId;
106+
helper.validateUserAndGetUserId(username, dbConnectionMap, cb);
107+
}, function (id, cb) {
108+
userId = id;
109+
async.auto({
110+
nextUserGroupId: function (ca) {
111+
api.dataAccess.executeQuery("get_next_admin_user_group_id", {}, dbConnectionMap, ca);
112+
},
113+
nextResourceId: function (ca) {
114+
api.dataAccess.executeQuery("get_next_admin_resource_id", {}, dbConnectionMap, ca);
115+
}
116+
}, cb);
117+
}, function (results, cb) {
118+
parameters = {
119+
userId: userId,
120+
userGroupId: results.nextUserGroupId[0].next_id,
121+
operatorId: operatorId,
122+
resourceId: results.nextResourceId[0].next_id
123+
};
124+
api.dataAccess.executeQuery("insert_admin_group", parameters, dbConnectionMap, function (err) {
125+
if (helper.isDuplicateResourceError(err)) {
126+
cb(new DuplicateResourceError("User " + username + " has already been added to Admin Group", err));
127+
} else {
128+
cb(err);
129+
}
130+
});
131+
}, function (cb) {
132+
api.dataAccess.executeQuery("clear_user_rating", parameters, dbConnectionMap, function (err) {
133+
cb(err);
134+
});
135+
}, function (cb) {
136+
api.dataAccess.executeQuery("get_admin_resource", {
137+
userId: userId
138+
}, dbConnectionMap, cb);
139+
}, function (resourceIds, cb) {
140+
if (!resourceIds || !resourceIds.length) {
141+
api.dataAccess.executeQuery("insert_new_admin_resource", parameters, dbConnectionMap, function (err) {
142+
if (err) {
143+
return cb(err);
144+
}
145+
api.dataAccess.executeQuery("insert_new_admin_resource_info", parameters, dbConnectionMap, function (err) {
146+
cb(err);
147+
});
148+
});
149+
} else {
150+
cb(null);
151+
}
152+
}, function (cb) {
153+
api.dataAccess.executeQuery("insert_admin_role", parameters, dbConnectionMap, function (err) {
154+
if (helper.isDuplicateResourceError(err)) {
155+
cb(new DuplicateResourceError("User " + username + " has already been assigned Admin role", err));
156+
} else {
157+
cb(err);
158+
}
159+
});
160+
}], function (err) {
161+
if (err) {
162+
helper.handleError(api, connection, err);
163+
} else {
164+
result.message = username + " has been successfully added as TopCoder Admin";
165+
connection.response = result;
166+
}
167+
next(connection, true);
168+
});
169+
170+
};
171+
172+
/**
173+
* The API for creating admin
174+
*/
175+
exports.createAdmin = {
176+
name: "createAdmin",
177+
description: "create TopCoder admin",
178+
inputs: {
179+
required: ['username'],
180+
optional: []
181+
},
182+
blockedConnectionTypes: [],
183+
outputExample: {},
184+
version: 'v2',
185+
cacheEnabled: false,
186+
transaction: 'write',
187+
databases: ['tcs_catalog', 'common_oltp'],
188+
run: function (api, connection, next) {
189+
if (connection.dbConnectionMap) {
190+
api.log("Execute createAdmin#run", 'debug');
191+
createAdmin(api, connection, connection.dbConnectionMap, next);
192+
} else {
193+
api.helper.handleNoConnection(api, connection, next);
194+
}
195+
}
196+
};
197+
198+
/**
199+
* This is the function that will actually remove admin.
200+
*
201+
* @param {Object} api The api object that is used to access the global infrastructure
202+
* @param {Object} connection The connection object for the current request
203+
* @param {Object} dbConnectionMap The database connection map for the current request
204+
* @param {Function} next The callback to be called after this function is done
205+
*/
206+
var removeAdmin = function (api, connection, dbConnectionMap, next) {
207+
var helper = api.helper, username = connection.params.username, operatorId, parameters,
208+
result = {
209+
success: true
210+
};
211+
async.waterfall([function (cb) {
212+
cb(helper.checkAdmin(connection, "You need to login for this api.", 'You don\'t have access to this api.'));
213+
}, function (cb) {
214+
operatorId = connection.caller.userId;
215+
helper.validateUserAndGetUserId(username, dbConnectionMap, cb);
216+
}, function (userId, cb) {
217+
parameters = {
218+
userId: userId,
219+
operatorId: operatorId
220+
};
221+
api.dataAccess.executeQuery("remove_admin_group", parameters, dbConnectionMap, function (err) {
222+
cb(err);
223+
});
224+
}, function (cb) {
225+
api.dataAccess.executeQuery("remove_admin_resource_info", parameters, dbConnectionMap, function (err) {
226+
cb(err);
227+
});
228+
}, function (cb) {
229+
api.dataAccess.executeQuery("remove_admin_resource", parameters, dbConnectionMap, function (err) {
230+
cb(err);
231+
});
232+
}, function (cb) {
233+
api.dataAccess.executeQuery("remove_admin_role", parameters, dbConnectionMap, function (err) {
234+
cb(err);
235+
});
236+
}], function (err) {
237+
if (err) {
238+
helper.handleError(api, connection, err);
239+
} else {
240+
result.message = "TopCoder Admin: " + username + " has been successfully removed";
241+
connection.response = result;
242+
}
243+
next(connection, true);
244+
});
245+
246+
};
247+
248+
/**
249+
* The API for removing admin
250+
*/
251+
exports.removeAdmin = {
252+
name: "removeAdmin",
253+
description: "remove TopCoder admin",
254+
inputs: {
255+
required: ['username'],
256+
optional: []
257+
},
258+
blockedConnectionTypes: [],
259+
outputExample: {},
260+
version: 'v2',
261+
cacheEnabled: false,
262+
transaction: 'write',
263+
databases: ['tcs_catalog', 'common_oltp'],
264+
run: function (api, connection, next) {
265+
if (connection.dbConnectionMap) {
266+
api.log("Execute removeAdmin#run", 'debug');
267+
removeAdmin(api, connection, connection.dbConnectionMap, next);
268+
} else {
269+
api.helper.handleNoConnection(api, connection, next);
270+
}
271+
}
272+
};

0 commit comments

Comments
 (0)