Activiti 使用自己的身份认证服务

Activiti 中内置了用户和组管理的服务,由identityService 提供调用接口,默认在spring配置中如下:

<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>

通常我们的系统会有自己的用户认证、授权等机制,这种情况下不会在使用Activiti内置的用户服务,需要将Activiti的身份认证服务指向我们系统自己的服务,需要自己添加一些配置以及实现几个接口。

 

1. 自定义user服务

import java.util.List;
import java.util.Map;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.identity.UserQuery;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.quauq.yanzhenxing.activiti.entity.Role;
import com.quauq.yanzhenxing.activiti.service.RoleService;
import com.quauq.yanzhenxing.activiti.service.UserService;

/**
 * 自定义user 服务
 * @author yanzhenxing
 * @createDate 2015年9月17日
 */
@Service
public class ActUserEntityService extends UserEntityManager {

	@Autowired
	private UserService userService;//系统的用户服务
	
	@Autowired
	private RoleService roleService;//系统的角色服务
	
	public User createNewUser(String userId) {
		return new UserEntity(userId);
	}

	public void insertUser(User user) {
//		getDbSqlSession().insert((PersistentObject) user);
		throw new RuntimeException("not implement method.");
	}

	public void updateUser(UserEntity updatedUser) {
//		CommandContext commandContext = Context.getCommandContext();
//		DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
//		dbSqlSession.update(updatedUser);
		throw new RuntimeException("not implement method.");
	}

	public UserEntity findUserById(String userId) {
//		return (UserEntity) getDbSqlSession().selectOne("selectUserById", userId);
		return toActivitiUser(userService.obtainUser(userId));
	}

	private UserEntity toActivitiUser(com.quauq.yanzhenxing.activiti.entity.User user){
		if (user == null){
			return null;
		}
		UserEntity userEntity = new UserEntity();
		userEntity.setId(user.getLoginName());
		userEntity.setFirstName(user.getName());
		userEntity.setLastName(StringUtils.EMPTY);
		userEntity.setPassword(user.getPassword());
		userEntity.setEmail(user.getEmail());
		userEntity.setRevision(1);
		return userEntity;
	}
	
	public void deleteUser(String userId) {
//		UserEntity user = findUserById(userId);
//		if (user != null) {
//			List<IdentityInfoEntity> identityInfos = getDbSqlSession().selectList("selectIdentityInfoByUserId", userId);
//			for (IdentityInfoEntity identityInfo : identityInfos) {
//				getIdentityInfoManager().deleteIdentityInfo(identityInfo);
//			}
//			getDbSqlSession().delete("deleteMembershipsByUserId", userId);
//			user.delete();
//		}
		throw new RuntimeException("not implement method.");
	}

	public List<User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
//		return getDbSqlSession().selectList("selectUserByQueryCriteria", query, page);
		throw new RuntimeException("not implement method.");
	}

	public long findUserCountByQueryCriteria(UserQueryImpl query) {
//		return (Long) getDbSqlSession().selectOne("selectUserCountByQueryCriteria", query);
		throw new RuntimeException("not implement method.");
	}

	public List<Group> findGroupsByUser(String userId) {
//		return getDbSqlSession().selectList("selectGroupsByUserId", userId);
		List<Group> list = Lists.newArrayList();
		com.quauq.yanzhenxing.activiti.entity.User user = userService.obtainUser(userId);
		if (user != null){
			List<Role> roles=roleService.obtainRoles(userId);
			for (Role role : roles){
				list.add(toActivitiGroup(role));
			}
		}
		return list;
	}
	private  GroupEntity toActivitiGroup(Role role){
		if (role == null){
			return null;
		}
		GroupEntity groupEntity = new GroupEntity();
		groupEntity.setId(role.getId());
		groupEntity.setName(role.getName());
		groupEntity.setType(role.getType());
		groupEntity.setRevision(1);
		return groupEntity;
	}
	public UserQuery createNewUserQuery() {
//		return new UserQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutorTxRequired());
		throw new RuntimeException("not implement method.");
	}

	public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId, String key) {
//		Map<String, String> parameters = new HashMap<String, String>();
//		parameters.put("userId", userId);
//		parameters.put("key", key);
//		return (IdentityInfoEntity) getDbSqlSession().selectOne("selectIdentityInfoByUserIdAndKey", parameters);
		throw new RuntimeException("not implement method.");
	}

	public List<String> findUserInfoKeysByUserIdAndType(String userId, String type) {
//		Map<String, String> parameters = new HashMap<String, String>();
//		parameters.put("userId", userId);
//		parameters.put("type", type);
//		return (List) getDbSqlSession().getSqlSession().selectList("selectIdentityInfoKeysByUserIdAndType", parameters);
		throw new RuntimeException("not implement method.");
	}

	public Boolean checkPassword(String userId, String password) {
//		User user = findUserById(userId);
//		if ((user != null) && (password != null) && (password.equals(user.getPassword()))) {
//			return true;
//		}
//		return false;
		throw new RuntimeException("not implement method.");
	}

	public List<User> findPotentialStarterUsers(String proceDefId) {
//		Map<String, String> parameters = new HashMap<String, String>();
//		parameters.put("procDefId", proceDefId);
//		return (List<User>) getDbSqlSession().selectOne("selectUserByQueryCriteria", parameters);
		throw new RuntimeException("not implement method.");

	}

	public List<User> findUsersByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
