overleaf/services/web/app/coffee/Features/UserMembership/UserMembershipController.coffee
Timothée Alby b123f830ff Merge pull request #1042 from sharelatex/ta-user-membership-access
User Membership Access Refactor

GitOrigin-RevId: 23e8d342bc4829450625146213ff92cb042550dd
2018-10-24 14:02:55 +00:00

40 lines
1.4 KiB
CoffeeScript

AuthenticationController = require('../Authentication/AuthenticationController')
UserMembershipHandler = require('./UserMembershipHandler')
EntityConfigs = require('./UserMembershipEntityConfigs')
Errors = require('../Errors/Errors')
logger = require("logger-sharelatex")
module.exports =
index: (req, res, next)->
{ entity, entityConfig } = req
UserMembershipHandler.getUsers entity, entityConfig, (error, users)->
return next(error) if error?
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
email = req.body.email
return res.sendStatus 422 unless email
if entityConfig.readOnly
return next(new Errors.NotFoundError("Cannot add users to entity"))
UserMembershipHandler.addUser entity, entityConfig, email, (error, user)->
return next(error) if error?
res.json(user: user)
remove: (req, res, next)->
{ entity, entityConfig } = req
userId = req.params.userId
if entityConfig.readOnly
return next(new Errors.NotFoundError("Cannot remove users from entity"))
UserMembershipHandler.removeUser entity, entityConfig, userId, (error, user)->
return next(error) if error?
res.send()