ФУНКЦИИ - NODEDC LAUNCHER: регистрация по инвайту
This commit is contained in:
@@ -461,6 +461,21 @@ export function createControlPlaneStore({ projectRoot }) {
|
||||
};
|
||||
}
|
||||
|
||||
function prepareInviteRegistration(token, payload = {}) {
|
||||
const data = readData();
|
||||
const result = applyInviteRegistration(data, token, payload, { commit: false });
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function commitInviteRegistration(token, payload = {}, provisioning) {
|
||||
const data = readData();
|
||||
const result = applyInviteRegistration(data, token, payload, { commit: true, provisioning });
|
||||
|
||||
await writeData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function acceptInvite(token, identity) {
|
||||
const data = readData();
|
||||
const invite = findInviteByToken(data, token);
|
||||
@@ -997,8 +1012,10 @@ export function createControlPlaneStore({ projectRoot }) {
|
||||
deleteMembership,
|
||||
deleteService,
|
||||
acceptInvite,
|
||||
commitInviteRegistration,
|
||||
getInviteByToken,
|
||||
getSnapshot,
|
||||
prepareInviteRegistration,
|
||||
readData,
|
||||
replaceData,
|
||||
reorderServices,
|
||||
@@ -1169,6 +1186,94 @@ function toPublicClient(client) {
|
||||
};
|
||||
}
|
||||
|
||||
function applyInviteRegistration(data, token, payload, { commit, provisioning = null } = {}) {
|
||||
const invite = findInviteByToken(data, token);
|
||||
const client = findById(data.clients, invite.clientId, "client");
|
||||
const now = isoNow();
|
||||
const email = invite.email.toLowerCase();
|
||||
const name = optionalString(payload?.name, email.split("@")[0]);
|
||||
|
||||
validateInviteCanBeRegistered(invite);
|
||||
|
||||
let user = data.users.find((item) => item.email.toLowerCase() === email);
|
||||
|
||||
if (user?.authentikUserId && !provisioning) {
|
||||
throw new Error("Аккаунт уже существует. Войдите под почтой инвайта.");
|
||||
}
|
||||
|
||||
if (user) {
|
||||
user.name = name;
|
||||
user.globalStatus = "active";
|
||||
user.authentikUserId = provisioning?.authentikUserId ?? user.authentikUserId ?? null;
|
||||
user.updatedAt = now;
|
||||
} else {
|
||||
user = {
|
||||
id: uniqueId(data.users, "user", email),
|
||||
authentikUserId: provisioning?.authentikUserId ?? null,
|
||||
name,
|
||||
email,
|
||||
phone: null,
|
||||
position: null,
|
||||
notes: `Создан через публичную регистрацию по инвайту клиента ${client.name}.`,
|
||||
avatarUrl: null,
|
||||
globalStatus: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
data.users.push(user);
|
||||
}
|
||||
|
||||
let membership = data.memberships.find((item) => item.clientId === invite.clientId && item.userId === user.id);
|
||||
|
||||
if (membership) {
|
||||
membership.role = invite.role;
|
||||
membership.status = "active";
|
||||
membership.updatedAt = now;
|
||||
} else {
|
||||
membership = {
|
||||
id: uniqueId(data.memberships, "mem", `${invite.clientId}-${email}`),
|
||||
clientId: invite.clientId,
|
||||
userId: user.id,
|
||||
role: invite.role,
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
data.memberships.push(membership);
|
||||
}
|
||||
|
||||
invite.status = "accepted";
|
||||
invite.updatedAt = now;
|
||||
markPendingSync(data, user, "user", email);
|
||||
|
||||
if (commit) {
|
||||
addAuditEvent(data, { id: user.id, name: user.name, email: user.email, source: "invite" }, {
|
||||
action: "Регистрация по инвайту",
|
||||
objectType: "invite",
|
||||
objectName: invite.email,
|
||||
clientId: client.id,
|
||||
result: "success",
|
||||
details: `Role: ${invite.role}`,
|
||||
});
|
||||
}
|
||||
|
||||
return { invite, client, user, membership, data };
|
||||
}
|
||||
|
||||
function validateInviteCanBeRegistered(invite) {
|
||||
if (invite.status === "accepted") {
|
||||
throw new Error("Инвайт уже принят");
|
||||
}
|
||||
|
||||
if (invite.status === "revoked") {
|
||||
throw new Error("Инвайт отозван");
|
||||
}
|
||||
|
||||
if (invite.status === "expired" || isInviteExpired(invite)) {
|
||||
throw new Error("Срок действия инвайта истёк");
|
||||
}
|
||||
}
|
||||
|
||||
function findById(items, id, label) {
|
||||
const item = items.find((candidate) => candidate.id === id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user