KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > security > DBSecurityHandler


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.security;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.ojb.samples.bean.User;
23 import org.apache.ojb.broker.PersistenceBroker;
24 import org.apache.ojb.broker.PersistenceBrokerFactory;
25 import org.apache.ojb.broker.query.Criteria;
26 import org.apache.ojb.broker.query.Query;
27 import org.apache.ojb.broker.query.QueryByCriteria;
28 import org.osoco.cowarp.AbstractSecurityHandler;
29 import org.osoco.cowarp.ApplicationManager;
30
31 /**
32  * @version $Id: DBSecurityHandler.java 234176 2005-08-21 10:39:27Z cziegeler $
33  */

34 public class DBSecurityHandler
35     extends AbstractSecurityHandler {
36
37     /**
38      * @see org.osoco.cowarp.SecurityHandler#login(Map)
39      */

40     public org.osoco.cowarp.User login(Map JavaDoc loginContext) throws Exception JavaDoc {
41         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
42
43         try {
44             Parameters para = (Parameters) loginContext.get(ApplicationManager.LOGIN_CONTEXT_PARAMETERS_KEY);
45             
46             final Criteria criteria = new Criteria();
47             criteria.addEqualTo("username", para.getParameter("name"));
48             criteria.addEqualTo("password", para.getParameter("password"));
49             final Query query = new QueryByCriteria(User.class, criteria);
50             final Collection JavaDoc c = broker.getCollectionByQuery(query);
51
52             if ( c.size() == 1 ) {
53                 User u = (User)c.iterator().next();
54                 PortalUser pUser = new PortalUser(u.getUsername());
55                 pUser.setUid(u.getUid());
56                 pUser.setFirstname(u.getFirstname());
57                 pUser.setLastname(u.getLastname());
58                 pUser.setPassword(u.getPassword());
59                 pUser.setRole(u.getRole());
60                 if ( this.getLogger().isInfoEnabled() ) {
61                     this.getLogger().info("Loggedin as: " + u.getFirstname() + " " + u.getLastname() + " (" + u.getUsername() + " " + u.getRole() +")");
62                 }
63                 return pUser;
64             }
65         } finally {
66             broker.close();
67         }
68         return null;
69     }
70     
71     /**
72      * @see org.osoco.cowarp.SecurityHandler#logout(Map, org.osoco.cowarp.User)
73      */

74     public void logout(Map JavaDoc context, org.osoco.cowarp.User user) {
75         // nothing to do
76
}
77 }
78
Popular Tags