UserMembershipController.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SessionManager = require('../Authentication/SessionManager')
  14. const UserMembershipHandler = require('./UserMembershipHandler')
  15. const Errors = require('../Errors/Errors')
  16. const EmailHelper = require('../Helpers/EmailHelper')
  17. const { csvAttachment } = require('../../infrastructure/Response')
  18. const {
  19. UserIsManagerError,
  20. UserAlreadyAddedError,
  21. UserNotFoundError,
  22. } = require('./UserMembershipErrors')
  23. const { SSOConfig } = require('../../models/SSOConfig')
  24. const CSVParser = require('json2csv').Parser
  25. async function manageGroupMembers(req, res, next) {
  26. const { entity, entityConfig } = req
  27. const ssoConfig = await SSOConfig.findById(entity.ssoConfig).exec()
  28. return entity.fetchV1Data(function (error, entity) {
  29. if (error != null) {
  30. return next(error)
  31. }
  32. return UserMembershipHandler.getUsers(
  33. entity,
  34. entityConfig,
  35. function (error, users) {
  36. let entityName
  37. if (error != null) {
  38. return next(error)
  39. }
  40. const entityPrimaryKey =
  41. entity[entityConfig.fields.primaryKey].toString()
  42. if (entityConfig.fields.name) {
  43. entityName = entity[entityConfig.fields.name]
  44. }
  45. return res.render('user_membership/group-members-react', {
  46. name: entityName,
  47. groupId: entityPrimaryKey,
  48. users,
  49. groupSize: entity.membersLimit,
  50. managedUsersActive: entity.managedUsersEnabled,
  51. groupSSOActive: ssoConfig?.enabled,
  52. })
  53. }
  54. )
  55. })
  56. }
  57. async function manageGroupManagers(req, res, next) {
  58. await _renderManagersPage(
  59. req,
  60. res,
  61. next,
  62. 'user_membership/group-managers-react'
  63. )
  64. }
  65. async function manageInstitutionManagers(req, res, next) {
  66. await _renderManagersPage(
  67. req,
  68. res,
  69. next,
  70. 'user_membership/institution-managers-react'
  71. )
  72. }
  73. async function managePublisherManagers(req, res, next) {
  74. await _renderManagersPage(
  75. req,
  76. res,
  77. next,
  78. 'user_membership/publisher-managers-react'
  79. )
  80. }
  81. async function _renderManagersPage(req, res, next, template) {
  82. const { entity, entityConfig } = req
  83. return entity.fetchV1Data(function (error, entity) {
  84. if (error != null) {
  85. return next(error)
  86. }
  87. return UserMembershipHandler.getUsers(
  88. entity,
  89. entityConfig,
  90. function (error, users) {
  91. let entityName
  92. if (error != null) {
  93. return next(error)
  94. }
  95. const entityPrimaryKey =
  96. entity[entityConfig.fields.primaryKey].toString()
  97. if (entityConfig.fields.name) {
  98. entityName = entity[entityConfig.fields.name]
  99. }
  100. return res.render(template, {
  101. name: entityName,
  102. users,
  103. groupId: entityPrimaryKey,
  104. })
  105. }
  106. )
  107. })
  108. }
  109. module.exports = {
  110. manageGroupMembers,
  111. manageGroupManagers,
  112. manageInstitutionManagers,
  113. managePublisherManagers,
  114. add(req, res, next) {
  115. const { entity, entityConfig } = req
  116. const email = EmailHelper.parseEmail(req.body.email)
  117. if (email == null) {
  118. return res.status(400).json({
  119. error: {
  120. code: 'invalid_email',
  121. message: req.i18n.translate('invalid_email'),
  122. },
  123. })
  124. }
  125. if (entityConfig.readOnly) {
  126. return next(new Errors.NotFoundError('Cannot add users to entity'))
  127. }
  128. return UserMembershipHandler.addUser(
  129. entity,
  130. entityConfig,
  131. email,
  132. function (error, user) {
  133. if (error && error instanceof UserAlreadyAddedError) {
  134. return res.status(400).json({
  135. error: {
  136. code: 'user_already_added',
  137. message: req.i18n.translate('user_already_added'),
  138. },
  139. })
  140. }
  141. if (error && error instanceof UserNotFoundError) {
  142. return res.status(404).json({
  143. error: {
  144. code: 'user_not_found',
  145. message: req.i18n.translate('user_not_found'),
  146. },
  147. })
  148. }
  149. if (error != null) {
  150. return next(error)
  151. }
  152. return res.json({ user })
  153. }
  154. )
  155. },
  156. remove(req, res, next) {
  157. const { entity, entityConfig } = req
  158. const { userId } = req.params
  159. if (entityConfig.readOnly) {
  160. return next(new Errors.NotFoundError('Cannot remove users from entity'))
  161. }
  162. const loggedInUserId = SessionManager.getLoggedInUserId(req.session)
  163. if (loggedInUserId === userId) {
  164. return res.status(400).json({
  165. error: {
  166. code: 'managers_cannot_remove_self',
  167. message: req.i18n.translate('managers_cannot_remove_self'),
  168. },
  169. })
  170. }
  171. return UserMembershipHandler.removeUser(
  172. entity,
  173. entityConfig,
  174. userId,
  175. function (error, user) {
  176. if (error && error instanceof UserIsManagerError) {
  177. return res.status(400).json({
  178. error: {
  179. code: 'managers_cannot_remove_admin',
  180. message: req.i18n.translate('managers_cannot_remove_admin'),
  181. },
  182. })
  183. }
  184. if (error != null) {
  185. return next(error)
  186. }
  187. return res.sendStatus(200)
  188. }
  189. )
  190. },
  191. exportCsv(req, res, next) {
  192. const { entity, entityConfig } = req
  193. const fields = ['email', 'last_logged_in_at', 'last_active_at']
  194. return UserMembershipHandler.getUsers(
  195. entity,
  196. entityConfig,
  197. function (error, users) {
  198. if (error != null) {
  199. return next(error)
  200. }
  201. const csvParser = new CSVParser({ fields })
  202. csvAttachment(res, csvParser.parse(users), 'Group.csv')
  203. }
  204. )
  205. },
  206. new(req, res, next) {
  207. return res.render('user_membership/new', {
  208. entityName: req.params.name,
  209. entityId: req.params.id,
  210. })
  211. },
  212. create(req, res, next) {
  213. const entityId = req.params.id
  214. const entityConfig = req.entityConfig
  215. return UserMembershipHandler.createEntity(
  216. entityId,
  217. entityConfig,
  218. function (error, entity) {
  219. if (error != null) {
  220. return next(error)
  221. }
  222. return res.redirect(entityConfig.pathsFor(entityId).index)
  223. }
  224. )
  225. },
  226. }