UserMembershipController.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 { UserIsManagerError } = require('./UserMembershipErrors')
  19. const { SSOConfig } = require('../../models/SSOConfig')
  20. const CSVParser = require('json2csv').Parser
  21. async function manageGroupMembers(req, res, next) {
  22. const { entity, entityConfig } = req
  23. const ssoConfig = await SSOConfig.findById(entity.ssoConfig).exec()
  24. return entity.fetchV1Data(function (error, entity) {
  25. if (error != null) {
  26. return next(error)
  27. }
  28. return UserMembershipHandler.getUsers(
  29. entity,
  30. entityConfig,
  31. function (error, users) {
  32. let entityName
  33. if (error != null) {
  34. return next(error)
  35. }
  36. const entityPrimaryKey =
  37. entity[entityConfig.fields.primaryKey].toString()
  38. if (entityConfig.fields.name) {
  39. entityName = entity[entityConfig.fields.name]
  40. }
  41. return res.render('user_membership/group-members-react', {
  42. name: entityName,
  43. groupId: entityPrimaryKey,
  44. users,
  45. groupSize: entity.membersLimit,
  46. managedUsersActive: entity.managedUsersEnabled,
  47. groupSSOActive: ssoConfig?.enabled,
  48. })
  49. }
  50. )
  51. })
  52. }
  53. async function manageGroupManagers(req, res, next) {
  54. await _renderManagersPage(
  55. req,
  56. res,
  57. next,
  58. 'user_membership/group-managers-react'
  59. )
  60. }
  61. async function manageInstitutionManagers(req, res, next) {
  62. await _renderManagersPage(
  63. req,
  64. res,
  65. next,
  66. 'user_membership/institution-managers-react'
  67. )
  68. }
  69. async function managePublisherManagers(req, res, next) {
  70. await _renderManagersPage(
  71. req,
  72. res,
  73. next,
  74. 'user_membership/publisher-managers-react'
  75. )
  76. }
  77. async function _renderManagersPage(req, res, next, template) {
  78. const { entity, entityConfig } = req
  79. return entity.fetchV1Data(function (error, entity) {
  80. if (error != null) {
  81. return next(error)
  82. }
  83. return UserMembershipHandler.getUsers(
  84. entity,
  85. entityConfig,
  86. function (error, users) {
  87. let entityName
  88. if (error != null) {
  89. return next(error)
  90. }
  91. const entityPrimaryKey =
  92. entity[entityConfig.fields.primaryKey].toString()
  93. if (entityConfig.fields.name) {
  94. entityName = entity[entityConfig.fields.name]
  95. }
  96. return res.render(template, {
  97. name: entityName,
  98. users,
  99. groupId: entityPrimaryKey,
  100. })
  101. }
  102. )
  103. })
  104. }
  105. module.exports = {
  106. manageGroupMembers,
  107. manageGroupManagers,
  108. manageInstitutionManagers,
  109. managePublisherManagers,
  110. add(req, res, next) {
  111. const { entity, entityConfig } = req
  112. const email = EmailHelper.parseEmail(req.body.email)
  113. if (email == null) {
  114. return res.status(400).json({
  115. error: {
  116. code: 'invalid_email',
  117. message: req.i18n.translate('invalid_email'),
  118. },
  119. })
  120. }
  121. if (entityConfig.readOnly) {
  122. return next(new Errors.NotFoundError('Cannot add users to entity'))
  123. }
  124. return UserMembershipHandler.addUser(
  125. entity,
  126. entityConfig,
  127. email,
  128. function (error, user) {
  129. if (error != null ? error.alreadyAdded : undefined) {
  130. return res.status(400).json({
  131. error: {
  132. code: 'user_already_added',
  133. message: req.i18n.translate('user_already_added'),
  134. },
  135. })
  136. }
  137. if (error != null ? error.userNotFound : undefined) {
  138. return res.status(404).json({
  139. error: {
  140. code: 'user_not_found',
  141. message: req.i18n.translate('user_not_found'),
  142. },
  143. })
  144. }
  145. if (error != null) {
  146. return next(error)
  147. }
  148. return res.json({ user })
  149. }
  150. )
  151. },
  152. remove(req, res, next) {
  153. const { entity, entityConfig } = req
  154. const { userId } = req.params
  155. if (entityConfig.readOnly) {
  156. return next(new Errors.NotFoundError('Cannot remove users from entity'))
  157. }
  158. const loggedInUserId = SessionManager.getLoggedInUserId(req.session)
  159. if (loggedInUserId === userId) {
  160. return res.status(400).json({
  161. error: {
  162. code: 'managers_cannot_remove_self',
  163. message: req.i18n.translate('managers_cannot_remove_self'),
  164. },
  165. })
  166. }
  167. return UserMembershipHandler.removeUser(
  168. entity,
  169. entityConfig,
  170. userId,
  171. function (error, user) {
  172. if (error && error instanceof UserIsManagerError) {
  173. return res.status(400).json({
  174. error: {
  175. code: 'managers_cannot_remove_admin',
  176. message: req.i18n.translate('managers_cannot_remove_admin'),
  177. },
  178. })
  179. }
  180. if (error != null) {
  181. return next(error)
  182. }
  183. return res.sendStatus(200)
  184. }
  185. )
  186. },
  187. exportCsv(req, res, next) {
  188. const { entity, entityConfig } = req
  189. const fields = ['email', 'last_logged_in_at', 'last_active_at']
  190. return UserMembershipHandler.getUsers(
  191. entity,
  192. entityConfig,
  193. function (error, users) {
  194. if (error != null) {
  195. return next(error)
  196. }
  197. const csvParser = new CSVParser({ fields })
  198. csvAttachment(res, csvParser.parse(users), 'Group.csv')
  199. }
  200. )
  201. },
  202. new(req, res, next) {
  203. return res.render('user_membership/new', {
  204. entityName: req.params.name,
  205. entityId: req.params.id,
  206. })
  207. },
  208. create(req, res, next) {
  209. const entityId = req.params.id
  210. const entityConfig = req.entityConfig
  211. return UserMembershipHandler.createEntity(
  212. entityId,
  213. entityConfig,
  214. function (error, entity) {
  215. if (error != null) {
  216. return next(error)
  217. }
  218. return res.redirect(entityConfig.pathsFor(entityId).index)
  219. }
  220. )
  221. },
  222. }