1.23 commit

This commit is contained in:
coderfengyun 2014-01-23 09:26:05 +08:00
parent 54c052b0a8
commit e74f032011
3 changed files with 134 additions and 147 deletions

View File

@ -1,118 +1,104 @@
package org.bench4q.master.service.infrastructure; package org.bench4q.master.service.infrastructure;
import org.bench4q.master.entity.User; import org.bench4q.master.entity.User;
import org.bench4q.master.helper.HashHelper; import org.bench4q.master.factory.UserFactory;
import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.helper.HashHelper;
import org.hibernate.Session; import org.bench4q.master.helper.SessionHelper;
import org.hibernate.Transaction; import org.bench4q.master.repository.UserRepository;
import org.hibernate.criterion.Restrictions; import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired; import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService { @Component
private SessionHelper sessionHelper; public class UserService {
private HashHelper hashHelper; private SessionHelper sessionHelper;
public static byte NORAML_AUTHENTICATION = 0; private UserFactory userFactory;
public static byte SUPER_AUTHENTICATION = 1; private UserRepository userRepository;
private HashHelper hashHelper;
private SessionHelper getSessionHelper() { public static byte NORAML_AUTHENTICATION = 0;
return sessionHelper; public static byte SUPER_AUTHENTICATION = 1;
}
private SessionHelper getSessionHelper() {
@Autowired return sessionHelper;
public void setSessionHelper(SessionHelper sessionHelper) { }
this.sessionHelper = sessionHelper;
} @Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
private HashHelper getHashHelper() { this.sessionHelper = sessionHelper;
return hashHelper; }
}
private UserFactory getUserFactory() {
@Autowired return userFactory;
private void setHashHelper(HashHelper hashHelper) { }
this.hashHelper = hashHelper;
} @Autowired
private void setUserFactory(UserFactory userFactory) {
public boolean register(String userName, String password) { this.userFactory = userFactory;
Session session = this.getSessionHelper().openSession(); }
Transaction transaction = session.beginTransaction();
try { private UserRepository getUserRepository() {
User user = (User) session.createCriteria(User.class) return userRepository;
.add(Restrictions.eq("userName", userName)).uniqueResult(); }
if (user != null) {
return false; @Autowired
} private void setUserRepository(UserRepository userRepository) {
user = new User(); this.userRepository = userRepository;
user.setUserName(userName); }
user.setPassword(this.hashPassword(password));
user.setScope(UserService.NORAML_AUTHENTICATION); private HashHelper getHashHelper() {
session.merge(user); return hashHelper;
transaction.commit(); }
return true;
} catch (Exception e) { @Autowired
e.printStackTrace(); private void setHashHelper(HashHelper hashHelper) {
transaction.rollback(); this.hashHelper = hashHelper;
return false; }
} finally {
if (session != null) { public boolean register(String userName, String password) {
session.close(); return this.getUserRepository().attatch(
} this.getUserFactory().createUser(userName, password));
} }
}
public boolean delete(String userName) {
public boolean validateUser(String userName, String password) { return this.getUserRepository().detach(userName);
Session session = this.getSessionHelper().openSession(); }
try {
User user = (User) session.createCriteria(User.class) public boolean validateUser(String userName, String password) {
.add(Restrictions.eq("userName", userName)).uniqueResult(); return this.getUserRepository().isExist(
if (user == null) { this.getUserFactory().createUser(userName, password));
return false; }
}
if (user.getPassword().equals(this.hashPassword(password))) { public String hashPassword(String password) {
return true; return this.getHashHelper().sha1Hash(password);
} }
return false;
} catch (Exception e) { public User getUserByName(String userName) {
e.printStackTrace(); Session session = this.getSessionHelper().openSession();
return false; try {
} finally { return (User) session.createCriteria(User.class)
if (session != null) { .add(Restrictions.eq("userName", userName)).uniqueResult();
session.close(); } catch (Exception e) {
} e.printStackTrace();
} return null;
} } finally {
if (session != null) {
public String hashPassword(String password) { session.close();
return this.getHashHelper().sha1Hash(password); }
} }
}
public User getUserByName(String userName) {
Session session = this.getSessionHelper().openSession(); public User getUserById(int userId) {
try { Session session = this.getSessionHelper().openSession();
return (User) session.createCriteria(User.class) try {
.add(Restrictions.eq("userName", userName)).uniqueResult(); return (User) session.get(User.class, userId);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} finally { } finally {
if (session != null) { if (session != null) {
session.close(); session.close();
} }
} }
} }
}
public User getUserById(int userId) {
Session session = this.getSessionHelper().openSession();
try {
return (User) session.get(User.class, userId);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (session != null) {
session.close();
}
}
}
}

View File

@ -3,6 +3,7 @@ package org.bench4q.master.test.service;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.bench4q.master.service.infrastructure.UserService; import org.bench4q.master.service.infrastructure.UserService;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,19 +29,19 @@ public class Test_UserService {
assertNotNull(this.getUserService()); assertNotNull(this.getUserService());
} }
// @Test @Test
// public void testQueryTestPlans() { public void testRegister() {
// List<TestPlanDB> testPlanDBs = this.getUserService().querytestPlans(5); assertTrue(this.getUserService().register("test1", "test1"));
// assertNotNull(testPlanDBs); }
// assertTrue(testPlanDBs.size() > 0);
// } @Test
// public void testIsExist() {
// @Test this.getUserService().register("test1", "test1");
// public void testQueryTestPlan() { assertTrue(this.getUserService().validateUser("test1", "test1"));
// TestPlanDB testPlan = this.getUserService().queryTestPlan(5, }
// UUID.fromString("ad10a4be-e6e2-4ffc-9282-3ba5c1a1316b"));
// assertNotNull(testPlan); @After
// assertEquals(testPlan.getTestPlanRunId().toString(), public void cleanUp() {
// "ad10a4be-e6e2-4ffc-9282-3ba5c1a1316b"); this.getUserService().delete("test1");
// } }
} }

View File

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan <context:component-scan
base-package="org.bench4q.master.service, org.bench4q.master.helper, org.bench4q.master.testplan, org.bench4q.share, org.bench4q.master.api.modelfactory" /> base-package="org.bench4q.master.service, org.bench4q.master.helper, org.bench4q.master.testplan, org.bench4q.share, org.bench4q.master.api.modelfactory, org.bench4q.master.factory, org.bench4q.master.repository" />
<mvc:annotation-driven /> <mvc:annotation-driven />
<task:annotation-driven /> <task:annotation-driven />
</beans> </beans>