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

Improves hasReadAccess to better handle difference between empty string and empty array #2076

Merged
merged 4 commits into from
Jan 31, 2024

Conversation

mknowlton89
Copy link
Collaborator

Features and Changes

Per a conversation with Jeremy, we're updating the logic of hasReadAccess and updating the signature to no longer access null or undefined values.

When evaluating read access, an empty projects array is different from an empty project string.
Empty array = "all projects" = everyone has read access, even with "noaccess" global role
Empty string = "no project" = read access determined by the global role

Some resources (metrics, data sources, fact tables, SDK connections) use a plural projects field as sort of a filter. If empty, there's no filter (all projects). If specified, it's filtering down to only the listed projects.
Other resources (feature flags, experiments) use a singular project field. It can either be empty (not in a project) or set to a specific id.

So thinking about a global "noaccess" user who only has access to Project A, it feels like they should be able to see unfiltered (empty array) resources. It also feels like they should NOT be able to access "not in project" resources (empty string).

@mknowlton89 mknowlton89 self-assigned this Jan 30, 2024
@mknowlton89 mknowlton89 requested a review from jdorn January 30, 2024 18:40
@mknowlton89 mknowlton89 marked this pull request as ready for review January 30, 2024 18:40
Copy link

Your preview environment pr-2076-bttf has been deployed.

Preview environment endpoints are available at:

// if the user doesn't have project specific roles, fallback to user's global role
if (!filter.projects.length) {
// if the user doesn't have project specific roles or resource doesn't have a project (project is an empty string), fallback to user's global role
if (!filter.projects.length || projects === "") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we not accomplish the same by doing the following? That way we could keep projects as string | string[] | null | undefined, and avoid having to pass in an empty string everywhere.

Suggested change
if (!filter.projects.length || projects === "") {
if (!filter.projects.length || !projects) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We're treating an empty string differently than we're treating an empty array, which is why we've gone the route of not allowing projects to be undefined.

If we were to introduce the change you're suggesting, we'd lose the ability to do this differentiation.

Some background:
Some resources (metrics, data sources, fact tables, SDK connections) use a projects array as sort of a filter. If empty, there's no filter (all projects). If specified, it's filtering down to only the listed projects.

Other resources (feature flags, experiments) use a singular project field. It can either be undefined or an empty string (not in a project) or set to a specific id.

So being able to differentiate whether it's an empty string or an empty array allows us to control the read access filtering more precisely.

Copy link
Contributor

@bttf bttf Jan 30, 2024

Choose a reason for hiding this comment

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

Could you double-check? An empty array and undefined are not equal in value so I still think what I suggested would work.

console.log(Boolean([])); // true
console.log(Boolean(undefined)) // false
console.log(Boolean('')) // false

Copy link
Collaborator Author

@mknowlton89 mknowlton89 Jan 30, 2024

Choose a reason for hiding this comment

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

Sorry - I think I misunderstood your request. I initially thought you were asking me to replace both of the following instances

hasReadAccess(...., resource.projects || [])

AND

hasReadAccess(..., resource.project || "")

So that we never had to use the || [] or || "" syntax. In that case, we'd not be able to differentiate whether it was undefined because the projects array was undefined or the project string was undefined.

But, correct me if I'm wrong, you were only asking me to update the hasReadAccess signature so we didn't have to pass in an empty string?

In that case, I've pushed up this commit with those changes. Let me know if that's what you intended.

Copy link
Contributor

@bttf bttf Jan 30, 2024

Choose a reason for hiding this comment

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

Yep, my comment was just regarding the signature of hasReadAccess. Changes look good 👍🏾

@mknowlton89 mknowlton89 merged commit a5b63b8 into main Jan 31, 2024
3 checks passed
@mknowlton89 mknowlton89 deleted the mk/update-hasReadAccess branch January 31, 2024 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants