001package org.apache.fulcrum.security.memory.dynamic;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021import org.apache.fulcrum.security.entity.Group;
022import org.apache.fulcrum.security.entity.Permission;
023import org.apache.fulcrum.security.entity.Role;
024import org.apache.fulcrum.security.entity.User;
025import org.apache.fulcrum.security.model.dynamic.AbstractDynamicModelManager;
026import org.apache.fulcrum.security.model.dynamic.DynamicModelManager;
027import org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup;
028import org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission;
029import org.apache.fulcrum.security.model.dynamic.entity.DynamicRole;
030import org.apache.fulcrum.security.model.dynamic.entity.DynamicUser;
031import org.apache.fulcrum.security.util.DataBackendException;
032import org.apache.fulcrum.security.util.UnknownEntityException;
033
034/**
035 * This implementation keeps all objects in memory. This is mostly meant to help
036 * with testing and prototyping of ideas.
037 * 
038 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
039 * @version $Id$
040 */
041public class MemoryModelManagerImpl extends AbstractDynamicModelManager implements DynamicModelManager {
042        /**
043         * Puts a user in a group.
044         * 
045         * This method is used when adding a user to a group
046         * 
047         * @param user  the User.
048         * @param group the Group
049         * @throws DataBackendException   if there was an error accessing the data
050         *                                backend.
051         * @throws UnknownEntityException if the account is not present.
052         */
053        public void grant(User user, Group group) throws DataBackendException, UnknownEntityException {
054                boolean groupExists = false;
055                boolean userExists = false;
056                try {
057                        groupExists = getGroupManager().checkExists(group);
058                        userExists = getUserManager().checkExists(user);
059                        if (groupExists && userExists) {
060                                ((DynamicUser) user).addGroup(group);
061                                ((DynamicGroup) group).addUser(user);
062                                return;
063                        }
064                } catch (Exception e) {
065                        throw new DataBackendException("grant(Role,Permission) failed", e);
066                }
067
068                if (!groupExists) {
069                        throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
070                }
071                if (!userExists) {
072                        throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
073                }
074        }
075
076        /**
077         * Revokes a user from a group
078         * 
079         * @param user  the User.
080         * @param group the Group
081         * @throws DataBackendException   if there was an error accessing the data
082         *                                backend
083         * @throws UnknownEntityException if the user or group is not present.
084         */
085        public void revoke(User user, Group group) throws DataBackendException, UnknownEntityException {
086                boolean groupExists = false;
087                boolean userExists = false;
088                try {
089                        groupExists = getGroupManager().checkExists(group);
090                        userExists = getUserManager().checkExists(user);
091                        if (groupExists && userExists) {
092                                ((DynamicUser) user).removeGroup(group);
093                                ((DynamicGroup) group).removeUser(user);
094                                return;
095                        }
096                } catch (Exception e) {
097                        throw new DataBackendException("grant(Role,Permission) failed", e);
098                }
099
100                if (!groupExists) {
101                        throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
102                }
103                if (!userExists) {
104                        throw new UnknownEntityException("Unknown user '" + user.getName() + "'");
105                }
106        }
107
108        /**
109         * Grants a Role to a Group
110         * 
111         * @param group the Group.
112         * @param role  the Role.
113         * @throws DataBackendException   if there was an error accessing the data
114         *                                backend.
115         * @throws UnknownEntityException if group or role is not present.
116         */
117        public synchronized void grant(Group group, Role role) throws DataBackendException, UnknownEntityException {
118                boolean groupExists = false;
119                boolean roleExists = false;
120                try {
121                        groupExists = getGroupManager().checkExists(group);
122                        roleExists = getRoleManager().checkExists(role);
123                        if (groupExists && roleExists) {
124                                ((DynamicGroup) group).addRole(role);
125                                ((DynamicRole) role).addGroup(group);
126                                return;
127                        }
128                } catch (Exception e) {
129                        throw new DataBackendException("grant(Group,Role) failed", e);
130                }
131                if (!groupExists) {
132                        throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
133                }
134                if (!roleExists) {
135                        throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
136                }
137        }
138
139        /**
140         * Revokes a Role from a Group.
141         * 
142         * @param group the Group.
143         * @param role  the Role.
144         * @throws DataBackendException   if there was an error accessing the data
145         *                                backend.
146         * @throws UnknownEntityException if group or role is not present.
147         */
148        public synchronized void revoke(Group group, Role role) throws DataBackendException, UnknownEntityException {
149                boolean groupExists = false;
150                boolean roleExists = false;
151                try {
152                        groupExists = getGroupManager().checkExists(group);
153                        roleExists = getRoleManager().checkExists(role);
154                        if (groupExists && roleExists) {
155                                ((DynamicGroup) group).removeRole(role);
156                                ((DynamicRole) role).removeGroup(group);
157                                return;
158                        }
159                } catch (Exception e) {
160                        throw new DataBackendException("revoke(Group,Role) failed", e);
161                }
162
163                if (!groupExists) {
164                        throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
165                }
166                if (!roleExists) {
167                        throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
168                }
169        }
170
171        /**
172         * Grants a Role a Permission
173         * 
174         * @param role       the Role.
175         * @param permission the Permission.
176         * @throws DataBackendException   if there was an error accessing the data
177         *                                backend.
178         * @throws UnknownEntityException if role or permission is not present.
179         */
180        public synchronized void grant(Role role, Permission permission)
181                        throws DataBackendException, UnknownEntityException {
182                boolean roleExists = false;
183                boolean permissionExists = false;
184                try {
185                        roleExists = getRoleManager().checkExists(role);
186                        permissionExists = getPermissionManager().checkExists(permission);
187                        if (roleExists && permissionExists) {
188                                ((DynamicRole) role).addPermission(permission);
189                                ((DynamicPermission) permission).addRole(role);
190                                return;
191                        }
192                } catch (Exception e) {
193                        throw new DataBackendException("grant(Role,Permission) failed", e);
194                }
195
196                if (!roleExists) {
197                        throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
198                }
199                if (!permissionExists) {
200                        throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
201                }
202        }
203
204        /**
205         * Revokes a Permission from a Role.
206         * 
207         * @param role       the Role.
208         * @param permission the Permission.
209         * @throws DataBackendException   if there was an error accessing the data
210         *                                backend.
211         * @throws UnknownEntityException if role or permission is not present.
212         */
213        public synchronized void revoke(Role role, Permission permission)
214                        throws DataBackendException, UnknownEntityException {
215                boolean roleExists = false;
216                boolean permissionExists = false;
217                try {
218                        roleExists = getRoleManager().checkExists(role);
219                        permissionExists = getPermissionManager().checkExists(permission);
220                        if (roleExists && permissionExists) {
221                                ((DynamicRole) role).removePermission(permission);
222                                ((DynamicPermission) permission).removeRole(role);
223                                return;
224                        }
225                } catch (Exception e) {
226                        throw new DataBackendException("revoke(Role,Permission) failed", e);
227                }
228
229                if (!roleExists) {
230                        throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
231                }
232                if (!permissionExists) {
233                        throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
234                }
235        }
236
237}