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.util.Iterator;
028:
029: import org.nemesis.forum.Authorization;
030: import org.nemesis.forum.Forum;
031: import org.nemesis.forum.ForumPermissions;
032: import org.nemesis.forum.Group;
033: import org.nemesis.forum.ProfileManager;
034: import org.nemesis.forum.User;
035: import org.nemesis.forum.config.Constants;
036: import org.nemesis.forum.exception.GroupAlreadyExistsException;
037: import org.nemesis.forum.exception.GroupNotFoundException;
038: import org.nemesis.forum.exception.UnauthorizedException;
039: import org.nemesis.forum.exception.UserAlreadyExistsException;
040: import org.nemesis.forum.exception.UserNotFoundException;
041:
042: /**
043: * Protection proxy for the ProfileManager class. It restricts access to
044: * protected methods by throwing UnauthorizedExceptions when necessary.
045: *
046: * @see ProfileManager
047: */
048: public class ProfileManagerProxy implements ProfileManager {
049:
050: private ProfileManager profileManager;
051: private Authorization authorization;
052: private ForumPermissions permissions;
053:
054: /**
055: * Creates a new ProfileManagerProxy.
056: */
057: public ProfileManagerProxy(ProfileManager profileManager,
058: Authorization authorization, ForumPermissions permissions) {
059: this .profileManager = profileManager;
060: this .authorization = authorization;
061: this .permissions = permissions;
062: }
063:
064: public User createUser(String username, String password,
065: String email) throws UserAlreadyExistsException {
066: return profileManager.createUser(username, password, email);
067: }
068:
069: public Group createGroup(String name) throws UnauthorizedException,
070: GroupAlreadyExistsException {
071: if (permissions.get(Constants.SYSTEM_ADMIN)) {
072: Group group = profileManager.createGroup(name);
073: return new GroupProxy(group, authorization, permissions);
074: } else {
075: throw new UnauthorizedException();
076: }
077: }
078:
079: public User getUser(int userID) throws UserNotFoundException {
080: User user = profileManager.getUser(userID);
081: ForumPermissions userPermissions = user
082: .getPermissions(authorization);
083: ForumPermissions newPermissions = new ForumPermissions(
084: permissions, userPermissions);
085: return new UserProxy(user, authorization, newPermissions);
086: }
087:
088: public User getUser(String username) throws UserNotFoundException {
089: User user = profileManager.getUser(username);
090: ForumPermissions userPermissions = user
091: .getPermissions(authorization);
092: ForumPermissions newPermissions = new ForumPermissions(
093: permissions, userPermissions);
094: return new UserProxy(user, authorization, newPermissions);
095: }
096:
097: public User getAnonymousUser() {
098: return profileManager.getAnonymousUser();
099: }
100:
101: public User getSpecialUser() {
102: return profileManager.getSpecialUser();
103: }
104:
105: public Group getGroup(int groupID) throws GroupNotFoundException {
106: Group group = profileManager.getGroup(groupID);
107: ForumPermissions groupPermissions = group
108: .getPermissions(authorization);
109: ForumPermissions newPermissions = new ForumPermissions(
110: permissions, groupPermissions);
111: return new GroupProxy(group, authorization, newPermissions);
112: }
113:
114: public Group getGroup(String name) throws GroupNotFoundException {
115: Group group = profileManager.getGroup(name);
116: ForumPermissions groupPermissions = group
117: .getPermissions(authorization);
118: ForumPermissions newPermissions = new ForumPermissions(
119: permissions, groupPermissions);
120: return new GroupProxy(group, authorization, newPermissions);
121: }
122:
123: public void deleteUser(User user) throws UnauthorizedException {
124: if (permissions.get(Constants.SYSTEM_ADMIN)) {
125: profileManager.deleteUser(user);
126: } else {
127: throw new UnauthorizedException();
128: }
129: }
130:
131: public void deleteGroup(Group group) throws UnauthorizedException {
132: if (permissions.get(Constants.SYSTEM_ADMIN)) {
133: profileManager.deleteGroup(group);
134: } else {
135: throw new UnauthorizedException();
136: }
137: }
138:
139: public int getUserCount() {
140: return profileManager.getUserCount();
141: }
142:
143: public int getGroupCount() {
144: return profileManager.getGroupCount();
145: }
146:
147: public Iterator users() {
148: Iterator iterator = profileManager.users();
149: return new UserIteratorProxy(iterator, authorization,
150: permissions);
151: }
152:
153: public Iterator users(int startIndex, int numResults) {
154: Iterator iterator = profileManager
155: .users(startIndex, numResults);
156: return new UserIteratorProxy(iterator, authorization,
157: permissions);
158: }
159:
160: public Iterator groups() {
161: Iterator iterator = profileManager.groups();
162: return new GroupIteratorProxy(iterator, authorization,
163: permissions);
164: }
165:
166: public Iterator groups(int startIndex, int numResults) {
167: Iterator iterator = profileManager.groups(startIndex,
168: numResults);
169: return new GroupIteratorProxy(iterator, authorization,
170: permissions);
171: }
172:
173: public int userMessageCount(User user, Forum forum) {
174: return profileManager.userMessageCount(user, forum);
175: }
176:
177: public Iterator userMessages(User user, Forum forum) {
178: Iterator iterator = profileManager.userMessages(user, forum);
179: return new MessageIteratorProxy(iterator, authorization,
180: permissions);
181: }
182: }
|