]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Group.java
add: defense-in-depth mechanism to prevent unauthorized adding of groups
[gigi.git] / src / org / cacert / gigi / dbObjects / Group.java
1 package org.cacert.gigi.dbObjects;
2
3 import org.cacert.gigi.database.GigiPreparedStatement;
4 import org.cacert.gigi.database.GigiResultSet;
5 import org.cacert.gigi.output.template.Outputable;
6 import org.cacert.gigi.output.template.TranslateCommand;
7
8 public enum Group {
9     SUPPORTER("supporter", "supporter", true, false, true), //
10     ARBITRATOR("arbitrator", "arbitrator", true, false, true), //
11     BLOCKEDASSURER("blockedassurer", "may not verify", true, false, false), //
12     BLOCKEDASSUREE("blockedassuree", "may not be verified", true, false, false), //
13     BLOCKEDLOGIN("blockedlogin", "may not login", true, false, false), //
14     BLOCKEDCERT("blockedcert", "may not issue certificates", true, false, false), //
15     TTP_ASSURER("ttp-assurer", "may verify via TTP", true, false, true), //
16     TTP_APPLICANT("ttp-applicant", "requests to be verified via ttp", false, true, false), //
17     CODESIGNING("codesigning", "may issue codesigning certificates", true, false, false), //
18     ORGASSURER("orgassurer", "may verify organisations", true, false, true), //
19     NUCLEUS_ASSURER("nucleus-assurer", "may enter nucleus verifications", true, false, true), //
20     LOCATE_AGENT("locate-agent", "wants access to the locate agent system", false, true, false);
21
22     private final String dbName;
23
24     private final TranslateCommand tc;
25
26     private final boolean managedBySupport;
27
28     private final boolean managedByUser;
29
30     private final boolean isSelfViewable;
31
32     /**
33      * Creates a new group. Users can join this group or be put into it
34      * (depending on the value of <code>managedBySupport</code>).
35      * 
36      * @param name
37      *            name of the group, used in database
38      * @param display
39      *            text displayed to user
40      * @param managedBySupport
41      *            true if flag is handled by support, false if handled by user
42      * @param isSelfViewable
43      *            true iff user should be able to see others in the same group
44      */
45     private Group(String name, String display, boolean managedBySupport, boolean managedByUser, boolean isSelfViewable) {
46         dbName = name;
47         tc = new TranslateCommand(display);
48         if (managedByUser && managedBySupport) {
49             throw new IllegalArgumentException("We do not allow groups to be user and support managable.");
50         }
51         if (managedByUser && isSelfViewable) {
52             throw new IllegalArgumentException("We do not allow groups to be self-viewable and managable by user.");
53         }
54         this.managedByUser = managedByUser;
55         this.managedBySupport = managedBySupport;
56         this.isSelfViewable = isSelfViewable;
57     }
58
59     public static Group getByString(String name) {
60         return valueOf(name.toUpperCase().replace('-', '_'));
61     }
62
63     public boolean isManagedBySupport() {
64         return managedBySupport;
65     }
66
67     public boolean isManagedByUser() {
68         return managedByUser;
69     }
70
71     public boolean isSelfViewable() {
72         return isSelfViewable;
73     }
74
75     public String getDatabaseName() {
76         return dbName;
77     }
78
79     public User[] getMembers(int offset, int count) {
80         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `user` FROM `user_groups` WHERE `permission`=?::`userGroup` AND `deleted` IS NULL OFFSET ? LIMIT ?", true)) {
81             gps.setString(1, dbName);
82             gps.setInt(2, offset);
83             gps.setInt(3, count);
84             GigiResultSet grs = gps.executeQuery();
85             grs.last();
86             User[] users = new User[grs.getRow()];
87             int i = 0;
88             grs.beforeFirst();
89             while (grs.next()) {
90                 users[i++] = User.getById(grs.getInt(1));
91             }
92             return users;
93         }
94     }
95
96     public int getMemberCount() {
97         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT COUNT(`user`) FROM `user_groups` WHERE `permission`=?::`userGroup` AND `deleted` IS NULL", true)) {
98             gps.setString(1, dbName);
99             GigiResultSet grs = gps.executeQuery();
100             if ( !grs.next()) {
101                 return 0;
102             }
103             return grs.getInt(1);
104         }
105     }
106
107     public Outputable getName() {
108         return tc;
109     }
110 }