Skip to content

Commit

Permalink
load perms everytime
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed May 27, 2024
1 parent fecc518 commit 1e916f4
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,41 @@ async function tokenize(req: Request): Promise<Response> {
userInfo["affiliation"] = "owner";
let tokenRoom = "*"

let roomName = path.slice(1);

let room = permissions.find(r => r.room === roomName);

if(room) {
const userName = userInfo["email"]
console.log(`Found config for room ${roomName}. Checking for user ${userName}`)
tokenRoom = roomName;
// check if the user is in the moderator list
if(room.moderators.includes(userName)) {
console.log(`${userName} is a moderator of ${roomName}`);
// we keep the defaults
}
else {
console.log(`${userName} is not a moderator of ${roomName}`);
// reduce permissions
userInfo["affiliation"] = "member";

}
} else {
console.log(`room ${roomName} not found in permissions.`);
// Loading permissions
if(PERMISSIONS_FILE) {
if (fs.existsSync(PERMISSIONS_FILE)) {
let permissions: any = [];
const rawData = fs.readFileSync(PERMISSIONS_FILE, 'utf-8');
permissions = JSON.parse(rawData);
// now you can use the 'permissions' object
console.log(`Loaded ${permissions.length} permissions from ${PERMISSIONS_FILE}`)

let roomName = path.slice(1);

let room = permissions.find(r => r.room === roomName);

if(room) {
const userName = userInfo["email"]
console.log(`Found config for room ${roomName}. Checking for user ${userName}`)
tokenRoom = roomName;
// check if the user is in the moderator list
if(room.moderators.includes(userName)) {
console.log(`${userName} is a moderator of ${roomName}`);
// we keep the defaults
}
else {
console.log(`${userName} is not a moderator of ${roomName}`);
// reduce permissions
userInfo["affiliation"] = "member";

}
} else {
console.log(`room ${roomName} not found in permissions.`);
}

} else {
console.error(`File not found: ${PERMISSIONS_FILE} - No permissions loaded.`);
}
}


Expand Down Expand Up @@ -333,7 +347,6 @@ async function handler(req: Request): Promise<Response> {
// -----------------------------------------------------------------------------
// main
// -----------------------------------------------------------------------------
let permissions: any = [];
function main() {
console.log(`KEYCLOAK_ORIGIN: ${KEYCLOAK_ORIGIN}`);
console.log(`KEYCLOAK_ORIGIN_INTERNAL: ${KEYCLOAK_ORIGIN_INTERNAL}`);
Expand All @@ -352,18 +365,6 @@ function main() {
console.log(`PERMISSIONS_FILE: ${PERMISSIONS_FILE}`);
}

// Loading permissions
if(PERMISSIONS_FILE) {
if (fs.existsSync(PERMISSIONS_FILE)) {
const rawData = fs.readFileSync(PERMISSIONS_FILE, 'utf-8');
permissions = JSON.parse(rawData);
// now you can use the 'permissions' object
console.log(`Loaded ${permissions.length} permissions from ${PERMISSIONS_FILE}`)
} else {
console.error(`File not found: ${PERMISSIONS_FILE} - No permissions loaded.`);
}
}

serve(handler, {
hostname: HOSTNAME,
port: PORT
Expand Down

0 comments on commit 1e916f4

Please sign in to comment.