activiti用户角色权限与自身系统用户角色同步

同步的方法有很多。由于本人也是新手,也只用了一种方法。在此介绍一种方法。就是表的数据同步。

思路:     系统添加角色则向act_id_group添加一条数据

系统添加用户则向act_id_user添加一条数据,同时向act_id_membership添加多条数据。

代码很简单如下:

              /**
* 根据角色添加activiti组
* @param role
*/
@SuppressWarnings("deprecation")
public void saveActivitiLxf(Role role) {
if(role!=null) {
String roleId = ObjectUtils.toString(role.getId());
Group activitiGroup = identityService.createGroupQuery().groupId(roleId).singleResult();
if (activitiGroup == null) {
activitiGroup = identityService.newGroup(roleId);
identityService.saveGroup(activitiGroup);
}
}
}


/** * 根据角色ID删除activiti组 * @param roleId */ public void deleteActivitiLxf(String roleId) { if(roleId!=null && !"".equals(roleId)) { identityService.deleteGroup(roleId); //删除组所对应的所有用户 List users = identityService.createUserQuery().memberOfGroup(roleId).list(); for(org.activiti.engine.identity.User user : users){ identityService.deleteMembership(user.getId(), roleId); } } }

/** * 同步activiti用户信息 * @param user */ @SuppressWarnings("deprecation") public void saveActivitiLxf(User user) { if(user!=null) { String userLoginName = ObjectUtils.toString(user.getLoginName()); org.activiti.engine.identity.User activitiUser = identityService.createUserQuery().userId(userLoginName).singleResult(); if (activitiUser == null) { activitiUser = identityService.newUser(userLoginName); identityService.saveUser(activitiUser); } //同步用户角色关联数据 List roles = user.getRoleList(); mergeLxf(user, roles); } }

/** * 根据用户角色同步用户角色信息 * @param user * @param roles */ @SuppressWarnings({ "deprecation", "unchecked" }) private void mergeLxf(User user,List roles) { String userLoginName = ObjectUtils.toString(user.getLoginName()); List activitiGroupList = identityService.createGroupQuery().groupMember(userLoginName).list(); if(Collections3.isEmpty(roles)) { for(Group group:activitiGroupList) { identityService.deleteMembership(userLoginName, group.getId()); } } else { Map groupMap =Maps.newHashMap(); for(Role role:roles) { groupMap.put(role.getId(), role.getName()); } Map activitiGroupMap = Collections3.extractToMap(activitiGroupList, "id", "name"); for(String groupId:activitiGroupMap.keySet()) { if(StringUtils.isNotBlank(groupId) && !groupMap.containsKey(groupId)) { identityService.deleteMembership(userLoginName, groupId); } } for(String groupId:groupMap.keySet()) { if(StringUtils.isNotBlank(groupId) && !activitiGroupMap.containsKey(groupId)) { identityService.createMembership(userLoginName, groupId); } } } } /** * 根据用户登陆名称删除activiti用户 * @param userLoginName */ public void deleteActiviti(String userLoginName) { if(userLoginName!=null && !"".equals(userLoginName)) { identityService.deleteUser(userLoginName); //删除用户对应的所有组 List groups = identityService.createGroupQuery().groupMember(userLoginName).list(); for(Group group : groups){ identityService.deleteMembership(userLoginName, group.getId()); } } }

            

一键同步。

/** * 手工同步所有Activiti数据 */ @SuppressWarnings("deprecation") @Transactional(readOnly = false) public void synToActiviti()  { permissionDao.updateBySql("delete from ACT_ID_MEMBERSHIP",null); permissionDao.updateBySql("delete from ACT_ID_GROUP", null); permissionDao.updateBySql("delete from ACT_ID_USER", null); List activitiGroupList = identityService.createGroupQuery().list(); List activitiUserList = identityService.createUserQuery().list(); if (activitiGroupList.size() == 0 && activitiUserList.size() == 0){ //添加所有角色 List roleList = roleDao.findAll(); for (Role role : roleList) { Group group = identityService.newGroup(role.getId()); identityService.saveGroup(group); } //添加所有用户以及用户角色 List userList = userDao.findAllList(); for(User user : userList){ org.activiti.engine.identity.User activitiUesr = identityService.newUser(ObjectUtils.toString(user.getLoginName())); identityService.saveUser(activitiUesr); List roles = user.getRoleList(); for(Role role : roles){ identityService.createMembership(user.getLoginName(), role.getId()); } } } }




如有不懂请加高级开发群共同讨论。135430763


/**
* 根据角色ID删除activiti组
* @param roleId
*/
public void deleteActivitiLxf(String roleId) {
if(roleId!=null && !"".equals(roleId)) {
identityService.deleteGroup(roleId);
//删除组所对应的所有用户
List users = identityService.createUserQuery().memberOfGroup(roleId).list();
for(org.activiti.engine.identity.User user : users){
identityService.deleteMembership(user.getId(), roleId);
}
}
}



/**
* 同步activiti用户信息
* @param user
*/
@SuppressWarnings("deprecation")
public void saveActivitiLxf(User user) {
if(user!=null) {
String userLoginName = ObjectUtils.toString(user.getLoginName());
org.activiti.engine.identity.User activitiUser = identityService.createUserQuery().userId(userLoginName).singleResult();
if (activitiUser == null) {
activitiUser = identityService.newUser(userLoginName);
identityService.saveUser(activitiUser);
}
//同步用户角色关联数据
List roles = user.getRoleList();
mergeLxf(user, roles);
}
}

/**
* 根据用户角色同步用户角色信息
* @param user
* @param roles
*/
@SuppressWarnings({ "deprecation", "unchecked" })
private void mergeLxf(User user,List roles) {
String userLoginName = ObjectUtils.toString(user.getLoginName());
List activitiGroupList = identityService.createGroupQuery().groupMember(userLoginName).list();
if(Collections3.isEmpty(roles)) {
for(Group group:activitiGroupList) {
identityService.deleteMembership(userLoginName, group.getId());
}
} else {
Map groupMap =Maps.newHashMap();
for(Role role:roles) {
groupMap.put(role.getId(), role.getName());
}
Map activitiGroupMap = Collections3.extractToMap(activitiGroupList, "id", "name");
for(String groupId:activitiGroupMap.keySet()) {
if(StringUtils.isNotBlank(groupId) && !groupMap.containsKey(groupId)) {
identityService.deleteMembership(userLoginName, groupId);
}
}
for(String groupId:groupMap.keySet()) {
if(StringUtils.isNotBlank(groupId) && !activitiGroupMap.containsKey(groupId)) {
identityService.createMembership(userLoginName, groupId);
}
}
}
}

/**
* 根据用户登陆名称删除activiti用户
* @param userLoginName
*/
public void deleteActiviti(String userLoginName) {
if(userLoginName!=null && !"".equals(userLoginName)) {
identityService.deleteUser(userLoginName);
//删除用户对应的所有组
List groups = identityService.createGroupQuery().groupMember(userLoginName).list();
for(Group group : groups){
identityService.deleteMembership(userLoginName, group.getId());
}
}
}

            

一键同步。

/**
* 手工同步所有Activiti数据
*/
@SuppressWarnings("deprecation")
@Transactional(readOnly = false)
public void synToActiviti()  {
permissionDao.updateBySql("delete from ACT_ID_MEMBERSHIP",null);
permissionDao.updateBySql("delete from ACT_ID_GROUP", null);
permissionDao.updateBySql("delete from ACT_ID_USER", null);

List activitiGroupList = identityService.createGroupQuery().list();
List activitiUserList = identityService.createUserQuery().list();

if (activitiGroupList.size() == 0 && activitiUserList.size() == 0){
//添加所有角色
List roleList = roleDao.findAll();
for (Role role : roleList) {
Group group = identityService.newGroup(role.getId());
identityService.saveGroup(group);
}
//添加所有用户以及用户角色
List userList = userDao.findAllList();
for(User user : userList){
org.activiti.engine.identity.User activitiUesr = identityService.newUser(ObjectUtils.toString(user.getLoginName()));
identityService.saveUser(activitiUesr);
List roles = user.getRoleList();
for(Role role : roles){
identityService.createMembership(user.getLoginName(), role.getId());
}
}
}
}


你可能感兴趣的:(activiti)