1 package org.splat.dal.bo.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012-2015
8 import java.security.Principal;
10 import org.splat.kernel.InvalidPropertyException;
11 import org.splat.kernel.MissedPropertyException;
12 import org.splat.kernel.MultiplyDefinedException;
13 import org.splat.kernel.Name;
16 public class User extends Persistent implements Principal, Name {
19 @SuppressWarnings("unused")
20 private String password; // Property without getter function
22 private String username; // Unique in the user directory
25 private String display; // Optional
26 private Role role; // Roles as list (as stored into the database)
28 private String organid;
30 // Fields initialization class
31 public static class Properties extends Persistent.Properties {
32 // ------------------------------------------------------------
33 private String username = null;
34 private String password = null;;
35 private String first = null;
36 private String last = null;
37 private String display = null;
38 private Role role = null;
39 private String email = null;
40 private String organid = null;
45 public void clear () {
56 public String getOrganizationName () {
59 public String getPassword () {
62 public String getUsername () {
67 public Properties addRole (final String role) throws InvalidPropertyException
69 if (role.length() == 0) {
70 throw new InvalidPropertyException("role");
72 if (this.role == null) {
73 this.role = new Role(username, role);
75 Role[] curole = this.role.toArray();
76 for (int i=0; i<curole.length; i++) {
77 if (curole[i].is(role)) {
81 this.role.addRole(role);
85 public Properties setDisplayName (final String display) throws InvalidPropertyException
87 if (display.length() == 0) {
88 throw new InvalidPropertyException("displayname");
90 this.display = display;
93 public Properties setFirstName (final String first) throws InvalidPropertyException
95 if (first.length() == 0) {
96 throw new InvalidPropertyException("firstname");
101 public Properties setMailAddress (final String address) throws InvalidPropertyException
103 String[] term = address.split("@"); // Must be of the form x@y
104 if (term.length != 2) {
105 throw new InvalidPropertyException("address");
107 term = term[1].split("\\x2E"); // Must be of the form x@y.z
108 if (term.length != 2) {
109 throw new InvalidPropertyException("address");
111 this.email = address;
114 public Properties setName (final String last) throws InvalidPropertyException
116 if (last.length() == 0) {
117 throw new InvalidPropertyException("lastname");
122 public Properties setOrganizationName (final String organization) throws InvalidPropertyException
124 if (organization.length() == 0) {
125 throw new InvalidPropertyException("organization");
127 this.organid = organization;
130 public Properties setPassword (final String password) throws InvalidPropertyException
132 if (password != null) {
133 if (password.length() < 1) {
134 throw new InvalidPropertyException("password");
136 this.password = String.valueOf(password.hashCode());
140 public Properties setUsername (final String username) throws InvalidPropertyException
142 if (username.length() == 0) {
143 throw new InvalidPropertyException("username");
145 this.username = username;
148 // - Global validity check
150 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
152 if (username == null) {
153 throw new MissedPropertyException("username");
156 throw new MissedPropertyException("firstname");
159 throw new MissedPropertyException("lastname");
162 throw new MissedPropertyException("role");
165 throw new MissedPropertyException("email");
166 //TODO: Check if username exists
171 // ==============================================================================================================================
173 // ==============================================================================================================================
175 // Database fetch constructor
179 // Anonymous user supposed not to be saved
180 public User (final String name) {
181 // -------------------------
192 public User (final Properties uprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
193 // ------------------------------
194 super(uprop); // Throws one of the above exception if not valid
195 this.username = uprop.username;
196 this.password = uprop.password;
197 this.first = uprop.first;
198 this.last = uprop.last;
199 this.display = uprop.display;
200 this.role = uprop.role;
201 this.email = uprop.email;
202 this.organid = uprop.organid;
205 // ==============================================================================================================================
206 // Public member functions
207 // ==============================================================================================================================
210 public boolean equals (final Object item) {
211 // -----------------------------------
215 if (item instanceof String) {
216 return this.username.equals(item); // Usernames are unique
218 else if (item instanceof User) {
219 User given = (User)item;
221 return (this.getIndex() == given.getIndex());
223 return (given.username.equals(this.username)); // Usernames are unique
230 public String getDisplayName () {
231 // -------------------------------
232 if (display == null) {
233 return last + " " + first;
239 public String getFirstName () {
240 // -----------------------------
244 public String getMailAddress () {
245 // -------------------------------
249 public String getName () {
250 // ------------------------
254 public String getOrganizationName () {
255 // ------------------------------------
259 public String getRoleNames () {
260 // -----------------------------
261 return role.getName();
264 public Role[] getRoles () {
265 // -------------------------
266 return role.toArray();
269 public String getUsername () {
270 // ----------------------------
276 * @see org.splat.dal.bo.kernel.Persistent#toString()
279 public String toString () {
280 return last + " " + first;
286 public Role getRole() {