Skip to content

feature_ubhan_universalUID_changes #57

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 16, 2020
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
2 changes: 2 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module.exports = {
KAFKA_GROUP_DELETE_TOPIC: process.env.KAFKA_GROUP_DELETE_TOPIC || 'groups.notification.delete',
KAFKA_GROUP_MEMBER_ADD_TOPIC: process.env.KAFKA_GROUP_MEMBER_ADD_TOPIC || 'groups.notification.member.add',
KAFKA_GROUP_MEMBER_DELETE_TOPIC: process.env.KAFKA_GROUP_MEMBER_DELETE_TOPIC || 'groups.notification.member.delete',
KAFKA_GROUP_UNIVERSAL_MEMBER_ADD_TOPIC: process.env.KAFKA_GROUP_UNIVERSAL_MEMBER_ADD_TOPIC || 'groups.notification.universalmember.add',
KAFKA_GROUP_UNIVERSAL_MEMBER_DELETE_TOPIC: process.env.KAFKA_GROUP_UNIVERSAL_MEMBER_DELETE_TOPIC || 'groups.notification.universalmember.delete',

USER_ROLES: {
Admin: 'Administrator',
Expand Down
48 changes: 30 additions & 18 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,36 @@ async function ensureExists(tx, model, id, isAdmin = false) {
if (model === 'Group') {
if (validate(id, 4)) {
if (!isAdmin) {
res = await tx.run(`MATCH (e:${model} {id: {id}, status: '${constants.GroupStatus.Active}'}) RETURN e`, {
id
})
res = await tx.run(`MATCH (e:${model} {id: {id}, status: '${constants.GroupStatus.Active}'}) RETURN e`, {id})
} else {
res = await tx.run(`MATCH (e:${model} {id: {id}}) RETURN e`, {
id
})
res = await tx.run(`MATCH (e:${model} {id: {id}}) RETURN e`, {id})
}
} else {
if (!isAdmin) {
res = await tx.run(`MATCH (e:${model} {oldId: {id}, status: '${constants.GroupStatus.Active}'}) RETURN e`, {
id
})
} else {
res = await tx.run(`MATCH (e:${model} {oldId: {id}}) RETURN e`, {
id
})
res = await tx.run(`MATCH (e:${model} {oldId: {id}}) RETURN e`, {id})
}
}

if (res && res.records.length === 0) {
throw new errors.NotFoundError(`Not found ${model} of id ${id}`)
}
} else if (model === 'User') {
res = await tx.run(`MATCH (e:${model} {id: {id}}) RETURN e`, {
id
})
if (validate(id, 4)) {
res = await tx.run(`MATCH (e:${model} {universalUID: {id}}) RETURN e`, {id})

if (res && res.records.length === 0) {
res = await tx.run(`CREATE (user:User {id: {id}}) RETURN user`, { id })
if (res && res.records.length === 0) {
res = await tx.run(`CREATE (user:User {id: '00000000', universalUID: {id}}) RETURN user`, {id})
}
} else {
res = await tx.run(`MATCH (e:${model} {id: {id}}) RETURN e`, {id})

if (res && res.records.length === 0) {
res = await tx.run(`CREATE (user:User {id: {id}, universalUID: '00000000'}) RETURN user`, {id})
}
}
}

Expand All @@ -123,7 +123,7 @@ async function ensureGroupMember(session, groupId, userId) {
try {
const memberCheckRes = await session.run(
'MATCH (g:Group {id: {groupId}})-[r:GroupContains {type: {membershipType}}]->(u:User {id: {userId}}) RETURN r',
{ groupId, membershipType: config.MEMBERSHIP_TYPES.User, userId }
{groupId, membershipType: config.MEMBERSHIP_TYPES.User, userId}
)
if (memberCheckRes.records.length === 0) {
throw new errors.ForbiddenError(`User is not member of the group`)
Expand All @@ -143,7 +143,7 @@ async function getChildGroups(session, groupId) {
try {
const res = await session.run(
'MATCH (g:Group {id: {groupId}})-[r:GroupContains]->(c:Group) RETURN c ORDER BY c.oldId',
{ groupId }
{groupId}
)
return _.map(res.records, (record) => record.get(0).properties)
} catch (error) {
Expand All @@ -161,7 +161,7 @@ async function getParentGroups(session, groupId) {
try {
const res = await session.run(
'MATCH (g:Group)-[r:GroupContains]->(c:Group {id: {groupId}}) RETURN g ORDER BY g.oldId',
{ groupId }
{groupId}
)
return _.map(res.records, (record) => record.get(0).properties)
} catch (error) {
Expand All @@ -176,7 +176,7 @@ async function getParentGroups(session, groupId) {
* @returns {String} link for the page
*/
function getPageLink(req, page) {
const q = _.assignIn({}, req.query, { page })
const q = _.assignIn({}, req.query, {page})
return `${req.protocol}://${req.get('Host')}${req.baseUrl}${req.path}?${querystring.stringify(q)}`
}

Expand All @@ -188,6 +188,10 @@ function getPageLink(req, page) {
*/
function setResHeaders(req, res, result) {
const totalPages = Math.ceil(result.total / result.perPage)
if (result.page > 1) {
res.set('X-Prev-Page', result.page - 1)
}

if (result.page < totalPages) {
res.set('X-Next-Page', result.page + 1)
}
Expand All @@ -206,6 +210,14 @@ function setResHeaders(req, res, result) {
}
res.set('Link', link)
}

// Allow browsers access pagination data in headers
let accessControlExposeHeaders = res.get('Access-Control-Expose-Headers') || ''
accessControlExposeHeaders += accessControlExposeHeaders ? ', ' : ''
// append new values, to not override values set by someone else
accessControlExposeHeaders += 'X-Page, X-Per-Page, X-Total, X-Total-Pages, X-Prev-Page, X-Next-Page'

res.set('Access-Control-Expose-Headers', accessControlExposeHeaders)
}

/**
Expand Down
32 changes: 29 additions & 3 deletions src/controllers/GroupMembershipController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ async function deleteGroupMember(req, res) {
const result = await service.deleteGroupMember(
req.authUser.isMachine ? 'M2M' : req.authUser,
req.params.groupId,
req.params.memberId
req.params.memberId ? req.params.memberId : null,
Object.keys(req.query).length !== 0 ? req.query : null
)
res.send(result)
}
Expand All @@ -71,13 +72,36 @@ async function getGroupMembersCount(req, res) {
res.send(result)
}

/**
* Get list of mapping of groups and members count
* @param req the request
* @param res the response
*/
async function listGroupsMemberCount(req, res) {
const result = await service.listGroupsMemberCount(req.query)
res.send(result)
}

/**
* Get group members
* @param req the request
* @param res the response
*/
async function getMemberGroups(req, res) {
const result = await service.getMemberGroups(req.authUser.isMachine ? 'M2M' : req.authUser, req.params.memberId)
const result = await service.getMemberGroups(req.authUser.isMachine ? 'M2M' : req.authUser, req.params.memberId, {})
helper.setResHeaders(req, res, result)
res.send(result)
}

/**
* Get group members
* @param req the request
* @param res the response
*/
async function searchMemberGroups(req, res) {
console.log('sssss')
console.log(JSON.stringify(req.query))
const result = await service.getMemberGroups(req.authUser.isMachine ? 'M2M' : req.authUser, {}, req.query)
helper.setResHeaders(req, res, result)
res.send(result)
}
Expand All @@ -88,5 +112,7 @@ module.exports = {
getGroupMember,
deleteGroupMember,
getGroupMembersCount,
getMemberGroups
listGroupsMemberCount,
getMemberGroups,
searchMemberGroups
}
22 changes: 22 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ module.exports = {
auth: 'jwt',
access: [constants.UserRoles.Admin, constants.UserRoles.User],
scopes: ['write:groups', 'all:groups']
},
delete: {
controller: 'GroupMembershipController',
method: 'deleteGroupMember',
auth: 'jwt',
access: [constants.UserRoles.Admin],
scopes: ['write:groups', 'all:groups']
}
},
'/groups/:groupId/members/:memberId': {
Expand All @@ -97,6 +104,12 @@ module.exports = {
method: 'getGroupMembersCount'
}
},
'/groupsMemberCount': {
get: {
controller: 'GroupMembershipController',
method: 'listGroupsMemberCount'
}
},
'/groups/memberGroups/:memberId': {
get: {
controller: 'GroupMembershipController',
Expand All @@ -106,6 +119,15 @@ module.exports = {
scopes: ['read:groups']
}
},
'/groups/memberGroups/': {
get: {
controller: 'GroupMembershipController',
method: 'searchMemberGroups',
auth: 'jwt',
access: [constants.UserRoles.Admin, constants.UserRoles.User],
scopes: ['read:groups']
}
},
'/health': {
get: {
controller: 'HealthController',
Expand Down
Loading