Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/create_test_users.php

<?php
/**
 * Create Test Users for Access Control Testing
 * Run this script once to create test users at different access levels
 */

require_once 'config/config.php';

// Check if user is logged in and is superuser
checkLogin();
if (!isSuperuser()) {
    die('<div style="padding: 20px; background: #fee; border: 2px solid #c00; color: #c00; font-family: Arial;">
    <h2>Access Denied</h2>
    <p>Only superusers can create test accounts.</p>
    <a href="dashboard.php" style="color: #00f;">Return to Dashboard</a>
    </div>');
}

$db = Database::getInstance()->getConnection();
$password = 'Test@2025';
$hash = password_hash($password, PASSWORD_BCRYPT);

$success = [];
$errors = [];

// Get first area, district, assembly for defaults
$area = $db->query("SELECT * FROM areas ORDER BY id LIMIT 1")->fetch();
$district = $db->query("SELECT * FROM districts ORDER BY id LIMIT 1")->fetch();
$assembly = $db->query("SELECT * FROM assemblies ORDER BY id LIMIT 1")->fetch();

if (!$area || !$district || !$assembly) {
    die('<div style="padding: 20px; background: #fee; border: 2px solid #c00; color: #c00; font-family: Arial;">
    <h2>Setup Required</h2>
    <p>Please ensure you have at least one Area, District, and Assembly created before running this script.</p>
    <a href="dashboard.php" style="color: #00f;">Return to Dashboard</a>
    </div>');
}

// 1. CREATE ASSEMBLY ADMIN
try {
    // Check if already exists
    $check = $db->prepare("SELECT id FROM users WHERE username = 'assembly_admin'");
    $check->execute();
    
    if ($check->fetch()) {
        $errors[] = "Assembly Admin user already exists";
    } else {
        $stmt = $db->prepare("
            INSERT INTO users (
                username, email, password_hash, full_name, phone,
                access_level, area_id, district_id, assembly_id, 
                is_superuser, is_active
            ) VALUES (
                :username, :email, :hash, :full_name, :phone,
                'assembly', :area_id, :district_id, :assembly_id,
                0, 1
            )
        ");
        
        $stmt->execute([
            'username' => 'assembly_admin',
            'email' => 'assembly.admin@test.com',
            'hash' => $hash,
            'full_name' => 'Assembly Admin (Test)',
            'phone' => '0200000001',
            'area_id' => $area['id'],
            'district_id' => $district['id'],
            'assembly_id' => $assembly['id']
        ]);
        
        $success[] = "✓ Assembly Admin created - Username: <strong>assembly_admin</strong> - Scope: {$assembly['assembly_name']}";
    }
} catch (Exception $e) {
    $errors[] = "Assembly Admin: " . $e->getMessage();
}

// 2. CREATE DISTRICT ADMIN
try {
    $check = $db->prepare("SELECT id FROM users WHERE username = 'district_admin'");
    $check->execute();
    
    if ($check->fetch()) {
        $errors[] = "District Admin user already exists";
    } else {
        $stmt = $db->prepare("
            INSERT INTO users (
                username, email, password_hash, full_name, phone,
                access_level, area_id, district_id, assembly_id, 
                is_superuser, is_active
            ) VALUES (
                :username, :email, :hash, :full_name, :phone,
                'district', :area_id, :district_id, NULL,
                0, 1
            )
        ");
        
        $stmt->execute([
            'username' => 'district_admin',
            'email' => 'district.admin@test.com',
            'hash' => $hash,
            'full_name' => 'District Admin (Test)',
            'phone' => '0200000002',
            'area_id' => $area['id'],
            'district_id' => $district['id']
        ]);
        
        $success[] = "✓ District Admin created - Username: <strong>district_admin</strong> - Scope: {$district['district_name']}";
    }
} catch (Exception $e) {
    $errors[] = "District Admin: " . $e->getMessage();
}

// 3. CREATE AREA ADMIN
try {
    $check = $db->prepare("SELECT id FROM users WHERE username = 'area_admin'");
    $check->execute();
    
    if ($check->fetch()) {
        $errors[] = "Area Admin user already exists";
    } else {
        $stmt = $db->prepare("
            INSERT INTO users (
                username, email, password_hash, full_name, phone,
                access_level, area_id, district_id, assembly_id, 
                is_superuser, is_active
            ) VALUES (
                :username, :email, :hash, :full_name, :phone,
                'area', :area_id, NULL, NULL,
                0, 1
            )
        ");
        
        $stmt->execute([
            'username' => 'area_admin',
            'email' => 'area.admin@test.com',
            'hash' => $hash,
            'full_name' => 'Area Admin (Test)',
            'phone' => '0200000003',
            'area_id' => $area['id']
        ]);
        
        $success[] = "✓ Area Admin created - Username: <strong>area_admin</strong> - Scope: {$area['area_name']}";
    }
} catch (Exception $e) {
    $errors[] = "Area Admin: " . $e->getMessage();
}

// Get all test users
$testUsers = $db->query("
    SELECT u.*, 
           a.assembly_name, 
           d.district_name, 
           ar.area_name
    FROM users u
    LEFT JOIN assemblies a ON u.assembly_id = a.id
    LEFT JOIN districts d ON u.district_id = d.id
    LEFT JOIN areas ar ON u.area_id = ar.id
    WHERE u.username IN ('assembly_admin', 'district_admin', 'area_admin')
    ORDER BY 
        CASE u.access_level
            WHEN 'area' THEN 1
            WHEN 'district' THEN 2
            WHEN 'assembly' THEN 3
        END
")->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Users Created</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-gray-100 p-8">
    <div class="max-w-4xl mx-auto">
        <div class="bg-white rounded-lg shadow-lg p-8">
            <h1 class="text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-users-cog text-blue-500 mr-3"></i>
                Test Users Creation Results
            </h1>
            
            <?php if (!empty($success)): ?>
                <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6">
                    <h2 class="text-lg font-semibold text-green-800 mb-3">
                        <i class="fas fa-check-circle mr-2"></i>Successfully Created
                    </h2>
                    <?php foreach ($success as $msg): ?>
                        <p class="text-green-700 mb-2"><?php echo $msg; ?></p>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            
            <?php if (!empty($errors)): ?>
                <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4 mb-6">
                    <h2 class="text-lg font-semibold text-yellow-800 mb-3">
                        <i class="fas fa-exclamation-triangle mr-2"></i>Notices
                    </h2>
                    <?php foreach ($errors as $msg): ?>
                        <p class="text-yellow-700 mb-2"><?php echo $msg; ?></p>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6">
                <h2 class="text-lg font-semibold text-blue-800 mb-2">
                    <i class="fas fa-key mr-2"></i>Login Credentials
                </h2>
                <p class="text-blue-700 mb-1">Password for all test accounts: <code class="bg-blue-200 px-2 py-1 rounded font-mono">Test@2025</code></p>
                <p class="text-blue-600 text-sm">Use these accounts to test access control restrictions</p>
            </div>
            
            <?php if (!empty($testUsers)): ?>
                <h2 class="text-xl font-semibold text-gray-800 mb-4">
                    <i class="fas fa-list mr-2"></i>Test User Accounts
                </h2>
                
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Username</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Email</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Access Level</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Scope</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($testUsers as $user): ?>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <span class="font-mono text-sm text-gray-900"><?php echo htmlspecialchars($user['username']); ?></span>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">
                                        <?php echo htmlspecialchars($user['email']); ?>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <?php
                                        $badges = [
                                            'area' => '<span class="px-2 py-1 text-xs font-semibold rounded-full bg-blue-100 text-blue-800">Area Admin</span>',
                                            'district' => '<span class="px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">District Admin</span>',
                                            'assembly' => '<span class="px-2 py-1 text-xs font-semibold rounded-full bg-yellow-100 text-yellow-800">Assembly Admin</span>'
                                        ];
                                        echo $badges[$user['access_level']] ?? $user['access_level'];
                                        ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-600">
                                        <?php
                                        if ($user['access_level'] === 'area') {
                                            echo '<i class="fas fa-map mr-1 text-blue-500"></i>' . htmlspecialchars($user['area_name']);
                                        } elseif ($user['access_level'] === 'district') {
                                            echo '<i class="fas fa-map-marked-alt mr-1 text-green-500"></i>' . htmlspecialchars($user['district_name']);
                                        } elseif ($user['access_level'] === 'assembly') {
                                            echo '<i class="fas fa-church mr-1 text-yellow-500"></i>' . htmlspecialchars($user['assembly_name']);
                                        }
                                        ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
            
            <div class="mt-8 pt-6 border-t border-gray-200">
                <h3 class="text-lg font-semibold text-gray-800 mb-3">
                    <i class="fas fa-vial mr-2 text-purple-500"></i>Testing Instructions
                </h3>
                <ol class="list-decimal list-inside space-y-2 text-gray-700">
                    <li>Log out from your current superuser account</li>
                    <li>Log in with one of the test accounts (password: <code class="bg-gray-200 px-2 py-1 rounded font-mono text-sm">Test@2025</code>)</li>
                    <li>Verify the dashboard shows only data for that access level</li>
                    <li>Check that members, events, programs are filtered correctly</li>
                    <li>Try accessing different modules to ensure proper restrictions</li>
                    <li>Check the access badge displayed at the top of the dashboard</li>
                </ol>
            </div>
            
            <div class="mt-6 flex gap-4">
                <a href="dashboard.php" class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600 transition">
                    <i class="fas fa-home mr-2"></i>Back to Dashboard
                </a>
                <a href="logout.php" class="bg-gray-500 text-white px-6 py-2 rounded-lg hover:bg-gray-600 transition">
                    <i class="fas fa-sign-out-alt mr-2"></i>Logout & Test
                </a>
                <a href="ACCESS_CONTROL_IMPLEMENTATION.md" class="bg-green-500 text-white px-6 py-2 rounded-lg hover:bg-green-600 transition" target="_blank">
                    <i class="fas fa-book mr-2"></i>View Documentation
                </a>
            </div>
        </div>
    </div>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists