Skip to content

Commit

Permalink
Include permissions to the result
Browse files Browse the repository at this point in the history
  • Loading branch information
jatindersingh93 committed Dec 9, 2023
1 parent a07a0c8 commit 533bc7c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
7 changes: 6 additions & 1 deletion app/src/controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,12 @@ const controller = {
public: isTruthy(req.query.public),
active: isTruthy(req.query.active),
deleteMarker: isTruthy(req.query.deleteMarker),
latest: isTruthy(req.query.latest)
latest: isTruthy(req.query.latest),
page: req.query.page,
limit: req.query.limit,
sort: req.query.sort,
order: req.query.order ? req.query.order : 'asc',
permissions: isTruthy(req.query.permissions)
};
// if scoping to current user permissions on objects
if (config.has('server.privacyMask')) {
Expand Down
7 changes: 7 additions & 0 deletions app/src/db/models/tables/objectModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ class ObjectModel extends Timestamps(Model) {
});
});
}
},
pagination(query, page, limit){
query.withGraphFetched('objectPermission');
if( page && limit) query.page(page - 1, limit);
},
sortOrder(query, column, order){
if(column) query.orderBy(column, order);
}
};
}
Expand Down
36 changes: 26 additions & 10 deletions app/src/services/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ const service = {
*/
searchObjects: async (params, etrx = undefined) => {
let trx;
let total = 0;

Check failure on line 120 in app/src/services/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (20.x)

'total' is assigned a value but never used

Check failure on line 120 in app/src/services/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

'total' is assigned a value but never used

Check failure on line 120 in app/src/services/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (20.x)

'total' is assigned a value but never used

Check failure on line 120 in app/src/services/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

'total' is assigned a value but never used

Check failure on line 120 in app/src/services/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x)

'total' is assigned a value but never used
try {
trx = etrx ? etrx : await ObjectModel.startTransaction();

const response = await ObjectModel.query(trx)
.allowGraph('version')
// .allowGraph('version')
.modify('filterIds', params.id)
.modify('filterBucketIds', params.bucketId)
.modify('filterName', params.name)
Expand All @@ -136,16 +137,31 @@ const service = {
tag: params.tag
})
.modify('hasPermission', params.userId, 'READ')
// format result
.modify('pagination', params.page, params.limit)
.modify('sortOrder', params.sort, params.order)
.then(result => {
// just return object table records
const res = result.map(row => {
// eslint-disable-next-line no-unused-vars
const { objectPermission, bucketPermission, version, ...object } = row;
return object;
});
// remove duplicates
return [...new Map(res.map(item => [item.id, item])).values()];
if (Object.hasOwn(result, 'results')) {
total = result.total;
const res = result.results.map(row => {
const { objectPermission, ...object } = row;
object.objectPermissions = [];
objectPermission.map(o => {
object.objectPermissions.push(o.permCode);
});
return object;
});
// remove duplicates
return [...new Map(res.map(item => [item.id, item])).values()];
} else {
//just return object table records
const res = result.map(row => {
// eslint-disable-next-line no-unused-vars
const { objectPermission, bucketPermission, version, ...object } = row;
return object;
});
// remove duplicates
return [...new Map(res.map(item => [item.id, item])).values()];
}
});

if (!etrx) await trx.commit();
Expand Down
7 changes: 6 additions & 1 deletion app/src/validators/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ const schema = {
public: type.truthy,
active: type.truthy,
deleteMarker: type.truthy,
latest: type.truthy
latest: type.truthy,
page: Joi.number().min(1),
limit: Joi.number(),
sort: Joi.string().valid('name', 'createdAt', "bucketId"),

Check failure on line 143 in app/src/validators/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (20.x)

Strings must use singlequote

Check failure on line 143 in app/src/validators/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

Strings must use singlequote

Check failure on line 143 in app/src/validators/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (20.x)

Strings must use singlequote

Check failure on line 143 in app/src/validators/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

Strings must use singlequote

Check failure on line 143 in app/src/validators/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x)

Strings must use singlequote
order: Joi.string().valid('asc', 'desc'),
permissions: type.truthy,
})
},

Expand Down

0 comments on commit 533bc7c

Please sign in to comment.