3 var Promise = require('bluebird');
4 var identityService = require('gitter-web-identity');
5 var userCanJoinRoom = require('gitter-web-shared/rooms/user-can-join-room');
6 var StatusError = require('statuserror');
8 module.exports = Promise.method(function assertUserValidInRoom(room, existingUser) {
9 // Don't bother loading in all the user's providers if the room allows all
11 if (!room.providers || !room.providers.length) return;
14 // TODO: in future, we should be able to add new members who are not
15 // existing gitter users, but for now throw an error
16 throw new StatusError(403, 'Unable to join room due to restriction in allowed members');
19 return identityService.listProvidersForUser(existingUser).then(function(userProviders) {
20 var userValidInRoom = userCanJoinRoom(userProviders, room.providers);
21 if (!userValidInRoom) {
22 throw new StatusError(403, 'Unable to join room due to restriction in allowed members');