0
|
1 /*
|
|
2
|
|
3 Copyright (C) 2003 Franklin Schmidt <frank@gustos.com>
|
|
4
|
|
5 */
|
|
6
|
|
7 package nabble.model;
|
|
8
|
|
9 import fschmidt.db.DbDatabase;
|
|
10 import fschmidt.db.DbNull;
|
|
11 import fschmidt.db.DbObjectFactory;
|
|
12 import fschmidt.db.DbRecord;
|
|
13 import fschmidt.db.DbTable;
|
|
14 import fschmidt.db.DbUtils;
|
|
15 import fschmidt.db.Listener;
|
|
16 import fschmidt.db.ListenerList;
|
|
17 import fschmidt.db.LongKey;
|
|
18 import fschmidt.db.postgres.DbDatabaseImpl;
|
|
19 import fschmidt.util.java.Computable;
|
|
20 import fschmidt.util.java.Memoizer;
|
|
21 import fschmidt.util.java.ObjectUtils;
|
|
22 import fschmidt.util.java.SimpleCache;
|
|
23 import fschmidt.util.java.TimedCacheMap;
|
|
24 import fschmidt.util.mail.MailAddress;
|
|
25 import org.jasypt.digest.PooledStringDigester;
|
|
26 import org.jasypt.salt.FixedByteArraySaltGenerator;
|
|
27 import org.slf4j.Logger;
|
|
28 import org.slf4j.LoggerFactory;
|
|
29
|
|
30 import java.awt.image.BufferedImage;
|
|
31 import java.sql.Connection;
|
|
32 import java.sql.PreparedStatement;
|
|
33 import java.sql.ResultSet;
|
|
34 import java.sql.SQLException;
|
|
35 import java.sql.Statement;
|
|
36 import java.util.ArrayList;
|
|
37 import java.util.Collection;
|
|
38 import java.util.Collections;
|
|
39 import java.util.Date;
|
|
40 import java.util.HashMap;
|
|
41 import java.util.HashSet;
|
|
42 import java.util.Iterator;
|
|
43 import java.util.List;
|
|
44 import java.util.Map;
|
|
45 import java.util.Set;
|
|
46 import java.util.WeakHashMap;
|
|
47 import java.util.concurrent.CopyOnWriteArrayList;
|
|
48
|
|
49
|
|
50 final class UserImpl extends PersonImpl implements User {
|
|
51 private static final Logger logger = LoggerFactory.getLogger(UserImpl.class);
|
|
52
|
|
53 final SiteKey siteKey;
|
|
54 private final DbRecord<LongKey,UserImpl> record;
|
|
55 private String email;
|
|
56 private String passwordDigest;
|
|
57 private String name;
|
|
58 private Date registered;
|
51
|
59 private boolean noArchive;
|
0
|
60 private Message signature = null;
|
|
61
|
|
62 private UserImpl(SiteKey siteKey,LongKey key,ResultSet rs)
|
|
63 throws SQLException
|
|
64 {
|
|
65 this.siteKey = siteKey;
|
|
66 record = table(siteKey).newRecord(this,key);
|
|
67 email = rs.getString("email");
|
|
68 passwordDigest = rs.getString("password_digest");
|
|
69 name = rs.getString("name");
|
|
70 registered = DbUtils.getDate(rs,"registered");
|
51
|
71 noArchive = rs.getBoolean("no_archive");
|
0
|
72 String signatureRaw = rs.getString("signature");
|
|
73 String signatureFormatS = rs.getString("signature_format");
|
|
74 if( signatureRaw!=null && signatureFormatS!=null ) {
|
|
75 Message.Format signatureFormat = Message.Format.getMessageFormat( signatureFormatS.charAt(0) );
|
|
76 signature = new Message(signatureRaw,signatureFormat);
|
|
77 }
|
|
78 for( ExtensionFactory<User,?> factory : extensionFactories ) {
|
|
79 Object obj = factory.construct(this,rs);
|
|
80 if( obj != null )
|
|
81 getExtensionMap().put(factory,obj);
|
|
82 }
|
|
83 }
|
|
84
|
|
85 private UserImpl(SiteImpl site) {
|
|
86 this.siteKey = site.siteKey;
|
|
87 record = table(siteKey).newRecord(this);
|
|
88 }
|
|
89
|
|
90
|
|
91 public DbRecord<LongKey,UserImpl> getDbRecord() {
|
|
92 return record;
|
|
93 }
|
|
94
|
|
95 private DbTable<LongKey,UserImpl> table() {
|
|
96 return record.getDbTable();
|
|
97 }
|
|
98
|
|
99 private DbDatabase db() {
|
|
100 return table().getDbDatabase();
|
|
101 }
|
|
102
|
|
103 public long getId() {
|
|
104 return record.getPrimaryKey().value();
|
|
105 }
|
|
106
|
|
107 SiteImpl getSiteImpl() {
|
|
108 return siteKey.site();
|
|
109 }
|
|
110
|
|
111 public Site getSite() {
|
|
112 return getSiteImpl();
|
|
113 }
|
|
114
|
|
115 public boolean isDeactivated() {
|
51
|
116 return !isRegistered() && noArchive;
|
|
117 }
|
|
118
|
|
119 private void setNoArchive(boolean noArchive) {
|
|
120 if( this.noArchive == noArchive )
|
|
121 return;
|
|
122
|
|
123 if( !db().isInTransaction() ) {
|
|
124 db().beginTransaction();
|
|
125 try {
|
|
126 UserImpl user = DbUtils.getGoodCopy(this);
|
|
127 user.setNoArchive(noArchive);
|
|
128 user.getDbRecord().update();
|
|
129 db().commitTransaction();
|
|
130 return;
|
|
131 } finally {
|
|
132 db().endTransaction();
|
|
133 }
|
|
134 }
|
|
135 this.noArchive = noArchive;
|
|
136 record.fields().put("no_archive",DbNull.fix(noArchive));
|
0
|
137 }
|
|
138
|
|
139 public String getEmail() {
|
|
140 return email;
|
|
141 }
|
|
142
|
|
143 static void validateEmail(String email) throws ModelException.EmailFormat {
|
|
144 if (!new MailAddress(email).isValid()) {
|
|
145 throw new ModelException.EmailFormat(email);
|
|
146 }
|
|
147 }
|
|
148
|
|
149 public void setEmail(String email) throws ModelException {
|
|
150 if( !db().isInTransaction() ) {
|
|
151 db().beginTransaction();
|
|
152 try {
|
|
153 UserImpl user = DbUtils.getGoodCopy(this);
|
|
154 user.setEmail(email);
|
|
155 user.getDbRecord().update();
|
|
156 db().commitTransaction();
|
|
157 return;
|
|
158 } finally {
|
|
159 db().endTransaction();
|
|
160 }
|
|
161 }
|
|
162 validateEmail(email);
|
|
163 setEmail2(email);
|
|
164 }
|
|
165
|
|
166 private void setEmail2(String email) throws ModelException {
|
|
167 if( email.equals(this.email) )
|
|
168 return;
|
|
169 SiteImpl site = getSiteImpl();
|
|
170 if( site.getUserImplFromEmail(email) != null )
|
|
171 throw ModelException.newInstance("email_already_in_user","Email already in use");
|
|
172 this.email = email;
|
|
173 record.fields().put("email",email);
|
|
174 }
|
|
175
|
|
176 public String getPasswordDigest() {
|
|
177 return passwordDigest;
|
|
178 }
|
|
179
|
|
180 public void setPassword(String password) throws ModelException {
|
|
181 if( "".equals(password) )
|
|
182 throw ModelException.newInstance("empty_password","Password cannot be empty");
|
|
183 setPasswordDigest(digestPassword(password));
|
|
184 }
|
|
185
|
|
186 public void setPasswordDigest(String passwordDigest) {
|
|
187 if( ObjectUtils.equals(passwordDigest,this.passwordDigest) )
|
|
188 return;
|
|
189 this.passwordDigest = passwordDigest;
|
|
190 record.fields().put("password_digest",DbNull.fix(passwordDigest));
|
|
191 synchronized (passcookieLock) {
|
|
192 this.passcookie = null;
|
|
193 }
|
|
194 }
|
|
195
|
|
196 private volatile String passcookie = null;
|
|
197 private Object passcookieLock = new Object();
|
|
198
|
|
199 public String getPasscookie() {
|
|
200 String p = passcookie;
|
|
201 if (p==null) {
|
|
202 synchronized (passcookieLock) {
|
|
203 p = passcookie;
|
|
204 if (p==null) {
|
|
205 p = calcPasscookie();
|
|
206 passcookie = p;
|
|
207 }
|
|
208 }
|
|
209 }
|
|
210 return p;
|
|
211 }
|
|
212
|
|
213 public String getName() {
|
|
214 return name;
|
|
215 }
|
|
216
|
|
217 public void setName(String name) throws ModelException {
|
|
218 setName(name,true);
|
|
219 }
|
|
220
|
|
221 private void setName(String name,boolean replaceUnregistered) throws ModelException {
|
|
222 name = name.trim();
|
|
223 if( name.equals("") )
|
|
224 throw ModelException.newInstance("empty_user_name","User name cannot be empty.");
|
|
225 if( name.equals(this.name) )
|
|
226 return;
|
|
227 if( !name.equalsIgnoreCase(this.name) ) {
|
|
228 UserImpl user = getSiteImpl().getUserImplFromName(name);
|
|
229 if( user != null ) {
|
|
230 if( !replaceUnregistered || user.isRegistered() )
|
|
231 throw ModelException.newInstance("user_name_already_in_use","User name '"+name+"' already in use");
|
|
232 user.setNameLike2(name);
|
|
233 user.update();
|
|
234 }
|
|
235 try {
|
|
236 Connection con = db().getConnection();
|
|
237 PreparedStatement stmt = con.prepareStatement(
|
|
238 "select 'x' from registration where email!=? and name=?"
|
|
239 );
|
|
240 stmt.setString(1,this.email);
|
|
241 stmt.setString(2,name);
|
|
242 try {
|
|
243 if( stmt.executeQuery().next() )
|
|
244 throw ModelException.newInstance("user_name_already_in_use","User name '"+name+"' already in use");
|
|
245 } finally {
|
|
246 stmt.close();
|
|
247 con.close();
|
|
248 }
|
|
249 } catch(SQLException e) {
|
|
250 throw new RuntimeException(e);
|
|
251 }
|
|
252 }
|
|
253 this.name = name;
|
|
254 record.fields().put("name",name);
|
|
255 }
|
|
256
|
|
257 void setNameLike(String name,boolean replaceUnregistered) {
|
|
258 try {
|
|
259 setName(name,replaceUnregistered);
|
|
260 } catch(ModelException e) {
|
|
261 setNameLike2(name);
|
|
262 }
|
|
263 }
|
|
264
|
|
265 private void setNameLike2(String name) {
|
|
266 for( int i=2; true; i++ ) {
|
|
267 try {
|
|
268 setName(name+"-"+i,false);
|
|
269 break;
|
|
270 } catch(ModelException e2) {}
|
|
271 }
|
|
272 }
|
|
273
|
|
274 /* To be called from the shell */
|
|
275 public void changeNameTo(String newName) {
|
|
276 db().beginTransaction();
|
|
277 try {
|
|
278 UserImpl u = (UserImpl) getGoodCopy();
|
|
279 u.setName(newName);
|
|
280 u.update();
|
|
281 db().commitTransaction();
|
|
282 DbUtils.uncache(u);
|
|
283 } catch (ModelException e) {
|
|
284 throw new RuntimeException(e);
|
|
285 } finally {
|
|
286 db().endTransaction();
|
|
287 }
|
|
288 }
|
|
289
|
|
290 public Date getRegistered() {
|
|
291 return registered;
|
|
292 }
|
|
293
|
|
294 void setRegistered(Date registered) {
|
|
295 if( ObjectUtils.equals(registered,this.registered) )
|
|
296 return;
|
|
297 this.registered = registered;
|
|
298 record.fields().put("registered",DbNull.fix(registered));
|
|
299 }
|
|
300
|
|
301 public boolean equals(Object obj) {
|
|
302 return obj instanceof User && ((User)obj).getId()==getId();
|
|
303 }
|
|
304
|
|
305 public int hashCode() {
|
|
306 return (int)getId();
|
|
307 }
|
|
308
|
|
309 public String toString() {
|
|
310 return record.isInDb() ? "user-"+getId() : "user-new";
|
|
311 }
|
|
312
|
|
313 public void register() throws ModelException {
|
|
314 register(new Date());
|
|
315 }
|
|
316
|
|
317 public void register(Date registerDate) throws ModelException {
|
|
318 if( !db().isInTransaction() ) {
|
|
319 db().beginTransaction();
|
|
320 try {
|
|
321 UserImpl user;
|
|
322 if( record.isInDb() ) {
|
|
323 user = DbUtils.getGoodCopy(this);
|
|
324 user.setEmail(email);
|
|
325 user.setName(name);
|
|
326 user.setPasswordDigest(passwordDigest);
|
|
327 } else {
|
|
328 user = this;
|
|
329 }
|
|
330 user.register();
|
|
331 db().commitTransaction();
|
|
332 } finally {
|
|
333 db().endTransaction();
|
|
334 }
|
|
335 return;
|
|
336 }
|
|
337 if( passwordDigest==null )
|
|
338 throw new RuntimeException();
|
|
339 setRegistered( registerDate );
|
|
340 if( record.isInDb() ) {
|
|
341 record.update();
|
|
342 } else {
|
|
343 insert();
|
|
344 }
|
|
345 }
|
|
346
|
|
347 public boolean isRegistered() {
|
|
348 return record.isInDb() && registered!=null;
|
|
349 }
|
|
350
|
|
351 void insert() {
|
|
352 if( email==null || name==null )
|
|
353 throw new RuntimeException();
|
|
354 record.insert();
|
|
355 }
|
|
356
|
|
357 public void update() {
|
|
358 if( !db().isInTransaction() )
|
|
359 throw new RuntimeException("this should be done in a transaction");
|
|
360 Set<String> keys = record.fields().keySet();
|
|
361 if( keys.contains("name") || keys.contains("signature") ) {
|
|
362 getSiteImpl().update(); // fire change listeners
|
|
363 }
|
|
364 getDbRecord().update();
|
|
365 }
|
|
366
|
|
367 public User getGoodCopy() {
|
|
368 return DbUtils.getGoodCopy(this);
|
|
369 }
|
|
370
|
|
371
|
|
372
|
|
373
|
|
374 public int getExternalHash(String url) {
|
|
375 return (url.toLowerCase() + getId()).hashCode();
|
|
376 }
|
|
377
|
|
378
|
|
379 static final ListenerList<UserImpl> preUpdateListeners = new ListenerList<UserImpl>();
|
|
380 static final ListenerList<UserImpl> postInsertListeners = new ListenerList<UserImpl>();
|
|
381
|
|
382 private static Computable<SiteKey,DbTable<LongKey,UserImpl>> tables = new SimpleCache<SiteKey,DbTable<LongKey,UserImpl>>(new WeakHashMap<SiteKey,DbTable<LongKey,UserImpl>>(), new Computable<SiteKey,DbTable<LongKey,UserImpl>>() {
|
|
383 public DbTable<LongKey,UserImpl> get(SiteKey siteKey) {
|
|
384 DbDatabase db = siteKey.getDb();
|
|
385 final long siteId = siteKey.getId();
|
|
386 DbTable<LongKey,UserImpl> table = db.newTable("user_",db.newIdentityLongKeySetter("user_id")
|
|
387 , new DbObjectFactory<LongKey,UserImpl>() {
|
|
388 public UserImpl makeDbObject(LongKey key,ResultSet rs,String tableName)
|
|
389 throws SQLException
|
|
390 {
|
|
391 SiteKey siteKey = SiteKey.getInstance(siteId);
|
|
392 return new UserImpl(siteKey,key,rs);
|
|
393 }
|
|
394 }
|
|
395 );
|
|
396 table.getPreUpdateListeners().add(preUpdateListeners);
|
|
397 table.getPostInsertListeners().add(postInsertListeners);
|
|
398 return table;
|
|
399 }
|
|
400 });
|
|
401
|
|
402 private static DbTable<LongKey,UserImpl> table(SiteKey siteKey) {
|
|
403 return tables.get(siteKey);
|
|
404 }
|
|
405
|
|
406 static UserImpl getUser(SiteKey siteKey,long id) {
|
|
407 UserImpl user = table(siteKey).findByPrimaryKey(new LongKey(id));
|
|
408 if( user==null )
|
|
409 logger.warn("user "+id+" not found");
|
|
410 return user;
|
|
411 }
|
|
412
|
|
413 static Collection<UserImpl> getUsers(SiteKey siteKey,Collection<Long> ids) {
|
|
414 List<LongKey> list = new ArrayList<LongKey>();
|
|
415 for( long id : ids ) {
|
|
416 list.add( new LongKey(id) );
|
|
417 }
|
|
418 return table(siteKey).findByPrimaryKey(list).values();
|
|
419 }
|
|
420
|
|
421 static UserImpl getUser(SiteKey siteKey,ResultSet rs)
|
|
422 throws SQLException
|
|
423 {
|
|
424 return table(siteKey).getDbObject(rs);
|
|
425 }
|
|
426
|
|
427 static void getUsers(SiteKey siteKey,PreparedStatement stmt,List<? super UserImpl> list)
|
|
428 throws SQLException
|
|
429 {
|
|
430 ResultSet rs = stmt.executeQuery();
|
|
431 while( rs.next() ) {
|
|
432 UserImpl user = getUser(siteKey,rs);
|
|
433 list.add(user);
|
|
434 }
|
|
435 rs.close();
|
|
436 stmt.close();
|
|
437 }
|
|
438
|
|
439 static List<UserImpl> getUsers(SiteKey siteKey,PreparedStatement stmt)
|
|
440 throws SQLException
|
|
441 {
|
|
442 List<UserImpl> list = new ArrayList<UserImpl>();
|
|
443 getUsers(siteKey,stmt,list);
|
|
444 return list;
|
|
445 }
|
|
446
|
|
447 private static UserImpl getUser(SiteImpl site,String val,String sql) {
|
|
448 try {
|
|
449 SiteKey siteKey = site.siteKey;
|
|
450 Connection con = siteKey.getDb().getConnection();
|
|
451 PreparedStatement stmt = con.prepareStatement(sql);
|
|
452 stmt.setString(1,val);
|
|
453 ResultSet rs = stmt.executeQuery();
|
|
454 UserImpl user = rs.next() ? getUser(siteKey,rs) : null;
|
|
455 rs.close();
|
|
456 stmt.close();
|
|
457 con.close();
|
|
458 return user;
|
|
459 } catch(SQLException e) {
|
|
460 throw new RuntimeException(e);
|
|
461 }
|
|
462 }
|
|
463
|
|
464 static UserImpl getUserFromEmail(SiteImpl site,String email) {
|
|
465 return getUser( site, email.toLowerCase(),
|
|
466 "select * from user_"
|
|
467 +" where lower(email)=?"
|
|
468 );
|
|
469 }
|
|
470
|
|
471 static UserImpl getUserFromName(SiteImpl site,String name) {
|
|
472 return getUser( site, name.toLowerCase(),
|
|
473 "select * from user_"
|
|
474 +" where lower(name)=?"
|
|
475 );
|
|
476 }
|
|
477
|
|
478 static UserImpl createGhost(SiteImpl site,String email) {
|
|
479 UserImpl user = new UserImpl(site);
|
|
480 try {
|
|
481 user.setEmail2(email);
|
|
482 } catch(ModelException e) {
|
|
483 throw new RuntimeException(e);
|
|
484 }
|
|
485 return user;
|
|
486 }
|
|
487
|
|
488 // Subscriptions -----------------------------------------------------------
|
|
489
|
|
490 public boolean isSubscribed(Node node) {
|
|
491 return SubscriptionImpl.isSubscribed(this, (NodeImpl) node);
|
|
492 }
|
|
493
|
|
494 public Subscription getSubscription(Node node) {
|
|
495 return SubscriptionImpl.getSubscription( this, (NodeImpl)node );
|
|
496 }
|
|
497
|
|
498 public Subscription subscribe(Node node,Subscription.To to,Subscription.Type type) {
|
|
499 Subscription subscription = getSubscription(node);
|
|
500 if( subscription != null ) {
|
|
501 subscription.setTo(to);
|
|
502 subscription.setType(type);
|
|
503 return subscription;
|
|
504 } else {
|
|
505 return SubscriptionImpl.insert( this, (NodeImpl)node, to, type );
|
|
506 }
|
|
507 }
|
|
508
|
|
509 /*10 posts in 5 minutes */
|
|
510 private static final RecentPostLimit postLimit1 = new RecentPostLimit(5 * 60 * 1000L, 10);
|
|
511
|
|
512 /* 30 posts in 15 minutes */
|
|
513 private static final RecentPostLimit postLimit2 = new RecentPostLimit(15 * 60 * 1000L, 30);
|
|
514
|
|
515 void updateNewPostLimit() {
|
|
516 String key = siteKey.getId() + "-" + record.getPrimaryKey().value();
|
|
517 postLimit1.insert(key);
|
|
518 postLimit2.insert(key);
|
|
519 }
|
|
520
|
|
521 public boolean hasTooManyPosts() {
|
|
522 String key = siteKey.getId() + "-" + record.getPrimaryKey().value();
|
|
523 return postLimit1.hasTooManyPosts(key) || postLimit2.hasTooManyPosts(key);
|
|
524 }
|
|
525
|
|
526 private static class RecentPostLimit {
|
|
527 private final long timeLimit;
|
|
528 private final int postLimit;
|
|
529 private final Map<String,long[]> floodMap;
|
|
530
|
|
531 private RecentPostLimit(long timeLimit, int postLimit) {
|
|
532 this.timeLimit = timeLimit;
|
|
533 this.postLimit = postLimit;
|
|
534 this.floodMap = new TimedCacheMap<String,long[]>(timeLimit);
|
|
535 }
|
|
536
|
|
537 public void insert(String key) {
|
|
538 long[] recentPostTimes;
|
|
539 synchronized(floodMap) {
|
|
540 recentPostTimes = floodMap.get(key);
|
|
541 if( recentPostTimes==null ) {
|
|
542 recentPostTimes = new long[postLimit];
|
|
543 floodMap.put(key,recentPostTimes);
|
|
544 }
|
|
545 }
|
|
546 long now = System.currentTimeMillis();
|
|
547 long recently = now - timeLimit;
|
|
548 synchronized(recentPostTimes) {
|
|
549 for( int i=0; i<recentPostTimes.length; i++ ) {
|
|
550 if( recentPostTimes[i] < recently ) {
|
|
551 recentPostTimes[i] = now;
|
|
552 return;
|
|
553 }
|
|
554 }
|
|
555 }
|
|
556 }
|
|
557
|
|
558 public boolean hasTooManyPosts(String key) {
|
|
559 long[] recentPostTimes;
|
|
560 synchronized(floodMap) {
|
|
561 recentPostTimes = floodMap.get(key);
|
|
562 if (recentPostTimes==null)
|
|
563 return false;
|
|
564 }
|
|
565 long now = System.currentTimeMillis();
|
|
566 long recently = now - timeLimit;
|
|
567 synchronized(recentPostTimes) {
|
|
568 for (long time : recentPostTimes) {
|
|
569 if (time < recently) {
|
|
570 return false;
|
|
571 }
|
|
572 }
|
|
573 }
|
|
574 return true;
|
|
575 }
|
|
576 }
|
|
577
|
|
578
|
|
579 static UserImpl getOrCreateUnregisteredUser(SiteImpl site,String email,String name)
|
|
580 throws ModelException
|
|
581 {
|
|
582 DbDatabase db = site.getDb();
|
|
583 if( !db.isInTransaction() ) {
|
|
584 db.beginTransaction();
|
|
585 try {
|
|
586 UserImpl user = getOrCreateUnregisteredUser(site,email,name);
|
|
587 db.commitTransaction();
|
|
588 return user;
|
|
589 } finally {
|
|
590 db.endTransaction();
|
|
591 }
|
|
592 }
|
|
593 UserImpl user = site.getUserImplFromEmail(email);
|
|
594 if( user==null ) {
|
|
595 user = new UserImpl(site);
|
|
596 user.setEmail(email);
|
|
597 } else {
|
|
598 if( user.isRegistered() )
|
|
599 throw ModelException.newInstance("email_already_registered","This email is already registered");
|
|
600 validateEmail(user.getEmail());
|
|
601 }
|
|
602 user.setName(name);
|
|
603 if( !user.record.isInDb() ) {
|
|
604 user.insert();
|
|
605 } else if( !user.record.fields().isEmpty() ) {
|
|
606 user.update();
|
|
607 }
|
|
608 return user;
|
|
609 }
|
|
610
|
|
611 // registration
|
|
612
|
|
613 static UserImpl createUser(SiteImpl site,String email,String password,String name) throws ModelException {
|
|
614 return createUser2(site, email, digestPassword(password), name);
|
|
615 }
|
|
616
|
|
617 private static UserImpl createUser2(SiteImpl site,String email,String passwordDigest,String name) throws ModelException {
|
|
618 // transaction used because setName() may update user
|
|
619 DbDatabase db = site.getDb();
|
|
620 if( !db.isInTransaction() ) {
|
|
621 db.beginTransaction();
|
|
622 try {
|
|
623 UserImpl user = createUser2(site,email,passwordDigest,name);
|
|
624 db.commitTransaction();
|
|
625 return user;
|
|
626 } finally {
|
|
627 db.endTransaction();
|
|
628 }
|
|
629 }
|
|
630 if (!new MailAddress(email).isValid()) {
|
|
631 throw new ModelException.EmailFormat("invalid_email");
|
|
632 }
|
|
633 UserImpl user = site.getUserImplFromEmail(email);
|
|
634 if( user==null ) {
|
|
635 user = new UserImpl(site);
|
|
636 user.setEmail(email);
|
|
637 } else {
|
|
638 if( user.isRegistered() )
|
|
639 throw ModelException.newInstance("user_already_registered","User is already registered");
|
|
640 validateEmail(user.getEmail());
|
|
641 }
|
|
642 user.setPasswordDigest(passwordDigest);
|
|
643 user.setName(name);
|
|
644 return user;
|
|
645 }
|
|
646
|
|
647 static UserImpl getOrCreateUser(SiteImpl site,String email) {
|
|
648 UserImpl user = site.getUserImplFromEmail(email);
|
|
649 if (user == null) {
|
|
650 String username = email.substring(0, email.indexOf('@'));
|
|
651 user = createGhost(site,email);
|
|
652 user.setNameLike(username, false);
|
|
653 user.insert();
|
|
654 }
|
|
655 return user;
|
|
656 }
|
|
657
|
|
658 private static final Object regLock = new Object();
|
|
659
|
|
660 String newRegistration(String nextUrl) {
|
|
661 if( nextUrl.equals("null") )
|
|
662 throw new RuntimeException("nextUrl is \"null\"");
|
|
663 synchronized(regLock) {
|
|
664 String key;
|
|
665 try {
|
|
666 Connection con = db().getConnection();
|
|
667 {
|
|
668 PreparedStatement stmt = con.prepareStatement(
|
|
669 "select 'x' from registration where key_=?"
|
|
670 );
|
|
671 do {
|
|
672 key = Double.toString(Math.random());
|
|
673 stmt.setString(1,key);
|
|
674 } while( stmt.executeQuery().next() );
|
|
675 stmt.close();
|
|
676 }
|
|
677 {
|
|
678 PreparedStatement stmt = con.prepareStatement(
|
|
679 "insert into registration"
|
|
680 +" ( key_, email, password_digest, name, next_url ) values (?,?,?,?,?)"
|
|
681 );
|
|
682 int i = 0;
|
|
683 stmt.setString(++i,key);
|
|
684 stmt.setString(++i,getEmail());
|
|
685 stmt.setString(++i,getPasswordDigest());
|
|
686 stmt.setString(++i,getName());
|
|
687 stmt.setString(++i,nextUrl);
|
|
688 stmt.executeUpdate();
|
|
689 stmt.close();
|
|
690 }
|
|
691 {
|
|
692 Statement stmt = con.createStatement();
|
|
693 stmt.executeUpdate(
|
|
694 "delete from registration where date_<" + Db.arcana.dateSub("now()",7,"day")
|
|
695 );
|
|
696 stmt.close();
|
|
697 }
|
|
698 con.close();
|
|
699 } catch(SQLException e) {
|
|
700 throw new RuntimeException(e);
|
|
701 }
|
|
702 return key;
|
|
703 }
|
|
704 }
|
|
705
|
|
706 static User getRegistration(SiteImpl site,String registrationKey)
|
|
707 throws ModelException
|
|
708 {
|
|
709 try {
|
|
710 DbDatabase db = site.getDb();
|
|
711 Connection con = db.getConnection();
|
|
712 PreparedStatement stmt = con.prepareStatement(
|
|
713 "select * from registration where key_=?"
|
|
714 );
|
|
715 stmt.setString(1,registrationKey);
|
|
716 ResultSet rs = stmt.executeQuery();
|
|
717 try {
|
|
718 if( !rs.next() )
|
|
719 return null;
|
|
720 String email = rs.getString("email");
|
|
721 String passwordDigest = rs.getString("password_digest");
|
|
722 String name = rs.getString("name");
|
|
723 return createUser2(site,email,passwordDigest,name);
|
|
724 } finally {
|
|
725 rs.close();
|
|
726 stmt.close();
|
|
727 con.close();
|
|
728 }
|
|
729 } catch(SQLException e) {
|
|
730 throw new RuntimeException(e);
|
|
731 }
|
|
732 }
|
|
733
|
|
734 static String getNextUrl(SiteKey siteKey,String registrationKey)
|
|
735 {
|
|
736 try {
|
|
737 Connection con = siteKey.getDb().getConnection();
|
|
738 PreparedStatement stmt = con.prepareStatement(
|
|
739 "select next_url from registration where key_=?"
|
|
740 );
|
|
741 stmt.setString(1,registrationKey);
|
|
742 ResultSet rs = stmt.executeQuery();
|
|
743 try {
|
|
744 if( !rs.next() )
|
|
745 return null;
|
|
746 return rs.getString("next_url");
|
|
747 } finally {
|
|
748 rs.close();
|
|
749 stmt.close();
|
|
750 con.close();
|
|
751 }
|
|
752 } catch(SQLException e) {
|
|
753 throw new RuntimeException(e);
|
|
754 }
|
|
755 }
|
|
756
|
|
757 // Called from beanshell
|
|
758 private static void deletePendingRegistration(Site site,String email, String username) {
|
|
759 try {
|
|
760 Connection con = site.getDb().getConnection();
|
|
761 PreparedStatement stmt = con.prepareStatement(
|
|
762 "delete from registration where email=? or name = ?"
|
|
763 );
|
|
764 stmt.setString(1,email);
|
|
765 stmt.setString(2,username);
|
|
766 stmt.executeUpdate();
|
|
767 stmt.close();
|
|
768 con.close();
|
|
769 } catch(SQLException e) {
|
|
770 throw new RuntimeException(e);
|
|
771 }
|
|
772 }
|
|
773
|
|
774 public void deactivate() {
|
|
775 db().beginTransaction();
|
|
776 try {
|
|
777 UserImpl user = DbUtils.getGoodCopy(this);
|
51
|
778 user.setNoArchive(true);
|
0
|
779 user.setRegistered(null);
|
|
780 user.setPasswordDigest(null);
|
|
781 user.record.update();
|
|
782 db().commitTransaction();
|
|
783 logger.info("User removed his/her account: " + getEmail());
|
|
784 } finally {
|
|
785 db().endTransaction();
|
|
786 }
|
|
787 }
|
|
788
|
|
789 private DbParamSetter simpleParamSetter() {
|
|
790 return new DbParamSetter() {
|
|
791 public void setParams(PreparedStatement stmt) throws SQLException {
|
|
792 stmt.setLong( 1, getId() );
|
|
793 }
|
|
794 };
|
|
795 }
|
|
796
|
|
797 public Message getSignature() {
|
|
798 return signature;
|
|
799 }
|
|
800
|
|
801 public User setSignature( String signatureRaw, Message.Format signatureFormat )
|
|
802 throws ModelException
|
|
803 {
|
|
804 if( !db().isInTransaction() ) {
|
|
805 db().beginTransaction();
|
|
806 try {
|
|
807 UserImpl user = DbUtils.getGoodCopy(this);
|
|
808 user.setSignature(signatureRaw,signatureFormat);
|
|
809 user.getDbRecord().update();
|
|
810 db().commitTransaction();
|
|
811 return DbUtils.getGoodCopy(user);
|
|
812 } finally {
|
|
813 db().endTransaction();
|
|
814 }
|
|
815 }
|
|
816 if( signatureRaw==null || signatureRaw.trim().length()==0 ) {
|
|
817 if( signature != null ) {
|
|
818 signature = null;
|
|
819 record.fields().put("signature",DbNull.STRING);
|
|
820 record.fields().put("signature_format",DbNull.STRING);
|
|
821 }
|
|
822 } else {
|
|
823 Message newSignature = new Message(signatureRaw,signatureFormat);
|
|
824 if( !newSignature.equals(signature) ) {
|
|
825 signature = newSignature;
|
|
826 record.fields().put("signature",signatureRaw);
|
|
827 record.fields().put("signature_format",Character.toString(signatureFormat.getCode()));
|
|
828 }
|
|
829 }
|
|
830 return this;
|
|
831 }
|
|
832
|
|
833
|
|
834 public void saveAvatar(BufferedImage smallImage,BufferedImage bigImage) throws ModelException {
|
|
835 if( !db().isInTransaction() ) {
|
|
836 db().beginTransaction();
|
|
837 try {
|
|
838 DbUtils.getGoodCopy(this).saveAvatar(smallImage,bigImage);
|
|
839 db().commitTransaction();
|
|
840 } finally {
|
|
841 db().endTransaction();
|
|
842 }
|
|
843 return;
|
|
844 }
|
|
845 Message.AvatarSource as = new Message.AvatarSource(this);
|
|
846 FileUpload.saveImage(smallImage,ModelHome.AVATAR_SMALL,as);
|
|
847 FileUpload.saveImage(bigImage,ModelHome.AVATAR_BIG,as);
|
|
848 getSiteImpl().update(); // fire change listeners
|
|
849 DbUtils.uncache(this);
|
|
850 }
|
|
851
|
|
852 public void deleteAvatar() {
|
|
853 if( !db().isInTransaction() ) {
|
|
854 db().beginTransaction();
|
|
855 try {
|
|
856 DbUtils.getGoodCopy(this).deleteAvatar();
|
|
857 db().commitTransaction();
|
|
858 } finally {
|
|
859 db().endTransaction();
|
|
860 }
|
|
861 return;
|
|
862 }
|
|
863 final Message.AvatarSource as = new Message.AvatarSource(this);
|
|
864 FileUpload.deleteFile(ModelHome.AVATAR_SMALL,as);
|
|
865 FileUpload.deleteFile(ModelHome.AVATAR_BIG,as);
|
|
866 getSiteImpl().update(); // fire change listeners
|
|
867 db().runAfterCommit(new Runnable(){public void run(){
|
|
868 FileUpload.fireFileUpdateListeners(as);
|
|
869 }});
|
|
870 DbUtils.uncache(this);
|
|
871 }
|
|
872
|
|
873 private boolean hasAvatar;
|
|
874 private boolean checkedAvatar = false;
|
|
875
|
|
876 public synchronized boolean hasAvatar() {
|
|
877 if( !checkedAvatar ) {
|
|
878 Message.AvatarSource as = new Message.AvatarSource(this);
|
|
879 hasAvatar = FileUpload.hasFile(as,ModelHome.AVATAR_SMALL) && FileUpload.hasFile(as,ModelHome.AVATAR_BIG);
|
|
880 checkedAvatar = true;
|
|
881 }
|
|
882 return hasAvatar;
|
|
883 }
|
|
884
|
|
885
|
|
886
|
|
887 public Node newRootNode(Node.Kind kind,String subject,String message,Message.Format msgFmt,Site site,String type) throws ModelException {
|
|
888 return NodeImpl.newRootNode(kind,this,subject,message,msgFmt,(SiteImpl)site,type);
|
|
889 }
|
|
890
|
|
891 public Node newChildNode(Node.Kind kind,String subject,String message,Message.Format msgFmt,Node parent) throws ModelException {
|
|
892 return NodeImpl.newChildNode(kind,this,subject,message,msgFmt,(NodeImpl)parent);
|
|
893 }
|
|
894
|
|
895 public String getSearchId() {
|
|
896 return Long.toString(getId());
|
|
897 }
|
|
898
|
|
899 public String getIdString() {
|
|
900 return Long.toString(getId());
|
|
901 }
|
|
902
|
|
903 boolean isAutoUnsubscribe() {
|
52
|
904 return isDeactivated();
|
0
|
905 }
|
|
906
|
|
907
|
|
908
|
|
909
|
|
910 private volatile Map<String, Integer> nodeCount = new HashMap<String, Integer>();
|
|
911
|
|
912 public final int getNodeCount(String cnd) {
|
|
913 String key = cnd == null? "none" : cnd;
|
|
914 if (!nodeCount.containsKey(key)) {
|
|
915 try {
|
|
916 Connection con = db().getConnection();
|
|
917 PreparedStatement stmt = con.prepareStatement(
|
|
918 "select count(*) as n from node where owner_id = ?" +
|
|
919 (cnd == null? "" : " and " + cnd)
|
|
920 );
|
|
921 stmt.setLong(1,getId());
|
|
922 ResultSet rs = stmt.executeQuery();
|
|
923 rs.next();
|
|
924 nodeCount.put(key, rs.getInt("n"));
|
|
925 rs.close();
|
|
926 stmt.close();
|
|
927 con.close();
|
|
928 } catch(SQLException e) {
|
|
929 throw new RuntimeException(e);
|
|
930 }
|
|
931 }
|
|
932 return nodeCount.get(key);
|
|
933 }
|
|
934
|
|
935 void setNodeCount(int nodeCount) {
|
|
936 this.nodeCount.put("none", nodeCount);
|
|
937 }
|
|
938
|
|
939 static {
|
|
940 Listener<NodeImpl> listener = new Listener<NodeImpl>() {
|
|
941 public void event(NodeImpl node) {
|
|
942 table(node.siteKey).uncache(new LongKey(node.getOwnerId()));
|
|
943 }
|
|
944 };
|
|
945 NodeImpl.postInsertListeners.add(listener);
|
|
946 NodeImpl.postDeleteListeners.add(listener);
|
|
947 }
|
|
948
|
|
949
|
|
950 public void moveToRegisteredAccount(final String cookie) {
|
|
951 List<NodeImpl> nodes = new CursorNodeIterator( siteKey,
|
|
952 "select * from node where cookie=?"
|
|
953 ,
|
|
954 new DbParamSetter() {
|
|
955 public void setParams(PreparedStatement stmt) throws SQLException {
|
|
956 stmt.setString(1,cookie);
|
|
957 }
|
|
958 }
|
|
959 ).asList();
|
|
960 for( NodeImpl n : nodes ) {
|
|
961 n.setOwner(this);
|
|
962 n.update();
|
|
963 }
|
|
964 }
|
|
965
|
|
966
|
|
967 public NodeIterator<? extends Node> getNodesByDateDesc(String cnd) {
|
|
968 return new CursorNodeIterator( siteKey,
|
|
969 "select * from node where owner_id = ?" +
|
|
970 (cnd == null? "" : " and " + cnd) +
|
|
971 " order by when_created desc"
|
|
972 ,
|
|
973 new DbParamSetter() {
|
|
974 public void setParams(PreparedStatement stmt) throws SQLException {
|
|
975 stmt.setLong( 1, getId() );
|
|
976 }
|
|
977 }
|
|
978 );
|
|
979 }
|
|
980
|
|
981
|
|
982 public int deleteNodes() {
|
|
983 List<NodeImpl> nodes = new CursorNodeIterator( siteKey,
|
|
984 "select *"
|
|
985 +" from node"
|
|
986 +" where owner_id = ?"
|
|
987 , simpleParamSetter()
|
|
988 ).asList();
|
|
989 int n = 0;
|
|
990 for( NodeImpl node : nodes ) {
|
|
991 db().beginTransaction();
|
|
992 try {
|
|
993 DbUtils.getGoodCopy(node).deleteMessageOrNode();
|
|
994 db().commitTransaction();
|
|
995 n++;
|
|
996 } finally {
|
|
997 db().endTransaction();
|
|
998 }
|
|
999 }
|
|
1000 return n;
|
|
1001 }
|
|
1002
|
|
1003 public int deleteNodesRecursively() {
|
|
1004 List<NodeImpl> nodes = new CursorNodeIterator( siteKey,
|
|
1005 "select *"
|
|
1006 +" from node"
|
|
1007 +" where owner_id = ?"
|
|
1008 , simpleParamSetter()
|
|
1009 ).asList();
|
|
1010 int n = 0;
|
|
1011 for( NodeImpl node : nodes ) {
|
|
1012 db().beginTransaction();
|
|
1013 try {
|
|
1014 DbUtils.getGoodCopy(node).deleteRecursively();
|
|
1015 db().commitTransaction();
|
|
1016 n++;
|
|
1017 } finally {
|
|
1018 db().endTransaction();
|
|
1019 }
|
|
1020 }
|
|
1021 return n;
|
|
1022 }
|
|
1023
|
|
1024
|
|
1025 private Map<ExtensionFactory<User,?>,Object> extensionMap;
|
|
1026
|
|
1027 private synchronized Map<ExtensionFactory<User, ?>, Object> getExtensionMap() {
|
|
1028 if (extensionMap == null)
|
|
1029 extensionMap = new HashMap<ExtensionFactory<User, ?>, Object>();
|
|
1030 return extensionMap;
|
|
1031 }
|
|
1032
|
|
1033 public <T> T getExtension(ExtensionFactory<User,T> factory) {
|
|
1034 synchronized(getExtensionMap()) {
|
|
1035 Object obj = extensionMap.get(factory);
|
|
1036 if( obj == null ) {
|
|
1037 obj = factory.construct(this);
|
|
1038 if( obj != null )
|
|
1039 extensionMap.put(factory,obj);
|
|
1040 }
|
|
1041 return factory.extensionClass().cast(obj);
|
|
1042 }
|
|
1043 }
|
|
1044
|
|
1045 private static Collection<ExtensionFactory<User,?>> extensionFactories = new CopyOnWriteArrayList<ExtensionFactory<User,?>>();
|
|
1046
|
|
1047 static <T> void addExtensionFactory(ExtensionFactory<User,T> factory) {
|
|
1048 extensionFactories.add(factory);
|
|
1049 Db.clearCache();
|
|
1050 }
|
|
1051
|
|
1052
|
|
1053
|
|
1054
|
|
1055 // visited node
|
|
1056
|
|
1057 private final Map<Long,Long> visitedNodeCache = new HashMap<Long,Long>();
|
|
1058
|
|
1059 public Long lastVisitedNodeId(long nodeId) {
|
|
1060 synchronized(visitedNodeCache) {
|
|
1061 return visitedNodeCache.containsKey(nodeId) ? visitedNodeCache.get(nodeId) : lastVisitedNodeIds(Collections.singletonList(nodeId)).get(nodeId);
|
|
1062 }
|
|
1063 }
|
|
1064
|
|
1065 public Map<Long,Long> lastVisitedNodeIds(Collection<Long> nodeIds) {
|
|
1066 synchronized(visitedNodeCache) {
|
|
1067 Set<Long> notCached = new HashSet<Long>();
|
|
1068 for( Long nodeId : nodeIds ) {
|
|
1069 if( !visitedNodeCache.containsKey(nodeId) )
|
|
1070 notCached.add(nodeId);
|
|
1071 }
|
|
1072 if( !notCached.isEmpty() ) {
|
|
1073 StringBuilder sql = new StringBuilder();
|
|
1074 sql
|
|
1075 .append( "select node_id, last_node_id from visited where user_id = " )
|
|
1076 .append( getId() )
|
|
1077 .append( " and node_id in (" )
|
|
1078 ;
|
|
1079 Iterator<Long> iter = notCached.iterator();
|
|
1080 sql.append( iter.next() );
|
|
1081 while( iter.hasNext() ) {
|
|
1082 sql.append( ',' ).append( iter.next() );
|
|
1083 }
|
|
1084 sql.append( ")" );
|
|
1085 try {
|
|
1086 Connection con = db().getConnection();
|
|
1087 Statement stmt = con.createStatement();
|
|
1088 ResultSet rs = stmt.executeQuery(sql.toString());
|
|
1089 while( rs.next() ) {
|
|
1090 Long nodeId = rs.getLong("node_id");
|
|
1091 Long lastNodeId = rs.getLong("last_node_id");
|
|
1092 visitedNodeCache.put(nodeId,lastNodeId);
|
|
1093 notCached.remove(nodeId);
|
|
1094 }
|
|
1095 rs.close();
|
|
1096 stmt.close();
|
|
1097 con.close();
|
|
1098 } catch(SQLException e) {
|
|
1099 throw new RuntimeException(e);
|
|
1100 }
|
|
1101 for( Long nodeId : notCached ) {
|
|
1102 visitedNodeCache.put(nodeId,null);
|
|
1103 }
|
|
1104 }
|
|
1105 Map<Long,Long> map = new HashMap<Long,Long>();
|
|
1106 for( Long nodeId : nodeIds ) {
|
|
1107 map.put( nodeId, visitedNodeCache.get(nodeId) );
|
|
1108 }
|
|
1109 return map;
|
|
1110 }
|
|
1111 }
|
|
1112
|
|
1113 public void markVisited(Node topic, long lastNodeId) {
|
|
1114 NodeImpl topicNode = (NodeImpl)topic;
|
|
1115 long nodeId = topicNode.getId();
|
|
1116 boolean updated = false;
|
|
1117 try {
|
|
1118 Connection con = db().getConnection();
|
|
1119 try {
|
|
1120 Long persistedLastVisitedNodeId = lastVisitedNodeId(nodeId);
|
|
1121 if( persistedLastVisitedNodeId == null ) {
|
|
1122 PreparedStatement stmt = con.prepareStatement(
|
|
1123 "insert into visited (user_id, node_id, last_node_id)"
|
|
1124 +" values (?, ?, ?)"
|
|
1125 );
|
|
1126 stmt.setLong( 1, getId() );
|
|
1127 stmt.setLong( 2, nodeId );
|
|
1128 stmt.setLong( 3, lastNodeId );
|
|
1129 DbDatabaseImpl.executeUpdateIgnoringDuplicateKeys(stmt);
|
|
1130 stmt.close();
|
|
1131 updated = true;
|
|
1132 } else if (lastNodeId > persistedLastVisitedNodeId) {
|
|
1133 PreparedStatement stmt = con.prepareStatement(
|
|
1134 "update visited set last_node_id = ?"
|
|
1135 +" where user_id = ? and node_id = ?"
|
|
1136 );
|
|
1137 stmt.setLong( 1, lastNodeId );
|
|
1138 stmt.setLong( 2, getId() );
|
|
1139 stmt.setLong( 3, nodeId );
|
|
1140 stmt.executeUpdate();
|
|
1141 stmt.close();
|
|
1142 updated = true;
|
|
1143 }
|
|
1144 } finally {
|
|
1145 con.close();
|
|
1146 }
|
|
1147 } catch(SQLException e) {
|
|
1148 if( !e.getMessage().contains("violates foreign key constraint \"visited_last_node_id_fkey\"") )
|
|
1149 throw new RuntimeException(e);
|
|
1150 }
|
|
1151 if (updated) {
|
|
1152 synchronized(visitedNodeCache) {
|
|
1153 visitedNodeCache.remove(nodeId);
|
|
1154 }
|
|
1155 }
|
|
1156 }
|
|
1157
|
|
1158 public void unmarkVisited(Node node) {
|
|
1159 long nodeId = node.getId();
|
|
1160 try {
|
|
1161 Connection con = db().getConnection();
|
|
1162 PreparedStatement stmt = con.prepareStatement(
|
|
1163 "delete from visited"
|
|
1164 +" where user_id = ? and node_id = ?"
|
|
1165 );
|
|
1166 stmt.setLong( 1, getId() );
|
|
1167 stmt.setLong( 2, nodeId );
|
|
1168 stmt.executeUpdate();
|
|
1169 stmt.close();
|
|
1170 con.close();
|
|
1171 } catch(SQLException e) {
|
|
1172 throw new RuntimeException(e);
|
|
1173 }
|
|
1174 synchronized(visitedNodeCache) {
|
|
1175 visitedNodeCache.remove(nodeId);
|
|
1176 }
|
|
1177 }
|
|
1178
|
|
1179
|
|
1180 static void addPostInsertListener(final Listener<? super UserImpl> listener) {
|
|
1181 postInsertListeners.add(listener);
|
|
1182 }
|
|
1183
|
|
1184
|
|
1185
|
|
1186 private final Memoizer<String,String> propertyCache = new Memoizer<String,String>(new Computable<String,String>() {
|
|
1187 public String get(String key) {
|
|
1188 try {
|
|
1189 Connection con = db().getConnection();
|
|
1190 PreparedStatement stmt = con.prepareStatement(
|
|
1191 "select value from user_property where user_id = ? and key = ?"
|
|
1192 );
|
|
1193 stmt.setLong( 1, getId() );
|
|
1194 stmt.setString( 2, key );
|
|
1195 ResultSet rs = stmt.executeQuery();
|
|
1196 try {
|
|
1197 return rs.next() ? rs.getString("value") : null;
|
|
1198 } finally {
|
|
1199 rs.close();
|
|
1200 stmt.close();
|
|
1201 con.close();
|
|
1202 }
|
|
1203 } catch(SQLException e) {
|
|
1204 throw new RuntimeException(e);
|
|
1205 }
|
|
1206 }
|
|
1207 });
|
|
1208
|
|
1209 public String getProperty(String key) {
|
|
1210 return propertyCache.get(key);
|
|
1211 }
|
|
1212
|
|
1213 public void setProperty(String key,String value) {
|
|
1214 try {
|
|
1215 Connection con = db().getConnection();
|
|
1216 PreparedStatement stmt = con.prepareStatement(
|
|
1217 "delete from user_property where user_id = ? and key = ?"
|
|
1218 );
|
|
1219 stmt.setLong( 1, getId() );
|
|
1220 stmt.setString( 2, key );
|
|
1221 stmt.executeUpdate();
|
|
1222 stmt.close();
|
|
1223 if( value != null ) {
|
|
1224 stmt = con.prepareStatement(
|
|
1225 "insert into user_property (user_id,key,value) values (?,?,?)"
|
|
1226 );
|
|
1227 stmt.setLong( 1, getId() );
|
|
1228 stmt.setString( 2, key );
|
|
1229 stmt.setString( 3, value );
|
|
1230 stmt.executeUpdate();
|
|
1231 stmt.close();
|
|
1232 }
|
|
1233 con.close();
|
|
1234 } catch(SQLException e) {
|
|
1235 throw new RuntimeException(e);
|
|
1236 } finally {
|
|
1237 propertyCache.remove(key);
|
|
1238 }
|
|
1239 }
|
|
1240
|
|
1241
|
|
1242 final Memoizer<String,Boolean> tagCache = new Memoizer<String,Boolean>(new Computable<String,Boolean>() {
|
|
1243 public Boolean get(String sqlCondition) {
|
|
1244 return TagImpl.countTags(siteKey,sqlCondition) > 0;
|
|
1245 }
|
|
1246 });
|
|
1247
|
|
1248
|
|
1249
|
|
1250
|
|
1251
|
|
1252 private final static PooledStringDigester passwordDigester = new PooledStringDigester();
|
|
1253
|
|
1254 static {
|
|
1255 passwordDigester.setAlgorithm(Init.get("passwordDigestAlgorithm","SHA-256"));
|
|
1256 passwordDigester.setIterations(Init.get("passwordDigestIterations",100000));
|
|
1257 passwordDigester.setSaltSizeBytes(Init.get("passwordDigestSaltSize",16));
|
|
1258 passwordDigester.setPoolSize(Init.get("passwordDigestPoolSize",4));
|
|
1259 passwordDigester.initialize();
|
|
1260 }
|
|
1261
|
|
1262 private final static PooledStringDigester passcookieDigester = new PooledStringDigester();
|
|
1263
|
|
1264 static {
|
|
1265 passcookieDigester.setAlgorithm(Init.get("passcookieDigestAlgorithm","SHA-256"));
|
|
1266 passcookieDigester.setIterations(Init.get("passcookieDigestIterations",100000));
|
|
1267 FixedByteArraySaltGenerator sg = new FixedByteArraySaltGenerator();
|
|
1268 // this fixed salt needs to be kept secret
|
|
1269 sg.setSalt(Init.get("passcookieSalt", new byte[]{105, 4, 40, 78, 24, 46, 30, 100, 18, -27, 114, -21, -44, -59, 103, 43}));
|
|
1270 passcookieDigester.setSaltGenerator(sg);
|
|
1271 passcookieDigester.setPoolSize(Init.get("passcookieDigestPoolSize",4));
|
|
1272 passcookieDigester.initialize();
|
|
1273 }
|
|
1274
|
|
1275 private final static PooledStringDigester resetcodeDigester = new PooledStringDigester();
|
|
1276
|
|
1277 static {
|
|
1278 resetcodeDigester.setAlgorithm(Init.get("resetcodeDigestAlgorithm","SHA-256"));
|
|
1279 resetcodeDigester.setIterations(Init.get("resetcodeDigestIterations",100000));
|
|
1280 FixedByteArraySaltGenerator sg = new FixedByteArraySaltGenerator();
|
|
1281 // this fixed salt needs to be kept secret
|
|
1282 sg.setSalt(Init.get("resetcodeSalt", new byte[]{-47, 9, -128, 109, 112, -88, -91, 39, 77, 111, 57, -102, 120, 12, 54, 16}));
|
|
1283 resetcodeDigester.setSaltGenerator(sg);
|
|
1284 resetcodeDigester.setPoolSize(Init.get("resetcodeDigestPoolSize",4));
|
|
1285 resetcodeDigester.initialize();
|
|
1286 }
|
|
1287
|
|
1288 public boolean checkPassword(String password) {
|
|
1289 return passwordDigest!=null && passwordDigester.matches(password, passwordDigest);
|
|
1290 }
|
|
1291
|
|
1292 private String calcPasscookie() {
|
|
1293 return passcookieDigester.digest(passwordDigest);
|
|
1294 }
|
|
1295
|
|
1296 public boolean checkPasscookie(String passcookie) {
|
|
1297 return passwordDigest!=null && getPasscookie().equals(passcookie);
|
|
1298 }
|
|
1299
|
|
1300 public String getResetcode() {
|
|
1301 return resetcodeDigester.digest(passwordDigest);
|
|
1302 }
|
|
1303
|
|
1304 public boolean checkResetcode(String resetcode) {
|
|
1305 return passwordDigest!=null && getResetcode().equals(resetcode);
|
|
1306 }
|
|
1307
|
|
1308 private static String digestPassword(String password) {
|
|
1309 return passwordDigester.digest(password);
|
|
1310 }
|
|
1311
|
|
1312 }
|