import { Authorized , CurrentUser , Get , JsonController , Param , UseBefore , ForbiddenError , NotFoundError } from 'routing-controllers' ; import passportJwtMiddleware from '../security/passportJwtMiddleware' ; import { Course , ICourseModel } from '../models/Course' ; import { Lecture } from '../models/Lecture' ; import { Unit } from '../models/units/Unit' ; import { IUser } from '../../../shared/models/IUser' ; import { User } from '../models/User' ; import { Notification } from '../models/Notification' ; import { NotificationSettings } from '../models/NotificationSettings' ; import { WhitelistUser } from '../models/WhitelistUser' ; import { Progress } from '../models/progress/Progress' ; import { Message } from '../models/Message' ; @ JsonController ( '/export' ) @ UseBefore ( passportJwtMiddleware ) @ Authorized ( [ 'teacher' , 'admin' ] ) export class ExportController { private static assertUserExportAuthorization ( user , course ) { if ( ! course . checkPrivileges ( user ) . userCanEditCourse ) { throw new ForbiddenError ( ) ; } } api apiName apiGroup apiPermission apiPermission apiParam apiSuccess apiSuccessExample apiError apiError @ Get ( '/course/:id' ) async exportCourse ( @ Param ( 'id' ) id , @ CurrentUser ( ) currentUser ) { const course = await Course . findById ( id ) . orFail ( new NotFoundError ( ) ) ; ExportController . assertUserExportAuthorization ( currentUser , course ) ; return course . exportJSON ( ) ; } api apiName apiGroup apiPermission apiPermission apiParam apiSuccess apiSuccessExample apiError apiError @ Get ( '/lecture/:id' ) async exportLecture ( @ Param ( 'id' ) id , @ CurrentUser ( ) currentUser ) { const lecture = await Lecture . findById ( id ) . orFail ( new NotFoundError ( ) ) ; const course = await Course . findOne ( { lectures : id } ) ; ExportController . assertUserExportAuthorization ( currentUser , course ) ; return lecture . exportJSON ( ) ; } api apiName apiGroup apiPermission apiPermission apiParam apiSuccess apiSuccessExample apiError apiError @ Get ( '/unit/:id' ) async exportUnit ( @ Param ( 'id' ) id , @ CurrentUser ( ) currentUser ) { const unit = await Unit . findById ( id ) . orFail ( new NotFoundError ( ) ) ; const course = await Course . findById ( unit . _course ) ; ExportController . assertUserExportAuthorization ( currentUser , course ) ; return unit . exportJSON ( ) ; } api apiName apiGroup apiPermission apiPermission apiPermission apiSuccess apiSuccessExample @ Get ( '/user' ) @ Authorized ( [ 'student' , 'teacher' , 'admin' ] ) async exportAllUserData ( @ CurrentUser ( ) currentUser ) { const user = await User . findById ( currentUser ) ; return { user : await user . exportPersonalData ( ) , notifications : await Notification . exportPersonalData ( user ) , notificationSettings : await NotificationSettings . exportPersonalData ( user ) , whitelists : await WhitelistUser . exportPersonalData ( user ) , courses : await Course . exportPersonalData ( user ) , progress : await Progress . exportPersonalUserData ( user ) , messages : await Message . exportPersonalData ( user ) } ; } }