chore: upload parte 1 - logica
DevSecOps Enterprise Pipeline / security-gate-and-deploy (push) Failing after 41s

This commit is contained in:
2026-06-01 15:35:53 +01:00
parent a63625d228
commit 9c14a19a87
127 changed files with 22255 additions and 0 deletions
+143
View File
@@ -0,0 +1,143 @@
import { NextResponse } from "next/server";
import { getDb } from "@/lib/db";
import bcrypt from "bcryptjs";
export async function PUT(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const sql = getDb();
const { id } = await params;
const body = await request.json();
const { email, name, role, is_active, permissions, password, new_password } = body;
const passwordToUpdate = password || new_password;
// Update user
await sql(
`UPDATE users SET email = $1, name = $2, role = $3, is_active = $4, updated_at = NOW()
WHERE id = $5`,
[email?.toLowerCase(), name, role || "user", is_active ?? true, Number(id)]
);
// Update password if provided
if (passwordToUpdate) {
const passwordHash = await bcrypt.hash(passwordToUpdate, 10);
await sql(`UPDATE users SET password_hash = $1 WHERE id = $2`, [
passwordHash,
Number(id),
]);
}
// Upsert permissions - convert sections array to individual flags
if (permissions) {
const sections: string[] = permissions.sections || [];
// First check if user_permissions record exists
const existingPerms = await sql(
`SELECT id FROM user_permissions WHERE user_id = $1`,
[Number(id)]
);
if (existingPerms.length === 0) {
// Insert new permissions record
await sql(
`INSERT INTO user_permissions (
user_id, can_view_financial, can_edit, can_create, can_delete, can_download,
can_view_dashboard, can_view_components, can_view_products, can_view_customers,
can_view_suppliers, can_view_production, can_view_stock_history, can_view_logistics,
can_view_stock_take, can_view_settings
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)`,
[
Number(id),
permissions.can_view_financial ?? false,
permissions.can_edit ?? false,
permissions.can_create ?? false,
permissions.can_delete ?? false,
permissions.can_download ?? false,
sections.includes("dashboard"),
sections.includes("components"),
sections.includes("products"),
sections.includes("customers"),
sections.includes("suppliers"),
sections.includes("production"),
sections.includes("stock-history"),
sections.includes("logistics"),
sections.includes("stock-take"),
sections.includes("settings") || role === "admin",
]
);
} else {
// Update existing permissions record
await sql(
`UPDATE user_permissions
SET can_view_financial = $1, can_edit = $2, can_create = $3, can_delete = $4, can_download = $5,
can_view_dashboard = $6, can_view_components = $7, can_view_products = $8, can_view_customers = $9,
can_view_suppliers = $10, can_view_production = $11, can_view_stock_history = $12, can_view_logistics = $13,
can_view_stock_take = $14, can_view_settings = $15, updated_at = NOW()
WHERE user_id = $16`,
[
permissions.can_view_financial ?? false,
permissions.can_edit ?? false,
permissions.can_create ?? false,
permissions.can_delete ?? false,
permissions.can_download ?? false,
sections.includes("dashboard"),
sections.includes("components"),
sections.includes("products"),
sections.includes("customers"),
sections.includes("suppliers"),
sections.includes("production"),
sections.includes("stock-history"),
sections.includes("logistics"),
sections.includes("stock-take"),
sections.includes("settings") || role === "admin",
Number(id),
]
);
}
}
// Fetch updated user
const users = await sql(
`SELECT u.id, u.email, u.name, u.role, u.is_active, u.created_at,
up.can_view_financial, up.can_edit, up.can_create, up.can_delete, up.can_download,
up.can_view_dashboard, up.can_view_components, up.can_view_products, up.can_view_customers,
up.can_view_suppliers, up.can_view_production, up.can_view_stock_history, up.can_view_logistics,
up.can_view_stock_take, up.can_view_settings
FROM users u
LEFT JOIN user_permissions up ON u.id = up.user_id
WHERE u.id = $1`,
[Number(id)]
);
return NextResponse.json(users[0]);
} catch (error) {
console.error("PUT user error:", error);
return NextResponse.json({ error: "Failed to update user" }, { status: 500 });
}
}
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const sql = getDb();
const { id } = await params;
// Delete sessions first
await sql(`DELETE FROM sessions WHERE user_id = $1`, [Number(id)]);
// Delete permissions
await sql(`DELETE FROM user_permissions WHERE user_id = $1`, [Number(id)]);
// Delete user
await sql(`DELETE FROM users WHERE id = $1`, [Number(id)]);
return NextResponse.json({ success: true });
} catch (error) {
console.error("DELETE user error:", error);
return NextResponse.json({ error: "Failed to delete user" }, { status: 500 });
}
}
+138
View File
@@ -0,0 +1,138 @@
import { NextResponse } from "next/server";
import { getDb } from "@/lib/db";
import bcrypt from "bcryptjs";
export async function GET() {
try {
const sql = getDb();
const rows = await sql(
`SELECT u.id, u.email, u.name, u.role, u.is_active, u.created_at,
up.can_view_financial, up.can_edit, up.can_create, up.can_delete, up.can_download,
up.can_view_dashboard, up.can_view_components, up.can_view_products, up.can_view_customers,
up.can_view_suppliers, up.can_view_production, up.can_view_stock_history, up.can_view_logistics,
up.can_view_stock_take, up.can_view_settings
FROM users u
LEFT JOIN user_permissions up ON u.id = up.user_id
ORDER BY u.created_at DESC`
);
// Transform rows to include permissions object with sections array
const users = rows.map((row: Record<string, unknown>) => {
const sections: string[] = [];
if (row.can_view_dashboard) sections.push("dashboard");
if (row.can_view_components) sections.push("components");
if (row.can_view_products) sections.push("products");
if (row.can_view_customers) sections.push("customers");
if (row.can_view_suppliers) sections.push("suppliers");
if (row.can_view_production) sections.push("production");
if (row.can_view_stock_history) sections.push("stock-history");
if (row.can_view_logistics) sections.push("logistics");
if (row.can_view_stock_take) sections.push("stock-take");
if (row.can_view_settings) sections.push("settings");
return {
id: row.id,
email: row.email,
name: row.name,
role: row.role,
is_active: row.is_active,
created_at: row.created_at,
permissions: {
sections,
can_view_financial: row.can_view_financial ?? false,
can_edit: row.can_edit ?? false,
can_create: row.can_create ?? false,
can_delete: row.can_delete ?? false,
can_download: row.can_download ?? false,
},
};
});
return NextResponse.json(users);
} catch (error) {
console.error("GET users error:", error);
return NextResponse.json({ error: "Failed to fetch users" }, { status: 500 });
}
}
export async function POST(request: Request) {
try {
const sql = getDb();
const body = await request.json();
const { email, name, password, role, permissions } = body;
if (!email || !name || !password) {
return NextResponse.json(
{ error: "Email, name, and password are required" },
{ status: 400 }
);
}
// Check if email exists
const existing = await sql(`SELECT id FROM users WHERE email = $1`, [
email.toLowerCase(),
]);
if (existing.length > 0) {
return NextResponse.json(
{ error: "Email already exists" },
{ status: 400 }
);
}
// Hash password
const passwordHash = await bcrypt.hash(password, 10);
// Create user
const users = await sql(
`INSERT INTO users (email, name, password_hash, role)
VALUES ($1, $2, $3, $4)
RETURNING id, email, name, role, is_active, created_at`,
[email.toLowerCase(), name, passwordHash, role || "user"]
);
const user = users[0];
// Create permissions - convert sections array to individual flags
const sections: string[] = permissions?.sections || [];
await sql(
`INSERT INTO user_permissions (
user_id, can_view_financial, can_edit, can_create, can_delete, can_download,
can_view_dashboard, can_view_components, can_view_products, can_view_customers,
can_view_suppliers, can_view_production, can_view_stock_history, can_view_logistics,
can_view_stock_take, can_view_settings
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)`,
[
user.id,
permissions?.can_view_financial ?? false,
permissions?.can_edit ?? false,
permissions?.can_create ?? false,
permissions?.can_delete ?? false,
permissions?.can_download ?? false,
sections.includes("dashboard"),
sections.includes("components"),
sections.includes("products"),
sections.includes("customers"),
sections.includes("suppliers"),
sections.includes("production"),
sections.includes("stock-history"),
sections.includes("logistics"),
sections.includes("stock-take"),
sections.includes("settings") || role === "admin",
]
);
return NextResponse.json({
...user,
permissions: permissions || {
can_view_financial: false,
can_edit: false,
can_create: false,
can_delete: false,
can_download: false,
},
});
} catch (error) {
console.error("POST user error:", error);
return NextResponse.json({ error: "Failed to create user" }, { status: 500 });
}
}