1 25 package org.nemesis.forum.proxy; 26 27 import java.util.Date ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 31 import org.nemesis.forum.Authorization; 32 import org.nemesis.forum.Forum; 33 import org.nemesis.forum.ForumPermissions; 34 import org.nemesis.forum.ForumThread; 35 import org.nemesis.forum.Group; 36 import org.nemesis.forum.Message; 37 import org.nemesis.forum.MessageFilter; 38 import org.nemesis.forum.User; 39 import org.nemesis.forum.config.Constants; 40 import org.nemesis.forum.exception.ForumAlreadyExistsException; 41 import org.nemesis.forum.exception.ForumMessageNotFoundException; 42 import org.nemesis.forum.exception.ForumThreadNotFoundException; 43 import org.nemesis.forum.exception.UnauthorizedException; 44 45 56 public class ForumProxy implements Forum { 57 58 private Forum forum; 59 private Authorization authorization; 60 private ForumPermissions permissions; 61 62 69 public ForumProxy(Forum forum, Authorization authorization, ForumPermissions permissions) { 70 this.forum = forum; 71 this.authorization = authorization; 72 this.permissions = permissions; 73 } 74 75 77 public int getModerationType(){ 79 return forum.getModerationType(); 80 } 81 82 public void setModerationType(int type) throws UnauthorizedException{ 83 if (permissions.isSystemOrForumAdmin()) { 84 forum.setModerationType(type); 85 } else { 86 throw new UnauthorizedException(); 87 } 88 } 89 90 91 92 public int getID() { 93 return forum.getID(); 94 } 95 96 public String getName() { 97 return forum.getName(); 98 } 99 100 public void setName(String name) throws UnauthorizedException, ForumAlreadyExistsException { 101 if (permissions.isSystemOrForumAdmin()) { 102 forum.setName(name); 103 } else { 104 throw new UnauthorizedException(); 105 } 106 } 107 108 public String getDescription() { 109 return forum.getDescription(); 110 } 111 112 public void setDescription(String description) throws UnauthorizedException { 113 if (permissions.isSystemOrForumAdmin()) { 114 forum.setDescription(description); 115 } else { 116 throw new UnauthorizedException(); 117 } 118 } 119 120 public Date getCreationDate() { 121 return forum.getCreationDate(); 122 } 123 124 public void setCreationDate(Date creationDate) throws UnauthorizedException { 125 if (permissions.isSystemOrForumAdmin()) { 126 forum.setCreationDate(creationDate); 127 } else { 128 throw new UnauthorizedException(); 129 } 130 } 131 132 public Date getModifiedDate() { 133 return forum.getModifiedDate(); 134 } 135 136 public void setModifiedDate(Date modifiedDate) throws UnauthorizedException { 137 if (permissions.isSystemOrForumAdmin()) { 138 forum.setModifiedDate(modifiedDate); 139 } else { 140 throw new UnauthorizedException(); 141 } 142 } 143 144 public String getProperty(String name) { 145 return forum.getProperty(name); 146 } 147 148 public void setProperty(String name, String value) throws UnauthorizedException { 149 if (permissions.isSystemOrForumAdmin()) { 150 forum.setProperty(name, value); 151 } else { 152 throw new UnauthorizedException(); 153 } 154 } 155 156 public Enumeration propertyNames() { 157 return forum.propertyNames(); 158 } 159 160 public ForumThread createThread(Message rootMessage) throws UnauthorizedException { 161 if (permissions.get(Constants.CREATE_THREAD)) { 162 ForumThread thread = forum.createThread(rootMessage); 163 return new ForumThreadProxy(thread, authorization, permissions); 164 } else { 165 throw new UnauthorizedException(); 166 } 167 } 168 169 public Message createMessage(User user) throws UnauthorizedException { 170 if (permissions.get(Constants.CREATE_MESSAGE) || permissions.get(Constants.CREATE_THREAD)) { 171 if (user.hasPermission(Constants.USER_ADMIN) || user.isAnonymous()) { 174 Message message = forum.createMessage(user); 175 return new MessageProxy(message, authorization, permissions); 176 } else { 177 throw new UnauthorizedException(); 178 } 179 180 } else { 181 throw new UnauthorizedException(); 182 } 183 } 184 185 public Message getMessage(int messageID) throws ForumMessageNotFoundException { 186 Message m = forum.getMessage(messageID); 187 return new MessageProxy(m, authorization, permissions); 189 } 190 191 public void deleteThread(ForumThread thread) throws UnauthorizedException { 192 if (permissions.isSystemOrForumAdmin() || permissions.get(Constants.MODERATOR)) { 193 forum.deleteThread(thread); 194 } else { 195 throw new UnauthorizedException(); 196 } 197 } 198 199 public void moveThread(ForumThread thread, Forum newForum) throws UnauthorizedException, IllegalArgumentException { 200 if (permissions.isSystemOrForumAdmin() 202 && (newForum.hasPermission(Constants.SYSTEM_ADMIN) || newForum.hasPermission(Constants.FORUM_ADMIN))) { 203 forum.moveThread(thread, newForum); 204 } else { 205 throw new UnauthorizedException(); 206 } 207 } 208 209 public void addThread(ForumThread thread) throws UnauthorizedException { 210 if (permissions.get(Constants.CREATE_THREAD)) { 211 forum.addThread(thread); 212 } else { 213 throw new UnauthorizedException(); 214 } 215 } 216 217 public ForumThread getThread(int threadID) throws ForumThreadNotFoundException { 218 ForumThread thread = forum.getThread(threadID); 219 return new ForumThreadProxy(thread, authorization, permissions); 221 } 222 223 public Iterator threads() { 224 Iterator iterator = forum.threads(); 225 return new ForumThreadIteratorProxy(iterator, authorization, permissions); 226 } 227 228 public Iterator threads(int startIndex, int numResults) { 229 Iterator iterator = forum.threads(startIndex, numResults); 230 return new ForumThreadIteratorProxy(iterator, authorization, permissions); 231 } 232 233 public Iterator threads(boolean approved) { 234 Iterator iterator = forum.threads(approved); 235 return new ForumThreadIteratorProxy(iterator, authorization, permissions); 236 } 237 238 public Iterator threads(boolean approved,int startIndex, int numResults) { 239 Iterator iterator = forum.threads(approved,startIndex, numResults); 240 return new ForumThreadIteratorProxy(iterator, authorization, permissions); 241 } 242 243 public int getThreadCount() { 244 return forum.getThreadCount(); 245 } 246 247 public int getMessageCount() { 248 return forum.getMessageCount(); 249 } 250 251 public int getMessageCount(boolean approved) { 252 return forum.getMessageCount(approved); 253 } 254 255 public int getThreadCount(boolean approved) { 256 return forum.getThreadCount(approved); 257 } 258 259 260 261 262 263 public void addUserPermission(User user, int permissionType) throws UnauthorizedException { 264 if (permissionType == Constants.SYSTEM_ADMIN) { 267 throw new UnauthorizedException(); 268 } 269 if (permissions.isSystemOrForumAdmin()) { 270 forum.addUserPermission(user, permissionType); 271 } else { 272 throw new UnauthorizedException(); 273 } 274 } 275 276 public void removeUserPermission(User user, int permissionType) throws UnauthorizedException { 277 if (permissions.isSystemOrForumAdmin()) { 278 forum.removeUserPermission(user, permissionType); 279 } else { 280 throw new UnauthorizedException(); 281 } 282 } 283 284 public int[] usersWithPermission(int permissionType) throws UnauthorizedException { 285 if (permissions.isSystemOrForumAdmin()) { 286 return forum.usersWithPermission(permissionType); 287 } else { 288 throw new UnauthorizedException(); 289 } 290 } 291 292 public void addGroupPermission(Group group, int permissionType) throws UnauthorizedException { 293 if (permissionType == Constants.SYSTEM_ADMIN) { 296 throw new UnauthorizedException(); 297 } 298 if (permissions.isSystemOrForumAdmin()) { 299 forum.addGroupPermission(group, permissionType); 300 } else { 301 throw new UnauthorizedException(); 302 } 303 } 304 305 public void removeGroupPermission(Group group, int permissionType) throws UnauthorizedException { 306 if (permissions.isSystemOrForumAdmin()) { 307 forum.removeGroupPermission(group, permissionType); 308 } else { 309 throw new UnauthorizedException(); 310 } 311 } 312 313 public int[] groupsWithPermission(int permissionType) throws UnauthorizedException { 314 if (permissions.isSystemOrForumAdmin()) { 315 return forum.groupsWithPermission(permissionType); 316 } else { 317 throw new UnauthorizedException(); 318 } 319 } 320 321 public MessageFilter[] getForumMessageFilters() throws UnauthorizedException { 322 if (permissions.isSystemOrForumAdmin()) { 323 return forum.getForumMessageFilters(); 324 } else { 325 throw new UnauthorizedException(); 326 } 327 } 328 329 public void addForumMessageFilter(MessageFilter filter) throws UnauthorizedException { 330 if (permissions.isSystemOrForumAdmin()) { 331 forum.addForumMessageFilter(filter); 332 } else { 333 throw new UnauthorizedException(); 334 } 335 } 336 337 public void addForumMessageFilter(MessageFilter filter, int index) throws UnauthorizedException { 338 if (permissions.isSystemOrForumAdmin()) { 339 forum.addForumMessageFilter(filter, index); 340 } else { 341 throw new UnauthorizedException(); 342 } 343 } 344 345 public void removeForumMessageFilter(int index) throws UnauthorizedException { 346 if (permissions.isSystemOrForumAdmin()) { 347 forum.removeForumMessageFilter(index); 348 } else { 349 throw new UnauthorizedException(); 350 } 351 } 352 353 public Message applyFilters(Message message) { 354 return forum.applyFilters(message); 355 } 356 357 public ForumPermissions getPermissions(Authorization authorization) { 358 return forum.getPermissions(authorization); 359 } 360 361 public boolean hasPermission(int type) { 362 return permissions.get(type); 363 } 364 365 public String toString() { 366 return forum.toString(); 367 } 368 } 369 | Popular Tags |