From 1695a1bf398f6cab3df7fc5b0a444cb681c89fb0 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Fri, 5 Aug 2016 13:29:11 +0100 Subject: [PATCH] move create-admin-user and delete-user to grunt folder --- server-ce/Gruntfile.coffee | 1 + server-ce/grunt/CreateAndDestoryUsers.coffee | 56 ++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 server-ce/grunt/CreateAndDestoryUsers.coffee diff --git a/server-ce/Gruntfile.coffee b/server-ce/Gruntfile.coffee index 984a6ae287..a6c0d84c6a 100644 --- a/server-ce/Gruntfile.coffee +++ b/server-ce/Gruntfile.coffee @@ -19,6 +19,7 @@ module.exports = (grunt) -> grunt.loadNpmTasks 'grunt-concurrent' grunt.loadNpmTasks "grunt-contrib-coffee" grunt.loadNpmTasks "grunt-shell" + require('load-grunt-config')(grunt) execute = {} diff --git a/server-ce/grunt/CreateAndDestoryUsers.coffee b/server-ce/grunt/CreateAndDestoryUsers.coffee new file mode 100644 index 0000000000..a6e73089a4 --- /dev/null +++ b/server-ce/grunt/CreateAndDestoryUsers.coffee @@ -0,0 +1,56 @@ + +module.exports = (grunt) -> + + grunt.registerTask 'create-admin-user', "Create a user with the given email address and make them an admin. Update in place if the user already exists. Usage: grunt create-admin-user --email joe@example.com", () -> + done = @async() + email = grunt.option("email") + if !email? + console.error "Usage: grunt create-admin-user --email joe@example.com" + process.exit(1) + + settings = require "settings-sharelatex" + UserRegistrationHandler = require "../web/app/js/Features/User/UserRegistrationHandler" + OneTimeTokenHandler = require "../web/app/js/Features/Security/OneTimeTokenHandler" + 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 user._id, { expiresIn: ONE_WEEK }, (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() + + grunt.registerTask 'delete-user', "deletes a user and all their data, Usage: grunt delete-user --email joe@example.com", () -> + done = @async() + email = grunt.option("email") + if !email? + console.error "Usage: grunt delete-user --email joe@example.com" + process.exit(1) + settings = require "settings-sharelatex" + UserGetter = require "../web/app/js/Features/User/UserGetter" + UserDeleter = require "../web/app/js/Features/User/UserDeleter" + 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() \ No newline at end of file