001: /*
002: * Copyright 2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package com.pentaho.security.ldap;
014:
015: import java.util.List;
016:
017: import org.acegisecurity.GrantedAuthority;
018: import org.acegisecurity.ldap.InitialDirContextFactory;
019: import org.acegisecurity.userdetails.UserDetails; //import org.apache.commons.logging.Log;
020: //import org.apache.commons.logging.LogFactory;
021: import org.springframework.beans.factory.InitializingBean;
022:
023: import com.pentaho.security.UserRoleListService;
024: import com.pentaho.security.ldap.search.LdapSearch;
025:
026: public class DefaultLdapUserRoleListService implements
027: UserRoleListService, InitializingBean {
028:
029: // ~ Static fields/initializers ============================================
030: // private static final Log logger = LogFactory.getLog(DefaultLdapUserRoleListService.class);
031:
032: // ~ Instance fields =======================================================
033: // private InitialDirContextFactory initialDirContextFactory;
034:
035: private LdapSearch allUsernamesSearch;
036:
037: private LdapSearch allAuthoritiesSearch;
038:
039: private LdapSearch usernamesInRoleSearch;
040:
041: /**
042: * Used only for <code>getAuthoritiesForUser</code>. This is preferred
043: * over an <code>LdapSearch</code> in
044: * <code>authoritiesForUserSearch</code> as it keeps roles returned by
045: * <code>UserDetailsService</code> and roles returned by
046: * <code>DefaultLdapUserRoleListService</code> consistent.
047: */
048: private LdapUserDetailsService userDetailsService;
049:
050: // ~ Constructors ==========================================================
051: public DefaultLdapUserRoleListService(
052: final InitialDirContextFactory initialDirContextFactory) {
053: // this.initialDirContextFactory = initialDirContextFactory;
054: }
055:
056: // ~ Methods ===============================================================
057:
058: public void afterPropertiesSet() throws Exception {
059: }
060:
061: public GrantedAuthority[] getAllAuthorities() {
062: List results = allAuthoritiesSearch.search(new Object[0]);
063: return (GrantedAuthority[]) results
064: .toArray(new GrantedAuthority[0]);
065: }
066:
067: public String[] getAllUsernames() {
068: List results = allUsernamesSearch.search(new Object[0]);
069: return (String[]) results.toArray(new String[0]);
070: }
071:
072: public String[] getUsernamesInRole(final GrantedAuthority authority) {
073: List results = usernamesInRoleSearch
074: .search(new Object[] { authority });
075: return (String[]) results.toArray(new String[0]);
076: }
077:
078: public GrantedAuthority[] getAuthoritiesForUser(String username) {
079: UserDetails user = null;
080: user = userDetailsService.loadUserByUsername(username);
081: return user.getAuthorities();
082: }
083:
084: public void setAllUsernamesSearch(
085: final LdapSearch allUsernamesSearch) {
086: this .allUsernamesSearch = allUsernamesSearch;
087: }
088:
089: public void setAllAuthoritiesSearch(
090: final LdapSearch allAuthoritiesSearch) {
091: this .allAuthoritiesSearch = allAuthoritiesSearch;
092: }
093:
094: public void setUsernamesInRoleSearch(
095: final LdapSearch usernamesInRoleSearch) {
096: this .usernamesInRoleSearch = usernamesInRoleSearch;
097: }
098:
099: public void setUserDetailsService(
100: final LdapUserDetailsService userDetailsService) {
101: this.userDetailsService = userDetailsService;
102: }
103:
104: }
|