View Javadoc

1   /*
2       Copyright (C) 2004 PjEr
3       
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8       
9       This program is distributed in the hope that it will be useful,
10      but WITHOUT ANY WARRANTY; without even the implied warranty of
11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12      GNU General Public License for more details.
13      
14      You should have received a copy of the GNU General Public License
15      along with this program; if not, write to the Free Software
16      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18  package ca.pjer.cm.module.access.defaultimpl;
19  
20  import ca.pjer.cm.api.access.*;
21  import ca.pjer.cm.api.store.StoreService;
22  import ca.pjer.cm.api.store.StoreServiceSystemException;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.commons.logging.Log;
25  
26  import java.util.List;
27  
28  /***
29   * <br />
30   * $Id: AccessServiceImpl.java,v 1.1 2004/05/19 03:29:17 pjer Exp $<br />
31   * <br />
32   * $RCSfile: AccessServiceImpl.java,v $<br />
33   * $Revision: 1.1 $<br />
34   * $Author: pjer $<br />
35   * $Date: 2004/05/19 03:29:17 $<br />
36   * 
37   * @TODO comment me.<br />
38   */
39  public class AccessServiceImpl implements AccessService {
40      private static final Log log = LogFactory.getLog(AccessServiceImpl.class);
41      private StoreService storeService;
42  
43      public void setStoreService(StoreService storeService) {
44          this.storeService = storeService;
45      }
46  
47      public AccessToken getAccessToken(AccessCredential accessCredential) throws AccessServiceSystemException, InvalidAccessCredentialException {
48          User templateUser = new User();
49          templateUser.setUsername(accessCredential.getUsername());
50          templateUser.setPassword(accessCredential.getPassword());
51          User user = null;
52          try {
53              List userList = storeService.listFromTemplate(templateUser);
54              if (userList.size() < 1) {
55                  throw new InvalidAccessCredentialException("Could not found user.");
56              }
57              if (userList.size() > 1) {
58                  throw new AccessServiceSystemException("More than one users match the credentials");
59              }
60              user = (User) userList.get(0);
61          } catch (StoreServiceSystemException e) {
62              String s = "Could not get access token : " + e.getMessage();
63              log.error(s);
64              throw new AccessServiceSystemException(s, e);
65          }
66          return new AccessTokenImpl(user.getId());
67      }
68  
69      public boolean hasAccess(AccessToken accessToken, Permission permission, Class clazz) throws AccessServiceSystemException, InvalidAccessTokenException {
70          AccessTokenImpl accessTokenImpl = (AccessTokenImpl) accessToken;
71          return accessTokenImpl != null;
72      }
73  }