1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package org.astrogrid.community.server.security.manager ;
35
36 import org.apache.commons.logging.Log ;
37 import org.apache.commons.logging.LogFactory ;
38
39 import org.exolab.castor.jdo.Database;
40 import org.exolab.castor.jdo.ObjectNotFoundException ;
41
42 import org.astrogrid.community.common.policy.data.AccountData ;
43
44 import org.astrogrid.community.server.security.data.PasswordData ;
45 import org.astrogrid.community.common.security.manager.SecurityManager ;
46
47 import org.astrogrid.community.common.ivorn.CommunityIvornParser ;
48
49 import org.astrogrid.community.server.service.CommunityServiceImpl ;
50 import org.astrogrid.community.server.database.configuration.DatabaseConfiguration ;
51
52 import org.astrogrid.community.common.exception.CommunityServiceException ;
53 import org.astrogrid.community.common.exception.CommunitySecurityException ;
54 import org.astrogrid.community.common.exception.CommunityIdentifierException ;
55
56 /***
57 * Implementation of our SecurityManager service.
58 *
59 */
60 public class SecurityManagerImpl
61 extends CommunityServiceImpl
62 implements SecurityManager
63 {
64 /***
65 * Our debug logger.
66 *
67 */
68 private static Log log = LogFactory.getLog(SecurityManagerImpl.class);
69
70 /***
71 * Public constructor, using default database configuration.
72 *
73 */
74 public SecurityManagerImpl()
75 {
76 super() ;
77 }
78
79 /***
80 * Public constructor, using specific database configuration.
81 *
82 */
83 public SecurityManagerImpl(DatabaseConfiguration config)
84 {
85 super(config) ;
86 }
87
88 /***
89 * Public constructor, using a parent service.
90 *
91 */
92 public SecurityManagerImpl(CommunityServiceImpl parent)
93 {
94 super(parent) ;
95 }
96
97 /***
98 * Set an Account password.
99 * @param account The account ident.
100 * @param password The account password.
101 * @return True if the password was set.
102 * @throws CommunitySecurityException If the password change fails.
103 * @throws CommunityServiceException If there is an internal error in service.
104 * @throws CommunityIdentifierException If the account identifier is invalid.
105 * @todo Check Account is local.
106 *
107 */
108 public boolean setPassword(String account, String password)
109 throws CommunityServiceException, CommunitySecurityException, CommunityIdentifierException
110 {
111 log.debug("") ;
112 log.debug("----\"----") ;
113 log.debug("SecurityManagerImpl.setPassword()") ;
114 log.debug(" Account : " + account) ;
115 log.debug(" Value : " + password) ;
116
117
118 if (null == account)
119 {
120 throw new CommunityIdentifierException(
121 "Null account"
122 ) ;
123 }
124
125
126 if (null == password)
127 {
128 throw new CommunityIdentifierException(
129 "Null password"
130 ) ;
131 }
132
133
134 CommunityIvornParser ident = new CommunityIvornParser(
135 account
136 ) ;
137
138
139 boolean result = false ;
140
141
142 Database database = null ;
143 try {
144
145
146 database = this.getDatabase() ;
147
148
149 database.begin();
150
151
152
153
154
155
156
157 PasswordData data = null ;
158 try {
159 data = (PasswordData) database.load(PasswordData.class, ident.getAccountIdent()) ;
160 }
161
162
163 catch (ObjectNotFoundException ouch)
164 {
165 logExpectedException(ouch, "SecurityManagerImpl.setPassword()") ;
166 }
167
168
169 if (null != data)
170 {
171 log.debug(" PASS : found password") ;
172 log.debug(" Account : " + data.getAccount()) ;
173 log.debug(" Password : " + data.getPassword()) ;
174
175
176 data.setPassword(password) ;
177 data.setEncryption(PasswordData.NO_ENCRYPTION) ;
178 log.debug(" PASS : changed password") ;
179 log.debug(" Account : " + data.getAccount()) ;
180 log.debug(" Password : " + data.getPassword()) ;
181 }
182
183
184 else {
185 log.debug(" PASS : missing password") ;
186
187
188 data = new PasswordData(ident.getAccountIdent(), password) ;
189 database.create(data) ;
190 log.debug(" PASS : created password") ;
191 log.debug(" Account : " + data.getAccount()) ;
192 log.debug(" Password : " + data.getPassword()) ;
193 }
194
195
196 database.commit() ;
197
198
199 result = true ;
200 }
201
202
203 catch (Exception ouch)
204 {
205
206
207 logException(
208 ouch,
209 "SecurityManagerImpl.setPassword()"
210 ) ;
211
212
213 rollbackTransaction(database) ;
214
215
216 throw new CommunityServiceException(
217 "Database transaction failed",
218 ident.toString(),
219 ouch
220 ) ;
221 }
222
223
224 finally
225 {
226 closeConnection(database) ;
227 }
228 return result ;
229 }
230 }