Skip to content

Commit

Permalink
Fixed role grants cloud function
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikHuber committed May 24, 2023
1 parent 5a4dcc6 commit 5497100
Showing 1 changed file with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import { database } from 'firebase-functions'
import admin from 'firebase-admin'
import { database } from "firebase-functions";
import admin from "firebase-admin";

export default database
.ref('/role_grants/{roleUid}/{grantUid}')
.ref("/role_grants/{roleUid}/{grantUid}")
.onWrite((eventSnapshot, context) => {
const roleUid = context.params.roleUid
const grantUid = context.params.grantUid
const roleUid = context.params.roleUid;
const grantUid = context.params.grantUid;

const userRolesRef = admin.database().ref(`user_roles`)
const userRolesRef = admin.database().ref(`user_roles`);

return userRolesRef.once('value').then(snapshot => {
let promises = []
return userRolesRef.once("value").then((snapshot) => {
let promises = [];

snapshot.forEach(userRoles => {
const userUid = userRoles.key
const roles = userRoles.val()
snapshot.forEach((userRoles) => {
const userUid = userRoles.key;
const roles = userRoles.val();

Object.keys(roles).forEach((key, index) => {
if (key === roleUid) {
let grantRef = false
let grantRef = false;

console.log('User role changed:', eventSnapshot.val())
console.log("User role changed:", eventSnapshot.after.val());

if (eventSnapshot.val()) {
if (eventSnapshot.after.val()) {
grantRef = admin
.database()
.ref(`user_grants/${userUid}/${grantUid}`)
.set(true)
.set(true);
} else {
grantRef = admin
.database()
.ref(`user_grants/${userUid}/${grantUid}`)
.remove()
.remove();
}

promises.push(grantRef)
promises.push(grantRef);

console.log('Role changed', userUid, roleUid, grantUid)
console.log("Role changed", userUid, roleUid, grantUid);
}
})
})
});
});

return Promise.all(promises)
})
})
return Promise.all(promises);
});
});

0 comments on commit 5497100

Please sign in to comment.