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;
import org.bench4q.master.entity.User;
import org.bench4q.master.helper.HashHelper;
import org.bench4q.master.helper.SessionHelper;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
private SessionHelper sessionHelper;
private HashHelper hashHelper;
public static byte NORAML_AUTHENTICATION = 0;
public static byte SUPER_AUTHENTICATION = 1;
private SessionHelper getSessionHelper() {
return sessionHelper;
}
@Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
private HashHelper getHashHelper() {
return hashHelper;
}
@Autowired
private void setHashHelper(HashHelper hashHelper) {
this.hashHelper = hashHelper;
}
public boolean register(String userName, String password) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
User user = (User) session.createCriteria(User.class)
.add(Restrictions.eq("userName", userName)).uniqueResult();
if (user != null) {
return false;
}
user = new User();
user.setUserName(userName);
user.setPassword(this.hashPassword(password));
user.setScope(UserService.NORAML_AUTHENTICATION);
session.merge(user);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public boolean validateUser(String userName, String password) {
Session session = this.getSessionHelper().openSession();
try {
User user = (User) session.createCriteria(User.class)
.add(Restrictions.eq("userName", userName)).uniqueResult();
if (user == null) {
return false;
}
if (user.getPassword().equals(this.hashPassword(password))) {
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public String hashPassword(String password) {
return this.getHashHelper().sha1Hash(password);
}
public User getUserByName(String userName) {
Session session = this.getSessionHelper().openSession();
try {
return (User) session.createCriteria(User.class)
.add(Restrictions.eq("userName", userName)).uniqueResult();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (session != null) {
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();
}
}
}
}
package org.bench4q.master.service.infrastructure;
import org.bench4q.master.entity.User;
import org.bench4q.master.factory.UserFactory;
import org.bench4q.master.helper.HashHelper;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.repository.UserRepository;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
private SessionHelper sessionHelper;
private UserFactory userFactory;
private UserRepository userRepository;
private HashHelper hashHelper;
public static byte NORAML_AUTHENTICATION = 0;
public static byte SUPER_AUTHENTICATION = 1;
private SessionHelper getSessionHelper() {
return sessionHelper;
}
@Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
private UserFactory getUserFactory() {
return userFactory;
}
@Autowired
private void setUserFactory(UserFactory userFactory) {
this.userFactory = userFactory;
}
private UserRepository getUserRepository() {
return userRepository;
}
@Autowired
private void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
private HashHelper getHashHelper() {
return hashHelper;
}
@Autowired
private void setHashHelper(HashHelper hashHelper) {
this.hashHelper = hashHelper;
}
public boolean register(String userName, String password) {
return this.getUserRepository().attatch(
this.getUserFactory().createUser(userName, password));
}
public boolean delete(String userName) {
return this.getUserRepository().detach(userName);
}
public boolean validateUser(String userName, String password) {
return this.getUserRepository().isExist(
this.getUserFactory().createUser(userName, password));
}
public String hashPassword(String password) {
return this.getHashHelper().sha1Hash(password);
}
public User getUserByName(String userName) {
Session session = this.getSessionHelper().openSession();
try {
return (User) session.createCriteria(User.class)
.add(Restrictions.eq("userName", userName)).uniqueResult();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (session != null) {
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 org.bench4q.master.service.infrastructure.UserService;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -28,19 +29,19 @@ public class Test_UserService {
assertNotNull(this.getUserService());
}
// @Test
// public void testQueryTestPlans() {
// List<TestPlanDB> testPlanDBs = this.getUserService().querytestPlans(5);
// assertNotNull(testPlanDBs);
// assertTrue(testPlanDBs.size() > 0);
// }
//
// @Test
// public void testQueryTestPlan() {
// TestPlanDB testPlan = this.getUserService().queryTestPlan(5,
// UUID.fromString("ad10a4be-e6e2-4ffc-9282-3ba5c1a1316b"));
// assertNotNull(testPlan);
// assertEquals(testPlan.getTestPlanRunId().toString(),
// "ad10a4be-e6e2-4ffc-9282-3ba5c1a1316b");
// }
@Test
public void testRegister() {
assertTrue(this.getUserService().register("test1", "test1"));
}
@Test
public void testIsExist() {
this.getUserService().register("test1", "test1");
assertTrue(this.getUserService().validateUser("test1", "test1"));
}
@After
public void cleanUp() {
this.getUserService().delete("test1");
}
}

View File

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<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: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
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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<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" />
<mvc:annotation-driven />
<task:annotation-driven />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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: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
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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<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, org.bench4q.master.factory, org.bench4q.master.repository" />
<mvc:annotation-driven />
<task:annotation-driven />
</beans>