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

com.aoindustries.aoserv.client.UsernameTable Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2001-2012, 2016  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;

import com.aoindustries.io.TerminalWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

/**
 * @see  Username
 *
 * @author  AO Industries, Inc.
 */
final public class UsernameTable extends CachedTableStringKey {

	UsernameTable(AOServConnector connector) {
		super(connector, Username.class);
	}

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

	void addUsername(Package packageObject, String username) throws IOException, SQLException {
		connector.requestUpdateIL(
			true,
			AOServProtocol.CommandID.ADD,
			SchemaTable.TableID.USERNAMES,
			packageObject.name,
			username
		);
	}

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

	@Override
	public SchemaTable.TableID getTableID() {
		return SchemaTable.TableID.USERNAMES;
	}

	List getUsernames(Package pack) throws IOException, SQLException {
		return getIndexedRows(Username.COLUMN_PACKAGE, pack.name);
	}

	@Override
	boolean handleCommand(String[] args, Reader in, TerminalWriter out, TerminalWriter err, boolean isInteractive) throws IllegalArgumentException, SQLException, IOException {
		String command=args[0];
		if(command.equalsIgnoreCase(AOSHCommand.ADD_USERNAME)) {
			if(AOSH.checkParamCount(AOSHCommand.ADD_USERNAME, args, 2, err)) {
				connector.getSimpleAOClient().addUsername(
					args[1],
						args[2]
				);
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.ARE_USERNAME_PASSWORDS_SET)) {
			if(AOSH.checkParamCount(AOSHCommand.ARE_USERNAME_PASSWORDS_SET, args, 1, err)) {
				int result=connector.getSimpleAOClient().areUsernamePasswordsSet(args[1]);
				if(result==PasswordProtected.NONE) out.println("none");
				else if(result==PasswordProtected.SOME) out.println("some");
				else if(result==PasswordProtected.ALL) out.println("all");
				else throw new RuntimeException("Unexpected value for result: "+result);
				out.flush();
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.CHECK_USERNAME)) {
			if(AOSH.checkParamCount(AOSHCommand.CHECK_USERNAME, args, 1, err)) {
				try {
					SimpleAOClient.checkUsername(args[1]);
					out.println("true");
				} catch(IllegalArgumentException iae) {
					out.print("aosh: "+AOSHCommand.CHECK_USERNAME+": ");
					out.println(iae.getMessage());
				}
				out.flush();
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.CHECK_USERNAME_PASSWORD)) {
			if(AOSH.checkParamCount(AOSHCommand.CHECK_USERNAME_PASSWORD, args, 2, err)) {
				List results = connector.getSimpleAOClient().checkUsernamePassword(args[1], args[2]);
				if(PasswordChecker.hasResults(results)) {
					PasswordChecker.printResults(results, out);
					out.flush();
				}
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.DISABLE_USERNAME)) {
			if(AOSH.checkParamCount(AOSHCommand.DISABLE_USERNAME, args, 2, err)) {
				out.println(
					connector.getSimpleAOClient().disableUsername(
						args[1],
						args[2]
					)
				);
				out.flush();
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.ENABLE_USERNAME)) {
			if(AOSH.checkParamCount(AOSHCommand.ENABLE_USERNAME, args, 1, err)) {
				connector.getSimpleAOClient().enableUsername(args[1]);
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.IS_USERNAME_AVAILABLE)) {
			if(AOSH.checkParamCount(AOSHCommand.IS_USERNAME_AVAILABLE, args, 1, err)) {
				try {
					out.println(connector.getSimpleAOClient().isUsernameAvailable(args[1]));
					out.flush();
				} catch(IllegalArgumentException iae) {
					err.print("aosh: "+AOSHCommand.IS_USERNAME_AVAILABLE+": ");
					err.println(iae.getMessage());
					err.flush();
				}
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.REMOVE_USERNAME)) {
			if(AOSH.checkParamCount(AOSHCommand.REMOVE_USERNAME, args, 1, err)) {
				connector.getSimpleAOClient().removeUsername(args[1]);
			}
			return true;
		} else if(command.equalsIgnoreCase(AOSHCommand.SET_USERNAME_PASSWORD)) {
			if(AOSH.checkParamCount(AOSHCommand.SET_USERNAME_PASSWORD, args, 2, err)) {
				connector.getSimpleAOClient().setUsernamePassword(args[1], args[2]);
			}
			return true;
		}
		return false;
	}

	public boolean isUsernameAvailable(String username) throws SQLException, IOException {
		String check = Username.checkUsername(username);
		if(check!=null) throw new SQLException(check);
		return connector.requestBooleanQuery(true, AOServProtocol.CommandID.IS_USERNAME_AVAILABLE, username);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy