KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > acl > ACLResource


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 24-JAN-2002, Jahia Solutions Sarl: Khue Nguyen
37  * 15-AUG-2003, Jahia Solutions Sarl: Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42 package org.jahia.services.acl;
43
44 import org.jahia.services.usermanager.JahiaGroup;
45 import org.jahia.services.usermanager.JahiaUser;
46
47 import java.security.Principal JavaDoc;
48 import java.security.acl.Group JavaDoc;
49
50
51 /**
52  * Holds static methods to manipulate acl rights on any resource
53  * that implements the ACLResourceInterface.
54  * They apply to a Principal that should be a JahiaUser or JahiaGroup
55  *
56  * @author Khue Nguyen
57  * @version 1.0
58  * @see ACLResourceInterface
59  */

60 public final class ACLResource {
61
62
63     //--------------------------------------------------------------------------
64
/**
65      * Check if the Principal has a given permission access on the specified resource.
66      *
67      * @param res a resource that implements the ACLResourceInterface.
68      * @param p reference to a JahiaUser or JahiaGroup.
69      * @param perm the permission.
70      *
71      * @return Return true if the Principal has a given permission for the specified res,
72      * or false in any other case.
73      */

74     public static boolean checkPermission (ACLResourceInterface res,
75                                            Principal JavaDoc p,
76                                            int perm) {
77         return checkAccess (res, p, perm);
78     }
79
80
81
82     //--------------------------------------------------------------------------
83
/**
84      * Check if the Principal has administration access on the specified resource.
85      *
86      * @param res a resource that implements the ACLResourceInterface.
87      * @param p reference to a JahiaUser or JahiaGroup.
88      *
89      * @return Return true if the Principal has admin access for the specified res,
90      * or false in any other case.
91      */

92     public static boolean checkAdminAccess (ACLResourceInterface res, Principal JavaDoc p) {
93         return checkAccess (res, p, JahiaBaseACL.ADMIN_RIGHTS);
94     }
95
96
97     //--------------------------------------------------------------------------
98
/**
99      * Check if the Principal has read access on the specified res.
100      *
101      * @param res a resource that implements the ACLResourceInterface.
102      * @param p reference to a JahiaUser or JahiaGroup.
103      *
104      * @return Return true if the Principal has read access for the specified res,
105      * or false in any other case.
106      */

107     public static boolean checkReadAccess (ACLResourceInterface res, Principal JavaDoc p) {
108         return checkAccess (res, p, JahiaBaseACL.READ_RIGHTS);
109     }
110
111
112     //--------------------------------------------------------------------------
113
/**
114      * Check if the Principal has Write access on the specified res.
115      *
116      * @param res a resource that implements the ACLResourceInterface.
117      * @param p reference to a JahiaUser or JahiaGroup.
118      *
119      * @return Return true if the Principal has write access for the specified res,
120      * or false in any other case.
121      */

122     public static boolean checkWriteAccess (ACLResourceInterface res, Principal JavaDoc p) {
123         return checkAccess (res, p, JahiaBaseACL.WRITE_RIGHTS);
124     }
125
126     //--------------------------------------------------------------------------
127
/**
128      * check ACL permissions
129      *
130      * @param res the resource
131      * @param p reference to a JahiaUser or JahiaGroup.
132      * @param permission the permission
133      *
134      * @return boolean true if the user has the given permission on this resource
135      */

136     private static boolean checkAccess (ACLResourceInterface res,
137                                         Principal JavaDoc p,
138                                         int permission) {
139
140         if ((p == null) || (res == null) || (res.getACL () == null))
141             return false;
142
143         // Test the access rights
144
boolean result = false;
145         try {
146             if (isGroup (p)) {
147                 result = res.getACL ().getPermission ((JahiaGroup) p, permission);
148             } else {
149                 result = res.getACL ().getPermission ((JahiaUser) p, permission);
150             }
151         } catch (JahiaACLException ex) {
152             // if an error occured, just return false;
153
}
154         return result;
155     }
156
157     //--------------------------------------------------------------------------
158
/**
159      * returns true if the principal is a Group
160      *
161      * @param p reference to a JahiaUser or JahiaGroup.
162      *
163      * @return boolean true if the Principal is a Group
164      */

165     private static boolean isGroup (Principal JavaDoc p) {
166
167         return (p instanceof Group);
168     }
169
170 }
171
172
Popular Tags