KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > UserAccount


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth;
18
19 import org.alfresco.filesys.util.StringList;
20
21 /**
22  * User Account Class
23  * <p>
24  * Holds the details of a user account on the server.
25  */

26 public class UserAccount
27 {
28
29     // User name and password
30

31     private String JavaDoc m_userName;
32     private String JavaDoc m_password;
33
34     // Real user name and comment
35

36     private String JavaDoc m_realName;
37     private String JavaDoc m_comment;
38
39     // List of shares this user is allowed to use
40

41     private StringList m_shares;
42
43     // Administrator flag
44

45     private boolean m_admin;
46
47     // Home directory
48

49     private String JavaDoc m_homeDir;
50
51     /**
52      * Default constructor
53      */

54     public UserAccount()
55     {
56         super();
57     }
58
59     /**
60      * Create a user with the specified name and password.
61      *
62      * @param user String
63      * @param pwd String
64      */

65     public UserAccount(String JavaDoc user, String JavaDoc pwd)
66     {
67         setUserName(user);
68         setPassword(pwd);
69     }
70
71     /**
72      * Add the specified share to the list of allowed shares for this user.
73      *
74      * @param shr java.lang.String
75      */

76     public final void addShare(String JavaDoc shr)
77     {
78         if (m_shares == null)
79             m_shares = new StringList();
80         m_shares.addString(shr);
81     }
82
83     /**
84      * Determine if this user is allowed to access the specified share.
85      *
86      * @return boolean
87      * @param shr java.lang.String
88      */

89     public final boolean allowsShare(String JavaDoc shr)
90     {
91         if (m_shares == null)
92             return true;
93         else if (m_shares.containsString(shr))
94             return true;
95         return false;
96     }
97
98     /**
99      * Check if the user has a home direectory configured
100      *
101      * @return boolean
102      */

103     public final boolean hasHomeDirectory()
104     {
105         return m_homeDir != null ? true : false;
106     }
107
108     /**
109      * Return the home directory for this user
110      *
111      * @return String
112      */

113     public final String JavaDoc getHomeDirectory()
114     {
115         return m_homeDir;
116     }
117
118     /**
119      * Return the password
120      *
121      * @return java.lang.String
122      */

123     public final String JavaDoc getPassword()
124     {
125         return m_password;
126     }
127
128     /**
129      * Return the user name.
130      *
131      * @return java.lang.String
132      */

133     public final String JavaDoc getUserName()
134     {
135         return m_userName;
136     }
137
138     /**
139      * Return the real user name
140      *
141      * @return String
142      */

143     public final String JavaDoc getRealName()
144     {
145         return m_realName;
146     }
147
148     /**
149      * Return the user comment
150      *
151      * @return String
152      */

153     public final String JavaDoc getComment()
154     {
155         return m_comment;
156     }
157
158     /**
159      * Check if the specified share is listed in the users allowed list.
160      *
161      * @return boolean
162      * @param shr java.lang.String
163      */

164     public final boolean hasShare(String JavaDoc shr)
165     {
166         if (m_shares != null && m_shares.containsString(shr) == false)
167             return false;
168         return true;
169     }
170
171     /**
172      * Detemrine if this account is restricted to using certain shares only.
173      *
174      * @return boolean
175      */

176     public final boolean hasShareRestrictions()
177     {
178         return m_shares == null ? false : true;
179     }
180
181     /**
182      * Return the list of shares
183      *
184      * @return StringList
185      */

186     public final StringList getShareList()
187     {
188         return m_shares;
189     }
190
191     /**
192      * Determine if this user in an administrator.
193      *
194      * @return boolean
195      */

196     public final boolean isAdministrator()
197     {
198         return m_admin;
199     }
200
201     /**
202      * Remove all shares from the list of restricted shares.
203      */

204     public final void removeAllShares()
205     {
206         m_shares = null;
207     }
208
209     /**
210      * Remove the specified share from the list of shares this user is allowed to access.
211      *
212      * @param shr java.lang.String
213      */

214     public final void removeShare(String JavaDoc shr)
215     {
216
217         // Check if the share list has been allocated
218

219         if (m_shares != null)
220         {
221
222             // Remove the share from the list
223

224             m_shares.removeString(shr);
225
226             // Check if the list is empty
227

228             if (m_shares.numberOfStrings() == 0)
229                 m_shares = null;
230         }
231     }
232
233     /**
234      * Set the administrator flag.
235      *
236      * @param admin boolean
237      */

238     public final void setAdministrator(boolean admin)
239     {
240         m_admin = admin;
241     }
242
243     /**
244      * Set the user home directory
245      *
246      * @param home String
247      */

248     public final void setHomeDirectory(String JavaDoc home)
249     {
250         m_homeDir = home;
251     }
252
253     /**
254      * Set the password for this account.
255      *
256      * @param pwd java.lang.String
257      */

258     public final void setPassword(String JavaDoc pwd)
259     {
260         m_password = pwd;
261     }
262
263     /**
264      * Set the user name.
265      *
266      * @param user java.lang.String
267      */

268     public final void setUserName(String JavaDoc user)
269     {
270         m_userName = user;
271     }
272
273     /**
274      * Set the real user name
275      *
276      * @param name String
277      */

278     public final void setRealName(String JavaDoc name)
279     {
280         m_realName = name;
281     }
282
283     /**
284      * Set the comment
285      *
286      * @param comment String
287      */

288     public final void setComment(String JavaDoc comment)
289     {
290         m_comment = comment;
291     }
292
293     /**
294      * Return the user account as a string.
295      *
296      * @return java.lang.String
297      */

298     public String JavaDoc toString()
299     {
300         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
301
302         str.append("[");
303         str.append(getUserName());
304         str.append(":");
305         str.append(getPassword());
306
307         if (isAdministrator())
308             str.append("(ADMIN)");
309
310         str.append(",Real=");
311
312         str.append(getRealName());
313         str.append(",Comment=");
314         str.append(getComment());
315         str.append(",Allow=");
316
317         if (m_shares == null)
318             str.append("<ALL>");
319         else
320             str.append(m_shares);
321         str.append("]");
322
323         str.append(",Home=");
324         if (hasHomeDirectory())
325             str.append(getHomeDirectory());
326         else
327             str.append("None");
328
329         return str.toString();
330     }
331 }
Popular Tags