01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.cocoon.portal.security;
18:
19: import java.util.Collection;
20: import java.util.Map;
21:
22: import org.apache.avalon.framework.parameters.Parameters;
23: import org.apache.cocoon.ojb.samples.bean.User;
24: import org.apache.ojb.broker.PersistenceBroker;
25: import org.apache.ojb.broker.PersistenceBrokerFactory;
26: import org.apache.ojb.broker.query.Criteria;
27: import org.apache.ojb.broker.query.Query;
28: import org.apache.ojb.broker.query.QueryByCriteria;
29: import org.apache.cocoon.auth.AbstractSecurityHandler;
30: import org.apache.cocoon.auth.ApplicationManager;
31:
32: /**
33: * @version $Id: DBSecurityHandler.java 433543 2006-08-22 06:22:54Z crossley $
34: */
35: public class DBSecurityHandler extends AbstractSecurityHandler {
36:
37: /**
38: * @see org.apache.cocoon.auth.SecurityHandler#login(Map)
39: */
40: public org.apache.cocoon.auth.User login(Map loginContext)
41: throws Exception {
42: PersistenceBroker broker = PersistenceBrokerFactory
43: .defaultPersistenceBroker();
44:
45: try {
46: Parameters para = (Parameters) loginContext
47: .get(ApplicationManager.LOGIN_CONTEXT_PARAMETERS_KEY);
48:
49: final Criteria criteria = new Criteria();
50: criteria.addEqualTo("username", para.getParameter("name"));
51: criteria.addEqualTo("password", para
52: .getParameter("password"));
53: final Query query = new QueryByCriteria(User.class,
54: criteria);
55: final Collection c = broker.getCollectionByQuery(query);
56:
57: if (c.size() == 1) {
58: User u = (User) c.iterator().next();
59: PortalUser pUser = new PortalUser(u.getUsername());
60: pUser.setUid(u.getUid());
61: pUser.setFirstname(u.getFirstname());
62: pUser.setLastname(u.getLastname());
63: pUser.setPassword(u.getPassword());
64: pUser.setRole(u.getRole());
65: if (this .getLogger().isInfoEnabled()) {
66: this .getLogger().info(
67: "Loggedin as: " + u.getFirstname() + " "
68: + u.getLastname() + " ("
69: + u.getUsername() + " "
70: + u.getRole() + ")");
71: }
72: return pUser;
73: }
74: } finally {
75: broker.close();
76: }
77: return null;
78: }
79:
80: /**
81: * @see org.apache.cocoon.auth.SecurityHandler#logout(Map, org.apache.cocoon.auth.User)
82: */
83: public void logout(Map context, org.apache.cocoon.auth.User user) {
84: // nothing to do
85: }
86: }
|