2018-09-25 09:10:06 -04:00
|
|
|
AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
UserMembershipHandler = require('./UserMembershipHandler')
|
2018-10-11 13:56:14 -04:00
|
|
|
EntityConfigs = require('./UserMembershipEntityConfigs')
|
|
|
|
Errors = require('../Errors/Errors')
|
2018-09-25 09:10:06 -04:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
index: (entityName, req, res, next)->
|
2018-10-11 13:56:14 -04:00
|
|
|
getEntity entityName, req, (error, entity, entityConfig) ->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
2018-10-11 13:56:14 -04:00
|
|
|
UserMembershipHandler.getUsers entity, entityConfig, (error, users)->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.render "user_membership/index",
|
|
|
|
users: users
|
2018-10-11 13:56:14 -04:00
|
|
|
groupSize: entity.membersLimit if entityConfig.hasMembersLimit
|
|
|
|
translations: entityConfig.translations
|
|
|
|
paths: entityConfig.pathsFor(entity._id.toString())
|
2018-09-25 09:10:06 -04:00
|
|
|
|
|
|
|
add: (entityName, req, res, next)->
|
|
|
|
email = req.body.email
|
|
|
|
return res.sendStatus 422 unless email
|
|
|
|
|
2018-10-11 13:56:14 -04:00
|
|
|
getEntity entityName, req, (error, entity, entityConfig) ->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
2018-10-11 13:56:14 -04:00
|
|
|
if entityConfig.readOnly
|
|
|
|
return next(new Errors.NotFoundError("Cannot add users to entity"))
|
|
|
|
|
|
|
|
UserMembershipHandler.addUser entity, entityConfig, email, (error, user)->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.json(user: user)
|
|
|
|
|
|
|
|
remove: (entityName, req, res, next)->
|
|
|
|
userId = req.params.userId
|
|
|
|
|
2018-10-11 13:56:14 -04:00
|
|
|
getEntity entityName, req, (error, entity, entityConfig) ->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
2018-10-11 13:56:14 -04:00
|
|
|
if entityConfig.readOnly
|
|
|
|
return next(new Errors.NotFoundError("Cannot remove users from entity"))
|
|
|
|
|
|
|
|
UserMembershipHandler.removeUser entity, entityConfig, userId, (error, user)->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.send()
|
|
|
|
|
2018-10-11 13:56:14 -04:00
|
|
|
getEntity = (entityName, req, callback) ->
|
|
|
|
entityConfig = EntityConfigs[entityName]
|
|
|
|
unless entityConfig
|
|
|
|
return callback(new Errors.NotFoundError("No such entity: #{entityName}"))
|
|
|
|
|
|
|
|
loggedInUser = AuthenticationController.getSessionUser(req)
|
|
|
|
entityId = req.params.id
|
|
|
|
UserMembershipHandler.getEntity entityId, entityConfig, loggedInUser, (error, entity)->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(new Errors.NotFoundError()) unless entity?
|
|
|
|
callback(null, entity, entityConfig)
|