-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a4dcc6
commit 5497100
Showing
1 changed file
with
23 additions
and
23 deletions.
There are no files selected for viewing
46 changes: 23 additions & 23 deletions
46
packages/rmw-shell/cra-template-rmw/template/firebase/functions/db/roleGrants/onWrite.f.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |