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

feat: filter projectless events for normal users #7914

Merged
merged 2 commits into from
Aug 21, 2024
Merged

Conversation

sjaanus
Copy link
Contributor

@sjaanus sjaanus commented Aug 19, 2024

Now events that do not have project ( for example user creation, segment creation etc), will not be displayed to non root admins.

Copy link

vercel bot commented Aug 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-monorepo-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2024 10:26am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Aug 21, 2024 10:26am

Copy link
Contributor

github-actions bot commented Aug 19, 2024

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

@@ -390,3 +455,63 @@ test('should filter events by project using IS_ANY_OF', async () => {
total: 2,
});
});

test('should not show user creation events for non-admins', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This tests the main logic.

@@ -222,6 +229,14 @@ export default class EventService {
async getEventCreators() {
return this.eventStore.getEventCreators();
}

async getProjectFilterForNonAdmins(userId: number): Promise<IQueryParam[]> {
const isRootAdmin = await this.accessReadModel.isRootAdmin(userId);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main logic. If you are not root admin, add db query filter to not include projects that have null as value.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, right. So effectively, this removes any non-project events for non-admins.

Copy link
Contributor

@thomasheartman thomasheartman left a comment

Choose a reason for hiding this comment

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

Nike ✅

Comment on lines +61 to +65
if (isSingleParam) {
query.whereNot(param.field, param.values[0]);
} else {
query.whereNotIn(param.field, param.values);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? A list with a single list is still a lilt, right? So is not in [a] and is not in [a, b] should both be valid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is because it is doing following sql query:

not in (x), and if you put in null for project , not in (null), then it will not work, because SQL things.

await this.eventService.storeEvent(
new SegmentCreatedEvent({
data: segment,
project: segment.project || 'no-project',
project: project,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why you're extracting the variable here. It's only used once, and I don't think this adds any more clarity?

But if you want to do it, we can at least avoid doubling up:

Suggested change
project: project,
project,

Your call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good type clarification here. This is just making the types more accurate to how we actually use them, right?

Comment on lines +249 to 258
project: 'default',
createdBy: '[email protected]',
createdByUserId: TEST_USER_ID + 1,
ip: '127.0.0.1',
});

await eventService.storeEvent({
type: FEATURE_CREATED,
project: 'default',
createdBy: '[email protected]',
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting: did this test not work without the project? If so, why not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually since feature created always has project, wanted to make it legit.

@@ -222,6 +229,14 @@ export default class EventService {
async getEventCreators() {
return this.eventStore.getEventCreators();
}

async getProjectFilterForNonAdmins(userId: number): Promise<IQueryParam[]> {
const isRootAdmin = await this.accessReadModel.isRootAdmin(userId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, right. So effectively, this removes any non-project events for non-admins.

@@ -222,6 +229,14 @@ export default class EventService {
async getEventCreators() {
return this.eventStore.getEventCreators();
}

async getProjectFilterForNonAdmins(userId: number): Promise<IQueryParam[]> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Small suggestion here: the naming was a little unclear to me at first, because it doesn't seem like we know whether the user is an admin or not at this point. If you read the method, you'll see that it's checked in there, though.

I'm not sure I have any better suggestions, but maybe getRoleBasedFilters would work? I ... dunno 🤷🏼

@sjaanus sjaanus merged commit df73c65 into main Aug 21, 2024
9 of 11 checks passed
@sjaanus sjaanus deleted the filter-events branch August 21, 2024 10:59
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.

2 participants