-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df42d2a
commit 9c3b21b
Showing
41 changed files
with
634 additions
and
778 deletions.
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
57 changes: 0 additions & 57 deletions
57
docs/docs/docs/01-core/admin/04-features/authentication.md
This file was deleted.
Oops, something went wrong.
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
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,58 @@ | ||
import { Location } from 'react-router-dom'; | ||
|
||
import type { Permission, User } from '../../features/Auth'; | ||
|
||
interface RBACContext extends Pick<Location, 'pathname' | 'search'> { | ||
/** | ||
* The current user. | ||
*/ | ||
user?: User; | ||
/** | ||
* The permissions of the current user. | ||
*/ | ||
permissions: Permission[]; | ||
} | ||
|
||
interface RBACMiddleware { | ||
( | ||
ctx: RBACContext | ||
): ( | ||
next: (permissions: Permission[]) => Promise<Permission[]> | Permission[], | ||
permissions: Permission[] | ||
) => Promise<Permission[]> | Permission[]; | ||
} | ||
|
||
class RBAC { | ||
private middlewares: RBACMiddleware[] = []; | ||
|
||
constructor() {} | ||
|
||
use(middleware: RBACMiddleware[]): void; | ||
use(middleware: RBACMiddleware): void; | ||
use(middleware: RBACMiddleware | RBACMiddleware[]): void { | ||
if (Array.isArray(middleware)) { | ||
this.middlewares.push(...middleware); | ||
} else { | ||
this.middlewares.push(middleware); | ||
} | ||
} | ||
|
||
run = async (ctx: RBACContext, permissions: Permission[]): Promise<Permission[]> => { | ||
let index = 0; | ||
|
||
const middlewaresToRun = this.middlewares.map((middleware) => middleware(ctx)); | ||
|
||
const next = async (permissions: Permission[]) => { | ||
if (index < this.middlewares.length) { | ||
return middlewaresToRun[index++](next, permissions); | ||
} | ||
|
||
return permissions; | ||
}; | ||
|
||
return next(permissions); | ||
}; | ||
} | ||
|
||
export { RBAC }; | ||
export type { RBACMiddleware, RBACContext }; |
Oops, something went wrong.