Refactor RolesService retrieveStorageContextByCode

In the current implementation, every time we have to upsert a privilege or retrieve a role by lookupKey, we call the method retrieveStorageContextByCode provided by the RolesService (microservice). This method perform the following steps:

  1. retrieve the role identified by lookupKey

  2. if the role has extendedRoleIds, it performs an additional query for each extended role id

  3. if the extended role has extendedRoleIds, repeat step 2 and 3 (recursion)

Note: steps 2 and 3 are executed sequentially

Currently the max depth for extended roles is 2:

organization-admin/owner โ†’ space-supervisor โ†’ space-folder, course-observer

This means executing up to 2 queries for each extended role.

In order to avoid this amount of queries, we can refactor the method by leveraging population ($lookup). This way we will perform a single query.

Note: if we want to support more than 2 levels of inheritance, we can adjust the method like this:

  1. retrieve role and populate up to the second level

  2. if second-level roles have extendedRoleIds, collect role ids and repeat step 1 on them (using find instead of findOne)

  3. repeat until second-level roles have no extendedRoleIds

Domain
AuthN-Z
Action
refactor
Department
Backend

Please authenticate to join the conversation.

Upvoters
Status

Completed

Board
๐Ÿ› ๏ธ

Bug & Fixes

Tags

High Priority

Date

11 months ago

Author

Ivan Ligotino

Subscribe to post

Get notified by email when there are changes.