/*!
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2002-2013 Pentaho Corporation..  All rights reserved.
*/

package org.pentaho.platform.dataaccess.datasource.wizard.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.platform.api.engine.IAuthorizationPolicy;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.engine.IPluginResourceLoader;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.engine.security.SecurityHelper;
import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;

public class SimpleDataAccessViewPermissionHandler implements IDataAccessViewPermissionHandler {

  private Log logger = LogFactory.getLog(SimpleDataAccessViewPermissionHandler.class);
  
  private IAuthorizationPolicy policy = PentahoSystem.get( IAuthorizationPolicy.class );

  public List<String> getPermittedRoleList(IPentahoSession session) {
    List<String> roleList = new ArrayList<String>();
    Authentication auth = SecurityHelper.getInstance().getAuthentication(session, true);
    IPluginResourceLoader resLoader = PentahoSystem.get(IPluginResourceLoader.class, null);
    String roles = null;

    try {
      roles = resLoader.getPluginSetting(getClass(), "settings/data-access-view-roles"); //$NON-NLS-1$
    } catch (Exception e) {
      logger.debug("Error getting plugin setting", e);
    }

    if (roles != null && roles.length() > 0) {
      String roleArr[] = roles.split(","); //$NON-NLS-1$

      for (String role : roleArr) {
        for (GrantedAuthority userRole : auth.getAuthorities()) {
          if (role != null && role.trim().equals(userRole.getAuthority())) {
            roleList.add(role);
          }
        }
      }
    }
    return roleList;
  }

  @Override
  public List<String> getPermittedUserList(IPentahoSession session) {
    List<String> userList = new ArrayList<String>();
    IPluginResourceLoader resLoader = PentahoSystem.get(IPluginResourceLoader.class, null);
    String users = null;

    try {
      users = resLoader.getPluginSetting(getClass(), "settings/data-access-view-users"); //$NON-NLS-1$
    } catch (Exception e) {
      logger.debug("Error getting plugin setting", e);
    }

    if (users != null && users.length() > 0) {
      String userArr[] = users.split(","); //$NON-NLS-1$
      for (String user : userArr) {
        if (user != null && user.trim().length() > 0) {
          userList.add(user);
        }
      }
    }

    return userList;
  }

  public int getDefaultAcls(IPentahoSession session) {
    IPluginResourceLoader resLoader = PentahoSystem.get(IPluginResourceLoader.class, null);
    String defaultAclsAsString = null;
    int defaultAcls = -1;
    try {
      defaultAclsAsString = resLoader.getPluginSetting(getClass(), "settings/data-access-default-view-acls"); //$NON-NLS-1$
    } catch (Exception e) {
      logger.debug("Error getting plugin setting", e);
    }
    if (defaultAclsAsString != null && defaultAclsAsString.length() > 0) {
      defaultAcls = Integer.parseInt(defaultAclsAsString);
    }
    return defaultAcls;
  }

  public boolean hasDataAccessViewPermission(IPentahoSession session) {
    return getPermittedUserList( session ).size() > 0 || getPermittedRoleList( session ).size() > 0;
  }
}

@@ -2248,24 +2248,25 @@
  %7D%0A%0A    if (
+ 
 roles != nul
@@ -2280,32 +2280,33 @@
 les.length() %3E 0
+ 
 ) %7B%0A      String
@@ -2322,35 +2322,37 @@
 %5D = roles.split(
+ 
 %22,%22
+ 
 ); //$NON-NLS-1$
@@ -2360,24 +2360,25 @@
 %0A      for (
+ 
 String role 
@@ -2390,80 +2390,13 @@
 eArr
-) %7B%0A        for (GrantedAuthority userRole : auth.getAuthorities()
+ 
 ) %7B%0A
-  
     
@@ -2403,16 +2403,17 @@
     if (
+ 
 role != 
@@ -2436,45 +2436,24 @@
 m().
-equals(userRole.getAuthority())
+length() %3E 0
 ) %7B%0A
-  
     
@@ -2475,27 +2475,17 @@
 add(
+ 
 role
+ 
 );%0A
-          %7D%0A
     

Fix 4.8 bug that filters roles out of permitted list if user is not a member of the role.