• Liferay7 BPM门户开发之12:acitiviti和liferay用户权限体系集成


    写到第12章才出现Liferay的内容,希望可以厚积薄发。

    我们的目标是不使用不维护Activiti的用户组织架构,只维护Liferay的体系,这样的好处是非常明显的,即不用做组织架构的同步工作。

    原理是继承实现org.activiti.engine.impl.interceptor.SessionFactory

    首先继承用户实例,用liferay的用户模型

    import org.activiti.engine.impl.persistence.entity.UserEntity;
    
    public class UserImpl extends UserEntity {
        private static final long serialVersionUID = -5809624687782521587L;
        
        public UserImpl(com.liferay.portal.model.User liferayUser) {
            id = String.valueOf(liferayUser.getUserId());
            firstName = liferayUser.getFirstName();
            lastName = liferayUser.getLastName();
            email = liferayUser.getEmailAddress();
        }
    }

    实现管理类UserEntityManager

    import java.util.List;
    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.IdentityInfoEntity;
    import org.activiti.engine.impl.persistence.entity.UserEntity;
    import org.activiti.engine.impl.persistence.entity.UserEntityManager;
    import org.springframework.stereotype.Service;
    import com.liferay.portal.kernel.log.Log;
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    
    @Service("liferayUserManagerSession")
    public class LiferayUserManagerSession extends UserEntityManager {
        
        private static Log _log = LogFactoryUtil.getLog(LiferayUserManagerSession.class);
    
        private LiferayIdentityService liferayIdentityService = new LiferayIdentityService();
        
        @Override
        public User createNewUser(String userId) {
            _log.error("Method is not implemented"); // TODO
            return null;
        }
    
        @Override
        public void insertUser(User user) {
            _log.error("Method is not implemented"); // TODO
        }
    
        @Override
        public void updateUser(UserEntity updatedUser) {
            _log.error("Method is not implemented"); // TODO
        }
        
        @Override
        public UserEntity findUserById(String userId) {
            return liferayIdentityService.findUserById(userId);
        }
        
        @Override
        public void deleteUser(String userId) {
            _log.error("Method is not implemented"); // TODO
        }
    
        @Override
        public List<User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
            _log.error("Method is not implemented"); // TODO
            return null;
        }
    
        @Override
        public long findUserCountByQueryCriteria(UserQueryImpl query) {
            _log.error("Method is not implemented"); // TODO
            return -1;
        }
    
        @Override
        public List<Group> findGroupsByUser(String userId) {
            return liferayIdentityService.findGroupsByUser(userId);
        }
    
        @Override
        public UserQuery createNewUserQuery() {
            _log.error("Method is not implemented"); // TODO
            return null;
        }
    
        @Override
        public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId,
                String key) {
            _log.error("Method is not implemented"); // TODO
            return null;
        }
    
        @Override
        public List<String> findUserInfoKeysByUserIdAndType(String userId,
                String type) {
            _log.error("Method is not implemented"); // TODO
            return null;
        }
    
    }
    View Code

    其中,最关键的方法是查找人员,重载为:

    @Override
        public UserEntity findUserById(String userId) {
            return liferayIdentityService.findUserById(userId);
        }

    实现工厂类

    import org.activiti.engine.impl.interceptor.Session;
    import org.activiti.engine.impl.interceptor.SessionFactory;
    
    public class LiferayUserManagerSessionFactory implements SessionFactory {
    
        @Override
        public Session openSession() {
            return new LiferayUserManagerSession();
        }
    
        @Override
        public Class<?> getSessionType() {
            return LiferayUserManagerSession.class;
        }
    
    }

    实用服务查询类:

    import org.activiti.engine.identity.Group;
    import org.activiti.engine.identity.User;
    import org.activiti.engine.impl.persistence.entity.UserEntity;
    import org.apache.commons.lang.ArrayUtils;
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.liferay.portal.kernel.log.Log;
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    import com.liferay.portal.model.Role;
    import com.liferay.portal.model.UserGroupRole;
    import com.liferay.portal.service.RoleLocalServiceUtil;
    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
    import com.liferay.portal.service.UserLocalServiceUtil;
    
    @Service("liferayIdentityService")
    public class LiferayIdentityService {
        private static Log _log = LogFactoryUtil.getLog(LiferayIdentityService.class);
    
        @Autowired
        IdMappingService idMappingService;
    
        // Groups
        
        public List<Group> findGroupsByUser(String userName) {
            try {
                // get regular roles
                List<Role> roles = RoleLocalServiceUtil.getUserRoles(idMappingService.getUserId(userName));
    
                // conert from site roles to the groups
                List<Group> groups = new ArrayList<Group>();
                for (Role role : roles) {
                    GroupImpl groupImpl = new GroupImpl(role);
                    groups.add(groupImpl);
                }
                
                // get group roles for specified user
                List<UserGroupRole> groupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(idMappingService.getUserId(userName));
                for (UserGroupRole groupRole : groupRoles) {
                    GroupImpl groupImpl = new GroupImpl(groupRole);
                    groups.add(groupImpl);
                }
                
                return groups;
            } catch (Exception e) {
                _log.error("Cannot get list of roles for user: " + userName, e);
                return new ArrayList<Group>();
            }
        }
    
        public List<User> findUsersByGroup(long companyId, String groupName) {
            return WorkflowUtil.findUsersByGroup(companyId, groupName);
        }
        
        public Role findRole(long companyId, String groupName) {
            // first - try to parse group to identify - it is regular group or org/community group
            String[] parsedName = groupName.split("/");
            List<com.liferay.portal.model.User> users = null;
            List<User> result = new ArrayList<User>();
            
            try {
                if (parsedName.length == 1) {
                    // regilar group
                    Role role = RoleLocalServiceUtil.getRole(companyId, groupName);
                    
                    return role;
                } else {
                    long groupId = Long.valueOf(parsedName[0]);
                    groupName = parsedName[1];
                    
                    if (parsedName.length > 2) {
                        groupName = StringUtils.join(ArrayUtils.subarray(parsedName, 1, parsedName.length), "/");
                    }
                    
                    Role role = RoleLocalServiceUtil.getRole(companyId, groupName);
                    
                    return role;
                }
            } catch (Exception ex) {
                _log.warn("Cannot get group users", ex);
                return null;
            }
        }
    
        // Users
        
        public UserEntity findUserById(String userName) {
            try {
                com.liferay.portal.model.User liferayUser = UserLocalServiceUtil.getUser(idMappingService.getUserId(userName));
                return new UserImpl(liferayUser);
            } catch (Exception ex) {
                _log.error("Cannot find user " + userName + " : " + ex.getMessage());
                return null;
            }
        }
    
    }

    非常优雅,不侵入,不破坏,依赖分离,松耦合.

    但我感觉这种注入接口还是Openfire的源代码接口(各种供应者interface接口)实现的更加直观和优雅。

    接下来是BPM门户开发攻坚关键,一步一步跟着我走,必定会逐渐脉络清晰。

  • 相关阅读:
    进程和程序的区别
    【Docker】5. 常用命令 — 镜像命令
    【Docker】4. 运行镜像的流程、docker工作原理
    【Docker】3. 配置阿里云镜像加速
    【Docker】2. Docker的架构介绍、安装与卸载 (CentOS 7)
    【Docker】Docker概述
    【Git】5. 远程库(GitHub)相关操作
    【Git】4. 团队内、跨团队协作机制
    【Git】3. Git重要特性-分支操作,合并冲突详解
    线性插值编写可视化代码
  • 原文地址:https://www.cnblogs.com/starcrm/p/6008804.html
Copyright © 2020-2023  润新知