KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > ForumPermissions


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;
28
29 import org.nemesis.forum.config.Constants;
30 import org.nemesis.forum.util.cache.CacheSizes;
31 import org.nemesis.forum.util.cache.Cacheable;
32
33 /**
34  * Defines a set of permissions for objects in the forum system that
35  * users can be granted. Forum permissions are used by the protection
36  * proxy objects defined for each major component of the system.
37  */

38 public class ForumPermissions implements Cacheable {
39
40     
41     private boolean[] values = new boolean[8];
42
43     /**
44      * Factory method to create full permissions.
45      */

46     public static ForumPermissions full() {
47         return new ForumPermissions(
48             true,
49             true,
50             true,
51             true,
52             true,
53             true,
54             true,
55             true);
56     }
57
58     /**
59      * Factory method to create an object with no permissions.
60      */

61     public static ForumPermissions none() {
62         return new ForumPermissions(
63             false,
64             false,
65             false,
66             false,
67             false,
68             false,
69             false,
70             false);
71     }
72
73     /**
74      * Factory method to create an object with read-only permissions.
75      */

76     public static ForumPermissions readOnly() {
77         return new ForumPermissions(
78             true,
79             false,
80             false,
81             false,
82             false,
83             false,
84             false,
85             false);
86     }
87
88     /**
89      * Create a new permissions object with the specified permissions.
90      */

91     public ForumPermissions(
92         boolean READ,
93         boolean SYSTEM_ADMIN,
94         boolean FORUM_ADMIN,
95         boolean USER_ADMIN,
96         boolean GROUP_ADMIN,
97         boolean MODERATOR,
98         boolean CREATE_THREAD,
99         boolean CREATE_MESSAGE) {
100         values[0] = READ;
101         values[1] = SYSTEM_ADMIN;
102         values[2] = FORUM_ADMIN;
103         values[3] = USER_ADMIN;
104         values[4] = GROUP_ADMIN;
105         values[5] = MODERATOR;
106         values[6] = CREATE_THREAD;
107         values[7] = CREATE_MESSAGE;
108     }
109
110     /**
111      * Creates a new ForumPermission object by combining two permissions
112      * objects. The higher permission of each permission type will be used.
113      */

114     public ForumPermissions(ForumPermissions perm1, ForumPermissions perm2) {
115         values[0] = perm1.get(0) || perm2.get(0);
116         values[1] = perm1.get(1) || perm2.get(1);
117         values[2] = perm1.get(2) || perm2.get(2);
118         values[3] = perm1.get(3) || perm2.get(3);
119         values[4] = perm1.get(4) || perm2.get(4);
120         values[5] = perm1.get(5) || perm2.get(5);
121         values[6] = perm1.get(6) || perm2.get(6);
122         values[7] = perm1.get(7) || perm2.get(7);
123     }
124
125     public ForumPermissions(boolean[] permissions) {
126         this.values = permissions;
127     }
128
129     public String JavaDoc toString() {
130         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
131         for (int i = 0; i < values.length - 1; i++) {
132             buf.append(values[i]).append(",");
133         }
134         buf.append(values[values.length - 1]);
135         return buf.toString();
136     }
137
138     /**
139      * Returns true if the permission of a particular type is allowed.
140      */

141     public boolean get(int type) {
142         if (type < 0 || type > 7) {
143             return false;
144         }
145         return values[type];
146     }
147
148     /**
149      * Returns true if the permissions include system or forum admin
150      * permissions.
151      */

152     public boolean isSystemOrForumAdmin() {
153         return (values[Constants.FORUM_ADMIN] || values[Constants.SYSTEM_ADMIN]);
154     }
155
156     //FROM THE CACHEABLE INTERFACE//
157

158     public int getSize() {
159         //Approximate the size of the object in bytes by calculating the size
160
//of each field.
161
int size = 0;
162         size += CacheSizes.sizeOfObject(); //overhead of object
163
size += CacheSizes.sizeOfObject(); //ref to array
164
size += CacheSizes.sizeOfBoolean() * 8; //boolean array vals
165
return size;
166     }
167 }
168
Popular Tags