source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/UserTermInfo.java@ 35287

Last change on this file since 35287 was 35287, checked in by anupama, 3 years ago

Part 2 of commit on expanded user groups, related to prev commit (revision 35286) which was part 1. In this commit, I think I've identified the only times UserTermInfo.getExpandedGroups() needs to be called: when authentication-ping/ServletRealmCheck is called, which redirect to Authentication.processRemoteAuthentication(). At other times, the original group listing is what's required (for user listing display/modification in GS3 Reader Interface) or is sufficient (to check user is in admin group, the group listing in userDB/originally entered group listing is enough and no expanded list is needed. Maybe origGroups can better be renamed back to groups and getOrigGroups replaced with plain getGroups(), since getExpandedGroups() is the method called in more exceptional circumstances after all.

File size: 7.3 KB
Line 
1/*
2 * UserTermInfo.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.gsdl3.util;
21
22import java.util.Set;
23import java.util.TreeSet;
24
25public class UserTermInfo
26{
27 private String username;
28 private String password;
29 private String origGroups;
30 private String expandedGroups = null;
31 private String accountstatus;
32 private String comment;
33 private String email;
34
35 public UserTermInfo(String username, String password, String groups,
36 String accountStatus, String comment, String email) {
37 this.username = username;
38 this.password = password;
39 this.origGroups = groups;
40 //this.expandedGroups = UserTermInfo.expandGroups(this.origGroups); // Will do lazy evaluation
41 this.accountstatus = accountStatus;
42 this.comment = comment;
43 this.email = email;
44 }
45
46 public String toString()
47 {
48 String result = "";
49 result += "<username = " + username + ">";
50 result += "<password = " + password + ">";
51 result += "<groups = " + origGroups + ">";
52 result += "<enable = " + accountstatus + ">";
53 result += "<comment = " + comment + ">";
54 // no line for email?
55 return result;
56 }
57
58
59 public String getUsername() {
60 return username;
61 }
62
63 public String getPassword() {
64 return password;
65 }
66
67 public String getOrigGroups() {
68 return origGroups;
69 }
70
71 public String getExpandedGroups() {
72 //return expandedGroups;
73 //return UserTermInfo.expandGroups(this.origGroups); // not storing, evaluating on demand
74
75 // lazy evaluation: getExpandedGroups() doesn't get called on every enquiry about
76 // a UserTermInfo struct. It only gets called on authenticated-ping system-action
77 // (including via ServletRealmCheck being run from run from commandline or gliserver.pl)
78 // So for a whole list of users and the UserTermInfo struct of each,
79 // don't pre-calculate and store the expanded groups.
80 // Is it meaningful to store this at all Ever or better to always evaluate on demand?
81 if(expandedGroups == null) {
82 expandedGroups = UserTermInfo.expandGroups(this.origGroups);
83 }
84 return expandedGroups;
85 }
86
87 public String getAccountStatus() {
88 return accountstatus;
89 }
90
91 public String getComment() {
92 return comment;
93 }
94
95 public String getEmail() {
96 return email;
97 }
98
99 public void setEmail(String email) {
100 this.email = email;
101 }
102
103 /** We might have groups of the form katoa.maori.(placename.)iwi-name.hapu-name.personal-name
104 * ("katoa" means all, we could also use "mema" meaning member for this initial position).
105 * We want to expand such a group into the multiple groups it encompasses:
106 * katoa, katoa.maori, katoa.maori.place, katoa.maori.place.iwi,
107 * katoa.maori.place.iwi.hapu, katoa.maori.place.iwi.hapu.person
108 * This method looks through all the groups and if any contains a period,
109 * it will expand such into all their explicit subgroups.
110 * @param groups: String listing command-separated groups, any of which may
111 * need expanding out.
112 * @return a comma-separated String listing all the original groups plus any expanded groups
113 * in natural order (so alphabetised).
114 *
115 * Tested at https://www.jdoodle.com/online-java-compiler/, where main contains:
116 * String groups = "all-collections-editor, katoa.maori.place.iwi.hapu.person, admin";
117 *
118 * System.out.println(expandGroups(groups));
119 * Produces:
120 * all-collections-editor,katoa,katoa.maori,katoa.maori.place,katoa.maori.place.iwi,katoa.maori.place.iwi.hapu,katoa.maori.place.iwi.hapu.person,admin
121 *
122 * Also tested with the expandable string at start and at end of list of groups input string.
123 *
124 * This expansion method doesn't preserve overall ordering,
125 * e.g. if groups = "all-collections-editor, katoa.maori.place.iwi.hapu.person, admin"
126 * result is:
127 * admin,all-collections-editor,katoa,katoa.maori,katoa.maori.place,katoa.maori.place.iwi,katoa.maori.place.iwi.hapu,katoa.maori.place.iwi.hapu.person
128 * Where admin is now at start and all-colls-editor now comes after it.
129 * However, this method will ensure the requirement of no duplicates,
130 * e.g. only one katoa, one katoa.maori etc.
131 * to minimising checking if a user has necessary group membership,
132 * e.g. String groups = "all-collections-editor, admin, org.waikato.cs.dl.anu , org.waikato.cs.dl.pinky";
133 * Produces the result:
134 * admin,all-collections-editor,org,org.waikato,org.waikato.cs,org.waikato.cs.dl,org.waikato.cs.dl.anu,org.waikato.cs.dl.pinky
135 */
136 public static String expandGroups(String groups) {
137 if(groups.indexOf('.') == -1) {
138 return groups;
139 }
140 else {
141 Set<String> allGroups = new TreeSet<String>();
142 String[] indivGroups = groups.split("\\s*,\\s*"); // simultaneously get rid of whitespace surrounding commas
143 for(String s : indivGroups) {
144 int indexOfPeriod = s.indexOf('.');
145
146 if(indexOfPeriod != -1) {
147 do {
148 String expandedGroup = s.substring(0, indexOfPeriod);
149 allGroups.add(expandedGroup);
150 indexOfPeriod += 1;
151 indexOfPeriod = s.indexOf('.', indexOfPeriod);
152 } while(indexOfPeriod != -1);
153 }
154
155 // Whether this particular group contained a period or not,
156 // remember to add this group string in in its entirety too
157 allGroups.add(s);
158 }
159
160
161 StringBuilder expandedGroupList = new StringBuilder();//"<groups = \"");
162 for(String s : allGroups) {
163 expandedGroupList.append(s);
164 expandedGroupList.append(',');
165 }
166 expandedGroupList.deleteCharAt(expandedGroupList.length()-1); // remove extraneous comma
167
168 //expandedGroupList.append("\">");
169
170 return expandedGroupList.toString();
171 }
172 }
173
174
175 /*
176 public static String expandGroups(String groups) {
177 if(groups.indexOf('.') == -1) {
178 return groups;
179 }
180 else {
181 // Build up the string again
182 StringBuilder expandedGroupList = new StringBuilder();
183
184 String[] indivGroups = groups.split("\\s*,\\s*"); // simultaneously get rid of any whitespace surrounding commas
185 for(String s : indivGroups) {
186 int indexOfPeriod = s.indexOf('.');
187
188 if(indexOfPeriod != -1) {
189 do {
190 String expandedGroup = s.substring(0, indexOfPeriod);
191 expandedGroupList.append(expandedGroup);
192 expandedGroupList.append(',');
193 indexOfPeriod += 1;
194 indexOfPeriod = s.indexOf('.', indexOfPeriod);
195 } while(indexOfPeriod != -1);
196 }
197
198 // Whether this particular group s contained a period or not,
199 // remember to add this group string, s, back in in its entirety too
200 expandedGroupList.append(s);
201 expandedGroupList.append(',');
202 }
203 expandedGroupList.deleteCharAt(expandedGroupList.length()-1); // remove extraneous comma
204 return expandedGroupList.toString();
205 }
206 }
207 */
208
209}
Note: See TracBrowser for help on using the repository browser.