2016-08-05 08:29:11 -04:00
module.exports = (grunt) ->
2016-09-22 07:45:45 -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 " , () ->
2016-08-05 08:29:11 -04:00
done = @ async ( )
email = grunt . option ( " email " )
if ! email ?
2019-10-08 06:46:53 -04:00
console . error " Usage: grunt user:create-admin --email=joe@example.com "
2016-08-05 08:29:11 -04:00
process . exit ( 1 )
settings = require " settings-sharelatex "
2020-10-05 04:45:43 -04:00
mongodb = require " ../web/app/src/infrastructure/mongodb "
2019-10-08 06:46:53 -04:00
UserRegistrationHandler = require " ../web/app/src/Features/User/UserRegistrationHandler "
OneTimeTokenHandler = require " ../web/app/src/Features/Security/OneTimeTokenHandler "
2020-10-05 04:45:43 -04:00
mongodb . waitForDb ( ) . then () ->
UserRegistrationHandler . registerNewUser {
email: email
password: require ( " crypto " ) . randomBytes ( 32 ) . toString ( " hex " )
} , (error, user) ->
if error ? and error ? . message != " EmailAlreadyRegistered "
throw error
user.isAdmin = true
user . save (error) ->
throw error if error ?
ONE_WEEK = 7 * 24 * 60 * 60 # seconds
OneTimeTokenHandler . getNewToken " password " , { expiresIn: ONE_WEEK , email : user . email , user_id: user . _id . toString ( ) } , (err, token)->
return next ( err ) if err ?
console . log " "
console . log """
Successfully created #{email} as an admin user.
Please visit the following URL to set a password for #{email} and log in:
#{settings.siteUrl}/user/password/set?passwordResetToken=#{token}
"""
done ( )
2016-08-05 08:29:11 -04:00
2016-09-22 07:45:45 -04:00
grunt . registerTask ' user:delete ' , " deletes a user and all their data, Usage: grunt user:delete --email joe@example.com " , () ->
2016-08-05 08:29:11 -04:00
done = @ async ( )
email = grunt . option ( " email " )
if ! email ?
2019-10-08 06:46:53 -04:00
console . error " Usage: grunt user:delete --email=joe@example.com "
2016-08-05 08:29:11 -04:00
process . exit ( 1 )
settings = require " settings-sharelatex "
2020-10-05 04:45:43 -04:00
mongodb = require " ../web/app/src/infrastructure/mongodb "
2019-10-08 06:46:53 -04:00
UserGetter = require " ../web/app/src/Features/User/UserGetter "
UserDeleter = require " ../web/app/src/Features/User/UserDeleter "
2020-10-05 04:45:43 -04:00
mongodb . waitForDb ( ) . then () ->
UserGetter . getUser email : email , (error, user) ->
if error ?
throw error
if ! user ?
console . log ( " user #{ email } not in database, potentially already deleted " )
return done ( )
UserDeleter . deleteUser user . _id , (err)->
if err ?
throw err
done ( )