KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > SecurityTools


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26
27 package org.nemesis.forum.util;
28
29 import java.util.Iterator JavaDoc;
30
31 import org.nemesis.forum.Authorization;
32 import org.nemesis.forum.Forum;
33 import org.nemesis.forum.ForumFactory;
34 import org.nemesis.forum.ForumPermissions;
35 import org.nemesis.forum.Group;
36 import org.nemesis.forum.ProfileManager;
37 import org.nemesis.forum.config.Constants;
38
39 /**
40  * @author dlaurent
41  *
42  */

43 public class SecurityTools {
44     
45     /**
46          * Returns true if the user is a system administrator.
47          *
48          * @param authToken the authentication token of the user
49          * @return true if the user is a system administrator, false otherwise.
50          */

51     public static boolean isSystemAdmin(Authorization authToken) {
52         ForumFactory forumFactory = ForumFactory.getInstance(authToken);
53         ForumPermissions permissions = forumFactory.getPermissions(authToken);
54         return permissions.get(Constants.SYSTEM_ADMIN);
55     }
56
57     /**
58      * Returns true if the user is a forum adminstrator of any forum in the
59      * system. For example, if there are 3 forums in the system and the user
60      * is an adminstrator of any one or more of them, this method will return
61      * true.<p>
62      *
63      * Use the method <code>isForumAdmin( Authorization, Forum)</code> to
64      * check an individual forum for administrator status.)
65      *
66      * @param authToken the authentication token of the user
67      * @return true if the user is a forum administrator of any forum in the system.
68      */

69     public static boolean isForumAdmin(Authorization authToken) {
70         
71         if(isSystemAdmin(authToken)) return true;
72         
73         ForumFactory forumFactory = ForumFactory.getInstance(authToken);
74         
75         Iterator JavaDoc forumIterator = forumFactory.forums();
76         if (!forumIterator.hasNext()) {
77             return false;
78         }
79         while (forumIterator.hasNext()) {
80             Forum forum = (Forum) forumIterator.next();
81             if (forum.hasPermission(Constants.FORUM_ADMIN)) {
82                 return true;
83             }
84         }
85         return false;
86     }
87
88     /**
89      * Returns true if the user is a forum moderator of any forum in the
90      * system. For example, if there are 3 forums in the system and the user
91      * is a moderator of any one or more of them, this method will return
92      * true.<p>
93      *
94      * Use the method <code>isForumModerator( Authorization, Forum)</code> to
95      * check an individual forum for moderator status.)
96      *
97      * @param authToken the authentication token of the user
98      * @return true if the user is a forum moderator of any forum in the system.
99      */

100     public static boolean isForumModerator(Authorization authToken) {
101         
102         if(isSystemAdmin(authToken)) return true;
103         
104         ForumFactory forumFactory = ForumFactory.getInstance(authToken);
105         Iterator JavaDoc forumIterator = forumFactory.forums();
106         if (!forumIterator.hasNext()) {
107             return false;
108         }
109         while (forumIterator.hasNext()) {
110             Forum forum = (Forum) forumIterator.next();
111             if (forum.hasPermission(Constants.MODERATOR)) {
112                 return true;
113             }
114         }
115         return false;
116     }
117
118     /**
119      * Returns true if the user is a forum adminstrator of the given forum.
120      *
121      * @param authToken the authentication token of the user
122      * @param forum the forum to check administrator status on.
123      * @return true if the user is a forum administrator of the given forum.
124      */

125     public static boolean isForumAdmin(Authorization authToken, Forum forum) {
126         if(isSystemAdmin(authToken)) return true;
127         
128         return (forum.hasPermission(Constants.FORUM_ADMIN));
129     }
130     /**
131      * Returns true if the user is a forum moderator of the given forum.
132      *
133      * @param authToken the authentication token of the user
134      * @param forum the forum to check moderator status on.
135      * @return true if the user is a forum moderator of the given forum.
136      */

137     public static boolean isForumModerator(Authorization authToken, Forum forum) {
138         if(isSystemAdmin(authToken)) return true;
139         
140         return (forum.hasPermission(Constants.MODERATOR));
141     }
142
143     /**
144      * Returns true if the user is a group administrator of any group in the
145      * system. For example, if there are 3 groups in the system and the user
146      * is an adminstrator of any one or more of them, this method will return
147      * true.<p>
148      *
149      * Use the method <code>isGroupAdmin( Authorization, Group)</code> to check
150      * an individual group for administrator status.)
151      *
152      */

153     public static boolean isGroupAdmin(Authorization authToken) {
154         if(isSystemAdmin(authToken)) return true;
155         
156         ForumFactory forumFactory = ForumFactory.getInstance(authToken);
157         ProfileManager manager = forumFactory.getProfileManager();
158         Iterator JavaDoc groupIterator = manager.groups();
159         if (!groupIterator.hasNext()) {
160             return false;
161         }
162         while (groupIterator.hasNext()) {
163             Group group = (Group) groupIterator.next();
164             if (group.hasPermission(Constants.GROUP_ADMIN)) {
165                 return true;
166             }
167         }
168         return false;
169     }
170
171     /**
172      * Returns true if the user is a group administrator of the given group.
173      *
174      * @param authToken the authentication token of the user
175      * @param group the group to check administrator status on.
176      * @return true if the user is a group administrator of the given group.
177      */

178     public static boolean isGroupAdmin(Authorization authToken, Group group) {
179         if(isSystemAdmin(authToken)) return true;
180         
181         return (group.hasPermission(Constants.GROUP_ADMIN));
182     }
183
184 }
185
Popular Tags