Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active January 5, 2022 17:15
Show Gist options
  • Save dipeshhkc/8331b93957eeebc2a5c1c1fd98edc49c to your computer and use it in GitHub Desktop.
Save dipeshhkc/8331b93957eeebc2a5c1c1fd98edc49c to your computer and use it in GitHub Desktop.
import { Permissions } from "~/types/team";
import { db, enforcer } from "./db.server";
export const findPermissionInAnObj = async ({ user_id, obj }) => {
//find teams in which he is a member
const teams = await db.usersOnTeams.findMany({
where: {
user_id: user_id,
},
});
for (let i = 0; i < teams.length; i++) {
//In each team, list his all permissions
const results = await enforcer.getImplicitPermissionsForUser(
String(user_id),
String(teams[i].team_id)
);
// if no permissions found, go to next team
if (!results.length) {
continue;
}
//find permissions of our interest
const filteredResult = results.find((result) => {
return result[2] == obj;
});
// if no permissions of our interest found, go to next team
if (!filteredResult) {
continue;
}
//For now - we assume an object is linked to a person only in 1 team(no diff permission for same person in diff team). So no need to search further in other team if found in one
const permission = filteredResult[3];
// const role = await enforcer.getRolesForUser(
// String(user_id),
// String(teams[i].team_id)
// );
return {
permission,
// role: role[0],
};
}
// if we are searching for permission of a person for obj but couldnot find, then he should be owner
return {
permission: "owner",
// role: "owner",
};
};
export const findListOfObjOfUserWithPermission = async ({
user_id,
obj,
}): Promise<
{
id: string;
permission: string;
// role: string;
}[]
> => {
//find teams in which he is a member
const teams = await db.usersOnTeams.findMany({
where: {
user_id: user_id,
},
});
const response = [];
for (let i = 0; i < teams.length; i++) {
//In each team, list his all permissions
const results = await enforcer.getImplicitPermissionsForUser(
String(user_id),
String(teams[i].team_id)
);
// if no permissions found, go to next team
if (!results.length) {
continue;
}
//find permissions of our interest
const filteredResult = results.filter((result) => {
return result[2].indexOf(obj) != -1;
});
for (let j = 0; j < filteredResult.length; j++) {
const obj = filteredResult[j][2];
const permission = filteredResult[j][3];
// const role = await enforcer.getRolesForUser(
// String(user_id),
// String(teams[i].team_id)
// );
const profileId = obj.split(":")[1];
response.push({
id: profileId,
permission,
// role: role[0],
});
}
}
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment