123 lines
4.6 KiB
TypeScript
123 lines
4.6 KiB
TypeScript
import { createDirectus, rest, authentication, createCollection, createDashboard, createPanel, createItems, createPermission, readPolicies, readRoles, readUsers } from '@directus/sdk';
|
|
|
|
async function setupHardened() {
|
|
const url = 'http://localhost:8059';
|
|
const email = 'marc@mintel.me';
|
|
const password = 'Tim300493.';
|
|
|
|
console.log('🚀 v11 HARDENED SETUP START...');
|
|
|
|
const client = createDirectus(url)
|
|
.with(authentication('json'))
|
|
.with(rest());
|
|
|
|
try {
|
|
console.log('🔑 Authenticating...');
|
|
await client.login(email, password);
|
|
|
|
console.log('👤 Identifying IDs...');
|
|
const me = await client.request(readUsers({ filter: { email: { _eq: email } } }));
|
|
const adminUser = me[0];
|
|
const roles = await client.request(readRoles());
|
|
const adminRole = roles.find(r => r.name === 'Administrator');
|
|
const policies = await client.request(readPolicies());
|
|
const adminPolicy = policies.find(p => p.name === 'Administrator');
|
|
|
|
console.log(`- User: ${adminUser.id}`);
|
|
console.log(`- Role: ${adminRole?.id}`);
|
|
console.log(`- Policy: ${adminPolicy?.id}`);
|
|
|
|
if (adminPolicy && adminRole) {
|
|
console.log('🔗 Linking Role -> Policy...');
|
|
try {
|
|
await client.request(() => ({
|
|
path: '/access',
|
|
method: 'POST',
|
|
body: JSON.stringify({ role: adminRole.id, policy: adminPolicy.id })
|
|
}));
|
|
} catch (e) { }
|
|
|
|
console.log('🔗 Linking User -> Policy (individual)...');
|
|
try {
|
|
await client.request(() => ({
|
|
path: '/access',
|
|
method: 'POST',
|
|
body: JSON.stringify({ user: adminUser.id, policy: adminPolicy.id })
|
|
}));
|
|
} catch (e) { }
|
|
}
|
|
|
|
console.log('🏗️ Creating Collection "visual_feedback"...');
|
|
try {
|
|
await client.request(createCollection({
|
|
collection: 'visual_feedback',
|
|
meta: { icon: 'feedback', display_template: '{{user_name}}: {{text}}' },
|
|
fields: [
|
|
{ field: 'id', type: 'uuid', schema: { is_primary_key: true } },
|
|
{ field: 'status', type: 'string', schema: { default_value: 'open' }, meta: { interface: 'select-dropdown' } },
|
|
{ field: 'url', type: 'string' },
|
|
{ field: 'text', type: 'text' },
|
|
{ field: 'user_name', type: 'string' },
|
|
{ field: 'date_created', type: 'timestamp', schema: { default_value: 'NOW()' } }
|
|
]
|
|
} as any));
|
|
} catch (e) {
|
|
console.log(' (Collection might already exist)');
|
|
}
|
|
|
|
if (adminPolicy) {
|
|
console.log('🔐 Granting ALL permissions to Administrator Policy...');
|
|
for (const action of ['create', 'read', 'update', 'delete']) {
|
|
try {
|
|
await client.request(createPermission({
|
|
collection: 'visual_feedback',
|
|
action,
|
|
fields: ['*'],
|
|
policy: adminPolicy.id
|
|
} as any));
|
|
} catch (e) { }
|
|
}
|
|
}
|
|
|
|
console.log('💉 Injecting Demo Item...');
|
|
try {
|
|
await client.request(createItems('visual_feedback', [
|
|
{ user_name: 'Antigravity', text: 'v11 Recovery Successful', status: 'open' }
|
|
]));
|
|
} catch (e) { }
|
|
|
|
console.log('📊 Recreating Dashboard...');
|
|
const dash = await client.request(createDashboard({
|
|
name: 'Feedback Final',
|
|
icon: 'check_circle',
|
|
color: '#00FF00'
|
|
}));
|
|
|
|
await client.request(createPanel({
|
|
dashboard: dash.id,
|
|
name: 'Total Feedbacks',
|
|
type: 'metric',
|
|
width: 12,
|
|
height: 6,
|
|
position_x: 1,
|
|
position_y: 1,
|
|
options: { collection: 'visual_feedback', function: 'count', field: 'id' }
|
|
} as any));
|
|
|
|
console.log('✅ Setup Complete! Setting static token...');
|
|
await client.request(() => ({
|
|
path: `/users/${adminUser.id}`,
|
|
method: 'PATCH',
|
|
body: JSON.stringify({ token: '59fb8f4c1a51b18fe28ad947f713914e' })
|
|
}));
|
|
|
|
console.log('✨ ALL DONE.');
|
|
|
|
} catch (e: any) {
|
|
console.error('❌ CRITICAL FAILURE:', e);
|
|
if (e.errors) console.error(JSON.stringify(e.errors, null, 2));
|
|
}
|
|
}
|
|
|
|
setupHardened();
|