⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcuserdatabase.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * (non-Javadoc)
     * 
     * @see com.sshtools.enterprise.admin.UserDatabase#updateAccount(com.sshtools.enterprise.admin.User,
     *      java.lang.String, java.lang.String, java.lang.String)
     */
    public void updateAccount(User user, String email, String fullname, Role[] roles, Properties attributes) throws Exception {
        JDBCPreparedStatement ps = db.getStatement("update.account");
        try {
            ps.setString(1, email);
            ps.setString(2, fullname);
            ps.setString(3, user.getPrincipalName());
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
        ps = db.getStatement("delete.roles");
        try {
            ps.setString(1, user.getPrincipalName());
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
        ps = db.getStatement("assign.role");
        try {
            for (int i = 0; roles != null && i < roles.length; i++) {
                ps.setString(1, user.getPrincipalName());
                ps.setString(2, roles[i].getPrincipalName());
                ps.execute();
                ps.reset();
            }
        } finally {
            ps.releasePreparedStatement();
        }
        updateAttributes(user.getPrincipalName(), attributes);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sshtools.enterprise.admin.UserDatabase#deleteAccount(com.sshtools.enterprise.admin.User)
     */
    public void deleteAccount(User user) throws Exception {
        JDBCPreparedStatement ps = db.getStatement("delete.account");
        try {
            ps.setString(1, user.getPrincipalName());
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
        ps = db.getStatement("delete.account.roles");
        try {
            ps.setString(1, user.getPrincipalName());
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
        updateAttributes(user.getPrincipalName(), new Properties());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.security.UserDatabase#checkPassword(java.lang.String,
     *      java.lang.String)
     */
    public boolean checkPassword(String username, String password) throws UserDatabaseException, InvalidLoginCredentialsException {
        try {
            JDBCPreparedStatement ps = db.getStatement("logon.user");
            try {
                ps.setString(1, username);
                ps.setString(2, password);
                ResultSet results = ps.executeQuery();
                try {
                    return results.next();
                } finally {
                    results.close();
                }
            } finally {
                ps.releasePreparedStatement();
            }
        } catch (Exception ex) {
            throw new UserDatabaseException("Failed to execute SQL query", ex);
        }
    }

    public void changePassword(String username, String password, boolean forcePasswordChangeAtLogon) throws UserDatabaseException,
                    InvalidLoginCredentialsException {
        JDBCPreparedStatement ps = null;
        try {
            if (forcePasswordChangeAtLogon) {
                ps = db.getStatement("change.password.force");
                ps.setString(1, password);
                ps.setString(2, username);
            } else {
                ps = db.getStatement("change.password");
                ps.setString(1, password);
                ps.setString(2, username);
            }
            ps.execute();
        } catch (Exception e) {
            throw new UserDatabaseException("Failed to change password for user " + username + ".");
        } finally {
            if (ps != null) {
                try {
                    ps.releasePreparedStatement();
                } catch (SQLException e) {
                }
            }
        }
    }

    public Principal[] listAvailablePrincipals() throws Exception {
        return listAllUsers(null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.security.UserDatabase#getPrincipal(com.sslexplorer.security.User,
     *      java.lang.String)
     */
    public Principal getPrincipal(String principalName) throws Exception {
        return getAccount(principalName);
    }

    public boolean isDefaultAdministrator(Principal principal) throws Exception {
        boolean found = false;
        for (Iterator j = administrators.iterator(); !found && j.hasNext();) {
            String admin = (String) j.next();
            found = principal.getPrincipalName().matches(admin);
        }
        return found;
    }

    public Role getRole(String rolename) throws Exception {
        JDBCPreparedStatement ps = db.getStatement("select.role");
        try {
            ps.setString(1, rolename);
            ResultSet results = ps.executeQuery();
            try {
                if (results.next()) {
                    JDBCRole r = new JDBCRole();
                    r.rolename = results.getString("rolename");
                    return r;
                }
            } finally {
                results.close();
            }
        } finally {
            ps.releasePreparedStatement();
        }
        throw new Exception("No role exists with rolename " + rolename);
    }

    public Role[] listAllRoles(String filter) throws Exception {
        String wildCard = "^" + CoreUtil.replaceAllTokens(filter, "*", ".*") + "$";
        Pattern p = Pattern.compile(wildCard, Pattern.CASE_INSENSITIVE);
        JDBCPreparedStatement ps = db.getStatement("select.roles");
        try {
            ResultSet results = ps.executeQuery();
            try {
                Vector tmp = new Vector();
                while (results.next()) {
                    String rolename = results.getString("rolename");
                    Matcher matcher = p.matcher(rolename);
                    if (matcher.matches()) {
                        JDBCRole r = new JDBCRole();
                        r.rolename = rolename;
                        tmp.add(r);
                    }
                }
                JDBCRole[] roles = new JDBCRole[tmp.size()];
                tmp.copyInto(roles);
                return roles;
            } finally {
                results.close();
            }
        } finally {
            ps.releasePreparedStatement();
        }
    }

    public Role createRole(String rolename) throws Exception {
        JDBCPreparedStatement ps = db.getStatement("create.role");
        try {
            ps.setString(1, rolename);
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
        return getRole(rolename);
    }

    public void deleteRole(String rolename) throws Exception {
        JDBCPreparedStatement ps = db.getStatement("delete.role.1");
        try {
            ps.setString(1, rolename);
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
        ps = db.getStatement("delete.role.2");
        try {
            ps.setString(1, rolename);
            ps.execute();
        } finally {
            ps.releasePreparedStatement();
        }
    }

    private void addRoles(DefaultUser u) throws UserDatabaseException {
        JDBCPreparedStatement ps = null;
        try {
            ps = db.getStatement("select.user.roles");
            ps.setString(1, u.getPrincipalName());
            ResultSet r2 = ps.executeQuery();
            List roles = new ArrayList();
            try {
                while (r2.next()) {
                    Role r = getRole(r2.getString("rolename"));
                    roles.add(r);
                }
            } finally {
                r2.close();
            }
            Role[] r = new Role[roles.size()];
            roles.toArray(r);
            u.setRoles(r);
        } catch (Exception e) {
            throw new UserDatabaseException("Failed to add roles to user.", e);
        } finally {
            if (ps != null) {
                try {
                    ps.releasePreparedStatement();
                } catch (SQLException e) {
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -