Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/lazy-pianos-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@rocket.chat/model-typings': minor
'@rocket.chat/core-typings': minor
'@rocket.chat/rest-typings': minor
'@rocket.chat/models': minor
'@rocket.chat/meteor': minor
---

Enhance user's deactivated state handling to correctly distinguish between pending and deactivated users.
22 changes: 22 additions & 0 deletions apps/meteor/app/api/server/lib/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type FindPaginatedUsersByStatusProps = {
searchTerm: string;
hasLoggedIn: boolean;
type: string;
inactiveReason?: ('deactivated' | 'pending_approval' | 'idle_too_long')[];
};

export async function findPaginatedUsersByStatus({
Expand All @@ -143,6 +144,7 @@ export async function findPaginatedUsersByStatus({
searchTerm,
hasLoggedIn,
type,
inactiveReason,
}: FindPaginatedUsersByStatusProps) {
const actualSort: Record<string, 1 | -1> = sort || { username: 1 };
if (sort?.status) {
Expand Down Expand Up @@ -199,6 +201,26 @@ export async function findPaginatedUsersByStatus({
match.roles = { $in: roles };
}

if (inactiveReason) {
const inactiveReasonCondition = {
$or: [
{ inactiveReason: { $in: inactiveReason } },
// This condition is to make it backward compatible with the old behavior
// The deactivated users not having the inactiveReason field should be returned as well
...(inactiveReason.includes('deactivated') || inactiveReason.includes('idle_too_long')
? [{ inactiveReason: { $exists: false } }]
: []),
],
};

if (match.$or) {
match.$and = [{ $or: match.$or }, inactiveReasonCondition];
delete match.$or;
} else {
Object.assign(match, inactiveReasonCondition);
}
}

const { cursor, totalCount } = Users.findPaginated(
{
...match,
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ API.v1.addRoute(

const { offset, count } = await getPaginationItems(this.queryParams);
const { sort } = await this.parseJsonQuery();
const { status, hasLoggedIn, type, roles, searchTerm } = this.queryParams;
const { status, hasLoggedIn, type, roles, searchTerm, inactiveReason } = this.queryParams;

return API.v1.success(
await findPaginatedUsersByStatus({
Expand All @@ -624,6 +624,7 @@ API.v1.addRoute(
searchTerm,
hasLoggedIn,
type,
inactiveReason,
}),
);
},
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ const onCreateUserAsync = async function (options, user = {}) {
}

user.status = 'offline';

user.active = user.active !== undefined ? user.active : !settings.get('Accounts_ManuallyApproveNewUsers');
user.inactiveReason = settings.get('Accounts_ManuallyApproveNewUsers') && !user.active ? 'pending_approval' : undefined;

if (!user.name) {
if (options.profile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ const useFilteredUsers = ({ searchTerm, prevSearchTerm, sortData, paginationData
const listUsersPayload: Partial<Record<AdminUsersTab, UsersListStatusParamsGET>> = {
all: {},
pending: {
hasLoggedIn: false,
type: 'user',
status: 'active',
status: 'deactivated',
inactiveReason: ['pending_approval'],
},
active: {
hasLoggedIn: true,
status: 'active',
},
deactivated: {
status: 'deactivated',
inactiveReason: ['deactivated', 'idle_too_long'],
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const usePendingUsersCount = (users: Serialized<DefaultUserInfo[]> | undefined)

queryFn: async () => {
const payload: UsersListStatusParamsGET = {
hasLoggedIn: false,
status: 'deactivated',
inactiveReason: ['pending_approval'],
type: 'user',
count: 1,
};
Expand Down
19 changes: 11 additions & 8 deletions apps/meteor/tests/e2e/admin-users.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Users } from './fixtures/userStates';
import { AdminUsers } from './page-objects';
import { setSettingValueById } from './utils/setSettingValueById';
import { test, expect } from './utils/test';
import type { ITestUser } from './utils/user-helpers';
import { createTestUser } from './utils/user-helpers';
Expand All @@ -11,11 +12,13 @@ test.use({ storageState: Users.admin.state });

test.describe('Admin > Users', () => {
test.beforeAll('Create a new user', async ({ api }) => {
await setSettingValueById(api, 'Accounts_ManuallyApproveNewUsers', true);
user = await createTestUser(api);
});

test.afterAll('Delete the new user', async () => {
test.afterAll('Delete the new user', async ({ api }) => {
await user.delete();
await setSettingValueById(api, 'Accounts_ManuallyApproveNewUsers', false);
});

test.beforeEach('Go to /admin/users', async ({ page }) => {
Expand Down Expand Up @@ -44,19 +47,19 @@ test.describe('Admin > Users', () => {
await expect(admin.getUserRowByUsername(user.data.username)).not.toBeVisible();
});

await test.step('should move from Pending to Deactivated tab', async () => {
await test.step('should move from Pending to Active tab', async () => {
await admin.getTabByName('Pending').click();
await admin.dispatchUserAction(user.data.username, 'Deactivate');
await admin.dispatchUserAction(user.data.username, 'Activate');
await expect(admin.getUserRowByUsername(user.data.username)).not.toBeVisible();
await admin.getTabByName('Deactivated').click();
await admin.getTabByName('Active').click();
await expect(admin.getUserRowByUsername(user.data.username)).toBeVisible();
});

await test.step('should move from Deactivated to Pending tab', async () => {
await admin.getTabByName('Deactivated').click();
await admin.dispatchUserAction(user.data.username, 'Activate');
await test.step('should move from Active to Deactivated tab', async () => {
await admin.getTabByName('Active').click();
await admin.dispatchUserAction(user.data.username, 'Deactivate');
await expect(admin.getUserRowByUsername(user.data.username)).not.toBeVisible();
await admin.getTabByName('Pending').click();
await admin.getTabByName('Deactivated').click();
await expect(admin.getUserRowByUsername(user.data.username)).toBeVisible();
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/core-typings/src/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export interface IUser extends IRocketChatRecord {
roomRolePriorities?: Record<string, number>;
isOAuthUser?: boolean; // client only field
__rooms?: string[];
inactiveReason?: 'deactivated' | 'pending_approval' | 'idle_too_long';
}

export interface IRegisterUser extends IUser {
Expand Down
1 change: 0 additions & 1 deletion packages/model-typings/src/models/IUsersModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ export interface IUsersModel extends IBaseModel<IUser> {
setAvatarData(userId: string, origin: string, etag?: Date | null | string, options?: UpdateOptions): Promise<UpdateResult>;
unsetAvatarData(userId: string): Promise<UpdateResult>;
setUserActive(userId: string, active: boolean): Promise<UpdateResult>;
setAllUsersActive(active: boolean): Promise<UpdateResult | Document>;
setActiveNotLoggedInAfterWithRole(latestLastLoginDate: Date, role?: string, active?: boolean): Promise<UpdateResult | Document>;
unsetRequirePasswordChange(userId: string): Promise<UpdateResult>;
resetPasswordAndSetRequirePasswordChange(
Expand Down
14 changes: 4 additions & 10 deletions packages/models/src/models/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2954,22 +2954,14 @@ export class UsersRaw extends BaseRaw<IUser, DefaultFields<IUser>> implements IU
const update = {
$set: {
active,
...(!active && { inactiveReason: 'deactivated' as const }),
},
...(active && { $unset: { inactiveReason: 1 as const } }),
};

return this.updateOne({ _id }, update);
}

setAllUsersActive(active: boolean) {
const update = {
$set: {
active,
},
};

return this.updateMany({}, update);
}

/**
* @param latestLastLoginDate
* @param {IRole['_id']} role the role id
Expand All @@ -2988,7 +2980,9 @@ export class UsersRaw extends BaseRaw<IUser, DefaultFields<IUser>> implements IU
const update = {
$set: {
active,
...(!active && { inactiveReason: 'idle_too_long' as const }),
},
...(active && { $unset: { inactiveReason: 1 as const } }),
};

return this.updateMany(query, update);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type UsersListStatusParamsGET = PaginatedRequest<{
type?: string;
roles?: string[];
searchTerm?: string;
inactiveReason?: ('deactivated' | 'pending_approval' | 'idle_too_long')[];
}>;
const UsersListStatusParamsGetSchema = {
type: 'object',
Expand Down Expand Up @@ -51,6 +52,13 @@ const UsersListStatusParamsGetSchema = {
type: 'number',
nullable: true,
},
inactiveReason: {
type: 'array',
items: {
type: 'string',
enum: ['deactivated', 'pending_approval', 'idle_too_long'],
},
},
},
additionalProperties: false,
};
Expand Down
Loading