]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Group.java
6e250794274ae607a0dd86d6c91d4b046736de45
[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), ARBITRATOR("arbitrator", "arbitrator", true), //
10     BLOCKEDASSURER("blockedassurer", "may not verify", true), BLOCKEDASSUREE("blockedassuree", "may not be verified", true), //
11     BLOCKEDLOGIN("blockedlogin", "may not login", true), BLOCKEDCERT("blockedcert", "may not issue certificates", true), //
12     TTP_ASSURER("ttp-assurer", "may verify via TTP", true), TTP_APPLICANT("ttp-applicant", "requests to be verified via ttp", true), //
13     CODESIGNING("codesigning", "may issue codesigning certificates", true), ORGASSURER("orgassurer", "may verify organisations", true), //
14     NUCLEUS_ASSURER("nucleus-assurer", "may enter nucleus verifications", true), LOCATE_AGENT("locate-agent", "wants access to the locate agent system", false);
15
16     private final String dbName;
17
18     private final TranslateCommand tc;
19
20     private final boolean managedBySupport; // true if flag is handled by
21                                             // support, false if handled by user
22
23     private Group(String name, String display, boolean managedBySupport) {
24         dbName = name;
25         tc = new TranslateCommand(display);
26         this.managedBySupport = managedBySupport;
27     }
28
29     public static Group getByString(String name) {
30         return valueOf(name.toUpperCase().replace('-', '_'));
31     }
32
33     public boolean isManagedBySupport() {
34         return managedBySupport;
35     }
36
37     public String getDatabaseName() {
38         return dbName;
39     }
40
41     public User[] getMembers(int offset, int count) {
42         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `user` FROM `user_groups` WHERE `permission`=?::`userGroup` AND `deleted` IS NULL OFFSET ? LIMIT ?", true)) {
43             gps.setString(1, dbName);
44             gps.setInt(2, offset);
45             gps.setInt(3, count);
46             GigiResultSet grs = gps.executeQuery();
47             grs.last();
48             User[] users = new User[grs.getRow()];
49             int i = 0;
50             grs.beforeFirst();
51             while (grs.next()) {
52                 users[i++] = User.getById(grs.getInt(1));
53             }
54             return users;
55         }
56     }
57
58     public Outputable getName() {
59         return tc;
60     }
61 }