use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class SchemaUtil method getSchemaInfo.
public static SchemaInfo getSchemaInfo(String user, String schema, SQLExpr expr, String tableAlias) throws SQLException {
SchemaInfo schemaInfo = new SchemaInfo();
if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
schemaInfo.schema = StringUtil.removeBackQuote(propertyExpr.getOwner().toString());
schemaInfo.table = StringUtil.removeBackQuote(propertyExpr.getName());
} else if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
schemaInfo.schema = schema;
schemaInfo.table = StringUtil.removeBackQuote(identifierExpr.getName());
if (identifierExpr.getName().equalsIgnoreCase("dual") && tableAlias == null) {
schemaInfo.dual = true;
return schemaInfo;
}
}
schemaInfo.tableAlias = tableAlias == null ? schemaInfo.table : tableAlias;
if (schemaInfo.schema == null) {
String msg = "No database selected";
throw new SQLException(msg, "3D000", ErrorCode.ER_NO_DB_ERROR);
}
if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
schemaInfo.table = schemaInfo.table.toLowerCase();
schemaInfo.schema = schemaInfo.schema.toLowerCase();
}
if (MYSQL_SCHEMA.equalsIgnoreCase(schemaInfo.schema) || INFORMATION_SCHEMA.equalsIgnoreCase(schemaInfo.schema)) {
return schemaInfo;
} else {
SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(schemaInfo.schema);
if (schemaConfig == null) {
String msg = "Table " + StringUtil.getFullName(schemaInfo.schema, schemaInfo.table) + " doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
if (user != null) {
UserConfig userConfig = DbleServer.getInstance().getConfig().getUsers().get(user);
if (!userConfig.getSchemas().contains(schemaInfo.schema)) {
String msg = " Access denied for user '" + user + "' to database '" + schemaInfo.schema + "'";
throw new SQLException(msg, "HY000", ErrorCode.ER_DBACCESS_DENIED_ERROR);
}
}
schemaInfo.schemaConfig = schemaConfig;
return schemaInfo;
}
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class ShowDatabases method response.
public static void response(ServerConnection c) {
ByteBuffer buffer = c.allocate();
// write header
buffer = HEADER.write(buffer, c, true);
// write fields
for (FieldPacket field : FIELDS) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = EOF.write(buffer, c, true);
// write rows
byte packetId = EOF.getPacketId();
ServerConfig conf = DbleServer.getInstance().getConfig();
Map<String, UserConfig> users = conf.getUsers();
UserConfig user = users == null ? null : users.get(c.getUser());
if (user != null) {
TreeSet<String> schemaSet = new TreeSet<>();
Set<String> schemaList = user.getSchemas();
if (schemaList == null || schemaList.size() == 0) {
schemaSet.addAll(conf.getSchemas().keySet());
} else {
for (String schema : schemaList) {
schemaSet.add(schema);
}
}
for (String name : schemaSet) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(name, c.getCharset().getResults()));
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.setPacketId(++packetId);
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class UserConfigLoader method load.
public void load(Element root, XMLServerLoader xsl, boolean isLowerCaseTableNames) throws IllegalAccessException, InvocationTargetException {
Map<String, UserConfig> users = xsl.getUsers();
NodeList list = root.getElementsByTagName("user");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String name = e.getAttribute("name");
UserConfig user = new UserConfig();
Map<String, Object> props = ConfigUtil.loadElements(e);
String password = (String) props.get("password");
String usingDecrypt = (String) props.get("usingDecrypt");
String passwordDecrypt = DecryptUtil.decrypt(usingDecrypt, name, password);
user.setName(name);
user.setPassword(passwordDecrypt);
user.setEncryptPassword(password);
String benchmark = (String) props.get("benchmark");
if (null != benchmark) {
user.setBenchmark(Integer.parseInt(benchmark));
}
String readOnly = (String) props.get("readOnly");
if (null != readOnly) {
user.setReadOnly(Boolean.parseBoolean(readOnly));
}
String manager = (String) props.get("manager");
if (null != manager) {
user.setManager(Boolean.parseBoolean(manager));
user.setSchemas(new HashSet<String>(0));
}
String schemas = (String) props.get("schemas");
if (user.isManager() && schemas != null) {
throw new ConfigException("manager user can't set any schema!");
} else if (!user.isManager()) {
if (schemas != null) {
if (isLowerCaseTableNames) {
schemas = schemas.toLowerCase();
}
String[] strArray = SplitUtil.split(schemas, ',', true);
user.setSchemas(new HashSet<>(Arrays.asList(strArray)));
}
// load DML
loadPrivileges(user, isLowerCaseTableNames, e);
}
if (users.containsKey(name)) {
throw new ConfigException("user " + name + " duplicated!");
}
users.put(name, user);
}
}
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class ServerPrivileges method checkPrivilege.
// check SQL Privilege
public static boolean checkPrivilege(ServerConnection source, String schema, String tableName, CheckType chekcType) {
UserConfig userConfig = DbleServer.getInstance().getConfig().getUsers().get(source.getUser());
if (userConfig == null) {
return true;
}
UserPrivilegesConfig userPrivilege = userConfig.getPrivilegesConfig();
if (userPrivilege == null || !userPrivilege.isCheck()) {
return true;
}
UserPrivilegesConfig.SchemaPrivilege schemaPrivilege = userPrivilege.getSchemaPrivilege(schema);
if (schemaPrivilege == null) {
return true;
}
UserPrivilegesConfig.TablePrivilege tablePrivilege = schemaPrivilege.getTablePrivilege(tableName);
if (tablePrivilege == null && schemaPrivilege.getDml().length == 0) {
return true;
}
int index = -1;
if (chekcType == CheckType.INSERT) {
index = 0;
} else if (chekcType == CheckType.UPDATE) {
index = 1;
} else if (chekcType == CheckType.SELECT) {
index = 2;
} else if (chekcType == CheckType.DELETE) {
index = 3;
}
if (tablePrivilege != null) {
return tablePrivilege.getDml()[index] > 0;
} else {
return schemaPrivilege.getDml()[index] > 0;
}
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class ServerPrivileges method isReadOnly.
@Override
public Boolean isReadOnly(String user) {
ServerConfig conf = DbleServer.getInstance().getConfig();
UserConfig uc = conf.getUsers().get(user);
Boolean result = null;
if (uc != null) {
result = uc.isReadOnly();
}
return result;
}
Aggregations