2021-07-07 07:39:42 -04:00
/ *
* decaffeinate suggestions :
* DS102 : Remove unnecessary code created because of implicit returns
* DS207 : Consider shorter variations of null checks
* Full docs : https : //github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
* /
2016-08-05 08:29:11 -04:00
2021-07-07 07:39:42 -04:00
module . exports = function ( grunt ) {
2016-08-05 08:29:11 -04:00
2021-07-07 07:39:42 -04:00
grunt . registerTask ( 'user:create-admin' , "Create a user with the given email address and make them an admin. Update in place if the user already exists. Usage: grunt user:create-admin --email joe@example.com" , function ( ) {
const done = this . async ( ) ;
const email = grunt . option ( "email" ) ;
if ( ( email == null ) ) {
console . error ( "Usage: grunt user:create-admin --email=joe@example.com" ) ;
process . exit ( 1 ) ;
}
2016-08-05 08:29:11 -04:00
2021-07-07 07:39:42 -04:00
const settings = require ( "settings-sharelatex" ) ;
const mongodb = require ( "../web/app/src/infrastructure/mongodb" ) ;
const UserRegistrationHandler = require ( "../web/app/src/Features/User/UserRegistrationHandler" ) ;
const OneTimeTokenHandler = require ( "../web/app/src/Features/Security/OneTimeTokenHandler" ) ;
return mongodb . waitForDb ( ) . then ( ( ) => UserRegistrationHandler . registerNewUser ( {
email ,
password : require ( "crypto" ) . randomBytes ( 32 ) . toString ( "hex" )
} , function ( error , user ) {
if ( ( error != null ) && ( ( error != null ? error . message : undefined ) !== "EmailAlreadyRegistered" ) ) {
throw error ;
}
user . isAdmin = true ;
return user . save ( function ( error ) {
if ( error != null ) { throw error ; }
const ONE _WEEK = 7 * 24 * 60 * 60 ; // seconds
return OneTimeTokenHandler . getNewToken ( "password" , { expiresIn : ONE _WEEK , email : user . email , user _id : user . _id . toString ( ) } , function ( err , token ) {
if ( err != null ) { return next ( err ) ; }
2020-10-05 04:45:43 -04:00
2021-07-07 07:39:42 -04:00
console . log ( "" ) ;
console . log ( ` \
Successfully created $ { email } as an admin user .
2020-10-05 04:45:43 -04:00
2021-07-07 07:39:42 -04:00
Please visit the following URL to set a password for $ { email } and log in :
2020-10-05 04:45:43 -04:00
2021-07-07 07:39:42 -04:00
$ { settings . siteUrl } / user / password / set ? passwordResetToken = $ { token }
\
`
) ;
return done ( ) ;
} ) ;
} ) ;
} ) ) ;
} ) ;
2020-10-05 04:45:43 -04:00
2021-07-07 07:39:42 -04:00
return grunt . registerTask ( 'user:delete' , "deletes a user and all their data, Usage: grunt user:delete --email joe@example.com" , function ( ) {
const done = this . async ( ) ;
const email = grunt . option ( "email" ) ;
if ( ( email == null ) ) {
console . error ( "Usage: grunt user:delete --email=joe@example.com" ) ;
process . exit ( 1 ) ;
}
const settings = require ( "settings-sharelatex" ) ;
const mongodb = require ( "../web/app/src/infrastructure/mongodb" ) ;
const UserGetter = require ( "../web/app/src/Features/User/UserGetter" ) ;
const UserDeleter = require ( "../web/app/src/Features/User/UserDeleter" ) ;
return mongodb . waitForDb ( ) . then ( ( ) => UserGetter . getUser ( { email } , function ( error , user ) {
if ( error != null ) {
throw error ;
}
if ( ( user == null ) ) {
console . log ( ` user ${ email } not in database, potentially already deleted ` ) ;
return done ( ) ;
}
return UserDeleter . deleteUser ( user . _id , function ( err ) {
if ( err != null ) {
throw err ;
}
return done ( ) ;
} ) ;
} ) ) ;
} ) ;
} ;