mirror of
https://github.com/freedbygrace/ActiveDirectoryManager.git
synced 2026-07-26 11:59:14 +00:00
a872feecd2
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 705f2157-ef97-4fbd-89e4-8c7f2ecaea90 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7ed01c5f-a82d-405a-b728-b2e3d127c60c/994ee92d-6a6d-4ca4-99c0-fe0dbc7f160b.jpg
99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
import { migrate } from 'drizzle-orm/node-postgres/migrator';
|
|
import pg from 'pg';
|
|
import * as schema from './shared/schema';
|
|
|
|
const { Pool } = pg;
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
});
|
|
|
|
const db = drizzle(pool, { schema });
|
|
|
|
async function main() {
|
|
console.log('Applying migrations...');
|
|
|
|
try {
|
|
// Apply the migrations
|
|
await migrate(db, { migrationsFolder: './migrations' });
|
|
console.log('Migrations applied successfully');
|
|
|
|
// Insert default roles if they don't exist
|
|
await db.transaction(async (tx) => {
|
|
const adminRoleExists = await tx.query.roles.findFirst({
|
|
where: (roles, { eq }) => eq(roles.name, 'admin')
|
|
});
|
|
|
|
if (!adminRoleExists) {
|
|
console.log('Creating admin role...');
|
|
const [adminRole] = await tx.insert(schema.roles).values({
|
|
name: 'admin',
|
|
description: 'Administrator with full system access',
|
|
isDefault: false
|
|
}).returning();
|
|
|
|
// Add all permissions to admin role
|
|
const permissions = Object.values(schema.PERMISSIONS);
|
|
await Promise.all(permissions.map(permission =>
|
|
tx.insert(schema.rolePermissions).values({
|
|
roleId: adminRole.id,
|
|
permission
|
|
})
|
|
));
|
|
}
|
|
|
|
const userRoleExists = await tx.query.roles.findFirst({
|
|
where: (roles, { eq }) => eq(roles.name, 'user')
|
|
});
|
|
|
|
if (!userRoleExists) {
|
|
console.log('Creating user role...');
|
|
const [userRole] = await tx.insert(schema.roles).values({
|
|
name: 'user',
|
|
description: 'Regular user with limited access',
|
|
isDefault: true
|
|
}).returning();
|
|
|
|
// Add basic permissions to user role
|
|
const basicPermissions = [
|
|
schema.PERMISSIONS.VIEW_USERS,
|
|
schema.PERMISSIONS.VIEW_LDAP_CONNECTIONS,
|
|
schema.PERMISSIONS.VIEW_AD_USERS,
|
|
schema.PERMISSIONS.VIEW_AD_GROUPS,
|
|
schema.PERMISSIONS.VIEW_AD_OUS,
|
|
schema.PERMISSIONS.VIEW_AD_COMPUTERS,
|
|
schema.PERMISSIONS.VIEW_AD_DOMAINS
|
|
];
|
|
|
|
await Promise.all(basicPermissions.map(permission =>
|
|
tx.insert(schema.rolePermissions).values({
|
|
roleId: userRole.id,
|
|
permission
|
|
})
|
|
));
|
|
}
|
|
|
|
const apiRoleExists = await tx.query.roles.findFirst({
|
|
where: (roles, { eq }) => eq(roles.name, 'api')
|
|
});
|
|
|
|
if (!apiRoleExists) {
|
|
console.log('Creating api role...');
|
|
await tx.insert(schema.roles).values({
|
|
name: 'api',
|
|
description: 'API access with customizable permissions',
|
|
isDefault: false
|
|
});
|
|
}
|
|
});
|
|
|
|
console.log('Setup completed successfully');
|
|
} catch (error) {
|
|
console.error('Migration failed:', error);
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
main();
|