KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > security > AuthoritiesPopulator


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.core.security;
19
20 import java.util.List JavaDoc;
21
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.ldap.LdapDataAccessException;
25 import org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator;
26 import org.acegisecurity.userdetails.UsernameNotFoundException;
27 import org.acegisecurity.userdetails.jdbc.JdbcDaoImpl;
28 import org.acegisecurity.userdetails.ldap.LdapUserDetails;
29 import org.springframework.util.Assert;
30
31 /**
32  * @author Elias Torres (<a HREF="mailto:eliast@us.ibm.com">eliast@us.ibm.com</a>)
33  *
34  */

35 public class AuthoritiesPopulator extends JdbcDaoImpl implements LdapAuthoritiesPopulator {
36   
37
38   /** A default role which will be assigned to all authenticated users if set */
39   private GrantedAuthority defaultRole = null;
40
41   /* (non-Javadoc)
42    * @see org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator#getGrantedAuthorities(org.acegisecurity.userdetails.ldap.LdapUserDetails)
43    */

44   public GrantedAuthority[] getGrantedAuthorities(LdapUserDetails userDetails) throws LdapDataAccessException {
45     
46     List JavaDoc dbAuths = authoritiesByUsernameMapping.execute(userDetails.getUsername());
47
48     addCustomAuthorities(userDetails.getUsername(), dbAuths);
49     
50     if (defaultRole != null) {
51       dbAuths.add(defaultRole);
52     }
53
54     if (dbAuths.size() == 0) {
55         throw new UsernameNotFoundException("User has no GrantedAuthority");
56     }
57
58     return (GrantedAuthority[]) dbAuths.toArray(new GrantedAuthority[dbAuths.size()]);
59   }
60   
61   /**
62    * The default role which will be assigned to all users.
63    *
64    * @param defaultRole the role name, including any desired prefix.
65    */

66   public void setDefaultRole(String JavaDoc defaultRole) {
67       Assert.notNull(defaultRole, "The defaultRole property cannot be set to null");
68       this.defaultRole = new GrantedAuthorityImpl(defaultRole);
69   }
70
71 }
72
Popular Tags