mirror of
https://github.com/theonedev/onedev.git
synced 2025-12-08 18:26:30 +00:00
Add user manager.
This commit is contained in:
parent
22e1a1303e
commit
5101c221f3
@ -13,7 +13,7 @@ import com.google.inject.util.Providers;
|
||||
import com.pmease.commons.loader.AbstractPlugin;
|
||||
import com.pmease.commons.loader.AbstractPluginModule;
|
||||
import com.pmease.commons.persistence.dao.GeneralDao;
|
||||
import com.pmease.commons.persistence.dao.GeneralDaoImpl;
|
||||
import com.pmease.commons.persistence.dao.DefaultGeneralDao;
|
||||
|
||||
public class PersistenceModule extends AbstractPluginModule {
|
||||
|
||||
@ -33,7 +33,7 @@ public class PersistenceModule extends AbstractPluginModule {
|
||||
bind(Session.class).toProvider(UnitOfWorkImpl.class);
|
||||
bind(SessionProvider.class).to(UnitOfWorkImpl.class);
|
||||
|
||||
bind(GeneralDao.class).to(GeneralDaoImpl.class);
|
||||
bind(GeneralDao.class).to(DefaultGeneralDao.class);
|
||||
|
||||
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
|
||||
requestInjection(transactionInterceptor);
|
||||
|
||||
@ -17,14 +17,14 @@ import com.pmease.commons.persistence.Transactional;
|
||||
|
||||
@Singleton
|
||||
@SuppressWarnings("unchecked")
|
||||
public class GeneralDaoImpl implements GeneralDao {
|
||||
public class DefaultGeneralDao implements GeneralDao {
|
||||
|
||||
private final Provider<SessionFactory> sessionFactoryProvider;
|
||||
|
||||
private final Provider<Session> sessionProvider;
|
||||
|
||||
@Inject
|
||||
public GeneralDaoImpl(Provider<SessionFactory> sessionFactoryProvider, Provider<Session> sessionProvider) {
|
||||
public DefaultGeneralDao(Provider<SessionFactory> sessionFactoryProvider, Provider<Session> sessionProvider) {
|
||||
this.sessionFactoryProvider = sessionFactoryProvider;
|
||||
this.sessionProvider = sessionProvider;
|
||||
}
|
||||
@ -2,6 +2,9 @@ package com.pmease.commons.persistence.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Provider;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
import org.hibernate.criterion.DetachedCriteria;
|
||||
import org.hibernate.criterion.Order;
|
||||
@ -11,17 +14,20 @@ import com.google.inject.Inject;
|
||||
import com.pmease.commons.persistence.AbstractEntity;
|
||||
import com.pmease.commons.util.ClassUtils;
|
||||
|
||||
public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
|
||||
public class DefaultGenericDao<T extends AbstractEntity> implements GenericDao<T> {
|
||||
|
||||
private GeneralDao generalDao;
|
||||
|
||||
private Provider<Session> sessionProvider;
|
||||
|
||||
protected final Class<T> entityClass;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Inject
|
||||
public GenericDaoImpl(GeneralDao generalDao) {
|
||||
public DefaultGenericDao(GeneralDao generalDao, Provider<Session> sessionProvider) {
|
||||
this.generalDao = generalDao;
|
||||
List<Class<?>> typeArguments = ClassUtils.getTypeArguments(GenericDaoImpl.class, getClass());
|
||||
this.sessionProvider = sessionProvider;
|
||||
List<Class<?>> typeArguments = ClassUtils.getTypeArguments(DefaultGenericDao.class, getClass());
|
||||
entityClass = ((Class<T>) typeArguments.get(0));
|
||||
}
|
||||
|
||||
@ -78,4 +84,19 @@ public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
|
||||
return (List<T>) generalDao.search(detachedCriteria, firstResult, maxResults);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object find(Criterion[] criterions) {
|
||||
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);
|
||||
|
||||
if (criterions != null) {
|
||||
for (Criterion criterion: criterions)
|
||||
detachedCriteria.add(criterion);
|
||||
}
|
||||
|
||||
return generalDao.find(detachedCriteria);
|
||||
}
|
||||
|
||||
protected Session getSession() {
|
||||
return sessionProvider.get();
|
||||
}
|
||||
}
|
||||
@ -101,4 +101,5 @@ public interface GeneralDao {
|
||||
* number of entities matching specified {@link DetachedCriteria}
|
||||
*/
|
||||
<T extends AbstractEntity> int count(DetachedCriteria detachedCriteria);
|
||||
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
import org.hibernate.criterion.Order;
|
||||
|
||||
@ -77,6 +78,18 @@ public interface GenericDao<T extends AbstractEntity> {
|
||||
*/
|
||||
List<T> search(@Nullable Criterion[] criterions, @Nullable Order[] orders, int firstResult, int maxResults);
|
||||
|
||||
/**
|
||||
* This method expects to find a single entity with specified criteria
|
||||
*
|
||||
* @param criterions
|
||||
* Hibernate criterions used to find the object
|
||||
* @return
|
||||
* the single entity. null if not found
|
||||
* @throws
|
||||
* HibernateException if there is more than one matching result
|
||||
*/
|
||||
Object find(@Nullable Criterion[] criterions);
|
||||
|
||||
/**
|
||||
* Count entity matching specified hibernate criterions.
|
||||
*
|
||||
|
||||
@ -14,19 +14,19 @@ import com.pmease.commons.persistence.AbstractEntity;
|
||||
public class AbstractUser extends AbstractEntity implements AuthenticationInfo {
|
||||
|
||||
@Column(unique=true, nullable=false)
|
||||
private String loginName;
|
||||
private String name;
|
||||
|
||||
private String fullName;
|
||||
|
||||
|
||||
@Column(length=1024)
|
||||
private String passwordHash;
|
||||
|
||||
public String getLoginName() {
|
||||
return loginName;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setLoginName(String loginName) {
|
||||
this.loginName = loginName;
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
|
||||
@ -41,7 +41,7 @@ public class SecurityHelper extends SecurityUtils {
|
||||
if (user.getFullName() != null)
|
||||
return user.getFullName();
|
||||
else
|
||||
return user.getLoginName();
|
||||
return user.getName();
|
||||
} else {
|
||||
return anonymousName;
|
||||
}
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
package com.pmease.gitop.core.entitymanager;
|
||||
|
||||
import com.pmease.commons.persistence.dao.GenericDao;
|
||||
import com.pmease.gitop.core.model.User;
|
||||
|
||||
public interface UserManager extends GenericDao<User> {
|
||||
/**
|
||||
* Find root user in the system.
|
||||
*
|
||||
* @return
|
||||
* root user of the system, or null if root user has not been populated
|
||||
*/
|
||||
public User findRoot();
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package com.pmease.gitop.core.entitymanager.impl;
|
||||
|
||||
import javax.inject.Provider;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Order;
|
||||
|
||||
import com.pmease.commons.persistence.Transactional;
|
||||
import com.pmease.commons.persistence.dao.DefaultGenericDao;
|
||||
import com.pmease.commons.persistence.dao.GeneralDao;
|
||||
import com.pmease.gitop.core.entitymanager.UserManager;
|
||||
import com.pmease.gitop.core.model.User;
|
||||
|
||||
public class DefaultUserManager extends DefaultGenericDao<User> implements UserManager {
|
||||
|
||||
|
||||
public DefaultUserManager(GeneralDao generalDao, Provider<Session> sessionProvider) {
|
||||
super(generalDao, sessionProvider);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public User findRoot() {
|
||||
Criteria criteria = getSession().createCriteria(User.class).addOrder(Order.asc("id"));
|
||||
|
||||
/* the first created user should be root user */
|
||||
criteria.setFirstResult(0);
|
||||
criteria.setMaxResults(1);
|
||||
return (User) criteria.uniqueResult();
|
||||
}
|
||||
|
||||
}
|
||||
@ -10,4 +10,19 @@ import com.pmease.commons.security.AbstractUser;
|
||||
usage=org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
public class User extends AbstractUser {
|
||||
|
||||
private boolean project;
|
||||
|
||||
/**
|
||||
* Whether or not this user represents a project.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
public void setProject(boolean project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ public class Product extends AbstractPlugin {
|
||||
criteria.add(Restrictions.eq("loginName", "admin"));
|
||||
if (generalDao.find(criteria) == null) {
|
||||
User user = new User();
|
||||
user.setLoginName("admin");
|
||||
user.setName("admin");
|
||||
user.setPasswordHash(passwordService.encryptPassword("12345"));
|
||||
user.setFullName("Administrator");
|
||||
generalDao.save(user);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user