%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.fulcrum.security.memory.basic.MemoryModelManagerImpl |
|
|
1 | package org.apache.fulcrum.security.memory.basic; |
|
2 | /* |
|
3 | * Copyright 2001-2004 The Apache Software Foundation |
|
4 | * |
|
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 | * you may not use this file except in compliance with the License. |
|
7 | * You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | import java.util.Iterator; |
|
18 | ||
19 | import org.apache.commons.logging.Log; |
|
20 | import org.apache.commons.logging.LogFactory; |
|
21 | import org.apache.fulcrum.security.entity.Group; |
|
22 | import org.apache.fulcrum.security.entity.User; |
|
23 | import org.apache.fulcrum.security.model.basic.BasicModelManager; |
|
24 | import org.apache.fulcrum.security.model.basic.entity.BasicGroup; |
|
25 | import org.apache.fulcrum.security.model.basic.entity.BasicUser; |
|
26 | import org.apache.fulcrum.security.spi.AbstractManager; |
|
27 | import org.apache.fulcrum.security.util.DataBackendException; |
|
28 | import org.apache.fulcrum.security.util.GroupSet; |
|
29 | import org.apache.fulcrum.security.util.UnknownEntityException; |
|
30 | ||
31 | /** |
|
32 | * This implementation keeps all objects in memory. This is mostly meant to help with testing and |
|
33 | * prototyping of ideas. |
|
34 | * |
|
35 | * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> |
|
36 | * @version $Id: MemoryModelManagerImpl.java 223060 2004-07-07 16:51:27Z epugh $ |
|
37 | */ |
|
38 | 111 | public class MemoryModelManagerImpl |
39 | extends AbstractManager |
|
40 | implements BasicModelManager |
|
41 | { |
|
42 | /** Logging */ |
|
43 | 12 | private static Log log = LogFactory.getLog(MemoryModelManagerImpl.class); |
44 | ||
45 | ||
46 | /** |
|
47 | * Puts a user in a group. |
|
48 | * |
|
49 | * This method is used when adding a user to a group |
|
50 | * |
|
51 | * @param user the User. |
|
52 | * @throws DataBackendException if there was an error accessing the data backend. |
|
53 | * @throws UnknownEntityException if the account is not present. |
|
54 | */ |
|
55 | public void grant(User user, Group group) throws DataBackendException, UnknownEntityException |
|
56 | { |
|
57 | 9 | boolean groupExists = false; |
58 | 9 | boolean userExists = false; |
59 | try |
|
60 | { |
|
61 | 9 | groupExists = getGroupManager().checkExists(group); |
62 | 9 | userExists = getUserManager().checkExists(user); |
63 | 9 | if (groupExists && userExists) |
64 | { |
|
65 | 9 | ((BasicUser) user).addGroup(group); |
66 | 9 | ((BasicGroup) group).addUser(user); |
67 | 9 | return; |
68 | } |
|
69 | } |
|
70 | 0 | catch (Exception e) |
71 | { |
|
72 | 0 | throw new DataBackendException("grant(Role,Permission) failed", e); |
73 | } |
|
74 | ||
75 | 0 | if (!groupExists) |
76 | { |
|
77 | 0 | throw new UnknownEntityException("Unknown group '" + group.getName() + "'"); |
78 | } |
|
79 | 0 | if (!userExists) |
80 | { |
|
81 | 0 | throw new UnknownEntityException("Unknown user '" + user.getName() + "'"); |
82 | } |
|
83 | 0 | } |
84 | /** |
|
85 | * Removes a user in a group. |
|
86 | * |
|
87 | * This method is used when removing a user to a group |
|
88 | * |
|
89 | * @param user the User. |
|
90 | * @throws DataBackendException if there was an error accessing the data backend. |
|
91 | * @throws UnknownEntityException if the user or group is not present. |
|
92 | */ |
|
93 | public void revoke(User user, Group group) throws DataBackendException, UnknownEntityException |
|
94 | { |
|
95 | 3 | boolean groupExists = false; |
96 | 3 | boolean userExists = false; |
97 | try |
|
98 | { |
|
99 | 3 | groupExists = getGroupManager().checkExists(group); |
100 | 3 | userExists = getUserManager().checkExists(user); |
101 | 3 | if (groupExists && userExists) |
102 | { |
|
103 | 3 | ((BasicUser) user).removeGroup(group); |
104 | 3 | ((BasicGroup) group).removeUser(user); |
105 | 3 | return; |
106 | } |
|
107 | } |
|
108 | 0 | catch (Exception e) |
109 | { |
|
110 | 0 | throw new DataBackendException("grant(Role,Permission) failed", e); |
111 | } |
|
112 | ||
113 | 0 | if (!groupExists) |
114 | { |
|
115 | 0 | throw new UnknownEntityException("Unknown group '" + group.getName() + "'"); |
116 | } |
|
117 | 0 | if (!userExists) |
118 | { |
|
119 | 0 | throw new UnknownEntityException("Unknown user '" + user.getName() + "'"); |
120 | } |
|
121 | 0 | } |
122 | /** |
|
123 | * Revokes all groups from a user |
|
124 | * |
|
125 | * This method is used when deleting an account. |
|
126 | * |
|
127 | * @param user the User. |
|
128 | * @throws DataBackendException if there was an error accessing the data backend. |
|
129 | * @throws UnknownEntityException if the account is not present. |
|
130 | */ |
|
131 | public synchronized void revokeAll(User user) |
|
132 | throws DataBackendException, UnknownEntityException |
|
133 | { |
|
134 | 3 | boolean userExists = false; |
135 | try |
|
136 | { |
|
137 | 3 | userExists = getUserManager().checkExists(user); |
138 | 3 | if (userExists) |
139 | { |
|
140 | 12 | for (Iterator i = ((BasicUser) user).getGroups().iterator(); i.hasNext();) |
141 | { |
|
142 | 6 | BasicGroup group = (BasicGroup) i.next(); |
143 | 6 | group.removeUser(user); |
144 | } |
|
145 | 3 | ((BasicUser) user).setGroups(new GroupSet()); |
146 | 3 | return; |
147 | } |
|
148 | } |
|
149 | 0 | catch (Exception e) |
150 | { |
|
151 | 0 | throw new DataBackendException("revokeAll(User) failed:" + e.getMessage(), e); |
152 | } |
|
153 | ||
154 | 0 | throw new UnknownEntityException("Unknown user '" + user.getName() + "'"); |
155 | } |
|
156 | ||
157 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |