-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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(editor): Replace middleware for Role checks with Scope checks #7847
Merged
alexgrozav
merged 12 commits into
master
from
pay-1046-update-middlewares-for-admin-role-checks
Nov 29, 2023
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e95905f
feat: update middleware to no longer use roles
alexgrozav a4bed4b
fix: update various user permission checks
alexgrozav b14435e
chore: merge master
alexgrozav 380231c
fix: remove duplicate scope
alexgrozav d0c0b17
fix: whitespace
alexgrozav 4cb3b85
fix: remove merge conflict
alexgrozav 09d96bc
fix: fix some scopes
alexgrozav 190aef6
fix: fix role changes
alexgrozav 4eadb9d
Merge remote-tracking branch 'origin/master' into pay-1046-update-mid…
cstuncsik 90db640
fix(editor): Lint fix
cstuncsik c9d741b
fix: fix registered permission checks
alexgrozav 28c1e98
fix: Fix unit test
cstuncsik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/editor-ui/src/rbac/checks/__tests__/isDefaultUser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useUsersStore } from '@/stores/users.store'; | ||
import { isDefaultUser } from '@/rbac/checks/isDefaultUser'; | ||
|
||
vi.mock('@/stores/users.store', () => ({ | ||
useUsersStore: vi.fn(), | ||
})); | ||
|
||
describe('Checks', () => { | ||
describe('isDefaultUser()', () => { | ||
it('should return false if user not logged in', () => { | ||
vi.mocked(useUsersStore).mockReturnValue({ currentUser: null } as ReturnType< | ||
typeof useUsersStore | ||
>); | ||
|
||
expect(isDefaultUser()).toBe(false); | ||
}); | ||
|
||
it('should return true if user is default user', () => { | ||
const mockUser = { id: 'user123', name: 'Test User', isDefaultUser: true }; | ||
vi.mocked(useUsersStore).mockReturnValue({ currentUser: mockUser } as unknown as ReturnType< | ||
typeof useUsersStore | ||
>); | ||
|
||
expect(isDefaultUser()).toBe(mockUser.isDefaultUser); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './hasRole'; | ||
export * from './hasScope'; | ||
export * from './isAuthenticated'; | ||
export * from './isDefaultUser'; | ||
export * from './isEnterpriseFeatureEnabled'; | ||
export * from './isGuest'; | ||
export * from './isValid'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useUsersStore } from '@/stores/users.store'; | ||
import type { DefaultUserMiddlewareOptions, RBACPermissionCheck } from '@/types/rbac'; | ||
|
||
export const isDefaultUser: RBACPermissionCheck<DefaultUserMiddlewareOptions> = () => { | ||
const usersStore = useUsersStore(); | ||
const currentUser = usersStore.currentUser; | ||
|
||
if (currentUser) { | ||
return currentUser.isDefaultUser; | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/editor-ui/src/rbac/middleware/__tests__/defaultUser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useUsersStore } from '@/stores/users.store'; | ||
import { VIEWS } from '@/constants'; | ||
import { defaultUserMiddleware } from '@/rbac/middleware/defaultUser'; | ||
import type { RouteLocationNormalized } from 'vue-router'; | ||
import { authenticatedMiddleware } from '@/rbac/middleware/authenticated'; | ||
|
||
vi.mock('@/stores/users.store', () => ({ | ||
useUsersStore: vi.fn(), | ||
})); | ||
|
||
describe('Middleware', () => { | ||
describe('defaultUser', () => { | ||
it('should redirect to homepage if user not logged in', async () => { | ||
vi.mocked(useUsersStore).mockReturnValue({ | ||
currentUser: null, | ||
} as ReturnType<typeof useUsersStore>); | ||
|
||
const nextMock = vi.fn(); | ||
const toMock = { query: {} } as RouteLocationNormalized; | ||
const fromMock = {} as RouteLocationNormalized; | ||
|
||
await defaultUserMiddleware(toMock, fromMock, nextMock, {}); | ||
|
||
expect(nextMock).toHaveBeenCalledWith({ name: VIEWS.HOMEPAGE }); | ||
}); | ||
|
||
it('should redirect to homepage if user is not default user', async () => { | ||
vi.mocked(useUsersStore).mockReturnValue({ | ||
currentUser: { id: '123', isDefaultUser: false }, | ||
} as ReturnType<typeof useUsersStore>); | ||
|
||
const nextMock = vi.fn(); | ||
const toMock = { query: {} } as RouteLocationNormalized; | ||
const fromMock = {} as RouteLocationNormalized; | ||
|
||
await defaultUserMiddleware(toMock, fromMock, nextMock, {}); | ||
|
||
expect(nextMock).toHaveBeenCalledWith({ name: VIEWS.HOMEPAGE }); | ||
}); | ||
|
||
it('should allow navigation if a current user is present', async () => { | ||
vi.mocked(useUsersStore).mockReturnValue({ | ||
currentUser: { id: '123', isDefaultUser: true }, | ||
} as ReturnType<typeof useUsersStore>); | ||
|
||
const nextMock = vi.fn(); | ||
const toMock = { query: {} } as RouteLocationNormalized; | ||
const fromMock = {} as RouteLocationNormalized; | ||
|
||
await defaultUserMiddleware(toMock, fromMock, nextMock, {}); | ||
|
||
expect(nextMock).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { RouterMiddleware } from '@/types/router'; | ||
import { VIEWS } from '@/constants'; | ||
import type { DefaultUserMiddlewareOptions } from '@/types/rbac'; | ||
import { isDefaultUser } from '@/rbac/checks'; | ||
|
||
export const defaultUserMiddleware: RouterMiddleware<DefaultUserMiddlewareOptions> = async ( | ||
to, | ||
from, | ||
next, | ||
) => { | ||
const valid = isDefaultUser(); | ||
if (!valid) { | ||
return next({ name: VIEWS.HOMEPAGE }); | ||
} | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are required on the backend for the user to be able to get the user list for sharing and to get the variables they can use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mistake! Got messed up from the conflicts.