chore: restart the repository with a clean history
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
This commit is contained in:
438 files changed
+45070
No files matched your search
@@ -0,0 +1,91 @@
|
||||
const Account = require("../models/Account");
|
||||
const Session = require("../models/Session");
|
||||
const Device = require("../models/Device");
|
||||
const Invite = require("../models/Invite");
|
||||
const { hashPassword, verifyPassword } = require("../utils/password");
|
||||
|
||||
const ABSENT = hashPassword("account-that-does-not-exist");
|
||||
|
||||
const USERNAME_REGEX = /^[a-zA-Z0-9._-]{3,32}$/;
|
||||
const ROLES = ["admin", "user"];
|
||||
|
||||
const describe = account => ({
|
||||
id: account.id,
|
||||
username: account.username,
|
||||
role: account.role,
|
||||
quotas: account.quotas ? JSON.parse(account.quotas) : null,
|
||||
createdAt: account.createdAt,
|
||||
});
|
||||
|
||||
const validate = ({ username, password }) => {
|
||||
if (username !== undefined && !USERNAME_REGEX.test(String(username || ""))) {
|
||||
return "The username must be 3 to 32 letters, digits, dots, dashes or underscores";
|
||||
}
|
||||
if (password !== undefined && String(password || "").length < 8) return "The password must be at least 8 characters";
|
||||
return null;
|
||||
};
|
||||
|
||||
const count = () => Account.count();
|
||||
|
||||
const byUsername = username => Account.findOne({ where: { username: String(username || "") } });
|
||||
|
||||
const byId = id => Account.findByPk(id);
|
||||
|
||||
const list = () => Account.findAll({ order: [["id", "ASC"]] }).then(rows => rows.map(describe));
|
||||
|
||||
const create = async ({ username, password, role = "user" }) => {
|
||||
const problem = validate({ username, password });
|
||||
if (problem) return { code: 400, message: problem };
|
||||
if (!ROLES.includes(role)) return { code: 400, message: `The role must be one of ${ROLES.join(", ")}` };
|
||||
if (await byUsername(username)) return { code: 409, message: "That username is taken" };
|
||||
|
||||
await Account.create({ username, passwordHash: hashPassword(password), role });
|
||||
return { account: describe(await byUsername(username)) };
|
||||
};
|
||||
|
||||
const adminCount = () => Account.count({ where: { role: "admin" } });
|
||||
|
||||
const update = async (id, { role, password, quotas }) => {
|
||||
const account = await byId(id);
|
||||
if (!account) return { code: 404, message: "That account does not exist" };
|
||||
|
||||
const values = {};
|
||||
|
||||
if (role !== undefined && role !== account.role) {
|
||||
if (!ROLES.includes(role)) return { code: 400, message: `The role must be one of ${ROLES.join(", ")}` };
|
||||
if (account.role === "admin" && await adminCount() <= 1) return { code: 409, message: "The last admin cannot lose the role" };
|
||||
values.role = role;
|
||||
}
|
||||
|
||||
if (password !== undefined) {
|
||||
const problem = validate({ password });
|
||||
if (problem) return { code: 400, message: problem };
|
||||
values.passwordHash = hashPassword(password);
|
||||
await Session.destroy({ where: { accountId: account.id } });
|
||||
}
|
||||
|
||||
if (quotas !== undefined) values.quotas = quotas ? JSON.stringify(quotas) : null;
|
||||
|
||||
if (Object.keys(values).length) await Account.update(values, { where: { id: account.id } });
|
||||
return { account: describe(await byId(account.id)) };
|
||||
};
|
||||
|
||||
const remove = async id => {
|
||||
const account = await byId(id);
|
||||
if (!account) return { code: 404, message: "That account does not exist" };
|
||||
if (account.role === "admin" && await adminCount() <= 1) return { code: 409, message: "The last admin cannot be deleted" };
|
||||
|
||||
await Session.destroy({ where: { accountId: account.id } });
|
||||
await Device.destroy({ where: { accountId: account.id } });
|
||||
await Invite.destroy({ where: { accountId: account.id } });
|
||||
await Account.destroy({ where: { id: account.id } });
|
||||
return { message: "Account deleted" };
|
||||
};
|
||||
|
||||
const checkCredentials = async ({ username, password } = {}) => {
|
||||
const account = await byUsername(username);
|
||||
const matches = await verifyPassword(String(password || ""), account ? account.passwordHash : ABSENT);
|
||||
return account && matches ? account : null;
|
||||
};
|
||||
|
||||
module.exports = { describe, validate, count, byId, byUsername, list, create, update, remove, checkCredentials, USERNAME_REGEX };
|
||||
Reference in new issue
Block a user