1 53 54 55 package com.Yasna.forum; 56 57 import java.util.*; 58 59 62 public class ForumMessageProxy implements ForumMessage { 63 64 private ForumMessage message; 65 private Authorization authorization; 66 private ForumPermissions permissions; 67 private boolean canReadText; 68 69 73 public ForumMessageProxy(ForumMessage message, Authorization authorization, 74 ForumPermissions permissions) { 75 this.message = message; 76 this.authorization = authorization; 77 this.permissions = permissions; 78 init(); 79 } 80 81 84 private void init() { 85 canReadText = false; 86 boolean isModerator = false; 87 boolean isSystemAdmin = permissions.get(ForumPermissions.SYSTEM_ADMIN); 88 boolean isUserAdmin = permissions.get(ForumPermissions.FORUM_ADMIN); 89 if (permissions != null) { 90 isModerator = permissions.get(ForumPermissions.MODERATOR); 91 } 92 boolean isAdmin = isSystemAdmin || isUserAdmin || isModerator; 93 if (isAdmin || 94 (message.getUser().getID() == authorization.getUserID() && 95 authorization.getUserID() != -1)) { 96 canReadText = true; 97 } else { 98 if (message.isApproved()) { 99 if (message.isPrivate()) { 101 if (message.getReplyPrivateUserId() == 103 authorization.getUserID()) { 104 canReadText = true; 105 } 106 } else { canReadText = true; 108 } 109 } 110 } } 112 113 115 public int getID() { 116 return message.getID(); 117 } 118 119 public Date getCreationDate() { 120 return message.getCreationDate(); 121 } 122 123 public void setCreationDate(Date creationDate) throws UnauthorizedException { 124 if (permissions.isSystemOrForumAdmin()) { 125 this.message.setCreationDate(creationDate); 126 } else { 127 throw new UnauthorizedException(); 128 } 129 } 130 131 public Date getModifiedDate() { 132 return message.getModifiedDate(); 133 } 134 135 public void setModifiedDate(Date modifiedDate) throws UnauthorizedException { 136 if (permissions.isSystemOrForumAdmin()) { 137 this.message.setModifiedDate(modifiedDate); 138 } else { 139 throw new UnauthorizedException(); 140 } 141 } 142 143 public String getSubject() { 144 return message.getSubject(); 145 } 146 147 public int getReplyPrivateUserId() { 148 return message.getReplyPrivateUserId(); 149 } 150 151 public boolean isApproved() { 152 return message.isApproved(); 153 } 154 155 public String getUnfilteredSubject() { 156 return message.getUnfilteredSubject(); 157 } 158 159 public void setSubject(String subject) throws UnauthorizedException { 160 if (permissions.isSystemOrForumAdmin() || 161 getUser().hasPermission(ForumPermissions.USER_ADMIN)) { 162 this.message.setSubject(subject); 163 } else { 164 throw new UnauthorizedException(); 165 } 166 } 167 168 public void setReplyPrivateUserId(int replyPrivateUserId) throws 169 UnauthorizedException { 170 if (permissions.isSystemOrForumAdmin() || 171 getUser().hasPermission(ForumPermissions.USER_ADMIN)) { 172 this.message.setReplyPrivateUserId(replyPrivateUserId); 173 } else { 174 throw new UnauthorizedException(); 175 } 176 } 177 178 public void setApprovment(boolean approved) throws UnauthorizedException { 179 if (permissions.isSystemOrForumAdmin() || 180 permissions.get(ForumPermissions.MODERATOR)) { 181 this.message.setApprovment(approved); 182 } else { 183 throw new UnauthorizedException(); 184 } 185 } 186 187 public String getBody() { 188 if (canReadText) { 189 return message.getBody(); 190 } else { 191 return null; 192 } 193 } 194 195 public boolean isPrivate() { 196 return message.isPrivate(); 197 } 198 199 public String getUnfilteredBody() { 200 if (canReadText) { 201 return message.getUnfilteredBody(); 202 } else { 203 return null; 204 } 205 } 206 207 public void setBody(String body) throws UnauthorizedException { 208 if (permissions.isSystemOrForumAdmin() || 209 getUser().hasPermission(ForumPermissions.USER_ADMIN)) { 210 this.message.setBody(body); 211 } else { 212 throw new UnauthorizedException(); 213 } 214 } 215 216 public User getUser() { 217 User user = message.getUser(); 218 ForumPermissions userPermissions = user.getPermissions(authorization); 219 ForumPermissions newPermissions = 220 new ForumPermissions(permissions, userPermissions); 221 return new UserProxy(user, authorization, newPermissions); 222 } 223 224 public String getProperty(String name) { 225 return message.getProperty(name); 226 } 227 228 public String getUnfilteredProperty(String name) { 229 return message.getUnfilteredProperty(name); 230 } 231 232 public void setProperty(String name, String value) { 233 message.setProperty(name, value); 234 } 235 236 public Iterator propertyNames() { 237 return message.propertyNames(); 238 } 239 240 public boolean isAnonymous() { 241 return message.isAnonymous(); 242 } 243 244 public ForumThread getForumThread() { 245 return message.getForumThread(); 246 } 247 248 public boolean hasPermission(int type) { 249 return permissions.get(type); 250 } 251 252 254 259 public String toString() { 260 return message.toString(); 261 } 262 263 269 public void insertIntoDb(java.sql.Connection con, ForumThread thread) throws 270 java.sql.SQLException { 271 ((com.Yasna.forum.database.DbForumMessage) message).insertIntoDb(con, 272 thread); 273 } 274 public MessageRanking getRanking(){ 275 return message.getRanking(); 276 } 277 public void setRanking(int para) throws UnauthorizedException{ 278 if (!message.getForumThread().getRootMessage().getUser().isAnonymous() && authorization.getUserID() == message.getForumThread().getRootMessage().getUser().getID() && authorization.getUserID() != message.getUser().getID()){ 279 message.setRanking(para); 280 } else { 281 throw new UnauthorizedException("Only the user who created the thread can rank other messages"); 282 } 283 } 284 285 286 } 287 | Popular Tags |