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;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import org.acegisecurity.Authentication;
019: import org.acegisecurity.GrantedAuthority;
020: import org.acegisecurity.GrantedAuthorityImpl;
021: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
022: import org.pentaho.core.session.IPentahoSession;
023: import org.pentaho.core.session.UserSession;
024: import org.pentaho.core.system.PentahoSystem;
025: import org.pentaho.messages.Messages;
026: import org.springframework.beans.factory.InitializingBean;
027:
028: public class UserDetailsRoleListService implements InitializingBean {
029:
030: private UserRoleListService userRoleListService;
031:
032: public UserDetailsRoleListService() {
033: super ();
034: }
035:
036: public void setUserRoleListService(UserRoleListService value) {
037: this .userRoleListService = value;
038: }
039:
040: public UserRoleListService getUserRoleListService() {
041: return this .userRoleListService;
042: }
043:
044: public void afterPropertiesSet() throws Exception {
045: if (this .userRoleListService == null) {
046: throw new Exception(
047: Messages
048: .getString("UserDetailsRoleListService.ERROR_0001_USERROLELISTSERVICE_NOT_SET")); //$NON-NLS-1$
049: }
050: PentahoSystem.setUserDetailsRoleListService(this );
051: }
052:
053: public List getAllRoles() {
054: List rtn = new ArrayList();
055: GrantedAuthority[] auths = userRoleListService
056: .getAllAuthorities();
057: for (int i = 0; i < auths.length; i++) {
058: rtn.add(auths[i].getAuthority());
059: }
060: return rtn;
061: }
062:
063: public List getAllUsers() {
064: List rtn = new ArrayList();
065: String[] users = userRoleListService.getAllUsernames();
066: for (int i = 0; i < users.length; i++) {
067: rtn.add(users[i]);
068: }
069: return rtn;
070: }
071:
072: public List getAllUsersInRole(String role) {
073: String[] users = userRoleListService
074: .getUsernamesInRole(new GrantedAuthorityImpl(role));
075: List rtn = new ArrayList();
076: for (int i = 0; i < users.length; i++) {
077: rtn.add(users[i]);
078: }
079: return rtn;
080:
081: }
082:
083: public List getRolesForUser(String userName) {
084: List rtn = new ArrayList();
085: GrantedAuthority[] auths = userRoleListService
086: .getAuthoritiesForUser(userName);
087: for (int i = 0; i < auths.length; i++) {
088: rtn.add(auths[i].getAuthority());
089: }
090: return rtn;
091: }
092:
093: public IPentahoSession getEffectiveUserSession(String userName) {
094: // Create user session object as un-authenticated so
095: // we can setup the roles before doing the startup actions.
096: UserSession session = new UserSession(userName, null, false);
097: session.setAuthenticated(userName);
098: // Get roles into the session
099: GrantedAuthority[] auths = userRoleListService
100: .getAuthoritiesForUser(userName);
101: Authentication auth = new UsernamePasswordAuthenticationToken(
102: userName, null, auths);
103: session.setAttribute(SecurityUtils.SESSION_PRINCIPAL, auth);
104: // Now that roles are in place, do startup actions
105: session.doStartupActions();
106: // OK - Return back to the user.
107: return session;
108: }
109: }
|