X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FCertificateProfile.java;h=0e6f29bac09b0dec6f641910a6414e2d4c0e8c00;hp=7ee206074e58536287d86c27828e640fc03e2279;hb=dc10b875c132eb7840a6b9827ec93916076d34f7;hpb=3a1808dadcea554995c71ac655938783458a2d30 diff --git a/src/org/cacert/gigi/dbObjects/CertificateProfile.java b/src/org/cacert/gigi/dbObjects/CertificateProfile.java index 7ee20607..0e6f29ba 100644 --- a/src/org/cacert/gigi/dbObjects/CertificateProfile.java +++ b/src/org/cacert/gigi/dbObjects/CertificateProfile.java @@ -1,21 +1,20 @@ package org.cacert.gigi.dbObjects; -import java.awt.Desktop; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.PrintWriter; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; -import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.Properties; -import java.util.TreeSet; import org.cacert.gigi.database.DatabaseConnection; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; -public class CertificateProfile { +public class CertificateProfile implements IdCachable { private final int id; @@ -23,13 +22,13 @@ public class CertificateProfile { private final String visibleName; - private static HashMap byName = new HashMap<>(); + private static final Map byName; - private static HashMap byId = new HashMap<>(); + private static final Map byId; - private final PropertyTemplate[] pt; + private final Map pt; - private final String[] req; + private final List req; private CertificateProfile(int id, String keyName, String visibleName, String requires, String include) { this.id = id; @@ -39,13 +38,13 @@ public class CertificateProfile { pt = parsePropertyTemplates(include); } - private static class PropertyTemplate implements Comparable { + public static class PropertyTemplate implements Comparable { - boolean required = true; + private boolean required = true; - boolean multiple = false; + private boolean multiple = false; - private String inc; + private String base; public PropertyTemplate(String inc) { if (inc.endsWith("?") || inc.endsWith("*") || inc.endsWith("+")) { @@ -60,14 +59,26 @@ public class CertificateProfile { } inc = inc.substring(0, inc.length() - 1); } - this.inc = inc; + this.base = inc; + } + + public String getBase() { + return base; + } + + public boolean isMultiple() { + return multiple; + } + + public boolean isRequired() { + return required; } @Override public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((inc == null) ? 0 : inc.hashCode()); + result = prime * result + ((base == null) ? 0 : base.hashCode()); result = prime * result + (multiple ? 1231 : 1237); result = prime * result + (required ? 1231 : 1237); return result; @@ -85,11 +96,11 @@ public class CertificateProfile { return false; } PropertyTemplate other = (PropertyTemplate) obj; - if (inc == null) { - if (other.inc != null) { + if (base == null) { + if (other.base != null) { return false; } - } else if ( !inc.equals(other.inc)) { + } else if ( !base.equals(other.base)) { return false; } if (multiple != other.multiple) { @@ -103,7 +114,7 @@ public class CertificateProfile { @Override public String toString() { - return inc + (multiple ? (required ? "+" : "*") : (required ? "" : "?")); + return base + (multiple ? (required ? "+" : "*") : (required ? "" : "?")); } @Override @@ -124,21 +135,24 @@ public class CertificateProfile { req = parseConditions(p.getProperty("requires", "")); } - private String[] parseConditions(String property) { + private List parseConditions(String property) { String[] split2 = property.split(","); if (split2.length == 1 && split2[0].equals("")) { split2 = new String[0]; } - return split2; + return Collections.unmodifiableList(Arrays.asList(split2)); } - private PropertyTemplate[] parsePropertyTemplates(String property) { + private Map parsePropertyTemplates(String property) { String[] split = property.split(","); - PropertyTemplate[] pt = new PropertyTemplate[split.length]; + HashMap map = new HashMap<>(split.length); for (int i = 0; i < split.length; i++) { - pt[i] = new PropertyTemplate(split[i]); + + PropertyTemplate value = new PropertyTemplate(split[i]); + + map.put(value.getBase(), value); } - return pt; + return Collections.unmodifiableMap(map); } public int getId() { @@ -153,16 +167,28 @@ public class CertificateProfile { return visibleName; } + public Map getTemplates() { + return pt; + } + + public List getReqireds() { + return req; + } + static { + final HashMap myName = new HashMap(); + final HashMap myId = new HashMap(); + for (File f : new File("config/profiles").listFiles()) { Properties p = new Properties(); try { p.load(new FileInputStream(f)); } catch (IOException e) { - e.printStackTrace(); + throw new Error("Unable to load profile from " + f.getName(), e); } + String[] parts = f.getName().split("\\.")[0].split("-", 2); - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT keyname, include, requires, name FROM `profiles` WHERE id=?"); + GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `keyname`, `include`, `requires`, `name` FROM `profiles` WHERE `id`=?"); ps.setInt(1, Integer.parseInt(parts[0])); GigiResultSet rs = ps.executeQuery(); @@ -177,7 +203,7 @@ public class CertificateProfile { throw new Error("Config error. Certificate Profile mismatch"); } } else { - GigiPreparedStatement insert = DatabaseConnection.getInstance().prepare("INSERT INTO `profiles` SET keyname=?, include=?, requires=?, name=?, id=?"); + GigiPreparedStatement insert = DatabaseConnection.getInstance().prepare("INSERT INTO `profiles` SET `keyname`=?, `include`=?, `requires`=?, `name`=?, `id`=?"); insert.setString(1, parts[1]); insert.setString(2, p.getProperty("include")); insert.setString(3, p.getProperty("requires", "")); @@ -185,16 +211,18 @@ public class CertificateProfile { insert.setInt(5, Integer.parseInt(parts[0])); insert.execute(); } - } - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name, requires, include FROM `profiles`"); + + GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `id`, `keyname`, `name`, `requires`, `include` FROM `profiles`"); GigiResultSet rs = ps.executeQuery(); while (rs.next()) { CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"), rs.getString("requires"), rs.getString("include")); - byId.put(cp.getId(), cp); - byName.put(cp.getKeyName(), cp); + myId.put(cp.getId(), cp); + myName.put(cp.getKeyName(), cp); } + byName = Collections.unmodifiableMap(myName); + byId = Collections.unmodifiableMap(myId); } public static CertificateProfile getById(int id) { @@ -209,75 +237,27 @@ public class CertificateProfile { return byId.values().toArray(new CertificateProfile[byId.size()]); } - public static void main(String[] args) throws IOException { - TreeSet pt = new TreeSet<>(); - TreeSet req = new TreeSet<>(); - LinkedList cps = new LinkedList<>(); - for (CertificateProfile cp : byId.values()) { - cps.add(cp); - for (PropertyTemplate p : cp.pt) { - pt.add(p.inc); - } - req.addAll(Arrays.asList(cp.req)); - } - PrintWriter pw = new PrintWriter("profiles.html"); - pw.println("Profiles"); - pw.println(""); - pw.println(""); - pw.println(""); - pw.println(""); - for (String p : pt) { - pw.println(""); - } - pw.println(""); - for (String p : req) { - pw.println(""); - } - pw.println(""); - for (CertificateProfile certificateProfile : cps) { - pw.println(""); - pw.println(""); - pw.println(""); - outer: - for (String p : pt) { - for (PropertyTemplate t : certificateProfile.pt) { - if (t.inc.equals(p)) { - pw.println(""); - continue outer; - } - } - pw.println(""); + public boolean canBeIssuedBy(CertificateOwner owner, User actor) { + if (pt.containsKey("orga")) { + if ( !(owner instanceof Organisation)) { + return false; } - pw.println(""); - outer: - for (String p : req) { - for (String t : certificateProfile.req) { - if (t.equals(p)) { - pw.println(""); - continue outer; - } - } - pw.println(""); + } else { + if (owner instanceof Organisation) { + return false; } - pw.println(""); } - pw.println("
id " + p + "" + p + "
" + certificateProfile.id + "" + certificateProfile.keyName + "" + (t.required ? (t.multiple ? "+" : "y") : (t.multiple ? "*" : "?")) + "y
"); - Desktop.getDesktop().browse(new File("profiles.html").toURI()); - pw.close(); - } - - public boolean canBeIssuedBy(User u) { for (String s : req) { if (s.equals("points>=50")) { - if (u.getAssurancePoints() < 50) { + if (actor.getAssurancePoints() < 50) { return false; } } else if (s.equals("points>=100")) { - if (u.getAssurancePoints() < 100) { + if (actor.getAssurancePoints() < 100) { return false; } } else if (s.equals("codesign")) { - if (u.isInGroup(Group.CODESIGNING)) { + if (actor.isInGroup(Group.CODESIGNING)) { return false; } } else { @@ -287,4 +267,5 @@ public class CertificateProfile { } return true; } + }