message: Add: Move "is user allowed to export this course" logic to own method
diff: @@ -21,6 +21,7 @@ const archiver = require('archiver');
  import crypto = require('crypto');
  import {User} from '../models/User';
  import {File} from '../models/mediaManager/File';
  +import {ICourse} from '../../../shared/models/ICourse';
  const cache = require('node-file-cache').create({life: config.timeToLiveCacheValue});
  const pdf = require('html-pdf');
  @@ -150,6 +151,7 @@ export class DownloadController {
  * "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  *
  * @apiError NotFoundError
  + * @apiError ForbiddenError
  */
  @Post('/pdf/individual')
  @ContentType('application/json')
  @@ -159,10 +161,7 @@ export class DownloadController {
  .findOne({_id: data.courseName})
  .orFail(new NotFoundError());
  - const courseAdmin = await User.findOne({_id: course.courseAdmin});
  -
  - if (course.students.indexOf(user._id) !== -1 || courseAdmin.equals(user._id.toString()) ||
  - course.teachers.indexOf(user._id) !== -1 || user.role === 'admin') {
  + this.userCanExportCourse(course, user);
  if (!data.lectures.length) {
  throw new NotFoundError();
  @@ -258,9 +257,6 @@ export class DownloadController {
  } else {
  return hash;
  }
  - } else {
  - throw new NotFoundError();
  - }
  }
  /**
  @@ -277,6 +273,7 @@ export class DownloadController {
  * "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  *
  * @apiError NotFoundError
  + * @apiError ForbiddenError
  */
  @Post('/pdf/single')
  @ContentType('application/json')
  @@ -288,10 +285,7 @@ export class DownloadController {
  .findOne({_id: data.courseName})
  .orFail(new NotFoundError());
  - const courseAdmin = await User.findOne({_id: course.courseAdmin});
  -
  - if (course.students.indexOf(user._id) !== -1 || courseAdmin.equals(user._id.toString()) ||
  - course.teachers.indexOf(user._id) !== -1 || user.role === 'admin') {
  + this.userCanExportCourse(course, user);
  if (!data.lectures.length) {
  throw new NotFoundError();
  @@ -426,9 +420,6 @@ export class DownloadController {
  } else {
  return hash;
  }
  - } else {
  - throw new NotFoundError();
  - }
  }
  private readMarkdownCss() {
  @@ -459,6 +450,30 @@ export class DownloadController {
  });
  }
  + /**
  + * @param course
  + * @param user
  + */
  + private userCanExportCourse(course: ICourse, user: IUser): boolean {
  + if (user.role === 'admin') {
  + return true;
  + }
  +
  + if (course.courseAdmin._id === user._id) {
  + return true;
  + }
  +
  + if (course.students.indexOf(user._id) !== -1) {
  + return true;
  + }
  +
  + if (course.teachers.indexOf(user._id) !== -1) {
  + return true;
  + }
  +
  + throw new ForbiddenError();
  + }
  +
  private appendToArchive(archive: any, name: String, pathToFile: String, hash: any) {
  return new Promise<void>((resolve, reject) => {
  archive.on('entry', () => {
  