public IteratorProxy(int type, Iterator iterator, Authorization authorization,
ForumPermissions permissions)
{
this.iterator = iterator;
this.authorization = authorization;
this.permissions = permissions;
// Load the appropriate proxy factory depending on the type of object
// that we're iterating through. Each proxy factory is responsible
// for checking that the user has permission to view the object, and
// then wrapping it with an appropriate proxy.
switch (type) {
// CATEGORY
case JiveGlobals.FORUM_CATEGORY:
// Create a class that wraps forums with proxies.
proxyFactory = new ProxyFactory() {
public Object createProxy(Object obj, Authorization auth,
ForumPermissions perms)
{
ForumCategory category = (ForumCategory)obj;
// Create a new permissions object with the combination
// of the permissions of this object and tempPermissions.
int parentPerms = perms.toInt();
// Never inherit the show category permission.
ForumPermissions newPerms = new ForumPermissions(
ForumPermissions.setBit(parentPerms,
ForumPermissions.SHOW_CATEGORY, false));
ForumPermissions catPerms = category.getPermissions(auth);
newPerms = new ForumPermissions(catPerms, newPerms);
// Return the object if the user has permission.
if (newPerms.get(ForumPermissions.READ_FORUM) ||
newPerms.get(ForumPermissions.SHOW_CATEGORY) ||
newPerms.get(ForumPermissions.MODERATE_MESSAGES) ||
newPerms.get(ForumPermissions.MODERATE_THREADS) ||
newPerms.get(ForumPermissions.FORUM_ADMIN) ||
newPerms.get(ForumPermissions.CATEGORY_ADMIN) ||
newPerms.get(ForumPermissions.SYSTEM_ADMIN))
{
return new ForumCategoryProxy(category, auth, newPerms);
}
// Otherwise return null.
else {
return null;
}
}
};
break;
分析:假设存在Category 1,2,3,4,5,其中Category2只允许注册用户访问。现在是匿名访问。则构造函数中传入
的Iterator包含了全部的Category对象,在匿名类的createProxy()中判断了权限,如果编历到category 2,毫无疑问会
返回null(这样,category2被过滤了)。匿名类的createProxy()将被iteratorProxy的getNextElement()调用,代码如下
:
public Object getNextElement() {
while (iterator.hasNext()) {
Object element = proxyFactory.createProxy(iterator.next(),
authorization, permissions);
if (element != null) {
return element;
}
}
return null;
}
这里的while()将跳过被过滤的对象(其实是null对象咯),返回当前auth可见的Category对象。
再观察ForumCategory接口,只有categories()方法,而没有categories(ResultFilter),说明没有做分页处理。
如果我们添加分页处理,得注意由于iteratorProxy的过滤,而导致实际得到的Category数目和page size的不一致。
比如:有30个Category,我一个categories(resultFilter)调用返回了15个,经过IteratorProxy的时候,被过滤掉了1个
这样实际上只有14个Category了,这样会让JSP迷惑,觉得ResultFilter.setPageSize()不对了。
附:本来是很长的一篇笔记,花费了一个多小时,想不到提交表单的时候,IE崩溃了!欲哭无泪!查出来罪魁祸首是
superbar.dll,不知道是什么垃圾,全面清剿中。。。。
本文地址:http://com.8s8s.com/it/it17762.htm