Salome HOME
Refactoring: configuration files are moved into Siman web project.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / User.java
1 package org.splat.kernel;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.security.Principal;
9
10 import org.splat.kernel.Persistent;
11
12
13 public class User extends Persistent implements Principal, Name {
14
15 //  Persistent fields
16         @SuppressWarnings("unused")
17         private String  password;   // Property without getter function
18
19     private String  username;   // Unique in the user directory
20     private String  first;
21     private String  last;
22     private String  display;    // Optional
23     private Role    role;       // Roles as list (as stored into the database)
24     private String  email;
25     private String  organid;
26
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;
38
39 //  - Public services
40
41       public void clear () {
42         super.clear();
43         username = null;
44         password = null;;
45         first    = null;
46         last     = null;
47         display  = null;
48         role     = null;
49         email    = null;
50         organid  = null;
51       }
52           public String getOrganizationName () {
53                 return organid;
54           }
55           public String getPassword () {
56                 return password;
57           }
58           public String getUsername () {
59                 return username;
60           }
61 //  - Property setters
62           
63       public Properties addRole (String role) throws InvalidPropertyException
64       {
65         if (role.length() == 0) throw new InvalidPropertyException("role");
66         if (this.role == null) {
67           this.role = new Role(username, role);
68         } else {
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);
72         }
73         return this;
74       }
75       public Properties setDisplayName (String display) throws InvalidPropertyException
76       {
77         if (display.length() == 0) throw new InvalidPropertyException("displayname");
78         this.display = display;
79         return this;
80       }
81       public Properties setFirstName (String first) throws InvalidPropertyException
82       {
83         if (first.length() == 0) throw new InvalidPropertyException("firstname");
84         this.first = first;
85         return this;
86       }
87       public Properties setMailAddress (String address) throws InvalidPropertyException
88       {
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");
93         this.email = address;
94         return this;
95       }
96       public Properties setName (String last) throws InvalidPropertyException
97       {
98         if (last.length() == 0) throw new InvalidPropertyException("lastname");
99         this.last = last;
100         return this;
101       }
102       public Properties setOrganizationName (String organization) throws InvalidPropertyException
103       {
104         if (organization.length() == 0) throw new InvalidPropertyException("organization");
105         this.organid = organization;
106         return this;
107       }
108       public Properties setPassword (String password) throws InvalidPropertyException
109       {
110         if (password != null) {
111           if (password.length() < 1) throw new InvalidPropertyException("password");
112           this.password = String.valueOf(password.hashCode());
113         }
114         return this;
115       }
116       public Properties setUsername (String username) throws InvalidPropertyException
117       {
118         if (username.length() == 0) throw new InvalidPropertyException("username");
119         this.username = username;
120         return this;
121       }
122 //  - Global validity check
123       
124       public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
125       {
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        
132       }
133     }
134
135 //  ==============================================================================================================================
136 //  Constructors
137 //  ==============================================================================================================================
138
139 //  Database fetch constructor
140     protected User () {
141 //  --------------
142         }
143 //  Anonymous user supposed not to be saved
144         public User (String name) {
145 //  -------------------------
146           username = null;
147           password = null;
148           first    = null;
149           last     = null;
150           display  = name;
151           role     = null;
152           email    = null;
153           organid  = null;
154         }
155 //  New user
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;
167         }
168
169 //  ==============================================================================================================================
170 //  Public member functions
171 //  ==============================================================================================================================
172
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
178       }
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
183       } else {
184         return false;
185       }
186     }
187
188     public String getDisplayName () {
189 //  -------------------------------
190       if (display == null) return last + " " + first;
191       else                 return display;
192     }
193
194     public String getFirstName () {
195 //  -----------------------------
196       return first;
197     }
198
199     public String getMailAddress () {
200 //  -------------------------------
201       return email;
202     }
203
204     public String getName () {
205 //  ------------------------
206       return last;
207     }
208
209     public String getOrganizationName () {
210 //  ------------------------------------
211       return organid;
212     }
213
214     public String getRoleNames () {
215 //  -----------------------------
216       return role.getName();
217     }
218
219     public Role[] getRoles () {
220 //  -------------------------
221       return role.toArray();
222     }
223
224     public String getUsername () {
225 //  ----------------------------
226       return username;
227     }
228
229     public String toString () {
230 //  -------------------------
231       return last + " " + first;
232     }
233 }