001    /*
002     * 
003     * $Revision: 14986 $ $Date: 2009-03-20 21:41:45 +0100 (Fri, 20 Mar 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.access;
025    
026    import java.util.Arrays;
027    import java.util.Collection;
028    import java.util.Collections;
029    
030    import org.apache.log4j.Logger;
031    import org.jdom.Element;
032    import org.mycore.common.MCRConfiguration;
033    import org.mycore.common.MCRException;
034    import org.mycore.user.MCRUser;
035    
036    /**
037     * This class is a base implementation of the <code>MCRAccessInterface</code>.
038     * 
039     * It will simply allow everything and will do nothing on persistent operations.
040     * Feel free to extend this class if your implementation can only support parts
041     * of the Interface definition.
042     * 
043     * @author Jens Kupferschmidt
044     * 
045     */
046    public class MCRAccessBaseImpl implements MCRAccessInterface {
047    
048        private static MCRAccessInterface SINGLETON;
049    
050        final protected static String AccessPermissions = MCRConfiguration.instance().getString("MCR.Access.AccessPermissions", "read,write,delete");
051    
052        /** the logger */
053        protected static Logger LOGGER = Logger.getLogger(MCRAccessBaseImpl.class.getName());
054    
055        public MCRAccessBaseImpl() {
056        }
057    
058        /**
059         * The method return a singleton instance of MCRAccessInterface.
060         * 
061         * @return a singleton instance of MCRAccessInterface
062         */
063        public static synchronized MCRAccessInterface instance() {
064            if (SINGLETON == null) {
065                SINGLETON = new MCRAccessBaseImpl();
066            }
067    
068            return SINGLETON;
069        }
070    
071        /*
072         * (non-Javadoc)
073         * 
074         * @see org.mycore.access.MCRAccessInterface#addRule(java.lang.String,
075         *      java.lang.String, org.jdom.Element)
076         */
077        public void addRule(String id, String permission, org.jdom.Element rule, String description) throws MCRException {
078            LOGGER.debug("Execute MCRAccessBaseImpl addRule for ID " + id + " for permission " + permission);
079        }
080    
081        /*
082         * (non-Javadoc)
083         * 
084         * @see org.mycore.access.MCRAccessInterface#addRule(java.lang.String,
085         *      org.jdom.Element)
086         */
087        public void addRule(String permission, Element rule, String description) throws MCRException {
088            LOGGER.debug("Execute MCRAccessBaseImpl addRule for permission " + permission);
089        }
090    
091        /*
092         * (non-Javadoc)
093         * 
094         * @see org.mycore.access.MCRAccessInterface#removeRule(java.lang.String,
095         *      java.lang.String)
096         */
097        public void removeRule(String id, String permission) throws MCRException {
098            LOGGER.debug("Execute MCRAccessBaseImpl removeRule for ID " + id + " for permission " + permission);
099        }
100    
101        /*
102         * (non-Javadoc)
103         * 
104         * @see org.mycore.access.MCRAccessInterface#removeRule(java.lang.String)
105         */
106        public void removeRule(String permission) throws MCRException {
107            LOGGER.debug("Execute MCRAccessBaseImpl removeRule for permission " + permission);
108        }
109    
110        /*
111         * (non-Javadoc)
112         * 
113         * @see org.mycore.access.MCRAccessInterface#removeAllRules(java.lang.String)
114         */
115        public void removeAllRules(String id) throws MCRException {
116            LOGGER.debug("Execute MCRAccessBaseImpl removeAllRules for ID " + id);
117        }
118    
119        /*
120         * (non-Javadoc)
121         * 
122         * @see org.mycore.access.MCRAccessInterface#updateRule(java.lang.String,
123         *      java.lang.String, org.jdom.Element)
124         */
125        public void updateRule(String id, String permission, org.jdom.Element rule, String description) throws MCRException {
126            LOGGER.debug("Execute MCRAccessBaseImpl updateRule for ID " + id + " for permission " + permission);
127        }
128    
129        /*
130         * (non-Javadoc)
131         * 
132         * @see org.mycore.access.MCRAccessInterface#updateRule(java.lang.String,
133         *      org.jdom.Element)
134         */
135        public void updateRule(String permission, Element rule, String description) throws MCRException {
136            LOGGER.debug("Execute MCRAccessBaseImpl updateRule for permission " + permission);
137        }
138    
139        /*
140         * (non-Javadoc)
141         * 
142         * @see org.mycore.access.MCRAccessInterface#checkAccess(java.lang.String,
143         *      java.lang.String)
144         */
145        public boolean checkPermission(String id, String permission) {
146            LOGGER.debug("Execute MCRAccessBaseImpl checkPermission for ID " + id + " for permission " + permission);
147            return true;
148        }
149    
150        /*
151         * (non-Javadoc)
152         * 
153         * @see org.mycore.access.MCRAccessInterface#checkAccess(java.lang.String,
154         *      java.lang.String, MCRUser)
155         */
156        public boolean checkPermission(String id, String permission, MCRUser user) {
157            LOGGER.debug("Execute MCRAccessBaseImpl checkPermission for ID " + id + " for permission " + permission + " for user" + user.getID());
158            return true;
159        }    
160    
161        /*
162         * (non-Javadoc)
163         * 
164         * @see org.mycore.access.MCRAccessInterface#checkAccess(java.lang.String)
165         */
166        public boolean checkPermission(String permission) {
167            LOGGER.debug("Execute MCRAccessBaseImpl checkPermission for permission " + permission);
168            return true;
169        }
170        
171        /*
172         * (non-Javadoc)
173         * 
174         * @see org.mycore.access.MCRAccessInterface#checkAccess(java.lang.String, MCRUser)
175         */
176        public boolean checkPermission(String permission, MCRUser user) {
177            LOGGER.debug("Execute MCRAccessBaseImpl checkPermission for permission " + permission + " for user " + user.getID());
178            return true;
179        }    
180        
181        /*
182         * (non-Javadoc)
183         * 
184         * @see org.mycore.access.MCRAccessInterface#checkPermission(java.lang.String, java.lang.String, org.jdom.Document)
185         */    
186            public boolean checkPermission(Element rule) {
187                    return true;
188            }    
189    
190        /*
191         * (non-Javadoc)
192         * 
193         * @see org.mycore.access.MCRAccessInterface#getAccessRule(java.lang.String,
194         *      java.lang.String)
195         */
196        public Element getRule(String objID, String permission) {
197            return null;
198        }
199    
200        /*
201         * (non-Javadoc)
202         * 
203         * @see org.mycore.access.MCRAccessInterface#getAccessRule(java.lang.String)
204         */
205        public Element getRule(String permission) {
206            return null;
207        }
208    
209        /*
210         * (non-Javadoc)
211         * 
212         * @see org.mycore.access.MCRAccessInterface#getRuleDescription(java.lang.String)
213         */    
214            public String getRuleDescription(String permission) {
215                    return "";
216            }
217    
218        /*
219         * (non-Javadoc)
220         * 
221         * @see org.mycore.access.MCRAccessInterface#getRuleDescription(java.lang.String, java.lang.String)
222         */ 
223            public String getRuleDescription(String id, String permission) {
224                    return "";
225            }    
226        
227        /*
228         * (non-Javadoc)
229         * 
230         * @see org.mycore.access.MCRAccessInterface#getPermissionsForID(java.lang.String)
231         */
232        public Collection<String> getPermissionsForID(String objid) {
233            return Collections.emptySet();
234        }
235        
236        /*
237         * (non-Javadoc)
238         * 
239         * @see org.mycore.access.MCRAccessInterface#getPermissions()
240         */
241        public Collection<String> getPermissions() {
242            return Collections.emptySet();
243        }    
244    
245        /**
246         * checks wether a rule with the id and permission is defined. It's the same
247         * as calling
248         * 
249         * <pre>
250         *    (getRule(id, permission)!=null);
251         * </pre>
252         * 
253         * @see #getRule(String, String)
254         */
255        public boolean hasRule(String id, String permission) {
256            return (getRule(id, permission) != null);
257        }
258    
259        /**
260         * checks wether a rule with the id is defined. It's the same as calling
261         * 
262         * <pre>
263         *    (getPermissionsForID(id).size()&gt;0);
264         * </pre>
265         * 
266         * @see #getRule(String, String)
267         */
268        public boolean hasRule(String id) {
269            return (getPermissionsForID(id).size() > 0);
270        }
271        
272        /**
273         * just returns the String of Access Permissions configured in
274         * property "MCR.AccessPermissions"
275         * 
276         * @return the permissions as List
277         */
278        public Collection<String> getAccessPermissionsFromConfiguration(){
279            String[] permissions = AccessPermissions.split(",");
280            return Arrays.asList(permissions);
281        }
282    
283        /*
284         * (non-Javadoc)
285         * 
286         * @see org.mycore.access.MCRAccessInterface#getAllControlledIDs()
287         */
288        public Collection<String> getAllControlledIDs() {
289            return null;
290        }
291    
292        public void createRule(String rule, String creator, String description) {
293            LOGGER.debug("Execute MCRAccessBaseImpl createRule with rule " + rule + " \n and description " + description);
294            
295        }
296    
297        public void createRule(Element rule, String creator, String description) {
298            // TODO Auto-generated method stub
299            
300        }
301    
302        public String getNormalizedRuleString(Element rule) {
303            // TODO Auto-generated method stub
304            return null;
305        }
306    }