//		return getDbSqlSession().selectListWithRawParameter("selectUserByNativeQuery", parameterMap, firstResult, maxResults);
		throw new RuntimeException("not implement method.");
	}

	public long findUserCountByNativeQuery(Map<String, Object> parameterMap) {
//		return (Long) getDbSqlSession().selectOne("selectUserCountByNativeQuery", parameterMap);
		throw new RuntimeException("not implement method.");
	}
	
}

 

2. 自定义user服务的Factory

 

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.entity.UserIdentityManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 自定义user 服务Factory
 * @author yanzhenxing
 * @createDate 2015年9月17日
 */
public class ActUserEntityServiceFactory implements SessionFactory {
    
    @Autowired
    private ActUserEntityService actUserEntityService;

    public Class<?> getSessionType() {
        // 返回原始的UserIdentityManager类型
        return UserIdentityManager.class;
    }

    public Session openSession() {
        // 返回自定义的GroupEntityManager实例
        return actUserEntityService;
    }

}

 

3. 自定义group服务

 

import java.util.List;
import java.util.Map;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.GroupQuery;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.quauq.yanzhenxing.activiti.entity.Role;
import com.quauq.yanzhenxing.activiti.entity.User;
import com.quauq.yanzhenxing.activiti.service.RoleService;
import com.quauq.yanzhenxing.activiti.service.UserService;

/**
 * 自定义 Group服务 
 * @author yanzhenxing
 * @createDate 2015年9月17日
 */
@Service
public class ActGroupEntityService extends GroupEntityManager {

    @Autowired
    private UserService userService;//系统的用户服务
    
    @Autowired
    private RoleService roleService;//系统的角色服务

    public Group createNewGroup(String groupId) {
        return new GroupEntity(groupId);
    }

    public void insertGroup(Group group) {
//        getDbSqlSession().insert((PersistentObject) group);
        throw new RuntimeException("not implement method.");
    }

    public void updateGroup(GroupEntity updatedGroup) {
//        CommandContext commandContext = Context.getCommandContext();
//        DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
//        dbSqlSession.update(updatedGroup);
        throw new RuntimeException("not implement method.");
    }

    public void deleteGroup(String groupId) {
//        GroupEntity group = getDbSqlSession().selectById(GroupEntity.class, groupId);
//        getDbSqlSession().delete("deleteMembershipsByGroupId", groupId);
//        getDbSqlSession().delete(group);
        throw new RuntimeException("not implement method.");
    }

    public GroupQuery createNewGroupQuery() {
//        return new GroupQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutorTxRequired());
        throw new RuntimeException("not implement method.");
    }

//    @SuppressWarnings("unchecked")
    public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
//        return getDbSqlSession().selectList("selectGroupByQueryCriteria", query, page);
        throw new RuntimeException("not implement method.");
    }

    public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
//        return (Long) getDbSqlSession().selectOne("selectGroupCountByQueryCriteria", query);
        throw new RuntimeException("not implement method.");
    }

    public List<Group> findGroupsByUser(String userId) {
//        return getDbSqlSession().selectList("selectGroupsByUserId", userId);
        List<Group> list = Lists.newArrayList();
        User user = userService.obtainUser(userId);
        if (user != null){
            List<Role> roles=roleService.obtainRoles(userId);
            for (Role role : roles){
                list.add(toActivitiGroup(role));
            }
        }
        return list;
    }
    
    private  GroupEntity toActivitiGroup(Role role){
        if (role == null){
            return null;
        }
        GroupEntity groupEntity = new GroupEntity();
        groupEntity.setId(role.getId());
        groupEntity.setName(role.getName());
        groupEntity.setType(role.getType());
        groupEntity.setRevision(1);
        return groupEntity;
    }
    
    public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
//        return getDbSqlSession().selectListWithRawParameter("selectGroupByNativeQuery", parameterMap, firstResult, maxResults);
        throw new RuntimeException("not implement method.");
    }

    public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) {
//        return (Long) getDbSqlSession().selectOne("selectGroupCountByNativeQuery", parameterMap);
        throw new RuntimeException("not implement method.");
    }

}

 

4. 自定义 group服务的Factory

 

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.entity.GroupIdentityManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 自定义Group服务Factory
 * @author yanzhenxing
 * @createDate 2015年9月17日
 */
public class ActGroupEntityServiceFactory implements SessionFactory {
    
    @Autowired
    private ActGroupEntityService actGroupEntityService;
    
    public Class<?> getSessionType() {
        // 返回原始的GroupIdentityManager类型
        return GroupIdentityManager.class;
    }

    public Session openSession() {
        // 返回自定义的GroupEntityManager实例
        return actGroupEntityService;
    }

}

 

5. 修改spring的配置

在processEngineConfiguration配置中添加属性

        <!-- 自定义用户组服务 -->
        <property name="customSessionFactories"> 
            <list>
                <bean class="com.quauq.yanzhenxing.activiti.service.ext.ActUserEntityServiceFactory"/>
                <bean class="com.quauq.yanzhenxing.activiti.service.ext.ActGroupEntityServiceFactory"/>
            </list>
        </property>

 

经过上面的配置和接口实现,就可以将Activiti中的身份认证服务指向系统的相关服务,user和group服务中的实现方法根据系统需求而定。

关于Activiti的学习内容,推荐大家多看看咖啡兔的博客以及他的github的例子,肯定会受益匪浅的。

 

posted @ 2015-10-09 17:48  yanzhenxing  阅读(6426)  评论(3编辑  收藏  举报