/*
Step: Accounts and access
Only shown when other people will use the server. Creates their accounts
in the administrator group or in the group of the people step, and, for
a limited group, sets who may use each drive the guide added.
*/
(function () {
"use strict";
var W = window.Wizard, T = W.T, esc = W.esc;
//Same rule as auth.ValidateUsername on the server: letters of any script,
//digits, and . - _ @ after the first character
var USERNAME = /^[\p{L}\p{N}][\p{L}\p{N}\p{M}._@-]{0,63}$/u;
function levelOptions(current) {
var levels = [
["readwrite", T("access/readwrite", "Everyone can open and change files")],
["readonly", T("access/readonly", "Everyone can open files, only administrators change them")],
["admins", T("access/admins", "Only administrators")]
];
return levels.map(function (l) {
return '";
}).join("");
}
function renderAccounts(el) {
var $list = $(el).find("#wzAccountList").empty();
var others = W.state.users.filter(function (u) { return u !== W.state.username; });
if (others.length === 0) {
$list.html('
' + esc(T("accounts/none", "No other accounts yet")) + "
");
return;
}
others.forEach(function (name) {
var created = W.progress.accounts.indexOf(name) >= 0;
$list.append('
' + W.icon("user") + "
" +
'
' + esc(name) + "
" +
'
' + esc(created ? T("accounts/created", "Added in this guide") : T("accounts/existing", "Existing account")) + "
");
});
window.WizardFillIcons($list[0]);
}
function addAccount(el, button) {
var $f = $(el).find("#wzAccountForm");
var username = $f.find("[data-field=username]").val().trim();
var password = $f.find("[data-field=password]").val();
var repeat = $f.find("[data-field=repeat]").val();
$f.find(".wz-input").removeClass("invalid");
if (!USERNAME.test(username) || username.indexOf("..") >= 0) {
$f.find("[data-field=username]").addClass("invalid").trigger("focus");
W.toast(T("accounts/username/invalid", "A username starts with a letter or number and can use letters, numbers, . - _ and @"), true);
return;
}
if (W.state.users.some(function (u) { return u.toLowerCase() === username.toLowerCase(); })) {
$f.find("[data-field=username]").addClass("invalid").trigger("focus");
W.toast(T("accounts/username/taken", "{name} already has an account", { name: username }), true);
return;
}
if (password.length < 6) {
$f.find("[data-field=password]").addClass("invalid").trigger("focus");
W.toast(T("accounts/password/short", "Use a password of at least 6 characters"), true);
return;
}
if (password !== repeat) {
$f.find("[data-field=repeat]").addClass("invalid").trigger("focus");
W.toast(T("accounts/password/mismatch", "The passwords do not match"), true);
return;
}
W.working(button, true);
W.api("POST", "system/auth/register", { username: username, password: password, group: W.peopleGroup() }).then(function () {
W.working(button, false);
W.state.users.push(username);
W.progress.accounts.push(username);
W.save();
$f.find("input").val("");
$f.find("[data-field=username]").trigger("focus");
W.toast(T("accounts/addedToast", "Account {name} created", { name: username }));
renderAccounts(el);
}, function (err) {
W.working(button, false);
W.toast(err.message, true);
});
}
function saveAccess() {
if (W.progress.people !== "group") {
return Promise.resolve();
}
var jobs = [];
$("#wzAccess select").each(function () {
var uuid = $(this).attr("data-uuid");
var level = $(this).val();
if (W.progress.access[uuid] === level) {
return;
}
var rule = { other: level === "admins" ? "denied" : level };
jobs.push(W.api("POST", "system/storage/access", { uuid: uuid, rule: JSON.stringify(rule) }).then(function () {
W.progress.access[uuid] = level;
}));
});
return Promise.all(jobs);
}
W.register({
id: "accounts",
visible: function () {
return W.progress.people === "admins" || W.progress.people === "group";
},
title: function () {
return T("step/accounts", "Accounts");
},
render: function (el) {
var admins = W.progress.people === "admins";
var html = '
' + esc(T("accounts/eyebrow", "Accounts")) + "
" +
'
' + esc(admins ? T("accounts/title/admins", "Add the people you trust") : T("accounts/title/group", "Add accounts for the others")) + "
" +
'
' + esc(T("accounts/lead", "Create an account for each person. Give them their username and password; they can change the password after signing in.")) + "