1. 程式人生 > >activiti使用者角色許可權與自身系統使用者角色同步

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);
}
}
}


<p>
<span style="white-space: pre;"></span>/**
<span style="white-space: pre;"></span>* 根據角色ID刪除activiti組
<span style="white-space: pre;"></span>* @param roleId
<span style="white-space: pre;"></span>*/
<span style="white-space: pre;"></span>public void deleteActivitiLxf(String roleId) {
<span style="white-space: pre;"></span>if(roleId!=null && !"".equals(roleId)) {
<span style="white-space: pre;"></span>identityService.deleteGroup(roleId);
<span style="white-space: pre;"></span>//刪除組所對應的所有使用者
<span style="white-space: pre;"></span>List<org.activiti.engine.identity.User> users = identityService.createUserQuery().memberOfGroup(roleId).list();
<span style="white-space: pre;"></span>for(org.activiti.engine.identity.User user : users){
<span style="white-space: pre;"></span>identityService.deleteMembership(user.getId(), roleId);
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
</p><p>
</p><p>
</p><p><span style="white-space: pre;"></span>/**
<span style="white-space: pre;"></span>* 同步activiti使用者資訊
<span style="white-space: pre;"></span>* @param user
<span style="white-space: pre;"></span>*/
<span style="white-space: pre;"></span>@SuppressWarnings("deprecation")
<span style="white-space: pre;"></span>public void saveActivitiLxf(User user) {
<span style="white-space: pre;"></span>if(user!=null) {
<span style="white-space: pre;"></span>String userLoginName = ObjectUtils.toString(user.getLoginName());
<span style="white-space: pre;"></span>org.activiti.engine.identity.User activitiUser = identityService.createUserQuery().userId(userLoginName).singleResult();
<span style="white-space: pre;"></span>if (activitiUser == null) {
<span style="white-space: pre;"></span>activitiUser = identityService.newUser(userLoginName);
<span style="white-space: pre;"></span>identityService.saveUser(activitiUser);
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>//同步使用者角色關聯資料
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>List<Role> roles = user.getRoleList();
<span style="white-space: pre;"></span>mergeLxf(user, roles);
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>
</p><p>/**
<span style="white-space: pre;"></span>* 根據使用者角色同步使用者角色資訊
<span style="white-space: pre;"></span>* @param user
<span style="white-space: pre;"></span>* @param roles
<span style="white-space: pre;"></span>*/
<span style="white-space: pre;"></span>@SuppressWarnings({ "deprecation", "unchecked" })
<span style="white-space: pre;"></span>private void mergeLxf(User user,List<Role> roles) {
<span style="white-space: pre;"></span>String userLoginName = ObjectUtils.toString(user.getLoginName());
<span style="white-space: pre;"></span>List<Group> activitiGroupList = identityService.createGroupQuery().groupMember(userLoginName).list();
<span style="white-space: pre;"></span>if(Collections3.isEmpty(roles)) {
<span style="white-space: pre;"></span>for(Group group:activitiGroupList) {
<span style="white-space: pre;"></span>identityService.deleteMembership(userLoginName, group.getId());
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>} else {
<span style="white-space: pre;"></span>Map<String,String> groupMap =Maps.newHashMap();
<span style="white-space: pre;"></span>for(Role role:roles) {
<span style="white-space: pre;"></span>groupMap.put(role.getId(), role.getName());
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>Map<String,String> activitiGroupMap = Collections3.extractToMap(activitiGroupList, "id", "name");
<span style="white-space: pre;"></span>for(String groupId:activitiGroupMap.keySet()) {
<span style="white-space: pre;"></span>if(StringUtils.isNotBlank(groupId) && !groupMap.containsKey(groupId)) {
<span style="white-space: pre;"></span>identityService.deleteMembership(userLoginName, groupId);
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>for(String groupId:groupMap.keySet()) {
<span style="white-space: pre;"></span>if(StringUtils.isNotBlank(groupId) && !activitiGroupMap.containsKey(groupId)) {
<span style="white-space: pre;"></span>identityService.createMembership(userLoginName, groupId);
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>
<span style="white-space: pre;"></span>/**
<span style="white-space: pre;"></span>* 根據使用者登陸名稱刪除activiti使用者
<span style="white-space: pre;"></span>* @param userLoginName
<span style="white-space: pre;"></span>*/
<span style="white-space: pre;"></span>public void deleteActiviti(String userLoginName) {
<span style="white-space: pre;"></span>if(userLoginName!=null && !"".equals(userLoginName)) {
<span style="white-space: pre;"></span>identityService.deleteUser(userLoginName);
<span style="white-space: pre;"></span>//刪除使用者對應的所有組
<span style="white-space: pre;"></span>List<Group> groups = identityService.createGroupQuery().groupMember(userLoginName).list();
<span style="white-space: pre;"></span>for(Group group : groups){
<span style="white-space: pre;"></span>identityService.deleteMembership(userLoginName, group.getId());
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
</p><p>            </p><p>一鍵同步。</p><p>/**
<span style="white-space: pre;"></span>* 手工同步所有Activiti資料
<span style="white-space: pre;"></span>*/
<span style="white-space: pre;"></span>@SuppressWarnings("deprecation")
<span style="white-space: pre;"></span>@Transactional(readOnly = false)
<span style="white-space: pre;"></span>public void synToActiviti()  {
<span style="white-space: pre;"></span>permissionDao.updateBySql("delete from ACT_ID_MEMBERSHIP",null);
<span style="white-space: pre;"></span>permissionDao.updateBySql("delete from ACT_ID_GROUP", null);
<span style="white-space: pre;"></span>permissionDao.updateBySql("delete from ACT_ID_USER", null);
<span style="white-space: pre;"></span>
<span style="white-space: pre;"></span>List<Group> activitiGroupList = identityService.createGroupQuery().list();
<span style="white-space: pre;"></span>List<org.activiti.engine.identity.User> activitiUserList = identityService.createUserQuery().list();
<span style="white-space: pre;"></span>
<span style="white-space: pre;"></span>if (activitiGroupList.size() == 0 && activitiUserList.size() == 0){
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>//新增所有角色
<span style="white-space: pre;"></span>List<Role> roleList = roleDao.findAll();
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>for (Role role : roleList) {
<span style="white-space: pre;"></span>Group group = identityService.newGroup(role.getId());
<span style="white-space: pre;"></span>identityService.saveGroup(group);
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>//新增所有使用者以及使用者角色
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>List<User> userList = userDao.findAllList();
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>for(User user : userList){
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>org.activiti.engine.identity.User activitiUesr = identityService.newUser(ObjectUtils.toString(user.getLoginName()));
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>identityService.saveUser(activitiUesr);
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>List<Role> roles = user.getRoleList();
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>for(Role role : roles){
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>identityService.createMembership(user.getLoginName(), role.getId());
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span><span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
<span style="white-space: pre;"></span>}
</p><div>
</div>


如有不懂請加高階開發群共同討論。135430763


/**
* 根據角色ID刪除activiti組
* @param roleId
*/
public void deleteActivitiLxf(String roleId) {
if(roleId!=null && !"".equals(roleId)) {
identityService.deleteGroup(roleId);
//刪除組所對應的所有使用者
List<org.activiti.engine.identity.User> 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<Role> roles = user.getRoleList();
mergeLxf(user, roles);
}
}

/**
* 根據使用者角色同步使用者角色資訊
* @param user
* @param roles
*/
@SuppressWarnings({ "deprecation", "unchecked" })
private void mergeLxf(User user,List<Role> roles) {
String userLoginName = ObjectUtils.toString(user.getLoginName());
List<Group> activitiGroupList = identityService.createGroupQuery().groupMember(userLoginName).list();
if(Collections3.isEmpty(roles)) {
for(Group group:activitiGroupList) {
identityService.deleteMembership(userLoginName, group.getId());
}
} else {
Map<String,String> groupMap =Maps.newHashMap();
for(Role role:roles) {
groupMap.put(role.getId(), role.getName());
}
Map<String,String> 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<Group> 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<Group> activitiGroupList = identityService.createGroupQuery().list();
List<org.activiti.engine.identity.User> activitiUserList = identityService.createUserQuery().list();

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