声明:Activiti版本5.22.0。 Activiti在初始化时会生成act_id_*系列与用户、用户组相关的表,但在实际业务中,我们系统可能初期并没有接入工作流,后期接入时就涉及到如何将现有业务表中的用户与Activiti进行对接,本文即简单介绍下思路。
1、自定义实现Activiti用户管理接口
自定义类CustomUserEntityManagerFactory实现SessionFactory接口,重写getSessionType和openSession方法,核心代码如下:
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; import org.springframework.stereotype.Service; @Servicepublic class CustomUserEntityManagerFactory implements SessionFactory { @Autowired private CustomUserEntityManager customUserEntityManager; @Override public Class<?> getSessionType() { return UserIdentityManager.class; } @Override public Session openSession() { return customUserEntityManager; } }
其中,类CustomUserEntityManager继承UserEntityManager类:
import java.util.ArrayList; import java.util.List; import java.util.Map; import org.activiti.engine.identity.Group; import org.activiti.engine.impl.Page; import org.activiti.engine.impl.UserQueryImpl; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import com.fengunion.scf.data.usermanage.role.service.RoleService; import com.fengunion.scf.data.usermanage.user.entity.User; import com.fengunion.scf.data.usermanage.user.service.UserService; import com.fengunion.scf.data.workflow.common.constant.WorkflowConstants; import com.fengunion.scf.data.workflow.common.util.ActivitiUserUtil; @Componentpublic class CustomUserEntityManager extends UserEntityManager { @Autowired @Lazy private UserService userService; @Autowired @Lazy private RoleService roleService; @Override public UserEntity findUserById(String userId) { User user = getUser(userId); return ActivitiUserUtil.toActivitiUser(user); } @Override public List<Group> findGroupsByUser(String userId) { if (userId == null) { return null; } User uparam = new User(); uparam.setPkid(Long.valueOf(userId)); User user = userService.selectOne(uparam); List<String> list = roleService.fingRoleCodeByUserId(Long.valueOf(userId)); List<Group> gs = ActivitiUserUtil.toActivitiGroups(user.getFkEnterpriseBasicId(), list); return gs; } @Override public List<org.activiti.engine.identity.User> findUserByQueryCriteria(UserQueryImpl query, Page page) { User user = getUser(query.getId()); List<org.activiti.engine.identity.User> list = new ArrayList<>(); list.add(ActivitiUserUtil.toActivitiUser(user)); return list; } private User getUser(String userId) { User user = new User(); if (WorkflowConstants.INTERFACE_SYSTEM_ID.equals(userId)) { user.setPkid(Long.valueOf(WorkflowConstants.INTERFACE_SYSTEM_ID)); user.setNickName(WorkflowConstants.INTERFACE_SYSTEM_NAME); user.setRealName(WorkflowConstants.INTERFACE_SYSTEM_NAME); user.setPswd(""); user.setEmail(""); } else { User prarmUser = new User(); prarmUser.setPkid(Long.valueOf(userId)); user = userService.selectOne(prarmUser); } return user; } @Override public long findUserCountByQueryCriteria(UserQueryImpl query) { throw new RuntimeException("not implement method."); } @Override public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId, String key) { throw new RuntimeException("not implement method."); } @Override public List<String> findUserInfoKeysByUserIdAndType(String userId, String type) { throw new RuntimeException("not implement method."); } @Override public List<org.activiti.engine.identity.User> findPotentialStarterUsers(String proceDefId) { throw new RuntimeException("not implement method."); } @Override public List<org.activiti.engine.identity.User> findUsersByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) { throw new RuntimeException("not implement method."); } @Override public long findUserCountByNativeQuery(Map<String, Object> parameterMap) { throw new RuntimeException("not implement method."); } }
2、自定义实现Activiti用户管理接口
自定义类CustomGroupEntityManagerFactory 实现SessionFactory接口,重写getSessionType和openSession方法,核心代码如下:
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; import org.springframework.stereotype.Service; @Servicepublic class CustomGroupEntityManagerFactory implements SessionFactory { @Autowired private CustomGroupEntityManager customGroupEntityManager; @Override public Class<?> getSessionType() { return GroupIdentityManager.class; } @Override public Session openSession() { return customGroupEntityManager; } }
其中,CustomGroupEntityManager类如下:
import java.util.List; import java.util.Map; import org.activiti.engine.identity.Group; import org.activiti.engine.impl.GroupQueryImpl; import org.activiti.engine.impl.Page; import org.activiti.engine.impl.persistence.entity.GroupEntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.stereotype.Component; import com.fengunion.scf.data.usermanage.role.service.RoleService; import com.fengunion.scf.data.usermanage.user.entity.User; import com.fengunion.scf.data.usermanage.user.service.UserService; import com.fengunion.scf.data.workflow.common.util.ActivitiUserUtil; @Componentpublic class CustomGroupEntityManager extends GroupEntityManager { @Lazy @Autowired private RoleService roleService; @Lazy @Autowired private UserService userService; @Override public List<Group> findGroupsByUser(String userId) { if (userId == null) { return null; } try { User uparam = new User(); uparam.setPkid(Long.valueOf(userId)); User user = userService.selectOne(uparam); List<String> list = roleService.fingRoleCodeByUserId(Long.valueOf(userId)); List<Group> gs = ActivitiUserUtil.toActivitiGroups(user.getFkEnterpriseBasicId(), list); return gs; } catch (EmptyResultDataAccessException e) { return null; } } @Override public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) { throw new RuntimeException("not implement method."); } @Override public long findGroupCountByQueryCriteria(GroupQueryImpl query) { throw new RuntimeException("not implement method."); } @Override public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) { throw new RuntimeException("not implement method."); } @Override public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) { throw new RuntimeException("not implement method."); } }
3、在Activiti配置中注册自己定义的用户、用户组管理类,核心代码如下(我这儿以springboot配置方式为例):
... @Autowired private CustomUserEntityManagerFactory customUserEntityManagerFactory; @Autowired private CustomGroupEntityManagerFactory customGroupEntityManagerFactory; ... @Override public void configure(SpringProcessEngineConfiguration processEngineConfiguration){ ... } //自定义用户和组 List<SessionFactory> customSessionFactories=new ArrayList<>(); customSessionFactories.add(customUserEntityManagerFactory); customSessionFactories.add(customGroupEntityManagerFactory); processEngineConfiguration.setCustomSessionFactories(customSessionFactories); // ...}
其中ActivitiUserUtil工具类如下:
import java.util.ArrayList; import java.util.List; import org.activiti.engine.identity.Group; import org.activiti.engine.impl.persistence.entity.GroupEntity; import org.activiti.engine.impl.persistence.entity.UserEntity; import com.fengunion.scf.data.usermanage.user.entity.User; public class ActivitiUserUtil { public static UserEntity toActivitiUser(User bUser) { UserEntity userEntity = new UserEntity(); userEntity.setId(bUser.getPkid().toString()); userEntity.setFirstName(bUser.getNickName()); userEntity.setLastName(bUser.getRealName()); userEntity.setPassword(bUser.getPswd()); userEntity.setEmail(bUser.getEmail()); userEntity.setRevision(1); return userEntity; } public static GroupEntity toActivitiGroup(Long enterpriseBasicId, String code) { GroupEntity groupEntity = new GroupEntity(); groupEntity.setRevision(1); groupEntity.setType("assignment"); groupEntity.setId(enterpriseBasicId + "_" + code); return groupEntity; } public static List<Group> toActivitiGroups(Long enterpriseBasicId, List<String> roleCodeList) { List<Group> groups = new ArrayList<>(); for (String code : roleCodeList) { GroupEntity groupEntity = toActivitiGroup(enterpriseBasicId, code); groups.add(groupEntity); } return groups; } }
至此,实现自定义用户和用户组就基本实现了。
转载于:https://my.oschina.net/u/180480/blog/2250441