Skip to content
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

Add user limits #22479

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Add user limits #22479

wants to merge 10 commits into from

Conversation

licitdev
Copy link
Member

@licitdev licitdev commented May 13, 2024

Scope

What's changed:

  • Added user limits for Admin, App and API accesses
  • Limit can be used to restrict public registration

Potential Risks / Drawbacks

  • Needs further testing for edge cases

Review Notes / Questions

  • Todo Unit Tests

Implements the limits mentioned in issue #21981 for Public Registration #22125

Copy link

changeset-bot bot commented May 13, 2024

⚠️ No Changeset found

Latest commit: 4076b19

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@licitdev licitdev marked this pull request as ready for review May 27, 2024 14:52
api/src/services/roles.test.ts Outdated Show resolved Hide resolved
packages/env/src/constants/directus-variables.ts Outdated Show resolved Hide resolved
Comment on lines 230 to 232
'USERS_ACTIVE_LIMIT_ADMIN_ACCESS',
'USERS_ACTIVE_LIMIT_APP_ACCESS',
'USERS_ACTIVE_LIMIT_API_ACCESS_ACCESS',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of words here is not very intuitive to me and the use of "active" and "access" feel redundant 🤔 Could we perhaps simplify to something like this: (similar and consistent with EXTENSIONS_LIMIT):

Suggested change
'USERS_ACTIVE_LIMIT_ADMIN_ACCESS',
'USERS_ACTIVE_LIMIT_APP_ACCESS',
'USERS_ACTIVE_LIMIT_API_ACCESS_ACCESS',
'ADMIN_USERS_LIMIT',
'APP_USERS_LIMIT',
'API_USERS_LIMIT',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we prefix with USERS instead?

Suggested change
'USERS_ACTIVE_LIMIT_ADMIN_ACCESS',
'USERS_ACTIVE_LIMIT_APP_ACCESS',
'USERS_ACTIVE_LIMIT_API_ACCESS_ACCESS',
'USERS_ADMIN_LIMIT',
'USERS_APP_LIMIT',
'USERS_API_LIMIT',

* Get the role type counts by role IDs
*/
export async function getRoleCountsByRoles(db: Knex, roles: string[]): Promise<UserCount> {
const counts: UserCount = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this type be aliased or renamed to be consistent with the function name? I would expect the getRoleCounts function to return a RoleCount type over a UserCount (or perhaps the types needs a more generic name like CountResult)

Suggested change
const counts: UserCount = {
const counts: CountResult = {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that but had left it untouched, naming is hard. CountResult might be a little generalised, how does AccessCount or AccessTypeCount sound? Or any other keyword is better suited?

Suggested change
const counts: UserCount = {
const counts: AccessCount = {
Suggested change
const counts: UserCount = {
const counts: AccessTypeCount = {


for (const role of roles) {
if (typeof role === 'object') {
if ('admin_access' in role && role['admin_access'] === true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since roles here are user provided, do we have to make use of the toBoolean util here?

Suggested change
if ('admin_access' in role && role['admin_access'] === true) {
if ('admin_access' in role && toBoolean(role['admin_access'])) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I believe our usage of toBoolean is for values fetched from DB to account for the inconsistencies. User provided values aren't cast with such magic elsewhere within our codebase. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the checkIncreasedUserLimits function in a file called get-role-counts-by-users.ts and the getRoleCountsByUsers function in a file called check-increased-user-limits.ts? 😂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥴 😵‍💫 😵

export async function checkIncreasedUserLimits(db: Knex, increasedUserCounts: UserCount): Promise<void> {
if (!increasedUserCounts.admin && !increasedUserCounts.app && !increasedUserCounts.api) return;

const userCounts = await getUserCount(db);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im wondering if this is something we should cache for large database? 🤔 will do some testing on a larger DB

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... Interesting! Are you thinking of caching the limits in Redis? 🤔

licitdev and others added 3 commits May 29, 2024 14:37
Co-authored-by: Brainslug <[email protected]>
Co-authored-by: Brainslug <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🔖 Ready
Development

Successfully merging this pull request may close these issues.

None yet

2 participants