001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025: package org.nemesis.forum.proxy;
026:
027: import java.sql.SQLException;
028: import java.util.Date;
029: import java.util.Iterator;
030:
031: import org.nemesis.forum.Authorization;
032: import org.nemesis.forum.ForumPermissions;
033: import org.nemesis.forum.ForumThread;
034: import org.nemesis.forum.Message;
035: import org.nemesis.forum.User;
036: import org.nemesis.forum.config.Constants;
037: import org.nemesis.forum.exception.UnauthorizedException;
038:
039: /**
040: * A protection proxy for ForumMessage objects.
041: */
042: public class MessageProxy implements Message {
043:
044: private Message message;
045: private Authorization authorization;
046: private ForumPermissions permissions;
047:
048: /**
049: * Creates a new ForumMessageProxy to protect the supplied message with
050: * the specified permissions
051: */
052: public MessageProxy(Message message, Authorization authorization,
053: ForumPermissions permissions) {
054: this .message = message;
055: this .authorization = authorization;
056: this .permissions = permissions;
057: }
058:
059: //FROM THE FORUMMESSAGE INTERFACE//
060:
061: public int getID() {
062: return message.getID();
063: }
064:
065: public boolean isApproved() {
066: return message.isApproved();
067: }
068:
069: public void setApproved(boolean approved)
070: throws UnauthorizedException {
071: if (permissions.isSystemOrForumAdmin()
072: || permissions.get(Constants.MODERATOR)) {
073: message.setApproved(approved);
074: } else {
075: throw new UnauthorizedException();
076: }
077: }
078:
079: public Date getCreationDate() {
080: return message.getCreationDate();
081: }
082:
083: public void setCreationDate(Date creationDate)
084: throws UnauthorizedException {
085: if (permissions.isSystemOrForumAdmin()) {
086: this .message.setCreationDate(creationDate);
087: } else {
088: throw new UnauthorizedException();
089: }
090: }
091:
092: public Date getModifiedDate() {
093: return message.getModifiedDate();
094: }
095:
096: public void setModifiedDate(Date modifiedDate)
097: throws UnauthorizedException {
098: if (permissions.isSystemOrForumAdmin()) {
099: this .message.setModifiedDate(modifiedDate);
100: } else {
101: throw new UnauthorizedException();
102: }
103: }
104:
105: public String getSubject() {
106: return message.getSubject();
107: }
108:
109: public String getUnfilteredSubject() {
110: return message.getUnfilteredSubject();
111: }
112:
113: public void setSubject(String subject) throws UnauthorizedException {
114: if (permissions.isSystemOrForumAdmin()
115: || getUser().hasPermission(Constants.USER_ADMIN)) {
116: this .message.setSubject(subject);
117: } else {
118: throw new UnauthorizedException();
119: }
120: }
121:
122: public String getBody() {
123: return message.getBody();
124: }
125:
126: public String getUnfilteredBody() {
127: return message.getUnfilteredBody();
128: }
129:
130: public void setBody(String body) throws UnauthorizedException {
131: if (permissions.isSystemOrForumAdmin()
132: || getUser().hasPermission(Constants.USER_ADMIN)) {
133: this .message.setBody(body);
134: } else {
135: throw new UnauthorizedException();
136: }
137: }
138:
139: public User getUser() {
140: User user = message.getUser();
141: ForumPermissions userPermissions = user
142: .getPermissions(authorization);
143: ForumPermissions newPermissions = new ForumPermissions(
144: permissions, userPermissions);
145: return new UserProxy(user, authorization, newPermissions);
146: }
147:
148: public String getProperty(String name) {
149: return message.getProperty(name);
150: }
151:
152: public String getUnfilteredProperty(String name) {
153: return message.getUnfilteredProperty(name);
154: }
155:
156: public void setProperty(String name, String value) {
157: message.setProperty(name, value);
158: }
159:
160: public Iterator propertyNames() {
161: return message.propertyNames();
162: }
163:
164: public boolean isAnonymous() {
165: return message.isAnonymous();
166: }
167:
168: public ForumThread getForumThread() {
169: return message.getForumThread();
170: }
171:
172: public boolean hasPermission(int type) {
173: return permissions.get(type);
174: }
175:
176: //OTHER METHODS//
177:
178: /**
179: * Converts the object to a String by returning the subject of the message.
180: * This functionality is primarily for Java applications that might be
181: * accessing CoolForum objects through a GUI.
182: */
183: public String toString() {
184: return message.toString();
185: }
186:
187: /**
188: * Small violation of our pluggable backend architecture so that database
189: * insertions can be made more efficiently and transactional. The fact
190: * that this violation is needed probably means that the proxy architecture
191: * needs to be adjusted a bit.
192: *
193: */
194: public void insertIntoDb(java.sql.Connection con, ForumThread thread)
195: throws SQLException {
196: ((org.nemesis.forum.impl.DbForumMessage) message).insertIntoDb(
197: con, thread);
198: }
199:
200: /**
201: *
202: * @author dlaurent
203: *
204: * another violation, need uml diagram ....
205: */
206: public void setApproved2(boolean approved)
207: throws UnauthorizedException {
208: message.setApproved(approved);
209: }
210:
211: }
|