<?php

/**
 * @author arson
 */
abstract class Eppe_Module_AclAbstract extends Eppe_Acl implements Eppe_Module_AclInterface {

	/**
	 * @var string Nazwa modułu
	 */
	protected $moduleName;

	/**
	 * Init
	 */
	public function init() {
		parent::init();
		$this->_setPrivileges();
	}

	/**
	 * Tutaj powinne być dodawane uprawnienia
	 */
	protected function _setPrivileges() {
		$resources = R::find('resource', 'module = :module', array(':module' => $this->moduleName));

		foreach ($resources as $resource_bean) {
			foreach ($resource_bean->sharedRole as $role_bean) {
				$this->_addModulePriv($resource_bean->controller, $resource_bean->action, array($role_bean->name));
			}
		}

//		$this->_addModulePriv('admin_index', 'index', array(self::ADMIN));
	}

	/**
	 * @param string $name Nazwa modułu
	 */
	public function setModuleName($name) {
		$this->moduleName = $name;
	}

	/**
	 * Dodaje uprawnienie
	 *
	 * @param type $controller
	 * @param type $action
	 * @param array $rolesAllowed
	 * @return \Eppe_Module_AclAbstract
	 * @throws Exception
	 */
	protected function _addModulePriv($controller, $action, array $rolesAllowed) {
		if (empty($this->moduleName)) {
			throw new Exception('Module name must be set first');
		}
		$this->_addPriv($this->moduleName, $controller, $action, $rolesAllowed);

		return $this;
	}

}
