1 25 package org.nemesis.forum.proxy; 26 27 import java.util.Iterator ; 28 29 import org.nemesis.forum.Authorization; 30 import org.nemesis.forum.Forum; 31 import org.nemesis.forum.ForumFactory; 32 import org.nemesis.forum.ForumPermissions; 33 import org.nemesis.forum.ProfileManager; 34 import org.nemesis.forum.config.Constants; 35 import org.nemesis.forum.exception.ForumAlreadyExistsException; 36 import org.nemesis.forum.exception.ForumNotFoundException; 37 import org.nemesis.forum.exception.UnauthorizedException; 38 39 43 public class ForumFactoryProxy extends ForumFactory { 44 45 private ForumFactory factory; 46 private Authorization authorization; 47 private ForumPermissions permissions; 48 49 public ForumFactoryProxy(ForumFactory factory, Authorization authorization, ForumPermissions permissions) { 50 this.factory = factory; 51 this.authorization = authorization; 52 this.permissions = permissions; 53 } 54 55 public Forum createForum(String name, String description) throws UnauthorizedException, ForumAlreadyExistsException { 56 if (permissions.get(Constants.SYSTEM_ADMIN)) { 57 Forum newForum = factory.createForum(name, description); 58 return new ForumProxy(newForum, authorization, permissions); 59 } else { 60 throw new UnauthorizedException(); 61 } 62 } 63 64 public void deleteForum(Forum forum) throws UnauthorizedException { 65 if (permissions.get(Constants.SYSTEM_ADMIN)) { 66 factory.deleteForum(forum); 67 } else { 68 throw new UnauthorizedException(); 69 } 70 } 71 72 public Forum getForum(int forumID) throws ForumNotFoundException, UnauthorizedException { 73 Forum forum = factory.getForum(forumID); 74 ForumPermissions forumPermissions = forum.getPermissions(authorization); 75 78 80 ForumPermissions newPermissions = ( forumPermissions); 81 if (!(newPermissions.get(Constants.READ) 84 || newPermissions.get(Constants.FORUM_ADMIN) 85 || newPermissions.get(Constants.MODERATOR) 86 || newPermissions.get(Constants.SYSTEM_ADMIN))) { 87 throw new UnauthorizedException(); 88 } 89 return new ForumProxy(forum, authorization, newPermissions); 90 } 91 92 public Forum getForum(String name) throws ForumNotFoundException, UnauthorizedException { 93 Forum forum = factory.getForum(name); 94 ForumPermissions forumPermissions = forum.getPermissions(authorization); 95 99 ForumPermissions newPermissions = ( forumPermissions); 100 if (!(newPermissions.get(Constants.READ) 103 || newPermissions.get(Constants.FORUM_ADMIN) 104 || newPermissions.get(Constants.MODERATOR) 105 || newPermissions.get(Constants.SYSTEM_ADMIN))) { 106 throw new UnauthorizedException(); 107 } 108 return new ForumProxy(forum, authorization, newPermissions); 109 } 110 111 public int getForumCount() { 112 return factory.getForumCount(); 113 } 114 115 public Iterator forums() { 116 return new ForumIteratorProxy(factory.forums(), authorization, permissions); 117 } 118 public Iterator forumsModeration() { 119 return new ForumModeratorIteratorProxy(factory.forums(), authorization, permissions); 120 } 121 122 public ProfileManager getProfileManager() { 123 ProfileManager profileManager = factory.getProfileManager(); 124 return new ProfileManagerProxy(profileManager, authorization, permissions); 125 } 126 127 134 135 public int[] usersWithPermission(int permissionType) throws UnauthorizedException { 136 if (permissions.get(Constants.SYSTEM_ADMIN)) { 137 return factory.usersWithPermission(permissionType); 138 } else { 139 throw new UnauthorizedException(); 140 } 141 } 142 143 public int[] groupsWithPermission(int permissionType) throws UnauthorizedException { 144 if (permissions.get(Constants.SYSTEM_ADMIN)) { 145 return factory.groupsWithPermission(permissionType); 146 } else { 147 throw new UnauthorizedException(); 148 } 149 } 150 151 public ForumPermissions getPermissions(Authorization authorization) { 152 return factory.getPermissions(authorization); 153 } 154 155 public boolean hasPermission(int type) { 156 return permissions.get(type); 157 } 158 159 168 public ForumFactory getUnderlyingForumFactory() throws UnauthorizedException { 169 if (permissions.get(Constants.SYSTEM_ADMIN)) { 170 return factory; 171 } else { 172 throw new UnauthorizedException(); 173 } 174 } 175 } 176 | Popular Tags |