1 package org.splat.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.security.Principal;
10 import org.splat.kernel.Persistent;
13 public class User extends Persistent implements Principal, Name {
16 @SuppressWarnings("unused")
17 private String password; // Property without getter function
19 private String username; // Unique in the user directory
22 private String display; // Optional
23 private Role role; // Roles as list (as stored into the database)
25 private String organid;
27 // Fields initialization class
28 public static class Properties extends Persistent.Properties {
29 // ------------------------------------------------------------
30 private String username = null;
31 private String password = null;;
32 private String first = null;
33 private String last = null;
34 private String display = null;
35 private Role role = null;
36 private String email = null;
37 private String organid = null;
41 public void clear () {
52 public String getOrganizationName () {
55 public String getPassword () {
58 public String getUsername () {
63 public Properties addRole (String role) throws InvalidPropertyException
65 if (role.length() == 0) throw new InvalidPropertyException("role");
66 if (this.role == null) {
67 this.role = new Role(username, role);
69 Role[] curole = this.role.toArray();
70 for (int i=0; i<curole.length; i++) if (curole[i].is(role)) return this;
71 this.role.addRole(role);
75 public Properties setDisplayName (String display) throws InvalidPropertyException
77 if (display.length() == 0) throw new InvalidPropertyException("displayname");
78 this.display = display;
81 public Properties setFirstName (String first) throws InvalidPropertyException
83 if (first.length() == 0) throw new InvalidPropertyException("firstname");
87 public Properties setMailAddress (String address) throws InvalidPropertyException
89 String[] term = address.split("@"); // Must be of the form x@y
90 if (term.length != 2) throw new InvalidPropertyException("address");
91 term = term[1].split("\\x2E"); // Must be of the form x@y.z
92 if (term.length != 2) throw new InvalidPropertyException("address");
96 public Properties setName (String last) throws InvalidPropertyException
98 if (last.length() == 0) throw new InvalidPropertyException("lastname");
102 public Properties setOrganizationName (String organization) throws InvalidPropertyException
104 if (organization.length() == 0) throw new InvalidPropertyException("organization");
105 this.organid = organization;
108 public Properties setPassword (String password) throws InvalidPropertyException
110 if (password != null) {
111 if (password.length() < 1) throw new InvalidPropertyException("password");
112 this.password = String.valueOf(password.hashCode());
116 public Properties setUsername (String username) throws InvalidPropertyException
118 if (username.length() == 0) throw new InvalidPropertyException("username");
119 this.username = username;
122 // - Global validity check
124 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
126 if (username == null) throw new MissedPropertyException("username");
127 if (first == null) throw new MissedPropertyException("firstname");
128 if (last == null) throw new MissedPropertyException("lastname");
129 if (role == null) throw new MissedPropertyException("role");
130 if (email == null) throw new MissedPropertyException("email");
131 //TODO: Check if username exists
135 // ==============================================================================================================================
137 // ==============================================================================================================================
139 // Database fetch constructor
143 // Anonymous user supposed not to be saved
144 public User (String name) {
145 // -------------------------
156 public User (Properties uprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
157 // ------------------------------
158 super(uprop); // Throws one of the above exception if not valid
159 this.username = uprop.username;
160 this.password = uprop.password;
161 this.first = uprop.first;
162 this.last = uprop.last;
163 this.display = uprop.display;
164 this.role = uprop.role;
165 this.email = uprop.email;
166 this.organid = uprop.organid;
169 // ==============================================================================================================================
170 // Public member functions
171 // ==============================================================================================================================
173 public boolean equals (Object item) {
174 // -----------------------------------
175 if (item == null) return false;
176 if (item instanceof String) {
177 return this.username.equals((String)item); // Usernames are unique
179 else if (item instanceof User) {
180 User given = (User)item;
181 if (isSaved()) return (this.getIndex() == given.getIndex());
182 else return (this.username.equals(given.username)); // Usernames are unique
188 public String getDisplayName () {
189 // -------------------------------
190 if (display == null) return last + " " + first;
194 public String getFirstName () {
195 // -----------------------------
199 public String getMailAddress () {
200 // -------------------------------
204 public String getName () {
205 // ------------------------
209 public String getOrganizationName () {
210 // ------------------------------------
214 public String getRoleNames () {
215 // -----------------------------
216 return role.getName();
219 public Role[] getRoles () {
220 // -------------------------
221 return role.toArray();
224 public String getUsername () {
225 // ----------------------------
229 public String toString () {
230 // -------------------------
231 return last + " " + first;