有什么想法可以根据用户所属的用户组隐藏或显示对话框选项卡面板吗?
我试图通过 CRX 内容浏览器(ACL)来做到这一点。但我运气不好。
干杯
正如anthonyh所指出的,ACL 方法是可行的方法(如果确实需要这种行为)。
例如,要隐藏基本页面组件的“图像”选项卡:
/libs/foundation/components/page/dialog/items/tabs/items/image
deny jcr:read
为author
请注意,如果包含选项卡,xtype=cqinclude
您必须在包含本身上设置它,而不是在包含的定义上。因为在运行时它会抱怨缺少的包含目标并且根本不呈现对话框。
这可以通过自定义 servlet 和对话事件侦听器来完成。
侦听器函数向 servlet 发出请求,传递当前用户 ID 和所需组。然后可以根据 servlet 响应隐藏对话框选项卡。
这是 CQ5 组件的示例 dialog.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root
xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Dialog"
xtype="dialog">
<listeners jcr:primaryType="nt:unstructured"
loadcontent="function(dialog) {
var url = '/bin/member.json';
// check if current user belongs to administrators group
url = CQ.HTTP.addParameter(url, 'userId', CQ.User.getUserID());
url = CQ.HTTP.addParameter(url, 'groupId', 'administrators');
var result = CQ.HTTP.eval(url);
if (!result.isMember) {
// hide "tab2" if user is not an administrator
dialog.findByType('tabpanel')[0].hideTabStripItem(1);
}
}" />
<items jcr:primaryType="cq:WidgetCollection">
<tab1 jcr:primaryType="cq:Widget" title="Text" xtype="panel">
<items jcr:primaryType="cq:WidgetCollection">
...
</items>
</tab1>
<tab2 jcr:primaryType="cq:Widget" title="Image" xtype="panel">
<items jcr:primaryType="cq:WidgetCollection">
...
</items>
</tab2>
</items>
</jcr:root>
这是相应的servlet:
import java.io.IOException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.security.Group;
import com.day.cq.security.User;
import com.day.cq.security.UserManager;
import com.google.common.collect.ImmutableMap;
@Component(immediate = true)
@Service
@Properties({
@Property(name = "service.description", value = "Group Member servlet checks if a user is a member of a group."),
@Property(name = "sling.servlet.paths", value = "/bin/member")
})
public class GroupMemberServlet extends SlingAllMethodsServlet {
/** Required. */
private static final long serialVersionUID = 1L;
/** Logger */
private static final Logger LOG = LoggerFactory.getLogger(GroupMemberServlet.class);
private static final JsonFactory FACTORY = new JsonFactory().disable(Feature.AUTO_CLOSE_TARGET);
private static final ObjectMapper MAPPER = new ObjectMapper();
@Override
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
final UserManager userManager = request.getResourceResolver().adaptTo(UserManager.class);
final String userId = request.getRequestParameter("userId").getString();
final String groupId = request.getRequestParameter("groupId").getString();
final Group group = (Group) userManager.get(groupId);
final User user = (User) userManager.get(userId);
writeJsonResponse(response, ImmutableMap.of("isMember", group.isMember(user)));
}
private void writeJsonResponse(final SlingHttpServletResponse response, final Object object) {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
try {
final JsonGenerator generator = FACTORY.createJsonGenerator(response.getWriter());
MAPPER.writeValue(generator, object);
} catch (final JsonGenerationException jge) {
LOG.error("error generating JSON response", jge);
} catch (final JsonMappingException jme) {
LOG.error("error mapping JSON response", jme);
} catch (final IOException ioe) {
LOG.error("error writing JSON response", ioe);
}
}
}
一个问题浮现在脑海中……为什么要限制对创作对话框的控制并删除选项卡?
ACL 没有理由不能为此工作。您是否将它们设置为对选项卡有足够的限制?您是否使用非管理员用户进行测试?我会谨慎使用代码量如此之大的东西来解决访问问题。
就个人而言,如果 ACL 不能像预期的那样工作,我会探索基于 tabpanel xtype 创建一个新的小部件,而不是最终可能特定于 CQ5 的一个版本的代码解决方案。
我的回答:使用 ACL。
请看一下这些模糊相关的官方文件 - 相同的原则但不同的目标:
http://dev.day.com/content/kb/home/cq5/CQ5SystemAdministration/CQ53HowToHideCQNavigationButtons.html
和
http://dev.day.com/docs/en/cq/current/administering/security.html