UserMembershipController.js 6.4 KB

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