KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > user > User


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.user;
26
27 import org.snipsnap.app.Application;
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.snip.SnipLink;
30 import org.snipsnap.render.macro.list.Linkable;
31
32 import java.sql.Timestamp JavaDoc;
33
34 /**
35  * User class.
36  *
37  * @author Stephan J. Schmidt
38  * @version $Id: User.java 1606 2004-05-17 10:56:18Z leo $
39  */

40 public class User implements Linkable {
41   private String JavaDoc applicationOid;
42   private String JavaDoc login;
43   private String JavaDoc passwd;
44   private String JavaDoc email;
45   private String JavaDoc status;
46   private Roles roles;
47   // @TODO: composite object
48
private Timestamp JavaDoc lastLogin, lastAccess, lastLogout;
49   // @TODO: -> Modified composite object
50
private Timestamp JavaDoc cTime, mTime;
51
52   private boolean guest = false;
53   private boolean nonUser = false;
54
55   public User() {
56     this("", "", "");
57   }
58   
59   public User(String JavaDoc login, String JavaDoc passwd, String JavaDoc email) {
60     this.login = login;
61     setPasswd(passwd);
62     setEmail(email);
63   }
64
65   public void setApplication(String JavaDoc applicationOid) {
66     this.applicationOid = applicationOid;
67   }
68
69   public String JavaDoc getApplication() {
70     return applicationOid;
71   }
72
73
74   public Timestamp JavaDoc getCTime() {
75     return cTime;
76   }
77
78   public void setCTime(Timestamp JavaDoc cTime) {
79     this.cTime = cTime;
80   }
81
82   public Timestamp JavaDoc getMTime() {
83     return mTime;
84   }
85
86   public void setMTime(Timestamp JavaDoc mTime) {
87     this.mTime = mTime;
88   }
89
90   /**
91    * LastAccess sets the time when the user
92    * last accessed SnipSnap. This is used
93    * to find snips since his last access.
94    */

95   public void lastAccess() {
96     this.lastAccess = new Timestamp JavaDoc(new java.util.Date JavaDoc().getTime());
97     //Logger.debug(this.login+" hashcode: "+((Object) this).hashCode());
98
//Logger.debug("Set lastAccess() "+this.login+" "+lastAccess);
99
}
100
101   public Timestamp JavaDoc getLastLogout() {
102     return lastLogout;
103   }
104
105   public void setLastLogout(Timestamp JavaDoc lastLogout) {
106     //Logger.debug(this.login+" hashcode: "+((Object) this).hashCode());
107
//Logger.debug("Set LastLogout() "+this.login+" "+lastLogout+" old: "+this.lastLogout);
108
this.lastLogout = lastLogout;
109   }
110
111   public Timestamp JavaDoc getLastAccess() {
112     return this.lastAccess;
113   }
114
115   public void setLastAccess(Timestamp JavaDoc lastAccess) {
116     this.lastAccess = lastAccess;
117   }
118
119   public Timestamp JavaDoc getLastLogin() {
120     return lastLogin;
121   }
122
123   public void lastLogin() {
124     setLastLogin(new Timestamp JavaDoc(new java.util.Date JavaDoc().getTime()));
125   }
126
127   public void setLastLogin(Timestamp JavaDoc lastLogin) {
128     this.lastLogin = lastLogin;
129   }
130
131   public void setStatus(String JavaDoc status) {
132     this.status = status;
133     return;
134   }
135
136   public String JavaDoc getStatus() {
137     if (null == status) {
138       status = "not set";
139     }
140     return status;
141   }
142
143   public void setEmail(String JavaDoc email) {
144     this.email = email;
145   }
146
147   public String JavaDoc getEmail() {
148     return email;
149   }
150
151   // Set passwd of user. Takes unecrypted
152
// passwd and then sets an encrypted version
153
public void setPasswd(String JavaDoc passwd) {
154     if (passwd != null && passwd.length() > 30) {
155       this.passwd = passwd;
156     } else {
157       this.passwd = Digest.getDigest(passwd);
158     }
159   }
160
161   public String JavaDoc getPasswd() {
162     return passwd;
163   }
164
165   /**
166    * WARNING: DO NOT USE THIS METHOD UNLESS YOU KNOW WHAT YOU DO.
167    * @param login
168    */

169   public void setLogin(String JavaDoc login) {
170     this.login = login;
171   }
172
173   public String JavaDoc getLogin() {
174     return login;
175   }
176
177   public void setRoles(Roles roles) {
178     this.roles = roles;
179     return;
180   }
181
182   public Roles getRoles() {
183     if (null == roles) {
184       roles = new Roles();
185     }
186     return roles;
187   }
188
189   public boolean isAdmin() {
190     Application app = Application.get();
191     Configuration config = app.getConfiguration();
192     return (config.getAdminLogin() != null && config.getAdminLogin().equals(login))
193       || getRoles().contains(Roles.ADMIN);
194   }
195
196   public void setGuest(boolean guest) {
197     this.guest = guest;
198   }
199
200   public boolean isGuest() {
201     return guest;
202   }
203
204   public void setNonUser(boolean nonUser) {
205     this.nonUser = nonUser;
206   }
207
208   public boolean isNonUser() {
209     return nonUser;
210   }
211
212   public int hashCode() {
213     return getLogin().hashCode();
214   }
215
216   public boolean equals(Object JavaDoc obj) {
217     if (obj instanceof User && obj != null && this.getLogin() != null) {
218       return this.getLogin().equals(((User) obj).getLogin());
219     }
220     return false;
221   }
222
223   public String JavaDoc toString() {
224     return "User[" + login + "," + (passwd != null ? "pass set" : "no pass") + "," + email + "," + status + "," + roles + "]";
225   }
226
227   public String JavaDoc getLink() {
228     if (isNonUser()) {
229       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
230       tmp.append("<a HREF=\"");
231       tmp.append(getEmail());
232       tmp.append("\">");
233       tmp.append(getLogin());
234       tmp.append("</a>");
235       return tmp.toString();
236     } else if (isGuest()) {
237       return "Guest";
238     } else {
239       return SnipLink.createLink(getLogin());
240     }
241   }
242
243 }
Popular Tags