Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Use string adapter for casbin model
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Sep 2, 2020
1 parent aea4d9f commit b3f2f9b
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/_staart/helpers/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUserById } from "../services/user.service";
import { AccessTokenResponse, ApiKeyResponse } from "./jwt";
import { newEnforcer, Model } from "casbin";
import { newEnforcer, Model, StringAdapter } from "casbin";
import { prisma } from "./prisma";
import { ScopesUser, ScopesGroup, ScopesAdmin } from "../../config";
import { readFileSync } from "fs-extra";
Expand Down Expand Up @@ -94,17 +94,16 @@ const getPolicyForUser = async (userId: number) => {
};

const model = new Model();
model.loadModelFromText(readFileSync(join(".", "casbin-model.conf"), "utf-8"));
model.loadModel(join(".", "casbin-model.conf"));

export const can = async (
subject: number | ApiKeyResponse | AccessTokenResponse,
action: string,
object: string
) => {
if (typeof subject === "number") {
console.log("Subject is", subject);
const policy = await getPolicyForUser(subject);
const enforcer = await newEnforcer(model, policy);
const enforcer = await newEnforcer(model, new StringAdapter(policy));
return enforcer.enforce(`user-${subject}`, object, action);
}
return false;
Expand Down

0 comments on commit b3f2f9b

Please sign in to comment.