Skip to content

Instantly share code, notes, and snippets.

@aravindarc
Created September 17, 2021 09:49
Show Gist options
  • Save aravindarc/cd843d9ab3aafede3091eed024bdfcca to your computer and use it in GitHub Desktop.
Save aravindarc/cd843d9ab3aafede3091eed024bdfcca to your computer and use it in GitHub Desktop.
var ROLES = {
PRESIDENT: {
id: 1,
actions: []
}
};
const COUNTRYACTIONS = {
FIRE_MISSILE: 1,
VETO_LEGISLATION: 2,
SIGN_LEGISLATION: 3
};
const COUNTRIES = {
"USA": 1
};
var users = {
"DonaldTrump": {},
"JoeBiden": {}
};
function assignActionsToRoles(action, role) {
role.actions.push(action);
}
function assignRolesToSubjects(subject, role, object) {
subject.role = role;
subject.object = object;
}
function revokeRoles(subject, role, object) {
if (subject.role == role) {
subject.role = null;
}
if (subject.object == object) {
subject.object = null;
}
}
function check(subject, action, object) {
if (subject.object == object) {
if (subject.role.actions.includes(action)) {
return true;
}
}
return false;
}
// long ago
assignActionsToRoles(COUNTRYACTIONS.FIRE_MISSILE, ROLES.PRESIDENT);
// 2016
assignRolesToSubjects(users["DonaldTrump"], ROLES.PRESIDENT, COUNTRIES.USA);
console.log(check(users["DonaldTrump"], COUNTRYACTIONS.FIRE_MISSILE, COUNTRIES.USA));
// 2020
revokeRoles(users["DonaldTrump"], ROLES.PRESIDENT, COUNTRIES.USA);
assignRolesToSubjects(users["JoeBiden"], ROLES.PRESIDENT, COUNTRIES.USA);
// can DonaldTrump still fire nuclear missile?
console.log(check(users["DonaldTrump"], COUNTRYACTIONS.FIRE_MISSILE, COUNTRIES.USA));
// can JoeBiden fire nuclear missile?
console.log(check(users["JoeBiden"], COUNTRYACTIONS.FIRE_MISSILE, COUNTRIES.USA));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment