-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
171 lines (168 loc) · 7.65 KB
/
admin.html
File metadata and controls
171 lines (168 loc) · 7.65 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html,body{height:100%;margin:0}
body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif}
.layout{display:flex;height:100%}
aside{width:220px;border-right:1px solid #ddd;padding:12px;box-sizing:border-box}
nav a{display:block;padding:8px 6px;text-decoration:none;color:#222;border-radius:6px}
nav a.active{background:#efefef}
main{flex:1;display:flex;flex-direction:column}
header{padding:16px;border-bottom:1px solid #ddd}
.content{padding:16px;overflow:auto}
.row{margin:8px 0}
label{display:block;margin-bottom:4px}
input,select{height:32px}
button{height:32px}
.actions{margin-top:12px}
.section{margin-top:16px}
table{width:100%;border-collapse:collapse;margin-top:8px}
th,td{border:1px solid #ddd;padding:6px;text-align:left}
#loader{position:fixed;top:0;left:0;width:100%;height:100%;display:none;align-items:center;justify-content:center;flex-direction:column;background:rgba(0,0,0,0.2);backdrop-filter:blur(6px)}
.spinner{width:48px;height:48px;border:5px solid #c7c7c7;border-top-color:#333;border-radius:50%;animation:spin 1s linear infinite;margin-bottom:12px}
@keyframes spin{to{transform:rotate(360deg)}}
#loader-text{font-weight:600}
dialog{border:none;border-radius:8px;padding:16px}
dialog::backdrop{background:rgba(0,0,0,0.25);backdrop-filter:blur(6px)}
</style>
</head>
<body>
<?!= HtmlService.createHtmlOutputFromFile('common').getContent(); ?>
<div class="layout">
<aside>
<nav>
<!-- Admin navigation -->
<a href="#" id="nav-overview" onclick="navigate('overview')">Overview</a>
<a href="#" id="nav-profile" onclick="navigate('profile')">Profile</a>
<a href="#" id="nav-admin" onclick="navigate('admin')">Admin</a>
</nav>
</aside>
<main role="main">
<header>
<h2>Welcome <?= name ?> <?= lastName ?></h2>
<p id="email"><?= email ?></p>
<p id="role">Role: <?= role ?></p>
</header>
<div class="content">
<!-- Overview section -->
<section id="overview" class="section" aria-labelledby="overview-title">
<h3 id="overview-title">Overview</h3>
<p>Admin overview.</p>
</section>
<!-- Profile section -->
<section id="profile" class="section" aria-labelledby="profile-title" hidden>
<h3 id="profile-title">Profile</h3>
<div class="row"><label>Full Name</label><input value="<?= name ?> <?= lastName ?>" readonly></div>
<div class="row"><label>Email</label><input value="<?= email ?>" readonly></div>
</section>
<!-- Admin panel: manage roles, register users, view list -->
<section id="admin" class="section" aria-labelledby="admin-title" hidden>
<h3 id="admin-title">Admin Panel</h3>
<div class="row">
<h4>Change User Role</h4>
<label for="user-select">User</label>
<select id="user-select"></select>
<label for="role-select" style="margin-top:8px">New Role</label>
<select id="role-select"><option>User</option><option>Admin</option></select>
<div class="actions"><button type="button" onclick="changeRole()">Update Role</button></div>
</div>
<div class="row">
<h4>Register New User</h4>
<label for="new-username">Username</label>
<input id="new-username" type="text" autocomplete="off">
<label for="new-password" style="margin-top:8px">Password</label>
<input id="new-password" type="password" autocomplete="off">
<label for="new-name" style="margin-top:8px">Name</label>
<input id="new-name" type="text" autocomplete="off">
<label for="new-lastname" style="margin-top:8px">Last Name</label>
<input id="new-lastname" type="text" autocomplete="off">
<label for="new-email" style="margin-top:8px">Email</label>
<input id="new-email" type="email" autocomplete="off">
<label for="new-role" style="margin-top:8px">Role</label>
<select id="new-role"><option>User</option><option>Admin</option></select>
<div class="actions"><button type="button" onclick="createUser()">Create User</button></div>
</div>
<div class="row">
<h4>Users</h4>
<table>
<thead><tr><th>Username</th><th>Name</th><th>Last Name</th><th>Email</th><th>Status</th><th>Role</th></tr></thead>
<tbody id="users-table"></tbody>
</table>
</div>
</section>
</div>
</main>
</div>
<script>
// Current logged-in admin username provided by server template
const ACTOR='<?= username ?>';
// Declares section ids used by shared navigation helper
window.SECTIONS=['overview','profile','admin'];
// Initializes default view and loads user list
function init(){
navigate('overview');
loadUsers();
}
// Loads users from server and renders select + table
function loadUsers(){
showLoader('Loading users...');
google.script.run.withSuccessHandler(function(res){
hideLoader();
if(!res||!res.success){showDialog('Failed to load users');return;}
const users=res.users||[];
const sel=document.getElementById('user-select'); sel.innerHTML='';
const tbody=document.getElementById('users-table'); tbody.innerHTML='';
users.forEach(u=>{
var opt=document.createElement('option');opt.value=u.username;opt.textContent=u.username;sel.appendChild(opt);
var tr=document.createElement('tr');
tr.innerHTML='<td>'+u.username+'</td><td>'+u.name+'</td><td>'+u.lastName+'</td><td>'+u.email+'</td><td>'+u.status+'</td><td>'+u.role+'</td>';
tbody.appendChild(tr);
});
}).getUsers(ACTOR);
}
// Sends role update request for selected user
function changeRole(){
const user=document.getElementById('user-select').value;
const role=document.getElementById('role-select').value;
if(!user||!role){showDialog('Select user and role');return;}
showLoader('Updating role...');
google.script.run.withSuccessHandler(function(res){
hideLoader();
if(res&&res.success){showDialog('Role updated');loadUsers();}
else{showDialog('Failed to update role');}
}).updateUserRole(ACTOR,user,role);
}
// Validates payload and requests server to create user
function createUser(){
const payload={
username:document.getElementById('new-username').value.trim(),
password:document.getElementById('new-password').value.trim(),
name:document.getElementById('new-name').value.trim(),
lastName:document.getElementById('new-lastname').value.trim(),
email:document.getElementById('new-email').value.trim(),
role:document.getElementById('new-role').value
};
if(!payload.username||!payload.password||!payload.name||!payload.lastName||!payload.email){showDialog('Complete all fields');return;}
if(!/^([^\s@]+)@([^\s@]+)\.[^\s@]+$/.test(payload.email)){showDialog('Invalid email');return;}
if(!(payload.password.length>=8 && /[A-Za-z]/.test(payload.password) && /[0-9]/.test(payload.password))){showDialog('Password must be at least 8 chars with letters and numbers');return;}
showLoader('Creating user...');
google.script.run.withSuccessHandler(function(res){
hideLoader();
if(res&&res.success){
document.getElementById('new-username').value='';
document.getElementById('new-password').value='';
document.getElementById('new-name').value='';
document.getElementById('new-lastname').value='';
document.getElementById('new-email').value='';
document.getElementById('new-role').value='User';
showDialog('User created');
loadUsers();
} else { showDialog(res&&res.message?res.message:'Failed to create user'); }
}).createUser(ACTOR,payload);
}
document.addEventListener('DOMContentLoaded',init);
</script>
</body>
</html>