diff --git a/server/classrooms/classroom_handler.coffee b/server/classrooms/classroom_handler.coffee index 734b37217..3456376f4 100644 --- a/server/classrooms/classroom_handler.coffee +++ b/server/classrooms/classroom_handler.coffee @@ -5,6 +5,7 @@ Classroom = require './Classroom' User = require '../users/User' sendwithus = require '../sendwithus' utils = require '../lib/utils' +UserHandler = require '../users/user_handler' ClassroomHandler = class ClassroomHandler extends Handler modelClass: Classroom @@ -35,8 +36,19 @@ ClassroomHandler = class ClassroomHandler extends Handler method = req.method.toLowerCase() return @inviteStudents(req, res, args[0]) if args[1] is 'invite-members' return @joinClassroomAPI(req, res, args[0]) if method is 'post' and args[1] is 'members' + return @getMembersAPI(req, res, args[0]) if args[1] is 'members' super(arguments...) + getMembersAPI: (req, res, classroomID) -> + Classroom.findById classroomID, (err, classroom) => + return @sendDatabaseError(res, err) if err + return @sendNotFoundError(res) unless classroom + memberIDs = classroom.get('members') ? [] + User.find {_id: {$in: memberIDs}}, (err, users) => + return @sendDatabaseError(res, err) if err + cleandocs = (UserHandler.formatEntity(req, doc) for doc in users) + @sendSuccess(res, cleandocs) + joinClassroomAPI: (req, res, classroomID) -> return @sendBadInputError(res, 'Need an object with a code') unless req.body?.code Classroom.findOne {code: req.body.code}, (err, classroom) => @@ -84,6 +96,12 @@ ClassroomHandler = class ClassroomHandler extends Handler Classroom.find {ownerID: mongoose.Types.ObjectId(ownerID)}, (err, classrooms) => return @sendDatabaseError(res, err) if err return @sendSuccess(res, (@formatEntity(req, classroom) for classroom in classrooms)) + else if memberID = req.query.memberID + return @sendForbiddenError(res) unless req.user and (req.user.isAdmin() or memberID is req.user.id) + return @sendBadInputError(res, 'Bad memberID') unless utils.isID memberID + Classroom.find {members: mongoose.Types.ObjectId(memberID)}, (err, classrooms) => + return @sendDatabaseError(res, err) if err + return @sendSuccess(res, (@formatEntity(req, classroom) for classroom in classrooms)) else super(arguments...)