Skip to content

Commit

Permalink
Added dedicated options for controlling admin search parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
shrihari-prakash committed Mar 4, 2024
1 parent 448ff23 commit e5eb208
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/service/api/user/admin-api/search.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const POST_SearchValidator = [body("query").exists().isString().isLength(
const redisPrefix = "search:";
const POST_Search = async (req: Request, res: Response) => {
try {
if (!ScopeManager.isScopeAllowedForSession("delegated:profile:search", res)) {
if (!ScopeManager.isScopeAllowedForSession("admin:profile:search", res)) {
return;
}
if (hasErrors(req, res)) return;
Expand All @@ -33,12 +33,12 @@ const POST_Search = async (req: Request, res: Response) => {
}
log.info("Cache miss for query: " + query);
const queryRegex = new RegExp(query, "i");
const $or = Configuration.get("user.search.search-fields").map((field: string) => ({ [field]: queryRegex }));
if (Configuration.get("privilege.user.search.can-use-id") && isValidObjectId(query)) {
const $or = Configuration.get("admin-api.user.search.search-fields").map((field: string) => ({ [field]: queryRegex }));
if (Configuration.get("admin-api.privilege.user.search.can-use-id") && isValidObjectId(query)) {
log.debug("Search by ID is enabled.");
$or.push({ _id: query });
}
if (Configuration.get("privilege.user.search.can-use-fullname")) {
if (Configuration.get("admin-api.privilege.user.search.can-use-fullname")) {
log.debug("Search by Full Name is enabled.");
$or.push({
$expr: {
Expand All @@ -52,12 +52,12 @@ const POST_Search = async (req: Request, res: Response) => {
}
log.debug("Search query is: %o", $or);
const results = (await UserModel.find({ $or }, UserProjection).limit(
Configuration.get("user.search-results.limit")
Configuration.get("admin-api.user.search-results.limit")
)) as unknown as UserInterface[];
await hydrateUserProfile(results);
const cacheKey = `${redisPrefix}${query}`;
const cacheValue = JSON.stringify(results);
const cacheExpiry = Configuration.get("user.search-results.cache-lifetime");
const cacheExpiry = Configuration.get("admin-api.user.search-results.cache-lifetime");
await Redis.setEx(cacheKey, cacheValue, cacheExpiry);
const milliseconds = +new Date() - startTime;
log.info("Search for query `%s` completed in %s ms", query, milliseconds);
Expand Down
41 changes: 41 additions & 0 deletions src/service/configuration/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,47 @@
"type": "number",
"default": 10
},
{
"name": "admin-api.user.search.search-fields",
"displayName": "User Search Fields",
"envName": "ADMIN_API_USER_SEARCH_SEARCH_FIELDS (Admin API)",
"description": "Specifies the field names that can be used to search for users.",
"type": "stringArray",
"default": "username,firstName,lastName,email"
},
{
"name": "admin-api.privilege.user.search.can-use-id",
"displayName": "Can Use ID for User Search (Admin API)",
"envName": "ADMIN_API_USER_SEARCH_CAN_USE_ID",
"description": "Specifies if _id field of records can be used to search for users.",
"type": "boolean",
"default": true
},
{
"name": "admin-api.privilege.user.search.can-use-fullname",
"displayName": "Can Use Fullname for User Search (Admin API)",
"envName": "ADMIN_API_USER_SEARCH_CAN_USE_FULLNAME",
"description": "Specifies if firstName and lastName fields can be concatenated and used to search for users.",
"type": "boolean",
"default": true
},
{
"name": "admin-api.user.search-results.cache-lifetime",
"displayName": "User Search Results Cache Lifetime (Admin API)",
"envName": "ADMIN_API_USER_SEARCH_RESULTS_CACHE_LIFETIME",
"description": "Specifies in seconds the amount of time for which results for a search query will be cached.",
"type": "number",
"default": 30,
"relatedOptions": ["privilege.can-use-cache"]
},
{
"name": "admin-api.user.search-results.limit",
"displayName": "User Search Results Limit (Admin API)",
"envName": "ADMIN_API_USER_SEARCH_RESULTS_LIMIT",
"description": "Specifies the maximum number of results to be returned in user search API.",
"type": "number",
"default": 10
},
{
"name": "user.block-status.cache-lifetime",
"displayName": "User Block Status Cache Lifetime",
Expand Down

0 comments on commit e5eb208

Please sign in to comment.