View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/server/src/java/org/astrogrid/community/server/security/manager/SecurityManagerImpl.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2005/01/07 14:14:25 $</cvs:date>
5    * <cvs:version>$Revision: 1.9 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: SecurityManagerImpl.java,v $
9    *   Revision 1.9  2005/01/07 14:14:25  jdt
10   *   merged from Reg_KMB_787
11   *
12   *   Revision 1.8.34.1  2004/12/16 11:38:23  KevinBenson
13   *   fixed a small bug on the jsp on editing passwords
14   *
15   *   Revision 1.8  2004/09/16 23:18:08  dave
16   *   Replaced debug logging in Community.
17   *   Added stream close() to FileStore.
18   *
19   *   Revision 1.7.82.1  2004/09/16 09:58:48  dave
20   *   Replaced debug with commons logging ....
21   *
22   *   Revision 1.7  2004/06/18 13:45:20  dave
23   *   Merged development branch, dave-dev-200406081614, into HEAD
24   *
25   *   Revision 1.6.32.2  2004/06/17 15:24:31  dave
26   *   Removed unused imports (PMD report).
27   *
28   *   Revision 1.6.32.1  2004/06/17 13:38:59  dave
29   *   Tidied up old CVS log entries
30   *
31   * </cvs:log>
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         // Check for null account.
118         if (null == account)
119             {
120             throw new CommunityIdentifierException(
121                 "Null account"
122                 ) ;
123             }
124         //
125         // Check for null password.
126         if (null == password)
127             {
128             throw new CommunityIdentifierException(
129                 "Null password"
130                 ) ;
131             }
132         //
133         // Get the Account ident.
134         CommunityIvornParser ident = new CommunityIvornParser(
135             account
136             ) ;
137         //
138         // Set the response to false.
139         boolean result = false ;
140         //
141         // Try update the database.
142         Database database = null ;
143         try {
144             //
145             // Open our database connection.
146             database = this.getDatabase() ;
147             //
148             // Begin a new database transaction.
149             database.begin();
150             //
151             // Try loading the Account from the database.
152 // Do we need this ?
153             //AccountData check = (AccountData) database.load(AccountData.class, ident.getAccountIdent()) ;
154             //log.debug("  PASS : found account") ;
155             //
156             // Try loading the PasswordData.
157             PasswordData data = null ;
158             try {
159                 data = (PasswordData) database.load(PasswordData.class, ident.getAccountIdent()) ;
160                 }
161             //
162             // Don't worry if it isn't there.
163             catch (ObjectNotFoundException ouch)
164                 {
165                 logExpectedException(ouch, "SecurityManagerImpl.setPassword()") ;
166                 }
167             //
168             // If we found the PasswordData.
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                 // Change the password value.
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             // If we didn't find the password.
184             else {
185                 log.debug("  PASS : missing password") ;
186                 //
187                 // Try to create a new PasswordData in the database.
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             // Commit the database transaction.
196             database.commit() ;
197             //
198             // Set the response to true.
199             result = true ;
200             }
201         //
202         // If anything went bang.
203         catch (Exception ouch)
204             {
205             //
206             // Log the exception.
207             logException(
208                 ouch,
209                 "SecurityManagerImpl.setPassword()"
210                 ) ;
211             //
212             // Cancel the database transaction.
213             rollbackTransaction(database) ;
214             //
215             // Throw a new Exception.
216             throw new CommunityServiceException(
217                 "Database transaction failed",
218                 ident.toString(),
219                 ouch
220                 ) ;
221             }
222         //
223         // Close our database connection.
224         finally
225             {
226             closeConnection(database) ;
227             }
228         return result ;
229         }
230     }