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 =
|
2018-10-24 09:50:34 -04:00
|
|
|
index: (req, res, next)->
|
|
|
|
{ entity, entityConfig } = req
|
|
|
|
UserMembershipHandler.getUsers entity, entityConfig, (error, users)->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
2018-10-24 09:50:34 -04:00
|
|
|
entityPrimaryKey = entity[entityConfig.fields.primaryKey].toString()
|
|
|
|
res.render "user_membership/index",
|
|
|
|
users: users
|
|
|
|
groupSize: entity.membersLimit if entityConfig.hasMembersLimit
|
|
|
|
translations: entityConfig.translations
|
|
|
|
paths: entityConfig.pathsFor(entityPrimaryKey)
|
|
|
|
|
|
|
|
add: (req, res, next)->
|
|
|
|
{ entity, entityConfig } = req
|
2018-09-25 09:10:06 -04:00
|
|
|
email = req.body.email
|
|
|
|
return res.sendStatus 422 unless email
|
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
if entityConfig.readOnly
|
|
|
|
return next(new Errors.NotFoundError("Cannot add users to entity"))
|
2018-09-25 09:10:06 -04:00
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
UserMembershipHandler.addUser entity, entityConfig, email, (error, user)->
|
2018-09-25 09:10:06 -04:00
|
|
|
return next(error) if error?
|
2018-10-24 09:50:34 -04:00
|
|
|
res.json(user: user)
|
2018-10-11 13:56:14 -04:00
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
remove: (req, res, next)->
|
|
|
|
{ entity, entityConfig } = req
|
|
|
|
userId = req.params.userId
|
2018-09-25 09:10:06 -04:00
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
if entityConfig.readOnly
|
|
|
|
return next(new Errors.NotFoundError("Cannot remove users from entity"))
|
2018-10-11 13:56:14 -04:00
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
UserMembershipHandler.removeUser entity, entityConfig, userId, (error, user)->
|
|
|
|
return next(error) if error?
|
|
|
|
res.send()
|