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
92 changes: 54 additions & 38 deletions ee/packages/abac/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,11 @@ export class AbacService extends ServiceClass implements IAbacService {
}

async setRoomAbacAttributes(rid: string, attributes: Record<string, string[]>, actor: AbacActor): Promise<void> {
const room = await Rooms.findOneByIdAndType<Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'teamDefault' | 'default'>>(
rid,
'p',
{
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1 },
},
);
const room = await Rooms.findOneByIdAndType<
Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'teamDefault' | 'default' | 'name'>
>(rid, 'p', {
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1, name: 1 },
});
if (!room) {
throw new Error('error-room-not-found');
}
Expand All @@ -299,7 +297,7 @@ export class AbacService extends ServiceClass implements IAbacService {

if (!Object.keys(attributes).length && room.abacAttributes?.length) {
await Rooms.unsetAbacAttributesById(rid);
void Audit.objectAttributesRemoved({ _id: room._id }, room.abacAttributes, actor);
void Audit.objectAttributesRemoved({ _id: room._id, name: room.name }, room.abacAttributes, actor);
return;
}

Expand All @@ -308,7 +306,7 @@ export class AbacService extends ServiceClass implements IAbacService {
await this.ensureAttributeDefinitionsExist(normalized);

const updated = await Rooms.setAbacAttributesById(rid, normalized);
void Audit.objectAttributeChanged({ _id: room._id }, room.abacAttributes || [], normalized, 'updated', actor);
void Audit.objectAttributeChanged({ _id: room._id, name: room.name }, room.abacAttributes || [], normalized, 'updated', actor);

const previous: IAbacAttributeDefinition[] = room.abacAttributes || [];
if (this.didAttributesChange(previous, normalized)) {
Expand Down Expand Up @@ -387,13 +385,11 @@ export class AbacService extends ServiceClass implements IAbacService {
}

async updateRoomAbacAttributeValues(rid: string, key: string, values: string[], actor: AbacActor): Promise<void> {
const room = await Rooms.findOneByIdAndType<Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'teamDefault' | 'default'>>(
rid,
'p',
{
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1 },
},
);
const room = await Rooms.findOneByIdAndType<
Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'teamDefault' | 'default' | 'name'>
>(rid, 'p', {
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1, name: 1 },
});
if (!room) {
throw new Error('error-room-not-found');
}
Expand All @@ -414,7 +410,13 @@ export class AbacService extends ServiceClass implements IAbacService {

if (isNewKey) {
await Rooms.updateSingleAbacAttributeValuesById(rid, key, values);
void Audit.objectAttributeChanged({ _id: room._id }, room.abacAttributes || [], [{ key, values }], 'key-added', actor);
void Audit.objectAttributeChanged(
{ _id: room._id, name: room.name },
room.abacAttributes || [],
[{ key, values }],
'key-added',
actor,
);
const next = [...previous, { key, values }];

await this.onRoomAttributesChanged(room, next);
Expand All @@ -428,7 +430,13 @@ export class AbacService extends ServiceClass implements IAbacService {
}

await Rooms.updateAbacAttributeValuesArrayFilteredById(rid, key, values);
void Audit.objectAttributeChanged({ _id: room._id }, room.abacAttributes || [], [{ key, values }], 'key-updated', actor);
void Audit.objectAttributeChanged(
{ _id: room._id, name: room.name },
room.abacAttributes || [],
[{ key, values }],
'key-updated',
actor,
);

if (this.wereAttributeValuesAdded(prevValues, values)) {
const next = previous.map((a, i) => (i === existingIndex ? { key, values } : a));
Expand All @@ -442,8 +450,8 @@ export class AbacService extends ServiceClass implements IAbacService {
}

async removeRoomAbacAttribute(rid: string, key: string, actor: AbacActor): Promise<void> {
const room = await Rooms.findOneByIdAndType<Pick<IRoom, '_id' | 'abacAttributes' | 'teamDefault' | 'default'>>(rid, 'p', {
projection: { abacAttributes: 1, default: 1, teamDefault: 1 },
const room = await Rooms.findOneByIdAndType<Pick<IRoom, '_id' | 'abacAttributes' | 'teamDefault' | 'default' | 'name'>>(rid, 'p', {
projection: { abacAttributes: 1, default: 1, teamDefault: 1, name: 1 },
});
if (!room) {
throw new Error('error-room-not-found');
Expand All @@ -469,7 +477,7 @@ export class AbacService extends ServiceClass implements IAbacService {

await Rooms.removeAbacAttributeByRoomIdAndKey(rid, key);
void Audit.objectAttributeRemoved(
{ _id: room._id },
{ _id: room._id, name: room.name },
previous,
previous.filter((a) => a.key !== key),
'key-removed',
Expand All @@ -480,13 +488,11 @@ export class AbacService extends ServiceClass implements IAbacService {
async addRoomAbacAttributeByKey(rid: string, key: string, values: string[], actor: AbacActor): Promise<void> {
await this.ensureAttributeDefinitionsExist([{ key, values }]);

const room = await Rooms.findOneByIdAndType<Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'default' | 'teamDefault'>>(
rid,
'p',
{
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1 },
},
);
const room = await Rooms.findOneByIdAndType<
Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'default' | 'teamDefault' | 'name'>
>(rid, 'p', {
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1, name: 1 },
});
if (!room) {
throw new Error('error-room-not-found');
}
Expand All @@ -507,21 +513,19 @@ export class AbacService extends ServiceClass implements IAbacService {
const updated = await Rooms.insertAbacAttributeIfNotExistsById(rid, key, values);
const next = updated?.abacAttributes || [...previous, { key, values }];

void Audit.objectAttributeChanged({ _id: room._id }, previous, next, 'key-added', actor);
void Audit.objectAttributeChanged({ _id: room._id, name: room.name }, previous, next, 'key-added', actor);

await this.onRoomAttributesChanged(room, next);
}

async replaceRoomAbacAttributeByKey(rid: string, key: string, values: string[], actor: AbacActor): Promise<void> {
await this.ensureAttributeDefinitionsExist([{ key, values }]);

const room = await Rooms.findOneByIdAndType<Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'default' | 'teamDefault'>>(
rid,
'p',
{
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1 },
},
);
const room = await Rooms.findOneByIdAndType<
Pick<IRoom, '_id' | 'abacAttributes' | 't' | 'teamMain' | 'default' | 'teamDefault' | 'name'>
>(rid, 'p', {
projection: { abacAttributes: 1, t: 1, teamMain: 1, teamDefault: 1, default: 1, name: 1 },
});
if (!room) {
throw new Error('error-room-not-found');
}
Expand All @@ -536,7 +540,13 @@ export class AbacService extends ServiceClass implements IAbacService {
const updated = await Rooms.updateAbacAttributeValuesArrayFilteredById(rid, key, values);
const prevValues = room.abacAttributes?.find((a) => a.key === key)?.values ?? [];

void Audit.objectAttributeChanged({ _id: room._id }, room.abacAttributes || [], updated?.abacAttributes || [], 'key-updated', actor);
void Audit.objectAttributeChanged(
{ _id: room._id, name: room.name },
room.abacAttributes || [],
updated?.abacAttributes || [],
'key-updated',
actor,
);
if (this.wereAttributeValuesAdded(prevValues, values)) {
await this.onRoomAttributesChanged(room, updated?.abacAttributes || []);
}
Expand All @@ -549,7 +559,13 @@ export class AbacService extends ServiceClass implements IAbacService {
}

const updated = await Rooms.insertAbacAttributeIfNotExistsById(rid, key, values);
void Audit.objectAttributeChanged({ _id: room._id }, room.abacAttributes || [], updated?.abacAttributes || [], 'key-added', actor);
void Audit.objectAttributeChanged(
{ _id: room._id, name: room.name },
room.abacAttributes || [],
updated?.abacAttributes || [],
'key-added',
actor,
);

await this.onRoomAttributesChanged(room, updated?.abacAttributes || []);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IUser, IRoom, IAuditServerEventType, IAbacAttributeDefinition, IServerEvents } from '..';

export type MinimalUser = Pick<IUser, '_id' | 'username'>;
export type MinimalRoom = Pick<IRoom, '_id'>;
export type MinimalRoom = Pick<IRoom, '_id' | 'name'>;

export type AbacAuditReason = 'ldap-sync' | 'room-attributes-change' | 'system' | 'api' | 'realtime-policy-eval';

Expand Down
Loading