Revision 242

Date:
2009/06/02 05:55:53
Author:
alan
Revision Log:
Added initial work on ACL
Files:

Legend:

 
Added
 
Removed
 
Modified
  • binarystor/trunk/src/java/org/binarystor/postgresql/PostgreSQLDump.java

     
    31 31 import java.util.ArrayList;
    32 32 import java.util.List;
    33 33 import java.util.TreeMap;
    34 import java.util.Map;
    34 import java.util.HashMap;
    35 35 import org.kohsuke.args4j.*;
    36 36
    37 37 /**
     
    66 66 private int databaseProductMajorVersion = 0;
    67 67 private int databaseProductMinorVersion = 0;
    68 68 private String posgresqlVersion = null;
    69 //Privilege codes
    70 private static final HashMap<String,String> pCodes = new HashMap<String, String>(){
    71 {
    72 put("a", "INSERT");
    73 put("c", "CONNECT");
    74 put("C", "CREATE");
    75 put("d", "DELETE");
    76 put("D", "TRUNCATE");
    77 put("r", "SELECT");
    78 put("t", "TRIGGER");
    79 put("T", "TEMPORARY");
    80 put("U", "USAGE");
    81 put("W", "UPDATE");
    82 put("x", "REFERENCES");
    83 }
    84 };
    69 85
    70 86 /**
    71 87 * Default contructor for PostgreSQLDump.
     
    140 156 }
    141 157 }
    142 158
    143 /**
    159 /**
    144 160 * Connect to PostgreSQL server
    145 161 *
    146 162 * @param host PostgreSQL Server Hostname
     
    152 168 connect(host,5432,username,password,db);
    153 169 }
    154 170
    171
    172 public static String parseACL(String acl, String name, String type){
    173 String aclCommands = "";
    174 String[] acls = acl.substring(1,acl.length()-1).split(",");
    175 for (String priv : acls) {
    176 String user = priv.split("=")[0];
    177 String privs = priv.split("=")[1];
    178 //Public Role?
    179 if (user.equals("")){
    180 user = "public";
    181 }
    182 //Schema privileges
    183 if (type.equals("SCHEMA")){
    184 //GRANT ALL?
    185 if (privs.contains("U")&&privs.contains("C")){
    186 aclCommands += "GRANT ALL ON SCHEMA \"" + name + "\" TO " + user;
    187 if (privs.contains("*")){
    188 aclCommands += " WITH GRANT OPTION";
    189 }
    190 aclCommands += ";\n";
    191 }else{
    192 aclCommands += "GRANT ";
    193 for(char c : privs.toCharArray()) {
    194 if (pCodes.containsKey(Character.toString(c))){
    195 if (c == privs.toCharArray()[privs.toCharArray().length-1]){
    196 aclCommands += pCodes.get(Character.toString(c)) + ",";
    197 }else{
    198 aclCommands += pCodes.get(Character.toString(c));
    199 }
    200 }
    201 }
    202 aclCommands += " SCHEMA \"" + name + "\" TO \"" + user + "\"";
    203 if (privs.contains("*")){
    204 aclCommands += " WITH GRANT OPTION";
    205 }
    206 aclCommands += ";\n";
    207 }
    208 }
    209 }
    210 return aclCommands;
    211 }
    212
    213 public String dumpCreateSchema(String schema) {
    214 String createSchema = null;
    215 try{
    216 Statement s = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    217 s.executeQuery ("SELECT nspacl, description, rolname FROM pg_catalog.pg_namespace JOIN pg_catalog.pg_description ON pg_catalog.pg_namespace.oid=pg_catalog.pg_description.objoid JOIN pg_catalog.pg_roles ON pg_catalog.pg_roles.oid=pg_catalog.pg_namespace.nspowner WHERE nspname = '" + schema + "'");
    218 ResultSet rs = s.getResultSet ();
    219 while (rs.next())
    220 {
    221 createSchema = "CREATE SCHEMA \"" + schema + "\" AUTHORIZATION " + rs.getString("rolname") + ";\n";
    222 if (rs.getString("nspacl")!=null){
    223 createSchema += parseACL(rs.getString("nspacl"),schema,"SCHEMA");
    224 }
    225 if (rs.getString("description")!=null){
    226 createSchema += "COMMENT ON SCHEMA \"" + schema + "\" IS '" + rs.getString("description") + "';\n";
    227 }
    228 }
    229 } catch (SQLException e) {
    230 System.err.println(e.getMessage());
    231 }
    232 if (verbose){
    233 System.out.println(createSchema);
    234 }
    235 return createSchema;
    236 }
    237
    155 238 public File dumpAllDatabases(){
    156 239 return null;
    157 240 }
     
    663 746 }
    664 747
    665 748
    666 }
    749 }