The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/apps/meteor/app/lib/server/functions/setUserActiveStatus.ts

142 lines
5.1 KiB

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base';
import type { IUser, IUserEmail } from '@rocket.chat/core-typings';
import { isUserFederated, isDirectMessageRoom } from '@rocket.chat/core-typings';
import { Rooms, Users, Subscriptions } from '@rocket.chat/models';
import * as Mailer from '../../../mailer/server/api';
import { settings } from '../../../settings/server';
import { callbacks } from '../../../../lib/callbacks';
import { relinquishRoomOwnerships } from './relinquishRoomOwnerships';
import { closeOmnichannelConversations } from './closeOmnichannelConversations';
import { shouldRemoveOrChangeOwner, getSubscribedRoomsForUserWithDetails } from './getRoomsWithSingleOwner';
import { getUserSingleOwnedRooms } from './getUserSingleOwnedRooms';
async function reactivateDirectConversations(userId: string) {
// since both users can be deactivated at the same time, we should just reactivate rooms if both users are active
// for that, we need to fetch the direct messages, fetch the users involved and then the ids of rooms we can reactivate
const directConversations = await Rooms.getDirectConversationsByUserId(userId, {
projection: { _id: 1, uids: 1, t: 1 },
}).toArray();
const userIds = directConversations.reduce<string[]>((acc: string[], r) => {
if (isDirectMessageRoom(r)) {
acc.push(...r.uids);
}
return acc;
}, []);
const uniqueUserIds = [...new Set(userIds)];
const activeUsers = await Users.findActiveByUserIds(uniqueUserIds, { projection: { _id: 1 } }).toArray();
const activeUserIds = activeUsers.map((u: IUser) => u._id);
const roomsToReactivate = directConversations.reduce((acc: string[], room) => {
const otherUserId = isDirectMessageRoom(room) ? room.uids.find((u: string) => u !== userId) : undefined;
if (otherUserId && activeUserIds.includes(otherUserId)) {
acc.push(room._id);
}
return acc;
}, []);
await Rooms.setDmReadOnlyByUserId(userId, roomsToReactivate, false, false);
}
export async function setUserActiveStatus(userId: string, active: boolean, confirmRelinquish = false): Promise<boolean | undefined> {
check(userId, String);
check(active, Boolean);
const user = await Users.findOneById(userId);
if (!user) {
return false;
}
if (isUserFederated(user)) {
throw new Meteor.Error('error-user-is-federated', 'Cannot change federated users status', {
method: 'setUserActiveStatus',
});
}
// Users without username can't do anything, so there is no need to check for owned rooms
if (user.username != null && !active) {
const userAdmin = await Users.findOneAdmin(userId || '');
const adminsCount = await Users.countActiveUsersInRoles(['admin']);
if (userAdmin && adminsCount === 1) {
throw new Meteor.Error('error-action-not-allowed', 'Leaving the app without an active admin is not allowed', {
method: 'removeUserFromRole',
action: 'Remove_last_admin',
});
}
const subscribedRooms = await getSubscribedRoomsForUserWithDetails(userId);
// give omnichannel rooms a special treatment :)
const chatSubscribedRooms = subscribedRooms.filter(({ t }) => t !== 'l');
const livechatSubscribedRooms = subscribedRooms.filter(({ t }) => t === 'l');
if (shouldRemoveOrChangeOwner(chatSubscribedRooms) && !confirmRelinquish) {
const rooms = await getUserSingleOwnedRooms(chatSubscribedRooms as []);
throw new Meteor.Error('user-last-owner', '', rooms);
}
// We don't want one killing the other :)
await Promise.allSettled([
closeOmnichannelConversations(user, livechatSubscribedRooms),
relinquishRoomOwnerships(user._id, chatSubscribedRooms, false),
]);
}
[NEW] Seats Cap (#23017) * Base commit * [NEW] licenses.maxActiveUsers endpoint (#23011) * [IMPROVE] Banner Service (#22989) * WIP * Fix type import Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] canAddNewUser function * [NEW] Seats usage bar (#23018) * usage bar component * Seats limit in admin users page * Remove dangling console.log * Add some details on StatisticsEndpoint type * Move to EE and use new endpoint * Rename some components and hooks * Refactor UsersPage Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * Fix edit and info page * [NEW] Seats Card (#23077) * Seats Card * Fix review, make ts * Fix review * Add type guard for CardIcon props Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Seats Cap: Request seats link (#23151) * create endpoint and consume it in the ui * Fix review * Remove unused param type Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Remove license downgrade if exceeding seats cap (#23220) * [IMPROVE] Ensure Seats-cap design and UI are the same (#23222) * Fix labels and buttons * Reload seats cap data on user changes * Use Fuselage on development version Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. (#23050) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] stats on seats request (#23225) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * [NEW] Seats cap banners (#23211) * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. * Seats cap banners * Deprecate preserveDismiss * use request seats link * Fix banner not closing and request seats link Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> * [FIX] Banner not parsing markdown (#23036) * Parse markdown * Use markdownText * Fix translations * Move startup * Always create seats limit banners * Remove uneffective conditional * [FIX] Seats Cap QA reports (#23272) * Fix create banner and link * Remove call from startup * QA * Improve readability * Avoid using an outdated absolute URL * Embedded counters into translation strings Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * Patch object-path so Snyk stop complaining Co-authored-by: pierre-lehnen-rc <55164754+pierre-lehnen-rc@users.noreply.github.com> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com> Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Co-authored-by: Gabriel Thomé <38537062+g-thome@users.noreply.github.com>
4 years ago
if (active && !user.active) {
await callbacks.run('beforeActivateUser', user);
[NEW] Seats Cap (#23017) * Base commit * [NEW] licenses.maxActiveUsers endpoint (#23011) * [IMPROVE] Banner Service (#22989) * WIP * Fix type import Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] canAddNewUser function * [NEW] Seats usage bar (#23018) * usage bar component * Seats limit in admin users page * Remove dangling console.log * Add some details on StatisticsEndpoint type * Move to EE and use new endpoint * Rename some components and hooks * Refactor UsersPage Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * Fix edit and info page * [NEW] Seats Card (#23077) * Seats Card * Fix review, make ts * Fix review * Add type guard for CardIcon props Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Seats Cap: Request seats link (#23151) * create endpoint and consume it in the ui * Fix review * Remove unused param type Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Remove license downgrade if exceeding seats cap (#23220) * [IMPROVE] Ensure Seats-cap design and UI are the same (#23222) * Fix labels and buttons * Reload seats cap data on user changes * Use Fuselage on development version Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. (#23050) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] stats on seats request (#23225) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * [NEW] Seats cap banners (#23211) * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. * Seats cap banners * Deprecate preserveDismiss * use request seats link * Fix banner not closing and request seats link Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> * [FIX] Banner not parsing markdown (#23036) * Parse markdown * Use markdownText * Fix translations * Move startup * Always create seats limit banners * Remove uneffective conditional * [FIX] Seats Cap QA reports (#23272) * Fix create banner and link * Remove call from startup * QA * Improve readability * Avoid using an outdated absolute URL * Embedded counters into translation strings Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * Patch object-path so Snyk stop complaining Co-authored-by: pierre-lehnen-rc <55164754+pierre-lehnen-rc@users.noreply.github.com> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com> Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Co-authored-by: Gabriel Thomé <38537062+g-thome@users.noreply.github.com>
4 years ago
}
await Users.setUserActive(userId, active);
[NEW] Seats Cap (#23017) * Base commit * [NEW] licenses.maxActiveUsers endpoint (#23011) * [IMPROVE] Banner Service (#22989) * WIP * Fix type import Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] canAddNewUser function * [NEW] Seats usage bar (#23018) * usage bar component * Seats limit in admin users page * Remove dangling console.log * Add some details on StatisticsEndpoint type * Move to EE and use new endpoint * Rename some components and hooks * Refactor UsersPage Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * Fix edit and info page * [NEW] Seats Card (#23077) * Seats Card * Fix review, make ts * Fix review * Add type guard for CardIcon props Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Seats Cap: Request seats link (#23151) * create endpoint and consume it in the ui * Fix review * Remove unused param type Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Remove license downgrade if exceeding seats cap (#23220) * [IMPROVE] Ensure Seats-cap design and UI are the same (#23222) * Fix labels and buttons * Reload seats cap data on user changes * Use Fuselage on development version Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. (#23050) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] stats on seats request (#23225) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * [NEW] Seats cap banners (#23211) * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. * Seats cap banners * Deprecate preserveDismiss * use request seats link * Fix banner not closing and request seats link Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> * [FIX] Banner not parsing markdown (#23036) * Parse markdown * Use markdownText * Fix translations * Move startup * Always create seats limit banners * Remove uneffective conditional * [FIX] Seats Cap QA reports (#23272) * Fix create banner and link * Remove call from startup * QA * Improve readability * Avoid using an outdated absolute URL * Embedded counters into translation strings Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * Patch object-path so Snyk stop complaining Co-authored-by: pierre-lehnen-rc <55164754+pierre-lehnen-rc@users.noreply.github.com> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com> Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Co-authored-by: Gabriel Thomé <38537062+g-thome@users.noreply.github.com>
4 years ago
if (active && !user.active) {
await callbacks.run('afterActivateUser', user);
[NEW] Seats Cap (#23017) * Base commit * [NEW] licenses.maxActiveUsers endpoint (#23011) * [IMPROVE] Banner Service (#22989) * WIP * Fix type import Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] canAddNewUser function * [NEW] Seats usage bar (#23018) * usage bar component * Seats limit in admin users page * Remove dangling console.log * Add some details on StatisticsEndpoint type * Move to EE and use new endpoint * Rename some components and hooks * Refactor UsersPage Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * Fix edit and info page * [NEW] Seats Card (#23077) * Seats Card * Fix review, make ts * Fix review * Add type guard for CardIcon props Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Seats Cap: Request seats link (#23151) * create endpoint and consume it in the ui * Fix review * Remove unused param type Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Remove license downgrade if exceeding seats cap (#23220) * [IMPROVE] Ensure Seats-cap design and UI are the same (#23222) * Fix labels and buttons * Reload seats cap data on user changes * Use Fuselage on development version Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. (#23050) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] stats on seats request (#23225) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * [NEW] Seats cap banners (#23211) * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. * Seats cap banners * Deprecate preserveDismiss * use request seats link * Fix banner not closing and request seats link Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> * [FIX] Banner not parsing markdown (#23036) * Parse markdown * Use markdownText * Fix translations * Move startup * Always create seats limit banners * Remove uneffective conditional * [FIX] Seats Cap QA reports (#23272) * Fix create banner and link * Remove call from startup * QA * Improve readability * Avoid using an outdated absolute URL * Embedded counters into translation strings Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * Patch object-path so Snyk stop complaining Co-authored-by: pierre-lehnen-rc <55164754+pierre-lehnen-rc@users.noreply.github.com> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com> Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Co-authored-by: Gabriel Thomé <38537062+g-thome@users.noreply.github.com>
4 years ago
}
if (!active && user.active) {
await callbacks.run('afterDeactivateUser', user);
[NEW] Seats Cap (#23017) * Base commit * [NEW] licenses.maxActiveUsers endpoint (#23011) * [IMPROVE] Banner Service (#22989) * WIP * Fix type import Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] canAddNewUser function * [NEW] Seats usage bar (#23018) * usage bar component * Seats limit in admin users page * Remove dangling console.log * Add some details on StatisticsEndpoint type * Move to EE and use new endpoint * Rename some components and hooks * Refactor UsersPage Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * Fix edit and info page * [NEW] Seats Card (#23077) * Seats Card * Fix review, make ts * Fix review * Add type guard for CardIcon props Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Seats Cap: Request seats link (#23151) * create endpoint and consume it in the ui * Fix review * Remove unused param type Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Remove license downgrade if exceeding seats cap (#23220) * [IMPROVE] Ensure Seats-cap design and UI are the same (#23222) * Fix labels and buttons * Reload seats cap data on user changes * Use Fuselage on development version Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. (#23050) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * [NEW] stats on seats request (#23225) Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> * [NEW] Seats cap banners (#23211) * [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. * Seats cap banners * Deprecate preserveDismiss * use request seats link * Fix banner not closing and request seats link Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> * [FIX] Banner not parsing markdown (#23036) * Parse markdown * Use markdownText * Fix translations * Move startup * Always create seats limit banners * Remove uneffective conditional * [FIX] Seats Cap QA reports (#23272) * Fix create banner and link * Remove call from startup * QA * Improve readability * Avoid using an outdated absolute URL * Embedded counters into translation strings Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> * Patch object-path so Snyk stop complaining Co-authored-by: pierre-lehnen-rc <55164754+pierre-lehnen-rc@users.noreply.github.com> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com> Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat> Co-authored-by: Gabriel Henriques <gabriel.henriques@rocket.chat> Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Co-authored-by: Gabriel Thomé <38537062+g-thome@users.noreply.github.com>
4 years ago
}
if (user.username) {
await Subscriptions.setArchivedByUsername(user.username, !active);
}
if (active === false) {
await Users.unsetLoginTokens(userId);
await Rooms.setDmReadOnlyByUserId(userId, undefined, true, false);
} else {
await Users.unsetReason(userId);
await reactivateDirectConversations(userId);
}
if (active && !settings.get('Accounts_Send_Email_When_Activating')) {
return true;
}
if (!active && !settings.get('Accounts_Send_Email_When_Deactivating')) {
return true;
}
if (!user.emails || !Array.isArray(user.emails) || user.emails.length === 0) {
return true;
}
const destinations = user.emails.map((email: IUserEmail) => `${user.name || user.username}<${email.address}>`);
type UserActivated = {
subject: (params: { active: boolean }) => string;
html: (params: { active: boolean; name: string; username: string }) => string;
};
const { subject, html } = (Accounts.emailTemplates as unknown as { userActivated: UserActivated }).userActivated;
const email = {
to: String(destinations),
from: String(settings.get('From_Email')),
subject: subject({ active } as any),
html: html({
active,
name: user.name,
username: user.username,
} as any),
};
void Mailer.sendNoWrap(email);
}