@Override public void registerRoleForUser(String sessID, String user, Set<UserRole> roles) throws RemoteException, SessionExpiredException, SQLException, SecurityException { checkAdmin(sessID); // Check if any of the roles given are already assigned, and if so remove them from the // roles to register. Set<UserRole> assignedRoles = getRolesForUser(sessID, user); if (assignedRoles.containsAll(roles)) { return; } else if (assignedRoles.size() > 0) { roles.removeAll(assignedRoles); } // register the remaining roles. TableSchema raTable = MedSavantDatabase.UserRoleAssignmentTableSchema; for (UserRole role : roles) { InsertQuery iq = new InsertQuery(raTable.getTableName()); iq.addColumn( raTable.getDBColumn( MedSavantDatabase.UserRoleAssignmentTableSchema.COLUMNNAME_OF_ROLE_ID), role.getRoleId()); iq.addColumn( raTable.getDBColumn( MedSavantDatabase.UserRoleAssignmentTableSchema.COLUMNNAME_OF_USERNAME), user); ConnectionController.executeUpdate(sessID, iq.toString()); } }
@Override public UserRole addRole(String sessID, String roleName, String roleDescription) throws RemoteException, SessionExpiredException, SQLException, SecurityException { String thisDatabase = SessionManager.getInstance().getDatabaseForSession(sessID); checkAdmin(sessID); // Check if role already exists, and if so, return it. Set<UserRole> roles = getAllRoles(sessID); for (UserRole r : roles) { if (r.getDatabase().equals(thisDatabase) && r.getRoleName().equals(roleName)) { return r; } } TableSchema roleTable = MedSavantDatabase.UserRoleTableSchema; InsertQuery iq = new InsertQuery(roleTable.getTableName()); iq.addColumn( roleTable.getDBColumn(MedSavantDatabase.UserRoleTableSchema.COLUMNNAME_OF_ROLENAME), roleName); iq.addColumn( roleTable.getDBColumn(MedSavantDatabase.UserRoleTableSchema.COLUMNNAME_OF_ROLE_DESCRIPTION), roleDescription); PooledConnection conn = ConnectionController.connectPooled(sessID); PreparedStatement stmt = null; ResultSet res = null; int roleId = -1; try { stmt = conn.prepareStatement(iq.toString(), Statement.RETURN_GENERATED_KEYS); stmt.execute(); res = stmt.getGeneratedKeys(); res.next(); roleId = res.getInt(1); return new UserRole(roleId, roleName, roleDescription, thisDatabase); } finally { if (stmt != null) { stmt.close(); } if (res != null) { res.close(); } if (conn != null) { conn.close(); } } }