现在的项目有一个需求,要求根据用户的类型来设置登陆到系统后的布局。
用户类型有四种:教师、学生、家长。要求这四种类型的用户进来后的布局分别是:
第四种懒得贴出来了。
解决方案:
1、首先创建四种布局 template
2、新建ext工程修改 com.liferay.portal.events.ServicePreAction 类
修改:
private Map<Long, Object> mapCurrUser; protected void addDefaultUserPrivateLayoutByProperties( long userId, long groupId) throws PortalException, SystemException { String friendlyURL = getFriendlyURL( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_FRIENDLY_URL); ServiceContext serviceContext = new ServiceContext(); Layout layout = LayoutLocalServiceUtil.addLayout( userId, groupId, true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_NAME, StringPool.BLANK, StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL, serviceContext); LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType(); UserInfo userInfoTemp = null; if(mapCurrUser == null || !mapCurrUser.containsKey(userId)){ mapCurrUser = new HashMap<Long, Object>(); //根据不同的角色制定不同的用户布局 userInfoTemp = UserInfoLocalServiceUtil.findByLiferayUserIdForUserInfo(userId); mapCurrUser.put(userId, userInfoTemp); System.out.println("current User : " + userId + " +++ " + mapCurrUser.get(userId)); }else{ userInfoTemp = (UserInfo)mapCurrUser.get(userId); } if(userInfoTemp != null){ if(userInfoTemp.getUserType().equals("TH")){ //教师布局 System.out.println("curr is teacher"); layoutTypePortlet.setLayoutTemplateId( 0, PropsUtil.get("default.teacher.role.private.layout.template.id"), false); }else if(userInfoTemp.getUserType().equals("PA")){ //家长布局 System.out.println("curr is parent"); layoutTypePortlet.setLayoutTemplateId( 0, PropsUtil.get("default.parent.role.private.layout.template.id"), false); }else if(userInfoTemp.getUserType().equals("ST")){ //学生布局 System.out.println("curr is student"); layoutTypePortlet.setLayoutTemplateId( 0, PropsUtil.get("default.student.role.private.layout.template.id"), false); }else if(userInfoTemp.getPosition() != null && !userInfoTemp.getPosition().equals("")){ //领导布局 System.out.println("curr is leader"); layoutTypePortlet.setLayoutTemplateId( 0, PropsUtil.get("default.leader.role.private.layout.template.id"), false); } }else { System.out.println("curr User is null"); layoutTypePortlet.setLayoutTemplateId( 0, PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_TEMPLATE_ID, false); } LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate(); for (String columnId : layoutTemplate.getColumns()) { String keyPrefix = PropsKeys.DEFAULT_USER_PRIVATE_LAYOUT_PREFIX; if(userInfoTemp.getUserType().equals(TeachContants.TEACHER_USER_POSITION)){ //老师 System.out.println("curr is teacher"); keyPrefix = "default.teacher.role.private.layout."; } if(userInfoTemp.getUserType().equals(TeachContants.PARENT_USER_POSITION)){ //家长 System.out.println("curr is parent"); keyPrefix = "default.parent.role.private.layout."; } if(userInfoTemp.getUserType().equals(TeachContants.STUDENT_USER_POSITION)){ //学生 System.out.println("curr is student"); keyPrefix = "default.student.role.private.layout."; } if(userInfoTemp.getPosition() != null && !userInfoTemp.getPosition().equals("")){ //领导布局 System.out.println("curr is leader"); keyPrefix = "default.leader.role.private.layout."; } String portletIds = PropsUtil.get(keyPrefix.concat(columnId)); layoutTypePortlet.addPortletIds( 0, StringUtil.split(portletIds), columnId, false); } LayoutLocalServiceUtil.updateLayout( layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings()); boolean updateLayoutSet = false; LayoutSet layoutSet = layout.getLayoutSet(); if (Validator.isNotNull( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_THEME_ID)) { layoutSet.setThemeId( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_THEME_ID); updateLayoutSet = true; } if (Validator.isNotNull( PropsValues. DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_COLOR_SCHEME_ID)) { layoutSet.setColorSchemeId( PropsValues. DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_COLOR_SCHEME_ID); updateLayoutSet = true; } if (Validator.isNotNull( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_THEME_ID)) { layoutSet.setWapThemeId( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_THEME_ID); updateLayoutSet = true; } if (Validator.isNotNull( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_COLOR_SCHEME_ID)) { layoutSet.setWapColorSchemeId( PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_COLOR_SCHEME_ID); updateLayoutSet = true; } if (updateLayoutSet) { LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet); } }
4、diect-deploy Ext工程。
5、在portal-ext.properties配置文件中配置一下几个配置:
default.parent.role.private.layout.template.id=xxxx default.parent.role.private.layout.column-1=xxxx,xxxx default.parent.role.private.layout.column-2=xxxx,xxxx default.student.role.private.layout.template.id=xxxx default.student.role.private.layout.column-1=xxxx,xxxx default.student.role.private.layout.column-2=xxxx,xxxx default.teacher.role.private.layout.template.id=xxx default.teacher.role.private.layout.column-1=xxxx,xxxx default.teacher.role.private.layout.column-2=xxxx,xxxx default.teacher.role.private.layout.column-3=xxxx,xxxx default.teacher.role.private.layout.column-4=xxxx,xxxx default.leader.role.private.layout.template.id=xxxx default.leader.role.private.layout.column-1=xxxx,xxxx default.leader.role.private.layout.column-2=xxxx,xxxx default.leader.role.private.layout.column-3=xxxx,xxxx default.leader.role.private.layout.column-4=xxxx,xxxx