001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.users;
019
020import io.stallion.boot.AppContextLoader;
021import io.stallion.Context;
022import io.stallion.boot.CommandOptionsBase;
023import io.stallion.boot.StallionRunAction;
024import io.stallion.exceptions.UsageException;
025import io.stallion.services.Log;
026import io.stallion.settings.Settings;
027import io.stallion.utils.GeneralUtils;
028import jline.console.ConsoleReader;
029import org.apache.commons.lang3.RandomStringUtils;
030import org.apache.commons.lang3.StringUtils;
031import org.mindrot.jbcrypt.BCrypt;
032
033import java.io.Console;
034import java.util.Scanner;
035
036import static io.stallion.utils.Literals.*;
037
038
039public class UserAdder implements StallionRunAction<CommandOptionsBase> {
040    @Override
041    public String getActionName() {
042        return "users";
043    }
044
045    @Override
046    public String getHelp() {
047        return "Add admin users or edit existing users";
048    }
049
050    @Override
051    public void loadApp(CommandOptionsBase options) {
052        AppContextLoader.loadCompletely(options);
053    }
054
055    public void execute(CommandOptionsBase options) throws Exception {
056        execute(options, "");
057    }
058    public void execute(CommandOptionsBase options, String action) throws Exception {
059
060
061        Log.info("Settings: siteName {0} email password {1}", Settings.instance().getSiteName(), Settings.instance().getEmail().getPassword());
062
063        Scanner scanner = new Scanner(System.in);
064        Console console = System.console();
065
066        if (empty(action)) {
067            //System.out.print("Create new user or edit existing? (new/edit): ");
068
069            //String newEdit = scanner.next();
070
071            System.out.print("Create new user or edit existing? (new/edit): ");
072            //String newEdit = console.readLine("Create new user or edit existing? (new/edit): ");
073            action = scanner.nextLine();
074        }
075        User user = null;
076        if ("new".equals(action)) {
077            user = new User();
078            user.setPredefined(true);
079        } else if("edit".equals(action)) {
080            System.out.print("Enter the email, username, or ID of the user you wish to edit:");
081            String idMaybe = scanner.next();
082            if (StringUtils.isNumeric(idMaybe)) {
083                user = (User)UserController.instance().forId(Long.parseLong(idMaybe));
084            }
085            if (user == null) {
086                user = (User)UserController.instance().forUniqueKey("email", idMaybe);
087            }
088            if (user == null) {
089                user = (User)UserController.instance().forUniqueKey("username", idMaybe);
090            }
091            if (user == null) {
092                System.out.print("Could not find user for key: " + idMaybe);
093                System.exit(1);
094            }
095        } else {
096            System.out.print("Invalid choice. Choose either 'new' or 'edit'");
097            System.exit(1);
098        }
099
100        System.out.print("User's given name: ");
101        String givenName = scanner.nextLine();
102        if (!empty(givenName)) {
103            user.setGivenName(givenName);
104        }
105
106        System.out.print("User's family name: ");
107        String familyName = scanner.nextLine();
108        if (!empty(familyName)) {
109            user.setFamilyName(familyName);
110            user.setDisplayName(user.getGivenName() + " " + user.getFamilyName());
111        }
112
113        System.out.print("User's email: ");
114        String email = scanner.nextLine();
115        if (!empty(email)) {
116            user.setEmail(email);
117            user.setUsername(user.getEmail());
118        }
119
120        System.out.print("Enter password: ");
121        String password = "";
122        if (console != null) {
123            password = new String(console.readPassword());
124        } else {
125            password = new String(scanner.nextLine());
126        }
127        //System.out.printf("password: \"%s\"\n", password);
128        if (empty(password) && empty(user.getBcryptedPassword())) {
129            throw new UsageException("You must set a password!");
130        } else if (!empty(password)) {
131            String hashed = BCrypt.hashpw(password, BCrypt.gensalt());
132            user.setBcryptedPassword(hashed);
133        }
134
135        if (empty(user.getSecret())) {
136            user.setSecret(RandomStringUtils.randomAlphanumeric(18));
137        }
138        if (empty(user.getEncryptionSecret())) {
139            user.setEncryptionSecret(RandomStringUtils.randomAlphanumeric(36));
140        }
141
142        user.setPredefined(true);
143        user.setRole(Role.ADMIN);
144        user.setId(Context.dal().getTickets().nextId());
145        user.setFilePath(GeneralUtils.slugify(user.getEmail() + "---" + user.getId().toString()) + ".json");
146        UserController.instance().save(user);
147
148        System.out.print("User saved with email " + user.getEmail() + " and id " + user.getId());
149
150    }
151
152
153}