blob: f78947dca18a870b3f70baf25456634742796a6e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package net.sf.oreka.services;
import java.util.ArrayList;
import net.sf.oreka.HibernateManager;
import net.sf.oreka.orkweb.ContextListener;
import net.sf.oreka.orkweb.OrkWeb;
import net.sf.oreka.persistent.User;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class UserServiceHbn implements UserService {
static Logger logger = Logger.getLogger(UserServiceHbn.class);
public User login(String username, String password) {
Session hbnSession = null;
User user = null;
logger.debug("Trying to login user:" + username + " with passwd:" + password);
try
{
hbnSession = OrkWeb.hibernateManager.getSession();
String queryString = new String("from LoginString as ls left join ls.user as user where ls.loginString=:ls");
if (ContextListener.debugSwitch == false) {
queryString = queryString + " and user.password=:password";
}
Query query = hbnSession.createQuery(queryString);
query.setString("ls", username);
if (ContextListener.debugSwitch == false) {
query.setString("password", password);
}
ArrayList results = (ArrayList)query.list();
Object[] row = (Object[])query.uniqueResult();
if (row != null) {
user = (User)row[1];
logger.debug("Found userid:" + user.getId() + " for login string:" + username);
}
}
catch ( HibernateException he ) {
logger.error("login: exception:" + he.getClass().getName());
}
catch (Exception e)
{
logger.error("login: exception:", e);
}
finally {
if(hbnSession != null) {hbnSession.close();}
}
return user;
}
public boolean changePassword(int userId, String oldPassword, String newPassword) {
Session hbnSession = null;
Transaction tx = null;
User user = null;
boolean success = false;
logger.debug("Trying to change password for userid:" + userId);
try
{
hbnSession = OrkWeb.hibernateManager.getSession();
user = (User)hbnSession.get(User.class, userId);
if(user == null) {
logger.warn("Userid:" + userId + " does not exist");
}
else {
if(user.getPassword().equals(oldPassword)) {
tx = hbnSession.beginTransaction();
user.setPassword(newPassword);
hbnSession.save(user);
tx.commit();
success = true;
logger.debug("Changed password for userid:" + userId);
}
}
}
catch ( HibernateException he ) {
logger.error("changePassword: exception:" + he.getClass().getName());
}
catch (Exception e)
{
logger.error("changePassword: exception:", e);
}
finally {
if(hbnSession != null) {hbnSession.close();}
}
return success;
}
}
|