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