不幸的是,它没有公共 API。
使用 LayoutSetPrototypeLocalServiceUtil 获取 SiteTemplate 的 ID。要按名称获取 SiteTemplate,您必须使用 dynamicQuery 或迭代 LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypes(-1, -1) 的结果
然后在门户的上下文中调用 SitesUtil 的 applyLayoutSetPrototypes。
MethodKey methodKey = new MethodKey("com.liferay.portlet.sites.util.SitesUtil","applyLayoutSetPrototypes", Group.class, long.class, long.class, ServiceContext.class);
PortalClassInvoker.invoke(false, methodKey, organization.getGroup(), publicLayoutSetId, privateLayoutSetId, serviceContext);
为 publicLayoutSetId 指定 -1。
必须登录管理员才能执行此操作。要在启动时或在后台执行此操作,需要一个新的 ServiceContext。
类似于以下内容
ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setSignedIn(false);
// set the following to an admin user / company or default user
User user = UserLocalServiceUtil.getDefaultUser(companyId); // or any user that has the permissions
serviceContext.setUserId(user.getUserId());
serviceContext.setCompanyId(companyId);
而且很可能您还必须设置 ThreadPermissionChecker
PrincipalThreadLocal.setName(user.getUserId());
PermissionChecker adminPermissionChecker = PermissionCheckerFactoryUtil.create(user, false);
PermissionThreadLocal.setPermissionChecker(adminPermissionChecker);
Don't forget to reset the permission checker in a final block otherwise the same permission checker might be used for other requests on the same thread.