Yii Framework Forum: Return True/false From Cfilter And Get That Value In Another Filter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Return True/false From Cfilter And Get That Value In Another Filter Rate Topic: -----

#1 User is offline   notsoluckycharm 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 15-February 11

Posted 30 December 2012 - 01:25 PM

Hey Guys,

I've set up an rest API that is super predictable.

/x
/x/y
/x/y/z
/x/y/z/&

X can be any controller, Y any ID, Z any action, and & any $id2

Because of this predictable nature, and my desire to log every action and it's consequence I've got the following example.


<?php

class LoggingFilter extends CFilter{
    
    private $_state;
    
    protected function preFilter($filterChain){
        $this->_state = 'ATTEMPTING TO ACCESS';
        
        return true; // false if the action should not be executed
    }
 
    protected function postFilter($filterChain){
        
        $this->_state = 'ATTEMPTING TO ACCESS FAILED';
        // LOG SUCCESS / FAILURE BASED ON ACCESSFILTER AND OTHER FILTERS
    }
}

<?php

class ApiFilter extends CFilter{
    protected function preFilter($filterChain){
        return false; // false if the action should not be executed
    }
 
    protected function postFilter($filterChain){
    }
}

// Defined in components/Controller
public function filters(){
    return array(
	array(
	    'application.filters.LoggingFilter',
	),
	array(
	    'application.filters.ApiFilter',
	),
    );
}


My postFilter for logging executes even if I return false from the api filter. PERFECT. But how do I know the API filter return false and that execution should end?

Idea: I basically want to log the following in pseudo

"User $x attempted to access resource $y and was denied/allowed";

Thanks guys. I've been looking at the docs and haven't gotten anywhere I want to yet.
0

#2 User is offline   notsoluckycharm 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 15-February 11

Posted 01 January 2013 - 11:45 AM

Hey Guys,

If anyone else is looking to do this, You can try using the following to get the error context if it is available. I just check this and if it is set, I save the response code.


if(:$error=Yii::app()->errorHandler->error){
}


Thank you for your time if you had looked and not found anything. The answer was in the blog demo site/error - can't believe I didn't think to look there! I thought it might have a function($x,$y) signature but no! It is awesome.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users