/*
* NEMESIS-FORUM.
* Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
*
* Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
*
* Copyright (C) 2001 Yasna.com. All rights reserved.
*
* Copyright (C) 2000 CoolServlets.com. All rights reserved.
*
* NEMESIS-FORUM. is free software; you can redistribute it and/or
* modify it under the terms of the Apache Software License, Version 1.1,
* or (at your option) any later version.
*
* NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
* application are parts of NEMESIS-FORUM and are distributed under
* same terms of licence.
*
*
* NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
* and software developed by CoolServlets.com (http://www.coolservlets.com).
* and software developed by Yasna.com (http://www.yasna.com).
*
*/
package org.nemesis.forum.proxy;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import org.nemesis.forum.Authorization;
import org.nemesis.forum.ForumPermissions;
import org.nemesis.forum.ForumThread;
import org.nemesis.forum.Message;
import org.nemesis.forum.User;
import org.nemesis.forum.config.Constants;
import org.nemesis.forum.exception.UnauthorizedException;
/**
* A protection proxy for ForumMessage objects.
*/
public class MessageProxy implements Message {
private Message message;
private Authorization authorization;
private ForumPermissions permissions;
/**
* Creates a new ForumMessageProxy to protect the supplied message with
* the specified permissions
*/
public MessageProxy(Message message, Authorization authorization, ForumPermissions permissions) {
this.message = message;
this.authorization = authorization;
this.permissions = permissions;
}
//FROM THE FORUMMESSAGE INTERFACE//
public int getID() {
return message.getID();
}
public boolean isApproved() {
return message.isApproved();
}
public void setApproved(boolean approved) throws UnauthorizedException {
if (permissions.isSystemOrForumAdmin() || permissions.get(Constants.MODERATOR)) {
message.setApproved(approved);
} else {
throw new UnauthorizedException();
}
}
public Date getCreationDate() {
return message.getCreationDate();
}
public void setCreationDate(Date creationDate) throws UnauthorizedException {
if (permissions.isSystemOrForumAdmin()) {
this.message.setCreationDate(creationDate);
} else {
throw new UnauthorizedException();
}
}
public Date getModifiedDate() {
return message.getModifiedDate();
}
public void setModifiedDate(Date modifiedDate) throws UnauthorizedException {
if (permissions.isSystemOrForumAdmin()) {
this.message.setModifiedDate(modifiedDate);
} else {
throw new UnauthorizedException();
}
}
public String getSubject() {
return message.getSubject();
}
public String getUnfilteredSubject() {
return message.getUnfilteredSubject();
}
public void setSubject(String subject) throws UnauthorizedException {
if (permissions.isSystemOrForumAdmin() || getUser().hasPermission(Constants.USER_ADMIN)) {
this.message.setSubject(subject);
} else {
throw new UnauthorizedException();
}
}
public String getBody() {
return message.getBody();
}
public String getUnfilteredBody() {
return message.getUnfilteredBody();
}
public void setBody(String body) throws UnauthorizedException {
if (permissions.isSystemOrForumAdmin() || getUser().hasPermission(Constants.USER_ADMIN)) {
this.message.setBody(body);
} else {
throw new UnauthorizedException();
}
}
public User getUser() {
User user = message.getUser();
ForumPermissions userPermissions = user.getPermissions(authorization);
ForumPermissions newPermissions = new ForumPermissions(permissions, userPermissions);
return new UserProxy(user, authorization, newPermissions);
}
public String getProperty(String name) {
return message.getProperty(name);
}
public String getUnfilteredProperty(String name) {
return message.getUnfilteredProperty(name);
}
public void setProperty(String name, String value) {
message.setProperty(name, value);
}
public Iterator propertyNames() {
return message.propertyNames();
}
public boolean isAnonymous() {
return message.isAnonymous();
}
public ForumThread getForumThread() {
return message.getForumThread();
}
public boolean hasPermission(int type) {
return permissions.get(type);
}
//OTHER METHODS//
/**
* Converts the object to a String by returning the subject of the message.
* This functionality is primarily for Java applications that might be
* accessing CoolForum objects through a GUI.
*/
public String toString() {
return message.toString();
}
/**
* Small violation of our pluggable backend architecture so that database
* insertions can be made more efficiently and transactional. The fact
* that this violation is needed probably means that the proxy architecture
* needs to be adjusted a bit.
*
*/
public void insertIntoDb(java.sql.Connection con, ForumThread thread) throws SQLException {
((org.nemesis.forum.impl.DbForumMessage) message).insertIntoDb(con, thread);
}
/**
*
* @author dlaurent
*
* another violation, need uml diagram ....
*/
public void setApproved2(boolean approved) throws UnauthorizedException {
message.setApproved(approved);
}
}