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:
026: package org.nemesis.forum.util;
027:
028: import java.util.Iterator;
029:
030: import org.nemesis.forum.Authorization;
031: import org.nemesis.forum.Forum;
032: import org.nemesis.forum.ForumFactory;
033: import org.nemesis.forum.ForumPermissions;
034: import org.nemesis.forum.Group;
035: import org.nemesis.forum.ProfileManager;
036: import org.nemesis.forum.config.Constants;
037:
038: /**
039: * @author dlaurent
040: *
041: */
042: public class SecurityTools {
043:
044: /**
045: * Returns true if the user is a system administrator.
046: *
047: * @param authToken the authentication token of the user
048: * @return true if the user is a system administrator, false otherwise.
049: */
050: public static boolean isSystemAdmin(Authorization authToken) {
051: ForumFactory forumFactory = ForumFactory.getInstance(authToken);
052: ForumPermissions permissions = forumFactory
053: .getPermissions(authToken);
054: return permissions.get(Constants.SYSTEM_ADMIN);
055: }
056:
057: /**
058: * Returns true if the user is a forum adminstrator of any forum in the
059: * system. For example, if there are 3 forums in the system and the user
060: * is an adminstrator of any one or more of them, this method will return
061: * true.<p>
062: *
063: * Use the method <code>isForumAdmin( Authorization, Forum)</code> to
064: * check an individual forum for administrator status.)
065: *
066: * @param authToken the authentication token of the user
067: * @return true if the user is a forum administrator of any forum in the system.
068: */
069: public static boolean isForumAdmin(Authorization authToken) {
070:
071: if (isSystemAdmin(authToken))
072: return true;
073:
074: ForumFactory forumFactory = ForumFactory.getInstance(authToken);
075:
076: Iterator forumIterator = forumFactory.forums();
077: if (!forumIterator.hasNext()) {
078: return false;
079: }
080: while (forumIterator.hasNext()) {
081: Forum forum = (Forum) forumIterator.next();
082: if (forum.hasPermission(Constants.FORUM_ADMIN)) {
083: return true;
084: }
085: }
086: return false;
087: }
088:
089: /**
090: * Returns true if the user is a forum moderator of any forum in the
091: * system. For example, if there are 3 forums in the system and the user
092: * is a moderator of any one or more of them, this method will return
093: * true.<p>
094: *
095: * Use the method <code>isForumModerator( Authorization, Forum)</code> to
096: * check an individual forum for moderator status.)
097: *
098: * @param authToken the authentication token of the user
099: * @return true if the user is a forum moderator of any forum in the system.
100: */
101: public static boolean isForumModerator(Authorization authToken) {
102:
103: if (isSystemAdmin(authToken))
104: return true;
105:
106: ForumFactory forumFactory = ForumFactory.getInstance(authToken);
107: Iterator forumIterator = forumFactory.forums();
108: if (!forumIterator.hasNext()) {
109: return false;
110: }
111: while (forumIterator.hasNext()) {
112: Forum forum = (Forum) forumIterator.next();
113: if (forum.hasPermission(Constants.MODERATOR)) {
114: return true;
115: }
116: }
117: return false;
118: }
119:
120: /**
121: * Returns true if the user is a forum adminstrator of the given forum.
122: *
123: * @param authToken the authentication token of the user
124: * @param forum the forum to check administrator status on.
125: * @return true if the user is a forum administrator of the given forum.
126: */
127: public static boolean isForumAdmin(Authorization authToken,
128: Forum forum) {
129: if (isSystemAdmin(authToken))
130: return true;
131:
132: return (forum.hasPermission(Constants.FORUM_ADMIN));
133: }
134:
135: /**
136: * Returns true if the user is a forum moderator of the given forum.
137: *
138: * @param authToken the authentication token of the user
139: * @param forum the forum to check moderator status on.
140: * @return true if the user is a forum moderator of the given forum.
141: */
142: public static boolean isForumModerator(Authorization authToken,
143: Forum forum) {
144: if (isSystemAdmin(authToken))
145: return true;
146:
147: return (forum.hasPermission(Constants.MODERATOR));
148: }
149:
150: /**
151: * Returns true if the user is a group administrator of any group in the
152: * system. For example, if there are 3 groups in the system and the user
153: * is an adminstrator of any one or more of them, this method will return
154: * true.<p>
155: *
156: * Use the method <code>isGroupAdmin( Authorization, Group)</code> to check
157: * an individual group for administrator status.)
158: *
159: */
160: public static boolean isGroupAdmin(Authorization authToken) {
161: if (isSystemAdmin(authToken))
162: return true;
163:
164: ForumFactory forumFactory = ForumFactory.getInstance(authToken);
165: ProfileManager manager = forumFactory.getProfileManager();
166: Iterator groupIterator = manager.groups();
167: if (!groupIterator.hasNext()) {
168: return false;
169: }
170: while (groupIterator.hasNext()) {
171: Group group = (Group) groupIterator.next();
172: if (group.hasPermission(Constants.GROUP_ADMIN)) {
173: return true;
174: }
175: }
176: return false;
177: }
178:
179: /**
180: * Returns true if the user is a group administrator of the given group.
181: *
182: * @param authToken the authentication token of the user
183: * @param group the group to check administrator status on.
184: * @return true if the user is a group administrator of the given group.
185: */
186: public static boolean isGroupAdmin(Authorization authToken,
187: Group group) {
188: if (isSystemAdmin(authToken))
189: return true;
190:
191: return (group.hasPermission(Constants.GROUP_ADMIN));
192: }
193:
194: }
|