-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement a unified Casbin engine interface and refactor the ex…
…ecution logic. (#180) * feat: implement CasbinEngine and refactor enforcement logic * refactor: simplify ModelKind type usage and remove unnecessary type declaration
- Loading branch information
1 parent
7e9d771
commit 3f74ea3
Showing
9 changed files
with
365 additions
and
217 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
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,106 @@ | ||
import { newEnforcer, newModel, StringAdapter } from 'casbin'; | ||
import { remoteEnforcer } from './hooks/useRemoteEnforcer'; | ||
import { setupRoleManager, setupCustomConfig, processRequests } from '@/app/utils/casbinEnforcer'; | ||
|
||
interface EnforceResult { | ||
allowed: boolean; | ||
reason: string[]; | ||
error: string | null; | ||
} | ||
|
||
export interface ICasbinEngine { | ||
enforce(params: { | ||
model: string; | ||
policy: string; | ||
request: string; | ||
customConfig?: string; | ||
enforceContextData?: Map<string, string>; | ||
}): Promise<EnforceResult>; | ||
|
||
getVersion(): string; | ||
getType(): 'node' | 'java' | 'go'; | ||
} | ||
|
||
// Node.js | ||
export class NodeCasbinEngine implements ICasbinEngine { | ||
async enforce(params) { | ||
try { | ||
const e = await newEnforcer( | ||
newModel(params.model), | ||
params.policy ? new StringAdapter(params.policy) : undefined | ||
); | ||
|
||
setupRoleManager(e); | ||
|
||
if (params.customConfig) { | ||
await setupCustomConfig(e, params.customConfig); | ||
} | ||
|
||
const results = await processRequests(params.request, e, params.enforceContextData); | ||
|
||
return { | ||
allowed: results[0].okEx, | ||
reason: results[0].reason, | ||
error: null, | ||
}; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
getVersion(): string { | ||
return process.env.CASBIN_VERSION || ''; | ||
} | ||
|
||
getType(): 'node' { | ||
return 'node'; | ||
} | ||
} | ||
|
||
// RemoteCasbinEngine | ||
export class RemoteCasbinEngine implements ICasbinEngine { | ||
constructor(private engine: 'java' | 'go') {} | ||
|
||
async enforce(params) { | ||
try { | ||
const result = await remoteEnforcer({ | ||
model: params.model, | ||
policy: params.policy, | ||
request: params.request, | ||
engine: this.engine, | ||
}); | ||
|
||
if (result.error) { | ||
throw new Error(result.error); | ||
} | ||
|
||
return { | ||
allowed: result.allowed, | ||
reason: result.reason, | ||
error: null, | ||
}; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
getVersion(): string { | ||
return ''; | ||
} | ||
|
||
getType(): 'java' | 'go' { | ||
return this.engine; | ||
} | ||
} | ||
|
||
export function createCasbinEngine(type: 'node' | 'java' | 'go'): ICasbinEngine { | ||
switch (type) { | ||
case 'node': | ||
return new NodeCasbinEngine(); | ||
case 'java': | ||
case 'go': | ||
return new RemoteCasbinEngine(type); | ||
default: | ||
throw new Error(`Unsupported engine type: ${type}`); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -451,5 +451,3 @@ export const defaultEnforceContext = `{ | |
"e": "e", | ||
"m": "m" | ||
}`; | ||
|
||
export type ModelKind = string; |
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
Oops, something went wrong.