mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 03:06:31 -05:00
fix(backend): updated realtime connection's acceptEdit, close connection based on permission
Signed-off-by: Avinash <avinash.kumar.cs92@gmail.com>
This commit is contained in:
parent
c6e283a111
commit
d15a8b18d9
2 changed files with 40 additions and 12 deletions
|
@ -191,8 +191,8 @@ export class PermissionsService {
|
|||
return false;
|
||||
}
|
||||
|
||||
private notifyOthers(noteId: Note['id']): void {
|
||||
this.eventEmitter.emit(NoteEvent.PERMISSION_CHANGE, noteId);
|
||||
private notifyOthers(note: Note): void {
|
||||
this.eventEmitter.emit(NoteEvent.PERMISSION_CHANGE, note);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -256,7 +256,7 @@ export class PermissionsService {
|
|||
createdPermission.note = Promise.resolve(note);
|
||||
(await note.groupPermissions).push(createdPermission);
|
||||
}
|
||||
this.notifyOthers(note.id);
|
||||
this.notifyOthers(note);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
@ -291,7 +291,7 @@ export class PermissionsService {
|
|||
);
|
||||
(await note.userPermissions).push(noteUserPermission);
|
||||
}
|
||||
this.notifyOthers(note.id);
|
||||
this.notifyOthers(note);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
@ -323,7 +323,7 @@ export class PermissionsService {
|
|||
}
|
||||
}
|
||||
note.userPermissions = Promise.resolve(newPermissions);
|
||||
this.notifyOthers(note.id);
|
||||
this.notifyOthers(note);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
@ -363,7 +363,7 @@ export class PermissionsService {
|
|||
);
|
||||
(await note.groupPermissions).push(noteGroupPermission);
|
||||
}
|
||||
this.notifyOthers(note.id);
|
||||
this.notifyOthers(note);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
@ -398,7 +398,7 @@ export class PermissionsService {
|
|||
}
|
||||
}
|
||||
note.groupPermissions = Promise.resolve(newPermissions);
|
||||
this.notifyOthers(note.id);
|
||||
this.notifyOthers(note);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
@ -411,7 +411,7 @@ export class PermissionsService {
|
|||
*/
|
||||
async changeOwner(note: Note, owner: User): Promise<Note> {
|
||||
note.owner = Promise.resolve(owner);
|
||||
this.notifyOthers(note.id);
|
||||
this.notifyOthers(note);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,9 @@ import appConfiguration, { AppConfig } from '../../config/app.config';
|
|||
import { NoteEvent } from '../../events';
|
||||
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
||||
import { Note } from '../../notes/note.entity';
|
||||
import { PermissionsService } from '../../permissions/permissions.service';
|
||||
import { RevisionsService } from '../../revisions/revisions.service';
|
||||
import { RealtimeConnection } from './realtime-connection';
|
||||
import { RealtimeNote } from './realtime-note';
|
||||
import { RealtimeNoteStore } from './realtime-note-store';
|
||||
|
||||
|
@ -25,6 +27,7 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
|
|||
private schedulerRegistry: SchedulerRegistry,
|
||||
@Inject(appConfiguration.KEY)
|
||||
private appConfig: AppConfig,
|
||||
private permissionService: PermissionsService,
|
||||
) {}
|
||||
|
||||
beforeApplicationShutdown(): void {
|
||||
|
@ -109,11 +112,36 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
|
|||
}
|
||||
|
||||
@OnEvent(NoteEvent.PERMISSION_CHANGE)
|
||||
public handleNotePermissionChanged(noteId: Note['id']): void {
|
||||
const realtimeNote = this.realtimeNoteStore.find(noteId);
|
||||
if (realtimeNote) {
|
||||
public async handleNotePermissionChanged(note: Note): Promise<void> {
|
||||
const realtimeNote = this.realtimeNoteStore.find(note.id);
|
||||
if (!realtimeNote) return;
|
||||
|
||||
realtimeNote.announcePermissionChange();
|
||||
const allConnections = realtimeNote.getConnections();
|
||||
await this.updateOrCloseConnection(allConnections, note);
|
||||
}
|
||||
|
||||
private async updateOrCloseConnection(
|
||||
connections: RealtimeConnection[],
|
||||
note: Note,
|
||||
): Promise<void> {
|
||||
for (const connection of connections) {
|
||||
if (await this.connectionCanRead(connection, note)) {
|
||||
connection.acceptEdits = await this.permissionService.mayWrite(
|
||||
connection.getUser(),
|
||||
note,
|
||||
);
|
||||
} else {
|
||||
connection.getTransporter().disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async connectionCanRead(
|
||||
connection: RealtimeConnection,
|
||||
note: Note,
|
||||
): Promise<boolean> {
|
||||
return await this.permissionService.mayRead(connection.getUser(), note);
|
||||
}
|
||||
|
||||
@OnEvent(NoteEvent.DELETION)
|
||||
|
|
Loading…
Reference in a new issue