Search in sources :

Example 11 with UserConfig

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;
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLException(java.sql.SQLException) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) UserConfig(com.actiontech.dble.config.model.UserConfig)

Example 12 with UserConfig

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);
}
Also used : ServerConfig(com.actiontech.dble.config.ServerConfig) TreeSet(java.util.TreeSet) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) UserConfig(com.actiontech.dble.config.model.UserConfig) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket)

Example 13 with UserConfig

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);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) UserConfig(com.actiontech.dble.config.model.UserConfig) HashSet(java.util.HashSet)

Example 14 with UserConfig

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;
    }
}
Also used : UserConfig(com.actiontech.dble.config.model.UserConfig) UserPrivilegesConfig(com.actiontech.dble.config.model.UserPrivilegesConfig)

Example 15 with UserConfig

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;
}
Also used : UserConfig(com.actiontech.dble.config.model.UserConfig)

Aggregations

UserConfig (com.actiontech.dble.config.model.UserConfig)16 ServerConfig (com.actiontech.dble.config.ServerConfig)5 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)5 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)5 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)3 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 List (java.util.List)3 Set (java.util.Set)3 ConfigInitializer (com.actiontech.dble.config.ConfigInitializer)2 ConfigException (com.actiontech.dble.config.util.ConfigException)2 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)2 ByteBuffer (java.nio.ByteBuffer)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2