102 lines
3.8 KiB
JavaScript
102 lines
3.8 KiB
JavaScript
$(document).ready(function() {
|
|
// Check if admin
|
|
$.get('/api/auth/me')
|
|
.done(function(user) {
|
|
if(user.role !== 'admin') {
|
|
alert("Kein Zugriff!");
|
|
window.location.href = '/';
|
|
} else {
|
|
loadUsers();
|
|
loadAssignmentImages();
|
|
$('body').removeClass('is-preload');
|
|
}
|
|
})
|
|
.fail(function() {
|
|
window.location.href = '/';
|
|
});
|
|
|
|
$('#logout-btn').click(function(e) {
|
|
e.preventDefault();
|
|
$.post('/api/auth/logout').done(function() { window.location.href = '/'; });
|
|
});
|
|
|
|
function loadUsers() {
|
|
$.get('/api/admin/users').done(function(users) {
|
|
$('#users-tbody').empty();
|
|
$('#user-select').html('<option value="">Nutzer wählen...</option>');
|
|
users.forEach(function(u) {
|
|
$('#users-tbody').append(
|
|
'<tr>' +
|
|
'<td>' + u.id + '</td>' +
|
|
'<td>' + u.username + ' ' + (u.role === 'admin' ? '(Admin)' : '') + '</td>' +
|
|
'<td>' + (u.id !== 1 ? '<button onclick="deleteUser(' + u.id + ')">Löschen</button>' : '') + '</td>' +
|
|
'</tr>'
|
|
);
|
|
if(u.role !== 'admin') {
|
|
$('#user-select').append('<option value="' + u.id + '">' + u.username + '</option>');
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
window.deleteUser = function(id) {
|
|
if(confirm("Nutzer loeschen?")) {
|
|
$.ajax({url: '/api/admin/users/'+id, type: 'DELETE'}).done(loadUsers);
|
|
}
|
|
};
|
|
|
|
$('#create-user-form').submit(function(e) {
|
|
e.preventDefault();
|
|
$.post('/api/admin/users', { username: $('#new-username').val(), password: $('#new-password').val(), role: 'user' })
|
|
.done(function() {
|
|
$('#new-username').val('');
|
|
$('#new-password').val('');
|
|
loadUsers();
|
|
}).fail(function(err) { alert("Fehler: " + err.responseJSON.error); });
|
|
});
|
|
|
|
function loadAssignmentImages() {
|
|
$.get('/images/fulls/').done(function(files) {
|
|
$('#image-checkboxes').empty();
|
|
files.forEach(function(f) {
|
|
$('#image-checkboxes').append(
|
|
'<label class="img-check">' +
|
|
'<img src="/images/thumbs/' + encodeURIComponent(f.name) + '" />' +
|
|
'<br/><input type="checkbox" value="' + f.name + '" class="img-cb"/>' +
|
|
'</label>'
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
$('#user-select').change(function() {
|
|
var uid = $(this).val();
|
|
$('.img-cb').prop('checked', false);
|
|
if(!uid) return;
|
|
$.get('/api/admin/assign').done(function(assignments) {
|
|
assignments.filter(function(a) { return a.user_id == uid; }).forEach(function(a) {
|
|
$('.img-cb[value="' + a.image_name + '"]').prop('checked', true);
|
|
});
|
|
});
|
|
});
|
|
|
|
$('#save-assignments').click(function() {
|
|
var uid = $('#user-select').val();
|
|
if(!uid) return alert('Bitte Nutzer waehlen');
|
|
var selected = [];
|
|
$('.img-cb:checked').each(function() { selected.push($(this).val()); });
|
|
|
|
$.post('/api/admin/assign', { user_id: uid, image_names: selected })
|
|
.done(function() { alert("Zuweisung gespeichert!"); });
|
|
});
|
|
|
|
$('#update-btn').click(function() {
|
|
if(!confirm("Achtung: Dies startet den Server auf dem NAS neu. Fortfahren?")) return;
|
|
$('#update-status').text('Update laeuft...');
|
|
$.post('/api/admin/update').done(function(res) {
|
|
$('#update-status').text(res.message);
|
|
setTimeout(function() { window.location.reload(); }, 5000);
|
|
});
|
|
});
|
|
});
|