All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.aoserv.client.mysql.UserTable Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ Platform.
 * Copyright (C) 2012-2009, 2016, 2017, 2018, 2019  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-client.
 *
 * aoserv-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-client.  If not, see .
 */
package com.aoindustries.aoserv.client.mysql;

import com.aoindustries.aoserv.client.AOServConnector;
import com.aoindustries.aoserv.client.SimpleAOClient;
import com.aoindustries.aoserv.client.account.Account;
import com.aoindustries.aoserv.client.aosh.AOSH;
import com.aoindustries.aoserv.client.aosh.Command;
import com.aoindustries.aoserv.client.billing.Package;
import com.aoindustries.aoserv.client.password.PasswordChecker;
import com.aoindustries.aoserv.client.password.PasswordProtected;
import com.aoindustries.aoserv.client.schema.AoservProtocol;
import com.aoindustries.aoserv.client.schema.Table;
import com.aoindustries.io.TerminalWriter;
import com.aoindustries.validation.ValidationResult;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @see  User
 *
 * @author  AO Industries, Inc.
 */
final public class UserTable extends CachedTableUserNameKey {

	UserTable(AOServConnector connector) {
		super(connector, User.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(User.COLUMN_USERNAME_name, ASCENDING)
	};
	@Override
	protected OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	public void addMySQLUser(User.Name username) throws IOException, SQLException {
		if(User.isSpecial(username)) throw new SQLException("Refusing to add special MySQL user: " + username);
		connector.requestUpdateIL(
			true,
			AoservProtocol.CommandID.ADD,
			Table.TableID.MYSQL_USERS,
			username
		);
	}

	@Override
	public User get(User.Name username) throws IOException, SQLException {
		return getUniqueRow(User.COLUMN_USERNAME, username);
	}

	public List getMySQLUsers(Package pack) throws IOException, SQLException {
		Account.Name name=pack.getName();
		List cached=getRows();
		int size=cached.size();
		List matches=new ArrayList<>(size);
		for(int c=0;c results=SimpleAOClient.checkMySQLPassword(
					AOSH.parseMySQLUserName(args[1], "username"),
					args[2]
				);
				if(PasswordChecker.hasResults(results)) {
					PasswordChecker.printResults(results, out);
					out.flush();
				}
			}
			return true;
		} else if(command.equalsIgnoreCase(Command.CHECK_MYSQL_USERNAME)) {
			if(AOSH.checkParamCount(Command.CHECK_MYSQL_USERNAME, args, 1, err)) {
				ValidationResult validationResult = User.Name.validate(args[1]);
				out.println(validationResult.isValid());
				out.flush();
				if(!validationResult.isValid()) {
					err.print("aosh: "+Command.CHECK_MYSQL_USERNAME+": ");
					err.println(validationResult.toString());
					err.flush();
				}
			}
			return true;
		} else if(command.equalsIgnoreCase(Command.DISABLE_MYSQL_USER)) {
			if(AOSH.checkParamCount(Command.DISABLE_MYSQL_USER, args, 2, err)) {
				out.println(
					connector.getSimpleAOClient().disableMySQLUser(
						AOSH.parseMySQLUserName(args[1], "username"),
						args[2]
					)
				);
				out.flush();
			}
			return true;
		} else if(command.equalsIgnoreCase(Command.ENABLE_MYSQL_USER)) {
			if(AOSH.checkParamCount(Command.ENABLE_MYSQL_USER, args, 1, err)) {
				connector.getSimpleAOClient().enableMySQLUser(
					AOSH.parseMySQLUserName(args[1], "username")
				);
			}
			return true;
		} else if(command.equalsIgnoreCase(Command.REMOVE_MYSQL_USER)) {
			if(AOSH.checkParamCount(Command.REMOVE_MYSQL_USER, args, 1, err)) {
				connector.getSimpleAOClient().removeMySQLUser(
					AOSH.parseMySQLUserName(args[1], "username")
				);
			}
			return true;
		} else if(command.equalsIgnoreCase(Command.SET_MYSQL_USER_PASSWORD)) {
			if(AOSH.checkParamCount(Command.SET_MYSQL_USER_PASSWORD, args, 2, err)) {
				connector.getSimpleAOClient().setMySQLUserPassword(
					AOSH.parseMySQLUserName(args[1], "username"),
					args[2]
				);
			}
			return true;
		} else if(command.equalsIgnoreCase(Command.WAIT_FOR_MYSQL_USER_REBUILD)) {
			if(AOSH.checkParamCount(Command.WAIT_FOR_MYSQL_USER_REBUILD, args, 1, err)) {
				connector.getSimpleAOClient().waitForMySQLUserRebuild(args[1]);
			}
			return true;
		}
		return false;
	}

	public void waitForRebuild(com.aoindustries.aoserv.client.linux.Server aoServer) throws IOException, SQLException {
		connector.requestUpdate(
			true,
			AoservProtocol.CommandID.WAIT_FOR_REBUILD,
			Table.TableID.MYSQL_USERS,
			aoServer.getPkey()
		);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy