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-10-30 10:17:53 -04:00
|
|
|
EmailHelper = require("../Helpers/EmailHelper")
|
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()
|
2018-11-13 05:32:51 -05:00
|
|
|
entityName = entity[entityConfig.fields.name] if entityConfig.fields.name
|
2018-10-24 09:50:34 -04:00
|
|
|
res.render "user_membership/index",
|
2018-11-13 05:32:51 -05:00
|
|
|
name: entityName
|
2018-10-24 09:50:34 -04:00
|
|
|
users: users
|
|
|
|
groupSize: entity.membersLimit if entityConfig.hasMembersLimit
|
|
|
|
translations: entityConfig.translations
|
|
|
|
paths: entityConfig.pathsFor(entityPrimaryKey)
|
|
|
|
|
|
|
|
add: (req, res, next)->
|
|
|
|
{ entity, entityConfig } = req
|
2018-10-30 10:17:53 -04:00
|
|
|
email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
if !email?
|
|
|
|
return res.status(400).json error:
|
|
|
|
code: 'invalid_email'
|
|
|
|
message: req.i18n.translate('invalid_email')
|
|
|
|
|
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 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-10-30 10:17:53 -04:00
|
|
|
if error?.alreadyAdded
|
|
|
|
return res.status(400).json error:
|
|
|
|
code: 'user_already_added'
|
|
|
|
message: req.i18n.translate('user_already_added')
|
|
|
|
if error?.userNotFound
|
|
|
|
return res.status(404).json error:
|
|
|
|
code: 'user_not_found'
|
|
|
|
message: req.i18n.translate('user_not_found')
|
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-30 10:17:53 -04:00
|
|
|
loggedInUserId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
if loggedInUserId == userId
|
|
|
|
return res.status(400).json error:
|
|
|
|
code: 'managers_cannot_remove_self'
|
|
|
|
message: req.i18n.translate('managers_cannot_remove_self')
|
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
UserMembershipHandler.removeUser entity, entityConfig, userId, (error, user)->
|
2018-10-30 10:17:53 -04:00
|
|
|
if error?.isAdmin
|
|
|
|
return res.status(400).json error:
|
|
|
|
code: 'managers_cannot_remove_admin'
|
|
|
|
message: req.i18n.translate('managers_cannot_remove_admin')
|
2018-10-24 09:50:34 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.send()
|
2018-10-25 11:10:02 -04:00
|
|
|
|
|
|
|
exportCsv: (req, res, next)->
|
|
|
|
{ entity, entityConfig } = req
|
|
|
|
logger.log subscriptionId: entity._id, "exporting csv"
|
|
|
|
UserMembershipHandler.getUsers entity, entityConfig, (error, users)->
|
|
|
|
return next(error) if error?
|
|
|
|
csvOutput = ""
|
|
|
|
for user in users
|
|
|
|
csvOutput += user.email + "\n"
|
|
|
|
res.header(
|
|
|
|
"Content-Disposition",
|
|
|
|
"attachment; filename=Group.csv"
|
|
|
|
)
|
|
|
|
res.contentType('text/csv')
|
|
|
|
res.send(csvOutput)
|