服务热线
135-6963-3175
方案一、用activiti自带的用户组织结构表
涉及到用户数据同步的问题
(1)增量同步
有注册新用户或增加新角色的时候同步更新activiti的组织结构,通过调用IdentifyService接口完成同步。
查询该用户是否存在的方法:
List<org.activiti.engine.identity.user> activitiUsers = userQuery.userId(userId).list();if(activitiUsers.size() == 1) {}
(2)全部同步
删除Activiti的User、Group、Membership数据
复制Role对象数据到Group
复制用户数据以及Membership数据
方案二:自定义SessionFactory方式
自定义类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; @Service public 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.usermanage.role.service.RoleService; import com.usermanage.user.entity.User; import com.usermanage.user.service.UserService; import com.workflow.common.constant.WorkflowConstants; import com.workflow.common.util.ActivitiUserUtil; @Component public 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; @Service public 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.usermanage.role.service.RoleService; import com.usermanage.user.entity.User; import com.usermanage.user.service.UserService; import com.workflow.common.util.ActivitiUserUtil; @Component public 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配置方式为例):
import java.util.ArrayList; import java.util.List; import org.activiti.engine.impl.interceptor.SessionFactory; import org.activiti.spring.SpringProcessEngineConfiguration; import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import com.workflow.manager.CustomGroupEntityManagerFactory; import com.workflow.manager.CustomProcessDiagramGeneratorI; import com.workflow.manager.CustomUserEntityManagerFactory; import com.workflow.manager.ProcessHistoryManagerSessionFactory; @Configuration public class ActivitiConfiguration implements ProcessEngineConfigurationConfigurer{ @Autowired private CustomUserEntityManagerFactory customUserEntityManagerFactory; @Autowired private CustomGroupEntityManagerFactory customGroupEntityManagerFactory; @Autowired private ProcessHistoryManagerSessionFactory processHistoryManagerSessionFactory; @Autowired private CustomProcessDiagramGeneratorI customProcessDiagramGeneratorI; @Override public void configure(SpringProcessEngineConfiguration processEngineConfiguration) { // TODO Auto-generated method stub //processEngineConfiguration.setDataSource(dataSource); processEngineConfiguration.setDatabaseSchemaUpdate("none");// none true processEngineConfiguration.setDatabaseType("mysql"); //processEngineConfiguration.setTransactionManager(transactionManager); // 流程图字体 processEngineConfiguration.setActivityFontName("宋体"); processEngineConfiguration.setAnnotationFontName("宋体"); processEngineConfiguration.setLabelFontName("宋体"); processEngineConfiguration.setJpaHandleTransaction(false); processEngineConfiguration.setJpaCloseEntityManager(false); // // processEngineConfiguration.setMailServerHost(mailProperty.getMailServerHost()); // processEngineConfiguration.setMailServerUsername(mailProperty.getMailServerUsername()); // processEngineConfiguration.setMailServerPassword(mailProperty.getMailServerPassword()); // processEngineConfiguration.setMailServerPort(mailProperty.getMailServerPort()); // processEngineConfiguration.setJobExecutorActivate(false); processEngineConfiguration.setAsyncExecutorEnabled(false); //processEngineConfiguration.setAsyncExecutorActivate(false); //自定义用户和组 List<SessionFactory> customSessionFactories = new ArrayList<>(); customSessionFactories.add(customUserEntityManagerFactory); customSessionFactories.add(customGroupEntityManagerFactory); customSessionFactories.add(processHistoryManagerSessionFactory); processEngineConfiguration.setCustomSessionFactories(customSessionFactories); //自定义流程图样式 processEngineConfiguration.setProcessDiagramGenerator(customProcessDiagramGeneratorI); } }
其中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.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; } }
方案三:用视图覆盖同名的ACT_ID_系列表
此方案和第二种类似,放弃使用系列表:ACT_ID_,创建同名的视图。
1.删除已创建的ACT_ID_*表
创建视图必须删除引擎自动创建的ACT_ID_*表,否则不能创建视图。
2.创建视图:
ACT_ID_GROUP
ACT_ID_INFO
ACT_ID_MEMBERSHIP
ACT_ID_USER
创建的视图要保证数据类型一致,例如用户的ACT_ID_MEMBERSHIP表的两个字段都是字符型,一般系统中都是用NUMBER作为用户、角色的主键类型,所以创建视图的时候要把数字类型转换为字符型。
3.修改引擎默认配置
在引擎配置中设置属性dbIdentityUsed为false即可。
<bean id="processEngineConfiguration"class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dbIdentityUsed" value="false"> </property>
</bean>
总结
方案一:通过数据推送方式同步数据到引擎的身份表,需要把数据备份到引擎的身份表或者公司有平台或者WebService推送用户数据的推荐使用
方案二:自定义SessionFactory,非侵入式替换接口实现,对于公司内部有统一身份访问接口的推荐使用
方案三:不需要编写Java代码,只需要创建同名视图即可