/*
 * Copyright 2014 The original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.vaadin.spring.security;

import com.vaadin.navigator.View;
import com.vaadin.ui.UI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.access.annotation.Secured;
import org.vaadin.spring.navigator.SpringViewProvider;

/**
 * Implementation of {@link org.vaadin.spring.navigator.SpringViewProvider.ViewProviderAccessDelegate} that
 * checks if a view has the {@link org.springframework.security.access.annotation.Secured} annotation and if so,
 * uses the {@link org.vaadin.spring.security.Security} instance to check if the current user is authorized to
 * access the view.
 *
 * @author Petter Holmström (petter@vaadin.com)
 * @see Security#hasAnyAuthority(String...)
 */
public class SpringSecurityViewProviderAccessDelegate implements SpringViewProvider.ViewProviderAccessDelegate {

    private final Security security;
    private final ApplicationContext applicationContext;

    @Autowired
    public SpringSecurityViewProviderAccessDelegate(Security security, ApplicationContext applicationContext) {
        this.security = security;
        this.applicationContext = applicationContext;
    }

    @Override
    public boolean isAccessGranted(String beanName, UI ui) {
    	
    	Object securedObject = applicationContext.getBean(beanName);
        Secured viewSecured = applicationContext.findAnnotationOnBean(beanName, Secured.class);
        
        if ( viewSecured == null )
        	return true;
        else if ( security.hasAccessDecisionManager() )
        	return security.hasAccessToSecuredObject(securedObject);
        else
        	return security.hasAnyAuthority(viewSecured.value());
    }

    @Override
    public boolean isAccessGranted(View view, UI ui) {
        return true;
    }
}

@@ -1807,32 +1807,37 @@
 Context;%0A    %7D%0A%0A
+    %0A
     @Override%0A  
@@ -1826,32 +1826,32 @@
  %0A    @Override%0A
-
     public boole
@@ -1910,77 +1910,8 @@
    %09
-Object securedObject = applicationContext.getBean(beanName);%0A        
 Secu
@@ -2136,57 +2136,50 @@
 urn 
-security.hasAccessToSecuredObject(securedObject);
+true; // Leave decision to the second hook
 %0A   
@@ -2335,23 +2335,338 @@
     
+%09%0A
     
-return true
+    Secured viewSecured = view.getClass().getAnnotation(Secured.class);%0A        %0A        if ( viewSecured == null )%0A        %09return true;%0A        else if ( security.hasAccessDecisionManager())%0A        %09return security.hasAccessToSecuredObject(view);%0A        else%0A        %09return security.hasAnyAuthority(viewSecured.value())
 ;%0A  

Use of AccessDecisionManager If an accessDecisionManager is set for Security, isAccessGranted(String beanName, UI ui) will always retrun true and leave the decision to the second hook with the actual object. If no accessDecisionManager is set, the behaviour is the same as before the change and hasAnyAuthority is used whenever there is a @Secured tag.