Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
search for policies and shared-policies by name
Browse files Browse the repository at this point in the history
  • Loading branch information
cianfoley-nearform committed Apr 3, 2018
1 parent a26ce53 commit 1cbff16
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/udaru-core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const Actions = {
ReadPolicy: 'authorization:policies:read',
DeletePolicy: 'authorization:policies:delete',
ListPolicies: 'authorization:policies:list',
SearchPolicies: 'authorization:policies:search',
AllPolicy: 'authorization:policies:*',

// authorization
Expand Down
3 changes: 2 additions & 1 deletion packages/udaru-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function buildUdaruCore (dbPool, config) {
createShared: hooks.wrap('policy:createShared', policyOps.createSharedPolicy),
updateShared: hooks.wrap('policy:updateShared', policyOps.updateSharedPolicy),
deleteShared: hooks.wrap('policy:deleteShared', policyOps.deleteSharedPolicy),
readShared: hooks.wrap('policy:readShared', policyOps.readSharedPolicy)
readShared: hooks.wrap('policy:readShared', policyOps.readSharedPolicy),
search: hooks.wrap('policy:search', policyOps.search)
},

teams: {
Expand Down
45 changes: 45 additions & 0 deletions packages/udaru-core/lib/ops/policyOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,50 @@ function buildPolicyOps (db, config) {
return promise
},

/**
* Search for policies
*
* @param {Object} params { organizationId, query, type } "type" is optional, default is organization wide search
* @param {Function} cb
*/
search: function search (params, cb) {
let promise = null
if (typeof cb !== 'function') [promise, cb] = asyncify('data', 'total')

const { organizationId, query, type } = params
Joi.validate({ organizationId, query, type }, validationRules.searchPolicy, function (err) {
if (err) {
return cb(Boom.badRequest(err))
}

const sqlQuery = SQL`
SELECT *
FROM policies
WHERE (
to_tsvector(name) @@ to_tsquery(${query.split(' ').join(' & ') + ':*'})
OR name LIKE(${'%' + query + '%'})
)
`

if (type !== 'all') {
if (type === 'shared') {
sqlQuery.append(SQL` AND org_id is NULL`)
} else {
sqlQuery.append(SQL` AND org_id=${organizationId}`)
}
}

sqlQuery.append(SQL` ORDER BY name;`)

db.query(sqlQuery, (err, result) => {
if (err) return cb(Boom.badImplementation(err))
return cb(null, result.rows.map(mapping.policy), result.rows.length)
})
})

return promise
},

deleteAllPolicyByIds: function deleteAllPolicyByIds (client, ids, orgId, cb) {
let promise = null
if (typeof cb !== 'function') [promise, cb] = asyncify()
Expand Down Expand Up @@ -691,6 +735,7 @@ function buildPolicyOps (db, config) {
policyOps.createPolicy.validate = validationRules.createPolicy
policyOps.updatePolicy.validate = validationRules.updatePolicy
policyOps.deletePolicy.validate = validationRules.deletePolicy
policyOps.search.validate = validationRules.searchPolicy

policyOps.listSharedPolicies.validate = validationRules.listSharedPolicies
policyOps.readSharedPolicy.validate = validationRules.readSharedPolicy
Expand Down
5 changes: 5 additions & 0 deletions packages/udaru-core/lib/ops/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ const policies = {
version: validationRules.version,
name: validationRules.policyName,
statements: validationRules.statements
},
searchPolicy: {
organizationId: validationRules.organizationId,
query: requiredString,
type: Joi.string().optional().allow('shared', 'organization', 'all').description('Flag to denote search for shared policy, defaults to organization wide search')
}
}

Expand Down
76 changes: 76 additions & 0 deletions packages/udaru-hapi-plugin/routes/public/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,82 @@ exports.register = function (server, options, next) {
}
})

server.route({
method: 'GET',
path: '/authorization/policies/search',
handler: function (request, reply) {
const { organizationId } = request.udaru
const query = request.query.query

request.udaruCore.policies.search({
organizationId,
query,
type: 'organization'
}, (err, data, total) => {
reply(
err,
err ? null : {
data,
total
}
)
})
},
config: {
description: 'Search for organization policies',
notes: 'The GET /authorization/policies/search endpoint returns a filtered list of policies.\n\n',
tags: ['api', 'teams'],
plugins: {
auth: {
action: Action.SearchPolicies
}
},
validate: {
headers,
query: _.pick(validation.searchPolicy, ['query'])
},
response: { schema: swagger.PagedPolicies }
}
})

server.route({
method: 'GET',
path: '/authorization/shared-policies/search',
handler: function (request, reply) {
const { organizationId } = request.udaru
const query = request.query.query

request.udaruCore.policies.search({
organizationId,
query,
type: 'shared'
}, (err, data, total) => {
reply(
err,
err ? null : {
data,
total
}
)
})
},
config: {
description: 'Search for shared policies',
notes: 'The GET /authorization/shared-policies/search endpoint returns a filtered list of shared policies.\n\n',
tags: ['api', 'teams'],
plugins: {
auth: {
action: Action.SearchPolicies
}
},
validate: {
headers,
query: _.pick(validation.searchPolicy, ['query'])
},
response: { schema: swagger.PagedPolicies }
}
})

server.route({
method: 'GET',
path: '/authorization/shared-policies/{id}',
Expand Down

0 comments on commit 1cbff16

Please sign in to comment.