mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-13 21:53:43 +01:00
Move admin register to user activate module
Move admin register to user activate module Co-authored-by: John Lees-Miller <jdleesmiller@gmail.com> & Davinder Singh GitOrigin-RevId: 79428f2932783086435bdad9b1efb5300c467511
This commit is contained in:
committed by
Copybot
co-authored by
John Lees-Miller
parent
bce02b25e4
commit
b3d55fa65e
@@ -1,8 +1,32 @@
|
||||
const Path = require('path')
|
||||
const UserGetter = require('../../../../app/src/Features/User/UserGetter')
|
||||
const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler')
|
||||
const ErrorController = require('../../../../app/src/Features/Errors/ErrorController')
|
||||
|
||||
module.exports = {
|
||||
registerNewUser(req, res, next) {
|
||||
res.render(Path.resolve(__dirname, '../views/user/register'))
|
||||
},
|
||||
|
||||
register(req, res, next) {
|
||||
const { email } = req.body
|
||||
if (email == null || email === '') {
|
||||
return res.sendStatus(422) // Unprocessable Entity
|
||||
}
|
||||
UserRegistrationHandler.registerNewUserAndSendActivationEmail(
|
||||
email,
|
||||
(error, user, setNewPasswordUrl) => {
|
||||
if (error != null) {
|
||||
return next(error)
|
||||
}
|
||||
res.json({
|
||||
email: user.email,
|
||||
setNewPasswordUrl,
|
||||
})
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
activateAccountPage(req, res, next) {
|
||||
// An 'activation' is actually just a password reset on an account that
|
||||
// was set with a random password originally.
|
||||
|
||||
@@ -1,12 +1,30 @@
|
||||
const logger = require('@overleaf/logger')
|
||||
const UserActivateController = require('./UserActivateController')
|
||||
const AuthenticationController = require('../../../../app/src/Features/Authentication/AuthenticationController')
|
||||
const AuthorizationMiddleware = require('../../../../app/src/Features/Authorization/AuthorizationMiddleware')
|
||||
|
||||
module.exports = {
|
||||
apply(webRouter) {
|
||||
logger.log({}, 'Init UserActivate router')
|
||||
|
||||
webRouter.get(
|
||||
'/admin/user',
|
||||
AuthorizationMiddleware.ensureUserIsSiteAdmin,
|
||||
(req, res) => res.redirect('/admin/register')
|
||||
)
|
||||
|
||||
webRouter.get('/user/activate', UserActivateController.activateAccountPage)
|
||||
AuthenticationController.addEndpointToLoginWhitelist('/user/activate')
|
||||
|
||||
webRouter.get(
|
||||
'/admin/register',
|
||||
AuthorizationMiddleware.ensureUserIsSiteAdmin,
|
||||
UserActivateController.registerNewUser
|
||||
)
|
||||
webRouter.post(
|
||||
'/admin/register',
|
||||
AuthorizationMiddleware.ensureUserIsSiteAdmin,
|
||||
UserActivateController.register
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
extends ../../../../../app/views/layout
|
||||
|
||||
block content
|
||||
.content.content-alt
|
||||
.container
|
||||
.row
|
||||
.col-md-12
|
||||
.card(ng-controller="RegisterUsersController")
|
||||
.page-header
|
||||
h1 Register New Users
|
||||
form.form
|
||||
.row
|
||||
.col-md-4.col-xs-8
|
||||
input.form-control(
|
||||
name="email",
|
||||
type="text",
|
||||
placeholder="jane@example.com, joe@example.com",
|
||||
ng-model="inputs.emails",
|
||||
on-enter="registerUsers()"
|
||||
)
|
||||
.col-md-8.col-xs-4
|
||||
button.btn.btn-primary(ng-click="registerUsers()") #{translate("register")}
|
||||
|
||||
.row-spaced(ng-show="error").ng-cloak.text-danger
|
||||
p Sorry, an error occured
|
||||
|
||||
.row-spaced(ng-show="users.length > 0").ng-cloak.text-success
|
||||
p We've sent out welcome emails to the registered users.
|
||||
p You can also manually send them URLs below to allow them to reset their password and log in for the first time.
|
||||
p (Password reset tokens will expire after one week and the user will need registering again).
|
||||
|
||||
hr(ng-show="users.length > 0").ng-cloak
|
||||
table(ng-show="users.length > 0").table.table-striped.ng-cloak
|
||||
tr
|
||||
th #{translate("email")}
|
||||
th Set Password Url
|
||||
tr(ng-repeat="user in users")
|
||||
td {{ user.email }}
|
||||
td(style="word-break: break-all;") {{ user.setNewPasswordUrl }}
|
||||
|
||||
@@ -17,21 +17,27 @@ describe('UserActivateController', function () {
|
||||
}
|
||||
|
||||
this.UserGetter = { getUser: sinon.stub() }
|
||||
this.UserRegistrationHandler = {}
|
||||
this.ErrorController = { notFound: sinon.stub() }
|
||||
this.UserActivateController = SandboxedModule.require(MODULE_PATH, {
|
||||
requires: {
|
||||
'../../../../app/src/Features/User/UserGetter': this.UserGetter,
|
||||
'../../../../app/src/Features/User/UserRegistrationHandler':
|
||||
this.UserRegistrationHandler,
|
||||
'../../../../app/src/Features/Errors/ErrorController':
|
||||
this.ErrorController,
|
||||
},
|
||||
})
|
||||
this.req = {
|
||||
body: {},
|
||||
query: {},
|
||||
session: {
|
||||
user: this.user,
|
||||
},
|
||||
}
|
||||
this.res = {}
|
||||
this.res = {
|
||||
json: sinon.stub(),
|
||||
}
|
||||
})
|
||||
|
||||
describe('activateAccountPage', function () {
|
||||
@@ -86,4 +92,30 @@ describe('UserActivateController', function () {
|
||||
this.UserActivateController.activateAccountPage(this.req, this.res)
|
||||
})
|
||||
})
|
||||
|
||||
describe('register', function () {
|
||||
beforeEach(function () {
|
||||
this.UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, this.user, (this.url = 'mock/url'))
|
||||
this.req.body.email = this.user.email = this.email = 'email@example.com'
|
||||
this.UserActivateController.register(this.req, this.res)
|
||||
})
|
||||
|
||||
it('should register the user and send them an email', function () {
|
||||
sinon.assert.calledWith(
|
||||
this.UserRegistrationHandler.registerNewUserAndSendActivationEmail,
|
||||
this.email
|
||||
)
|
||||
})
|
||||
|
||||
it('should return the user and activation url', function () {
|
||||
this.res.json
|
||||
.calledWith({
|
||||
email: this.email,
|
||||
setNewPasswordUrl: this.url,
|
||||
})
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user