001package org.apache.fulcrum.security.memory.basic; 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.User; 023import org.apache.fulcrum.security.model.basic.BasicModelManager; 024import org.apache.fulcrum.security.model.basic.entity.BasicGroup; 025import org.apache.fulcrum.security.model.basic.entity.BasicUser; 026import org.apache.fulcrum.security.spi.AbstractManager; 027import org.apache.fulcrum.security.util.DataBackendException; 028import org.apache.fulcrum.security.util.GroupSet; 029import org.apache.fulcrum.security.util.UnknownEntityException; 030 031/** 032 * This implementation keeps all objects in memory. This is mostly meant to help 033 * with testing and prototyping of ideas. 034 * 035 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> 036 * @version $Id$ 037 */ 038public class MemoryModelManagerImpl extends AbstractManager implements BasicModelManager 039{ 040 /** 041 * Puts a user in a group. 042 * 043 * This method is used when adding a user to a group 044 * 045 * @param user the User 046 * @param group the Group 047 * @throws DataBackendException 048 * if there was an error accessing the data backend. 049 * @throws UnknownEntityException 050 * if the account is not present. 051 */ 052 public void grant(User user, Group group) throws DataBackendException, UnknownEntityException 053 { 054 boolean groupExists = false; 055 boolean userExists = false; 056 try 057 { 058 groupExists = getGroupManager().checkExists(group); 059 userExists = getUserManager().checkExists(user); 060 if (groupExists && userExists) 061 { 062 ((BasicUser) user).addGroup(group); 063 ((BasicGroup) group).addUser(user); 064 return; 065 } 066 } 067 catch (Exception e) 068 { 069 throw new DataBackendException("grant(Role,Permission) failed", e); 070 } 071 072 if (!groupExists) 073 { 074 throw new UnknownEntityException("Unknown group '" + group.getName() + "'"); 075 } 076 if (!userExists) 077 { 078 throw new UnknownEntityException("Unknown user '" + user.getName() + "'"); 079 } 080 } 081 082 /** 083 * Removes a user in a group. 084 * 085 * This method is used when removing a user to a group 086 * 087 * @param user 088 * the User. 089 * @param group the Group 090 * @throws DataBackendException 091 * if there was an error accessing the data backend. 092 * @throws UnknownEntityException 093 * if the user or group is not present. 094 */ 095 public void revoke(User user, Group group) throws DataBackendException, UnknownEntityException 096 { 097 boolean groupExists = false; 098 boolean userExists = false; 099 try 100 { 101 groupExists = getGroupManager().checkExists(group); 102 userExists = getUserManager().checkExists(user); 103 if (groupExists && userExists) 104 { 105 ((BasicUser) user).removeGroup(group); 106 ((BasicGroup) group).removeUser(user); 107 return; 108 } 109 } 110 catch (Exception e) 111 { 112 throw new DataBackendException("grant(Role,Permission) failed", e); 113 } 114 115 if (!groupExists) 116 { 117 throw new UnknownEntityException("Unknown group '" + group.getName() + "'"); 118 } 119 if (!userExists) 120 { 121 throw new UnknownEntityException("Unknown user '" + user.getName() + "'"); 122 } 123 } 124 125 /** 126 * Revokes all groups from a user 127 * 128 * This method is used when deleting an account. 129 * 130 * @param user 131 * the User. 132 * @throws DataBackendException 133 * if there was an error accessing the data backend. 134 * @throws UnknownEntityException 135 * if the account is not present. 136 */ 137 public synchronized void revokeAll(User user) throws DataBackendException, UnknownEntityException 138 { 139 boolean userExists = false; 140 try 141 { 142 userExists = getUserManager().checkExists(user); 143 if (userExists) 144 { 145 for (Group group : ((BasicUser) user).getGroups()) 146 { 147 ((BasicGroup) group).removeUser(user); 148 } 149 ((BasicUser) user).setGroups(new GroupSet()); 150 return; 151 } 152 } 153 catch (Exception e) 154 { 155 throw new DataBackendException("revokeAll(User) failed:" + e.getMessage(), e); 156 } 157 158 throw new UnknownEntityException("Unknown user '" + user.getName() + "'"); 159 } 160 161}