Skip to content

Commit 50b2087

Browse files
author
Vikas Agarwal
committed
Github issue#1243, Connect Admin role
— refactoring
1 parent 50f60aa commit 50b2087

File tree

9 files changed

+40
-18
lines changed

9 files changed

+40
-18
lines changed

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export const USER_ROLE = {
3333
CONNECT_ADMIN: 'Connect Admin',
3434
};
3535

36+
export const ADMIN_ROLES = [USER_ROLE.CONNECT_ADMIN, USER_ROLE.TOPCODER_ADMIN];
37+
3638

3739
export const EVENT = {
3840
ROUTING_KEY: {

src/permissions/project.delete.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import _ from 'lodash';
55
import util from '../util';
66
import models from '../models';
7-
import { USER_ROLE, PROJECT_MEMBER_ROLE } from '../constants';
7+
import { PROJECT_MEMBER_ROLE } from '../constants';
88

99
/**
1010
* Super admin, Topcoder Managers are allowed to edit any project
@@ -20,8 +20,7 @@ module.exports = freq => new Promise((resolve, reject) => {
2020
req.context = req.context || {};
2121
req.context.currentProjectMembers = members;
2222
// check if auth user has acecss to this project
23-
const hasAccess = util.hasRole(req, USER_ROLE.TOPCODER_ADMIN) ||
24-
util.hasRole(req, USER_ROLE.CONNECT_ADMIN) ||
23+
const hasAccess = util.hasAdminRole(req) ||
2524
!_.isUndefined(_.find(members, m => m.userId === req.authUser.userId &&
2625
((m.role === PROJECT_MEMBER_ROLE.CUSTOMER && m.isPrimary) ||
2726
m.role === PROJECT_MEMBER_ROLE.MANAGER)));

src/permissions/project.edit.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module.exports = freq => new Promise((resolve, reject) => {
1919
req.context = req.context || {};
2020
req.context.currentProjectMembers = members;
2121
// check if auth user has acecss to this project
22-
const hasAccess = util.hasRole(req, USER_ROLE.TOPCODER_ADMIN)
23-
|| util.hasRole(req, USER_ROLE.CONNECT_ADMIN)
22+
const hasAccess = util.hasAdminRole(req)
2423
|| util.hasRole(req, USER_ROLE.MANAGER)
2524
|| !_.isUndefined(_.find(members, m => m.userId === req.authUser.userId));
2625

src/permissions/project.view.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ module.exports = freq => new Promise((resolve, reject) => {
2020
req.context = req.context || {};
2121
req.context.currentProjectMembers = members;
2222
// check if auth user has acecss to this project
23-
const hasAccess = util.hasRole(req, USER_ROLE.TOPCODER_ADMIN)
24-
|| util.hasRole(req, USER_ROLE.CONNECT_ADMIN)
23+
const hasAccess = util.hasAdminRole(req)
2524
|| util.hasRole(req, USER_ROLE.MANAGER)
2625
|| !_.isUndefined(_.find(members, m => m.userId === currentUserId));
2726

src/permissions/projectMember.delete.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import _ from 'lodash';
22
import util from '../util';
33
import models from '../models';
44
import {
5-
USER_ROLE,
65
PROJECT_MEMBER_ROLE,
76
} from '../constants';
87

@@ -25,8 +24,7 @@ module.exports = freq => new Promise((resolve, reject) => {
2524
const prjMemberId = _.parseInt(req.params.id);
2625
const memberToBeRemoved = _.find(members, m => m.id === prjMemberId);
2726
// check if auth user has acecss to this project
28-
const hasAccess = util.hasRole(req, USER_ROLE.TOPCODER_ADMIN)
29-
|| util.hasRole(req, USER_ROLE.CONNECT_ADMIN)
27+
const hasAccess = util.hasAdminRole(req)
3028
|| (authMember && memberToBeRemoved && (authMember.role === PROJECT_MEMBER_ROLE.MANAGER ||
3129
(authMember.role === PROJECT_MEMBER_ROLE.CUSTOMER && authMember.isPrimary &&
3230
memberToBeRemoved.role === PROJECT_MEMBER_ROLE.CUSTOMER) ||

src/routes/projects/list-db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ module.exports = [
123123
req.log.debug(criteria);
124124

125125
if (!memberOnly
126-
&& (util.hasRole(req, USER_ROLE.TOPCODER_ADMIN)
126+
&& (util.hasAdminRole(req)
127127
|| util.hasRole(req, USER_ROLE.MANAGER))) {
128128
// admins & topcoder managers can see all projects
129129
return retrieveProjects(req, criteria, sort, req.query.fields)

src/routes/projects/list.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ module.exports = [
217217
req.log.info(criteria);
218218

219219
if (!memberOnly
220-
&& (util.hasRole(req, USER_ROLE.TOPCODER_ADMIN)
221-
|| util.hasRole(req, USER_ROLE.CONNECT_ADMIN)
220+
&& (util.hasAdminRole(req)
222221
|| util.hasRole(req, USER_ROLE.MANAGER))) {
223222
// admins & topcoder managers can see all projects
224223
return retrieveProjects(req, criteria, sort, req.query.fields)

src/routes/projects/update.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const updateProjectValdiations = {
8282
};
8383

8484
// NOTE- decided to disable all additional checks for now.
85-
const validateUpdates = (existingProject, updatedProps, authUser) => {
85+
const validateUpdates = (existingProject, updatedProps, req) => {
8686
const errors = [];
8787
switch (existingProject.status) {
8888
case PROJECT_STATUS.COMPLETED:
@@ -101,7 +101,7 @@ const validateUpdates = (existingProject, updatedProps, authUser) => {
101101
// }
102102
}
103103
if (_.has(updatedProps, 'directProjectId') &&
104-
_.intersection(authUser.roles, [USER_ROLE.MANAGER, USER_ROLE.TOPCODER_ADMIN]).length === 0) {
104+
!util.hasRoles(req, [USER_ROLE.MANAGER, USER_ROLE.TOPCODER_ADMIN])) {
105105
errors.push('Don\'t have permission to update \'directProjectId\' property');
106106
}
107107

@@ -142,7 +142,7 @@ module.exports = [
142142
}
143143
previousValue = _.clone(project.get({ plain: true }));
144144
// run additional validations
145-
const validationErrors = validateUpdates(previousValue, updatedProps, req.authUser);
145+
const validationErrors = validateUpdates(previousValue, updatedProps, req);
146146
if (validationErrors.length > 0) {
147147
const err = new Error('Unable to update project');
148148
_.assign(err, {
@@ -160,7 +160,7 @@ module.exports = [
160160
].map(x => x.toLowerCase());
161161
const matchRole = role => _.indexOf(validRoles, role.toLowerCase()) >= 0;
162162
if (updatedProps.status === PROJECT_STATUS.ACTIVE &&
163-
(!util.hasRole(req, USER_ROLE.TOPCODER_ADMIN) || !util.hasRole(req, USER_ROLE.CONNECT_ADMIN)) &&
163+
!util.hasAdminRole(req) &&
164164
_.isUndefined(_.find(members,
165165
m => m.userId === req.authUser.userId && matchRole(m.role)))
166166
) {

src/util.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import urlencode from 'urlencode';
1717
import elasticsearch from 'elasticsearch';
1818
import Promise from 'bluebird';
1919
import AWS from 'aws-sdk';
20+
import { ADMIN_ROLES } from './constants';
2021

2122
const exec = require('child_process').exec;
2223
const models = require('./models').default;
@@ -73,6 +74,27 @@ _.assignIn(util, {
7374
roles = roles.map(s => s.toLowerCase());
7475
return _.indexOf(roles, role.toLowerCase()) >= 0;
7576
},
77+
/**
78+
* Helper funtion to verify if user has specified roles
79+
* @param {object} req Request object that should contain authUser
80+
* @param {Array} roles specified roles
81+
* @return {boolean} true/false
82+
*/
83+
hasRoles: (req, roles) => {
84+
let authRoles = _.get(req, 'authUser.roles', []);
85+
authRoles = authRoles.map(s => s.toLowerCase());
86+
return _.intersection(authRoles, roles.map(r => r.toLowerCase())).length > 0;
87+
},
88+
/**
89+
* Helper funtion to verify if user has admin roles
90+
* @param {object} req Request object that should contain authUser
91+
* @return {boolean} true/false
92+
*/
93+
hasAdminRole: (req) => {
94+
let roles = _.get(req, 'authUser.roles', []);
95+
roles = roles.map(s => s.toLowerCase());
96+
return _.intersection(roles, ADMIN_ROLES.map(r => r.toLowerCase())).length > 0;
97+
},
7698

7799
/**
78100
* Parses query fields and groups them per table
@@ -206,6 +228,7 @@ _.assignIn(util, {
206228
getSystemUserToken: (logger, id = 'system') => {
207229
const httpClient = util.getHttpClient({ id, log: logger });
208230
const url = `${config.get('identityServiceEndpoint')}authorizations`;
231+
console.log(url, 'url');
209232
const formData = `clientId=${config.get('systemUserClientId')}&` +
210233
`secret=${encodeURIComponent(config.get('systemUserClientSecret'))}`;
211234
return httpClient.post(url, formData,
@@ -269,8 +292,11 @@ _.assignIn(util, {
269292
*/
270293
getMemberDetailsByUserIds: Promise.coroutine(function* (userIds, logger, requestId) { // eslint-disable-line func-names
271294
try {
272-
const token = yield this.getSystemUserToken(logger);
295+
console.log('getMemberDetailsByUserIds');
296+
const token = yield 'farzi';// this.getSystemUserToken(logger);
297+
console.log('token', token);
273298
const httpClient = this.getHttpClient({ id: requestId, log: logger });
299+
console.log(config.memberServiceEndpoint, 'config.memberServiceEndpoint');
274300
return httpClient.get(`${config.memberServiceEndpoint}/_search`, {
275301
params: {
276302
query: `${userIds.join(urlencode(' OR ', 'utf8'))}`,

0 commit comments

Comments
 (0)