File size: 4,968 Bytes
d4672d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{% extends "base.html" %} {% block title %}Admin Dashboard - Grimvault{%
endblock %} {% block content %}
<div class="container">
    <h1>Admin Dashboard</h1>
    <table id="user-accounts">
        <thead>
            <tr>
                <th>Username</th>
                <th>Created At</th>
                <th>Last Active</th>
                <th>Storage Used</th>
                <th>Storage Limit</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for account in accounts %}
            <tr>
                <td>{{ account.username }}</td>
                <td>{{ account.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td>{{ account.last_active.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td>
                    {{ (account.storage_used / 1024 / 1024) | round(2) }} MB
                </td>
                <td>
                    {{ (account.storage_limit / 1024 / 1024 / 1024) | round(2)
                    }} GB
                </td>
                <td>{{ 'Banned' if account.is_banned else 'Active' }}</td>
                <td>
                    <button
                        class="btn btn-secondary update-storage"
                        data-username="{{ account.username }}"
                    >
                        Update Storage
                    </button>
                    <button
                        class="btn btn-warning toggle-ban"
                        data-username="{{ account.username }}"
                        data-banned="{{ account.is_banned }}"
                    >
                        {{ 'Unban' if account.is_banned else 'Ban' }}
                    </button>
                    <button
                        class="btn btn-danger delete-account"
                        data-username="{{ account.username }}"
                    >
                        Delete
                    </button>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %} {% block scripts %}
<script>
    document.addEventListener("DOMContentLoaded", () => {
        const userAccounts = document.getElementById("user-accounts");

        userAccounts.addEventListener("click", async (e) => {
            const username = e.target.dataset.username;

            if (e.target.classList.contains("update-storage")) {
                const newLimit = prompt("Enter new storage limit in GB:");
                if (newLimit) {
                    try {
                        const response = await axios.post(
                            '{{ url_for("admin.update_storage") }}',
                            {
                                username: username,
                                new_limit: newLimit * 1024 * 1024 * 1024, // Convert GB to bytes
                            },
                        );
                        alert(response.data.message);
                        location.reload();
                    } catch (error) {
                        alert(
                            "Failed to update storage: " +
                                error.response.data.error,
                        );
                    }
                }
            } else if (e.target.classList.contains("toggle-ban")) {
                const currentStatus = e.target.dataset.banned === "true";
                const newStatus = !currentStatus;
                try {
                    const response = await axios.post(
                        '{{ url_for("admin.ban_user_route") }}',
                        {
                            username: username,
                            ban_status: newStatus,
                        },
                    );
                    alert(response.data.message);
                    location.reload();
                } catch (error) {
                    alert(
                        "Failed to update ban status: " +
                            error.response.data.error,
                    );
                }
            } else if (e.target.classList.contains("delete-account")) {
                if (
                    confirm(
                        `Are you sure you want to delete the account for ${username}?`,
                    )
                ) {
                    try {
                        const response = await axios.delete(
                            `{{ url_for("admin.admin_delete_account", username="") }}${username}`,
                        );
                        alert(response.data.message);
                        location.reload();
                    } catch (error) {
                        alert(
                            "Failed to delete account: " +
                                error.response.data.error,
                        );
                    }
                }
            }
        });
    });
</script>
{% endblock %}