Skip to content

set SSO flag for invite events #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"connectUrl": "CONNECT_URL",
"accountsAppUrl": "ACCOUNTS_APP_URL",
"inviteEmailSubject": "INVITE_EMAIL_SUBJECT",
"inviteEmailSectionTitle": "INVITE_EMAIL_SECTION_TITLE"
"inviteEmailSectionTitle": "INVITE_EMAIL_SECTION_TITLE",
"SSO_REFCODES": "SSO_REFCODES"
}
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"connectUrl":"https://connect.topcoder-dev.com",
"accountsAppUrl": "https://accounts.topcoder-dev.com",
"MAX_REVISION_NUMBER": 100,
"UNIQUE_GMAIL_VALIDATION": false
"UNIQUE_GMAIL_VALIDATION": false,
"SSO_REFCODES": "[]"
}
138 changes: 81 additions & 57 deletions src/events/busApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EVENT, BUS_API_EVENT, PROJECT_STATUS, PROJECT_PHASE_STATUS, PROJECT_MEM
from '../constants';
import { createEvent } from '../services/busApi';
import models from '../models';
import getTopcoderProjectMembers from '../util';
import util from '../util';

/**
* Map of project status and event name sent to bus api
Expand Down Expand Up @@ -367,7 +367,7 @@ module.exports = (app, logger) => {
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
allowedUsers: created.status === PROJECT_PHASE_STATUS.DRAFT ?
getTopcoderProjectMembers(project.members) : null,
util.getTopcoderProjectMembers(project.members) : null,
}, logger);
return sendPlanReadyEventIfNeeded(req, project, created);
}).catch(err => null); // eslint-disable-line no-unused-vars
Expand All @@ -393,7 +393,7 @@ module.exports = (app, logger) => {
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
allowedUsers: deleted.status === PROJECT_PHASE_STATUS.DRAFT ?
getTopcoderProjectMembers(project.members) : null,
util.getTopcoderProjectMembers(project.members) : null,
}, logger);
}).catch(err => null); // eslint-disable-line no-unused-vars
});
Expand Down Expand Up @@ -446,7 +446,7 @@ module.exports = (app, logger) => {
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
allowedUsers: updated.status === PROJECT_PHASE_STATUS.DRAFT ?
getTopcoderProjectMembers(project.members) : null,
util.getTopcoderProjectMembers(project.members) : null,
}, logger));
events.forEach((event) => { eventsMap[event] = true; });
}
Expand Down Expand Up @@ -493,7 +493,7 @@ module.exports = (app, logger) => {
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
allowedUsers: updated.status === PROJECT_PHASE_STATUS.DRAFT ?
getTopcoderProjectMembers(project.members) : null,
util.getTopcoderProjectMembers(project.members) : null,
}, logger);
}
}).catch(err => null); // eslint-disable-line no-unused-vars
Expand Down Expand Up @@ -700,63 +700,87 @@ module.exports = (app, logger) => {
app.on(EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_CREATED, ({ req, userId, email, status, role }) => {
logger.debug('receive PROJECT_MEMBER_INVITE_CREATED event');
const projectId = _.parseInt(req.params.projectId);

if (status === INVITE_STATUS.REQUESTED) {
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_REQUESTED, {
projectId,
userId,
email,
role,
initiatorUserId: req.authUser.userId,
}, logger);
} else {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_CREATED, {
projectId,
userId,
email,
role,
initiatorUserId: req.authUser.userId,
}, logger);
}
models.Project.findOne({
where: { id: projectId },
})
.then((project) => {
logger.debug(util.isSSO);
if (status === INVITE_STATUS.REQUESTED) {
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_REQUESTED, {
projectId,
userId,
email,
role,
initiatorUserId: req.authUser.userId,
isSSO: util.isSSO(project),
}, logger);
} else {
// send event to bus api
logger.debug(JSON.stringify({
projectId,
userId,
email,
role,
initiatorUserId: req.authUser.userId,
isSSO: util.isSSO(project),
}));
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_CREATED, {
projectId,
userId,
email,
role,
initiatorUserId: req.authUser.userId,
isSSO: util.isSSO(project),
}, logger);
}
}).catch(err => logger.error(err)); // eslint-disable-line no-unused-vars
});

app.on(EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_UPDATED, ({ req, userId, email, status, role, createdBy }) => {
logger.debug('receive PROJECT_MEMBER_INVITE_UPDATED event');
const projectId = _.parseInt(req.params.projectId);

if (status === INVITE_STATUS.REQUEST_APPROVED) {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_APPROVED, {
projectId,
userId,
originator: createdBy,
email,
role,
status,
initiatorUserId: req.authUser.userId,
}, logger);
} else if (status === INVITE_STATUS.REQUEST_REJECTED) {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_REJECTED, {
projectId,
userId,
originator: createdBy,
email,
role,
status,
initiatorUserId: req.authUser.userId,
}, logger);
} else {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_UPDATED, {
projectId,
userId,
email,
role,
status,
initiatorUserId: req.authUser.userId,
}, logger);
}
models.Project.findOne({
where: { id: projectId },
})
.then((project) => {
logger.debug(util.isSSO);
if (status === INVITE_STATUS.REQUEST_APPROVED) {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_APPROVED, {
projectId,
userId,
originator: createdBy,
email,
role,
status,
initiatorUserId: req.authUser.userId,
isSSO: util.isSSO(project),
}, logger);
} else if (status === INVITE_STATUS.REQUEST_REJECTED) {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_REJECTED, {
projectId,
userId,
originator: createdBy,
email,
role,
status,
initiatorUserId: req.authUser.userId,
isSSO: util.isSSO(project),
}, logger);
} else {
// send event to bus api
createEvent(BUS_API_EVENT.PROJECT_MEMBER_INVITE_UPDATED, {
projectId,
userId,
email,
role,
status,
initiatorUserId: req.authUser.userId,
isSSO: util.isSSO(project),
}, logger);
}
}).catch(err => null); // eslint-disable-line no-unused-vars
});
};
2 changes: 2 additions & 0 deletions src/routes/projectMemberInvites/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ describe('Project Member Invite create', () => {
projectId: project1.id,
userId: 3,
email: null,
isSSO: false,
})).should.be.true;
done();
});
Expand Down Expand Up @@ -888,6 +889,7 @@ describe('Project Member Invite create', () => {
projectId: project1.id,
userId: null,
email: 'hello@world.com',
isSSO: false,
})).should.be.true;
done();
});
Expand Down
1 change: 1 addition & 0 deletions src/routes/projectMemberInvites/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ describe('Project member invite update', () => {
userId: invite1.userId,
status: INVITE_STATUS.ACCEPTED,
email: null,
isSSO: false,
})).should.be.true;
createEventSpy.secondCall.calledWith(BUS_API_EVENT.MEMBER_JOINED, sinon.match({
projectId: project1.id,
Expand Down
9 changes: 9 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const m2m = tcCoreLibAuth.m2m(config);

const util = _.cloneDeep(require('tc-core-library-js').util(config));

const ssoRefCodes = JSON.parse(config.get('SSO_REFCODES'));

// the client modifies the config object, so always passed the cloned object
let esClient = null;

Expand Down Expand Up @@ -468,6 +470,13 @@ _.assignIn(util, {
*/
getTopcoderProjectMembers: members => _(members).filter(m => m.role !== PROJECT_MEMBER_ROLE.CUSTOMER),

/**
* Check if project is for SSO users
* @param {Object} project project
* @return {Boolean} is SSO project
*/
isSSO: project => ssoRefCodes.indexOf(_.get(project, 'details.utm.code')) > -1,

/**
* Check if the following model exist
* @param {Object} keyInfo key information, it includes version and key
Expand Down