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

com.aoindustries.aoserv.client.mysql.DatabaseUserTable 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, 2017, 2018, 2019, 2020  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.CachedTableIntegerKey;
import com.aoindustries.aoserv.client.aosh.AOSH;
import com.aoindustries.aoserv.client.aosh.Command;
import com.aoindustries.aoserv.client.schema.AoservProtocol;
import com.aoindustries.aoserv.client.schema.Table;
import com.aoindustries.collections.IntList;
import com.aoindustries.io.TerminalWriter;
import com.aoindustries.io.stream.StreamableInput;
import com.aoindustries.io.stream.StreamableOutput;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @see  DatabaseUser
 *
 * @author  AO Industries, Inc.
 */
final public class DatabaseUserTable extends CachedTableIntegerKey {

	DatabaseUserTable(AOServConnector connector) {
		super(connector, DatabaseUser.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(DatabaseUser.COLUMN_MYSQL_DATABASE_name+'.'+Database.COLUMN_NAME_name, ASCENDING),
		new OrderBy(DatabaseUser.COLUMN_MYSQL_DATABASE_name+'.'+Database.COLUMN_MYSQL_SERVER_name+'.'+Server.COLUMN_AO_SERVER_name+'.'+com.aoindustries.aoserv.client.linux.Server.COLUMN_HOSTNAME_name, ASCENDING),
		new OrderBy(DatabaseUser.COLUMN_MYSQL_DATABASE_name+'.'+Database.COLUMN_MYSQL_SERVER_name+'.'+Server.COLUMN_NAME_name, ASCENDING),
		new OrderBy(DatabaseUser.COLUMN_MYSQL_SERVER_USER_name+'.'+UserServer.COLUMN_USERNAME_name, ASCENDING)
	};
	@Override
	protected OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	public int addMySQLDBUser(
		final Database md,
		final UserServer msu,
		final boolean canSelect,
		final boolean canInsert,
		final boolean canUpdate,
		final boolean canDelete,
		final boolean canCreate,
		final boolean canDrop,
		final boolean canReference,
		final boolean canIndex,
		final boolean canAlter,
		final boolean canCreateTempTable,
		final boolean canLockTables,
		final boolean canCreateView,
		final boolean canShowView,
		final boolean canCreateRoutine,
		final boolean canAlterRoutine,
		final boolean canExecute,
		final boolean canEvent,
		final boolean canTrigger
	) throws IOException, SQLException {
		if(msu.isSpecial()) throw new SQLException("Refusing to grant access to a special MySQL user: " + msu);
		if(md.isSpecial()) throw new SQLException("Refusing to grant access to a special MySQL database: " + md);
		return connector.requestResult(
			true,
			AoservProtocol.CommandID.ADD,
			new AOServConnector.ResultRequest() {
				int pkey;
				IntList invalidateList;

				@Override
				public void writeRequest(StreamableOutput out) throws IOException {
					out.writeCompressedInt(Table.TableID.MYSQL_DB_USERS.ordinal());
					out.writeCompressedInt(md.getPkey());
					out.writeCompressedInt(msu.getPkey());
					out.writeBoolean(canSelect);
					out.writeBoolean(canInsert);
					out.writeBoolean(canUpdate);
					out.writeBoolean(canDelete);
					out.writeBoolean(canCreate);
					out.writeBoolean(canDrop);
					out.writeBoolean(canReference);
					out.writeBoolean(canIndex);
					out.writeBoolean(canAlter);
					out.writeBoolean(canCreateTempTable);
					out.writeBoolean(canLockTables);
					out.writeBoolean(canCreateView);
					out.writeBoolean(canShowView);
					out.writeBoolean(canCreateRoutine);
					out.writeBoolean(canAlterRoutine);
					out.writeBoolean(canExecute);
					out.writeBoolean(canEvent);
					out.writeBoolean(canTrigger);
				}

				@Override
				public void readResponse(StreamableInput in) throws IOException, SQLException {
					int code=in.readByte();
					if(code==AoservProtocol.DONE) {
						pkey=in.readCompressedInt();
						invalidateList=AOServConnector.readInvalidateList(in);
					} else {
						AoservProtocol.checkResult(code, in);
						throw new IOException("Unexpected response code: "+code);
					}
				}

				@Override
				public Integer afterRelease() {
					connector.tablesUpdated(invalidateList);
					return pkey;
				}
			}
		);
	}

	@Override
	public DatabaseUser get(int pkey) throws IOException, SQLException {
		return getUniqueRow(DatabaseUser.COLUMN_PKEY, pkey);
	}

	DatabaseUser getMySQLDBUser(Database db, UserServer msu) throws IOException, SQLException {
		int msuPKey=msu.getPkey();

		// Use index first on database
		List cached=getMySQLDBUsers(db);
		int size=cached.size();
		for(int c=0;c getMySQLDBUsers(Server ms) throws IOException, SQLException {
		int msPKey=ms.getBind_id();

		List cached=getRows();
		int size=cached.size();
		List matches=new ArrayList<>(size);
		for(int c=0;c getMySQLDBUsers(UserServer msu) throws IOException, SQLException {
		return getIndexedRows(DatabaseUser.COLUMN_MYSQL_SERVER_USER, msu.getPkey());
	}

	List getMySQLDBUsers(Database md) throws IOException, SQLException {
		return getIndexedRows(DatabaseUser.COLUMN_MYSQL_DATABASE, md.getPkey());
	}

	List getMySQLServerUsers(Database md) throws IOException, SQLException {
		// Use index first
		List cached=getMySQLDBUsers(md);
		int len=cached.size();
		List array=new ArrayList<>(len);
		for(int c=0;c




© 2015 - 2025 Weber Informatics LLC | Privacy Policy