@tanglemedia/directus-collection-embed
Version:
Installs the Embed Collection to your Directus project.
157 lines (136 loc) • 4.48 kB
JavaScript
import crypto from "crypto";
const COLLECTION_NAME = "tngl_embed";
const ROLE_NAME = "Website";
const ROLE_DESCRIPTION =
"Access the data to use to prebuild data into the static HUGO Website";
const ROLE_ICON = "build_circle";
const ROLE_ACTIONS = ["read"];
// preset that applies to any user or role
// create function to generate a uuidv4
function generateUUIDv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
(c ^ ((crypto.randomBytes(1)[0] & 15) >> (c / 4))).toString(16)
);
}
export async function up(knex) {
let roleID = null;
// check existence of the directus_permissions table
const permTableExists = await knex.schema.hasTable("directus_permissions");
// check existence of the directus_policies table
const policyTableExists = await knex.schema.hasTable("directus_policies");
// check existence of the directus_roles table
const roleTableExists = await knex.schema.hasTable("directus_roles");
if (!roleTableExists) return;
if (!permTableExists) return;
// check if the role exists
const roleRow = await knex("directus_roles")
.select("id")
.where("name", ROLE_NAME)
.first();
// if the role does not exist, create one
if (!roleRow) {
roleID = generateUUIDv4();
await knex("directus_roles").insert({
id: roleID,
name: ROLE_NAME,
icon: ROLE_ICON,
description: ROLE_DESCRIPTION,
});
} else {
roleID = roleRow.id;
}
// then create a new policy
if (policyTableExists) {
// check if policy whose id = roleID exists
const policyRow = await knex("directus_policies")
.select("id")
.where("id", roleID)
.first();
// if policy whose id = roleID does not exist, create one
if (!policyRow) {
await knex("directus_policies").insert({
id: roleID,
name: ROLE_NAME,
icon: ROLE_ICON,
description: ROLE_DESCRIPTION,
});
}
}
if (roleID) {
// if directus_permissions table exists, add a new permission for the role to create and upate the collection
const columns = await knex("directus_permissions").columnInfo();
const columnNames = Object.keys(columns);
const newPermissionRow = ROLE_ACTIONS.map((action) => {
const newPermission = {};
// fields that exist in v10 and v11
if (columnNames.includes("collection")) {
newPermission.collection = COLLECTION_NAME;
}
if (columnNames.includes("action")) {
newPermission.action = action;
}
if (columnNames.includes("permissions")) {
newPermission.permissions = {};
}
if (columnNames.includes("validation")) {
newPermission.validation = {};
}
if (columnNames.includes("presets")) {
newPermission.presets = null;
}
if (columnNames.includes("fields")) {
newPermission.fields = "*";
}
// fields that exist in v10
if (columnNames.includes("role")) {
newPermission.role = roleID;
}
// fields that exist in v11
if (columnNames.includes("policy")) {
newPermission.policy = roleID;
}
return knex("directus_permissions").insert(newPermission);
});
await Promise.all(newPermissionRow);
}
}
export async function down(knex) {
// check existence of the directus_permissions table
const permTableExists = await knex.schema.hasTable("directus_permissions");
// check existence of the directus_policies table
const policyTableExists = await knex.schema.hasTable("directus_policies");
// check existence of the directus_roles table
const roleTableExists = await knex.schema.hasTable("directus_roles");
if (!roleTableExists) return;
if (!permTableExists) return;
// check if the role exists
const roleRow = await knex("directus_roles")
.select("id")
.where("name", ROLE_NAME)
.first();
// if the role does not exists, do not proceed with migration
if (!roleRow) {
return;
}
if (roleRow && roleRow.id) {
const roleID = roleRow.id;
if (permTableExists) {
const columns = await knex("directus_permissions").columnInfo();
const columnNames = Object.keys(columns);
// remove permission for the collection if policy === roleID and policy column exists
if (columnNames.includes("policy")) {
await knex("directus_permissions")
.where("collection", COLLECTION_NAME)
.andWhere("policy", roleID)
.del();
}
// remove permission for the collection if role === roleID and role column exists
if (columnNames.includes("role")) {
await knex("directus_permissions")
.where("collection", COLLECTION_NAME)
.andWhere("role", roleID)
.del();
}
}
}
}