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