-
Notifications
You must be signed in to change notification settings - Fork 0
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
7b36832
commit 3c10767
Showing
4 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict" | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.addColumn("Roles", "baseCapabilities", { | ||
type: Sequelize.JSON, | ||
allowNull: true, // allows null if you want the field to be optional | ||
}) | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.removeColumn("Roles", "baseCapabilities") | ||
}, | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Role } from "../database/Role" | ||
|
||
export default async function updateCapabilities() { | ||
const allRoles = await Role.findAll() | ||
|
||
if (!allRoles || !Array.isArray(allRoles)) return console.log("No roles found") | ||
|
||
async function getParentRoles(role, depth = 0) { | ||
if (depth > 100) return console.warn("Max depth reached") | ||
depth++ | ||
|
||
if (!role?.dataValues?.inheritFrom) return [] | ||
const parentRole = await Role.findOne({ where: { id: role.dataValues.inheritFrom } }) | ||
const parentRoles = await getParentRoles(parentRole, depth) // Await the recursive call | ||
return [parentRole, ...parentRoles] | ||
} | ||
|
||
allRoles.forEach(async (role) => { | ||
const parentRoles = await getParentRoles(role) | ||
const allRoles = Array.isArray(role?.dataValues?.baseCapabilities) ? [role, ...parentRoles] : [...parentRoles] | ||
|
||
const allCapabilities = allRoles.reduce((acc, role) => { | ||
return [...acc, ...role.dataValues.baseCapabilities] | ||
}, []) | ||
const uniqueCapabilities = [...new Set(allCapabilities)] | ||
await role.update({ capabilities: uniqueCapabilities }) | ||
}) | ||
} |