<?php

/**
 * Role based access control filter
 *
 * Filters user requests based on 3-layer roles definitions.
 * Redirects user to specified action when access is denied.
 *
 * @version DR1.6
 */
class RoleBasedAccessControlFilter implements InterceptingFilter {



	protected $userId;
	protected $errorAction;



	/**
	 * Creates filter object
	 *
	 * Filter is configured with user identifier and action user will be redirected to
	 * when access is denied.
	 *
	 * @param int unique user identifier
	 * @param string action string
	 */
	public function __construct($userId, $errorAction) {
		$this->userId = $userId;
		$this->errorAction = $errorAction;
	}



	/**
	 * Performs initial request processing
	 *
	 * Grants access to controller method if authorization is successfull.
	 * If access is not granted user is redirected to action provided in constructor.
	 *
	 * @param RequestContext
	 * @param ResponseContext
	 */
	public function preProcess(RequestContext $request, ResponseContext $response) {
		$service = RoleBasedAccessControlService::getInstance();
		if (!$service->isAuthorized($request->getAction(), $this->userId)) {
			$request->configure($this->errorAction, new ArrayObject());
		}
	}



	public function postProcess(RequestContext $request, ResponseContext $response) {

	}



} // class RoleBasedAccessControlFilter

?>