Commits

Christoph Mewes committed 6a99e81

moved pac library into own repository (and thereby fixing the fucked up naming scheme), fixed wrongly declared 0.6 compat

Comments (0)

Files changed (14)

 define('SLY_RBAC_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
 
 sly_Loader::addLoadPath(SLY_RBAC_PATH.'lib');
-sly_Loader::addLoadPath(SLY_RBAC_PATH.'vendor/pac/lib', 'pac_');
+sly_Loader::addLoadPath(SLY_VENDORFOLDER.'/webvariants/pac/src');
 
 // register the auth provider as soon as possible
 sly_Authorisation::setAuthorisationProvider(new WV42_RBAC());
     "type": "sallycms-addon",
     "require": {
         "sallycms/composer-installer": "~1.1",
-        "sallycms/sallycms": ">=0.6,<0.8"
+        "sallycms/sallycms": ">=0.7,<0.9",
+        "webvariants/pac": "~1.0"
     },
     "autoload": {
         "psr-0": {
-            "": "lib/"
+            "WV24_": "lib/",
+            "sly_Controller_": "lib/"
         }
     },
     "extra": {
         "sallycms": {
-            "version": "0.4.x-dev"
+            "version": "0.x-dev"
         },
         "branch-alias": {
             "dev-default": "0.x-dev"

vendor/pac/LICENSE

-Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-
-http://www.opensource.org/licenses/mit-license.php
-http://de.wikipedia.org/wiki/MIT-Lizenz

vendor/pac/lib/Core.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-abstract class pac_Core {
-
-	const ROLES_TABLE              = 'pac_roles';
-	const ROLES_USERS_TABLE        = 'pac_users_roles';
-	const ROLES_INCLUDES_TABLE     = 'pac_role_includes';
-	const ROLES_DESTINATIONS_TABLE = 'pac_roles_destinations_values';
-
-	protected static $pdoProvider;   ///< pac_PDOProvider
-	protected static $destProvider;  ///< pac_DestinationProvider
-
-	/**
-	 * @param pac_PDOProvider         $pdoProvider
-	 * @param pac_DestinationProvider $destProvider
-	 */
-	public static function init(pac_PDOProvider $pdoProvider, pac_DestinationProvider $destProvider) {
-		self::$pdoProvider  = $pdoProvider;
-		self::$destProvider = $destProvider;
-	}
-
-	/**
-	 * @param  string $statement
-	 * @return PDOStatement
-	 */
-	public static function prepare($statement) {
-		self::selfTest();
-		return self::pdo()->prepare($statement);
-	}
-
-	/**
-	 * Return table prefix
-	 *
-	 * @param  string $tablename
-	 * @return string
-	 */
-	public static function dbTable($tablename) {
-		self::selfTest();
-		return self::$pdoProvider->getPrefix().$tablename;
-	}
-
-	/**
-	 * return current PDO instance
-	 *
-	 * @return PDO
-	 */
-	public static function pdo() {
-		self::selfTest();
-		return self::$pdoProvider->getPDO();
-	}
-
-	/**
-	 * @param  string $token
-	 * @return pac_Destination
-	 */
-	public static function getDestination($id) {
-		self::selfTest();
-		$destination = self::$destProvider->getDestination($id);
-		if(!($destination instanceof pac_Destination))
-			throw new pac_Exception ('Destination for '.$id.' could not be get from configured DestinationProvider');
-
-		return $destination;
-	}
-
-	/**
-	 * @throws pac_Exception
-	 */
-	protected static function selfTest() {
-		if (!(self::$pdoProvider instanceof pac_PDOProvider) || !(self::$destProvider instanceof pac_DestinationProvider)) {
-			throw new pac_Exception('pac_Core is not initialized!');
-		}
-	}
-}

vendor/pac/lib/Destination.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-abstract class pac_Destination {
-	protected $id;       ///< string
-	protected $tokens;   ///< array
-	protected $supers;   ///< array
-	protected $vartype;  ///< string
-
-	/**
-	 * @param string $token
-	 */
-	public function __construct($id, array $tokens, $vartype, array $supers) {
-		$this->id      = $id;
-		$this->tokens  = $tokens;
-		$this->supers  = $supers;
-		$this->vartype = $vartype;
-	}
-
-	/**
-	 * @return array
-	 */
-	public function getTokens() {
-		return $this->tokens;
-	}
-
-	/**
-	 *
-	 * @param string $token
-	 * @return array
-	 */
-	public function getSupers($token) {
-		if(isset($this->supers[$token])) {
-			return $this->supers[$token];
-		}
-	}
-
-	/**
-	 * @param  mixed $value
-	 * @return mixed
-	 */
-	public function normalizeValue($value) {
-		if (is_array($value)) {
-			foreach ($value as $idx => &$val) {
-				settype($val, $this->vartype);
-			}
-		}
-		else {
-			settype($value, $this->vartype);
-		}
-
-		return $value;
-	}
-
-	/**
-	 * get the id of this destination
-	 *
-	 * @return string
-	 */
-	public function getId() {
-		return $this->id;
-	}
-
-	public function validateToken($token, $throw = true) {
-		if(!in_array($token, $this->tokens)) {
-			if($throw) throw new pac_Exception('The Destination: '.$this->id.' does not conatin the token: '.$token);
-			return false;
-		}
-		return true;
-	}
-
-	/**
-	 *
-	 * @param  mixed $value       from roles
-	 * @param  mixed $checkValue  value to have
-	 * @return boolean            hasPermission
-	 */
-	abstract public function evaluate($value, $checkValue);
-}

vendor/pac/lib/Destination/List.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_Destination_List extends pac_Destination {
-
-	public function __construct($id, $tokens, $vartype, $supers = array()) {
-		parent::__construct($id, $tokens, $vartype, $supers);
-	}
-
-	public function evaluate($value, $checkValue) {
-		return in_array($checkValue, $value, true);
-	}
-}

vendor/pac/lib/Destination/Plain.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_Destination_Plain extends pac_Destination {
-
-	/**
-	 * @param string $token
-	 */
-	public function __construct($id, $tokens, array $supers = array()) {
-		parent::__construct($id, $tokens, 'boolean', $supers);
-	}
-
-	public function evaluate($value, $checkValue) {
-		return $value === $checkValue;
-	}
-
-	public function normalizeValue($value) {
-		$value = parent::normalizeValue($value);
-
-		if (empty($value))   return false;
-		if (is_bool($value)) return $value;
-
-		if (is_array($value) && count($value) == 1) {
-			return $value[0];
-		}
-
-		throw new pac_Exception('Destination value is not valid.');
-	}
-}

vendor/pac/lib/DestinationProvider.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-interface pac_DestinationProvider {
-	/**
-	 * returns a pac_Destination
-	 *
-	 * @param  string $destination   the individual id of the destination
-	 * @return pac_Destination  destination
-	 */
-	public function getDestination($destination);
-}

vendor/pac/lib/Exception.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_Exception extends Exception {
-
-}

vendor/pac/lib/Loader.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_Loader {
-	public static function loadClass($className) {
-		$className = str_replace('pac_', '', $className);
-		if (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.$className.'.php')){
-			require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.$className.'.php');
-		}
-	}
-
-	public static function register() {
-		if (function_exists('spl_autoload_register')) {
-			spl_autoload_register(array(__CLASS__, 'loadClass'));
-		}
-		else {
-			function __autoload($className) {
-				self::loadClass($className);
-			}
-		}
-	}
-}

vendor/pac/lib/PDOProvider.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-interface pac_PDOProvider {
-
-	/**
-	 * return current PDO instance
-	 *
-	 * @return PDO
-	 */
-	public function getPDO();
-
-	/**
-	 * return current table prefix
-	 *
-	 * @return string
-	 */
-	public function getPrefix();
-
-}

vendor/pac/lib/Role.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_Role {
-
-	private $id;
-	private $includes;
-	private $title;
-	private $cache = array();
-
-	public function __construct($id, $title, $includes) {
-		$this->id       = $id;
-		$this->title    = $title;
-		$this->includes = $includes;
-	}
-
-	public function getId() {
-		return $this->id;
-	}
-
-	public function getIncludes() {
-		return $this->includes;
-	}
-
-	public function getTitle() {
-		return $this->title;
-	}
-
-	public function setIncludes($includes) {
-		$this->includes = $includes;
-	}
-
-	/**
-	 * @return array
-	 */
-	public function isIncludedIn() {
-		$stmt = pac_Core::pdo()->prepare('SELECT `role_id` FROM `'.pac_Core::dbTable(pac_Core::ROLES_INCLUDES_TABLE).'` WHERE `included_role_id` = :included_role_id');
-		$stmt->execute(array('included_role_id' => $this->id));
-		$included_in = $stmt->fetchAll(PDO::FETCH_COLUMN, 'role_id');
-
-		return $included_in;
-	}
-
-	/**
-	 * @return boolean
-	 */
-	public function isIncluded() {
-		$included_in = $this->isIncludedIn();
-		return !empty($included_in);
-	}
-
-	/**
-	 * @return array
-	 */
-	public function getAllIncludedRoles() {
-		$res     = array();
-		$service = pac_RoleService::getInstance();
-
-		foreach ($this->includes as $include_id) {
-			if (!array_key_exists($include_id, $res)) {
-				$role             = $service->getRoleById($include_id);
-				$res[$include_id] = $role;
-
-				foreach ($role->getAllIncludedRoles() as $xrole) {
-					if (!array_key_exists($xrole->getId(), $res)) {
-						$res[] = $xrole;
-					}
-				}
- 			}
-		}
-
-		return $res;
-	}
-
-	public function setTitle($title) {
-		$this->title = (string) $title;
-	}
-
-	public function hasIncludes() {
-		return !empty($this->includes);
-	}
-
-	public function hasPermission(pac_Destination $destination, $token, $checkValue = true) {
-		$destination->validateToken($token);
-		if(!isset($this->cache[$destination->getId()])) $this->cache[$destination->getId()] = array();
-		if(!isset($this->cache[$destination->getId()][$token])) {
-			$stmt = pac_Core::prepare('SELECT `value` FROM '.pac_Core::dbTable(pac_Core::ROLES_DESTINATIONS_TABLE).' WHERE `destination` = :destination AND `token` = :token AND `role_id` = :role_id');
-			$stmt->execute(array('destination' => $destination->getId(), 'token' => $token, 'role_id' => $this->getId()));
-			$this->cache[$destination->getId()][$token] = $destination->normalizeValue($stmt->fetchAll(PDO::FETCH_COLUMN, 'value'));
-		}
-		$value = $this->cache[$destination->getId()][$token];
-		if(empty($value)) return false;
-		return $destination->evaluate($value, $checkValue);
-	}
-}

vendor/pac/lib/RoleService.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_RoleService {
-
-	private $list;
-	private static $instance;
-
-	/**
-	 *
-	 * @return pac_RoleService
-	 */
-	public static function getInstance() {
-		if (!isset(self::$instance)) {
-			self::$instance = new self();
-		}
-		return self::$instance;
-	}
-
-	private function __construct() {
-		$this->list = array();
-		$this->build();
-	}
-
-	/**
-	 * do not clone!!!
-	 */
-	private function __clone() {
-
-	}
-
-	/**
-	 * Reads the role graph.
-	 *
-	 */
-	private function build() {
-		// TODO: get from cache
-		$pdo   = pac_Core::pdo();
-		$stmt  = $pdo->prepare('SELECT * FROM '.pac_Core::dbTable(pac_Core::ROLES_TABLE));
-		$stmt2 = $pdo->prepare('SELECT `included_role_id` FROM `'.pac_Core::dbTable(pac_Core::ROLES_INCLUDES_TABLE).'` WHERE `role_id` = :role_id');
-
-		$stmt->execute();
-
-		foreach ($stmt->fetchAll() as $row) {
-			$stmt2->execute(array('role_id' => $row['id']));
-			$includes = $stmt2->fetchAll(PDO::FETCH_COLUMN, 'included_role_id');
-
-			$role = new pac_Role($row['id'], $row['title'], $includes);
-			$this->list[$role->getId()] = $role;
-		}
-	}
-
-	public function __destruct() {
-		// TODO: set to cache
-	}
-
-	/**
-	 * Createa a role.
-	 *
-	 * @param string $title
-	 * @param array $includes
-	 */
-	public function createRole($title, $includes) {
-		$pdo = pac_Core::pdo();
-		$pdo->beginTransaction();
-
-		try {
-			$stmt = $pdo->prepare('INSERT INTO `'.pac_Core::dbTable(pac_Core::ROLES_TABLE).'` (title) VALUES (:title)');
-			$stmt->execute(array('title' => $title));
-			$roleId = $pdo->lastInsertId();
-
-			if (!empty($includes)) {
-				$stmt = $pdo->prepare('INSERT INTO `'.pac_Core::dbTable(pac_Core::ROLES_INCLUDES_TABLE).'` (`role_id`, `included_role_id`) VALUES (:role_id, :included_role_id)');
-
-				foreach ($includes as $include) {
-					$stmt->execute(array('role_id' => $roleId, 'included_role_id' => $include));
-				}
-			}
-
-			$pdo->commit();
-
-			$role = new pac_Role($roleId, $title, $includes);
-			$this->list[$role->getId()] = $role;
-		}
-		catch (Exception $e) {
-			$pdo->rollBack();
-			throw new pac_Exception($e->getMessage(), $e->getCode());
-		}
-	}
-
-	/**
-	 * Sets some Parames on a role.
-	 *
-	 * @param int    $roleId
-	 * @param string $title
-	 * @param array  $includes
-	 */
-	public function editRole($roleId, $title, $includes) {
-		$this->validate($roleId);
-		$pdo = pac_Core::pdo();
-		$pdo->beginTransaction();
-
-		try {
-			$stmt = $pdo->prepare('UPDATE `'.pac_Core::dbTable(pac_Core::ROLES_TABLE).'` SET `title` = :title WHERE `id` = :role_id');
-			$stmt->execute(array('title' => $title, 'role_id' => $roleId));
-			$this->deleteRoleIncludes($roleId);
-
-			if (!empty($includes)) {
-				$stmt = $pdo->prepare('INSERT INTO `'.pac_Core::dbTable(pac_Core::ROLES_INCLUDES_TABLE).'` (`role_id`, `included_role_id`) VALUES (:role_id, :included_role_id)');
-
-				foreach ($includes as $include) {
-					$stmt->execute(array('role_id' => $roleId, 'included_role_id' => $include));
-				}
-			}
-
-			$pdo->commit();
-
-			$role = new pac_Role($roleId, $title, $includes);
-			$this->list[$role->getId()] = $role;
-		}
-		catch (Exception $e) {
-			$pdo->rollBack();
-			throw new pac_Exception($e->getMessage().' '.$e->getCode());
-		}
-	}
-
-	/**
-	 * Deletes a role.
-	 *
-	 * @param int $roleId
-	 */
-	public function deleteRole($roleId) {
-		$role       = $this->getRoleById($roleId);
-		$includedIn = $role->isIncludedIn();
-
-		// Do not allow deletion of roles which are included in others.
-		if (!empty($includedIn)) {
-			throw new pac_Exception('Can not delete role! The role with ID: '.$roleId.' in included in roles '.implode(', ', $includedIn).'.');
-		}
-
-		$pdo  = pac_Core::pdo();
-		$data = array('role_id' => $roleId);
-		$pdo->beginTransaction();
-
-		try {
-			$stmt = $pdo->prepare('DELETE FROM `'.pac_Core::dbTable(pac_Core::ROLES_TABLE).'` WHERE `id` = :role_id');
-			$stmt->execute($data);
-			$stmt = $pdo->prepare('DELETE FROM `'.pac_Core::dbTable(pac_Core::ROLES_USERS_TABLE).'` WHERE `role_id` = :role_id');
-			$stmt->execute($data);
-			$this->deleteRoleIncludes($roleId);
-			$this->deleteDestinationValues($roleId);
-			$pdo->commit();
-			unset($this->list[$roleId]);
-		}
-		catch (Exception $e) {
-			$pdo->rollBack();
-			throw new pac_Exception($e->getMessage().' '.$e->getCode());
-		}
-	}
-
-	/**
-	 * Sets permission values for a role.
-	 *
-	 * $values is an assoc array of destination token and the value to set.
-	 * Values can be arrays, to set list type destinations at once.
-	 *
-	 * @param int    $roleId
-	 * @param string $destinationId
-	 * @param array  $values
-	 */
-	public function setDestinationValues($roleId, $destinationId, $values) {
-		$this->validate($roleId);
-		$destination = pac_Core::getDestination($destinationId);
-
-		$pdo = pac_Core::pdo();
-		pac_Core::pdo()->beginTransaction();
-
-		try {
-			$this->deleteDestinationValues($roleId, $destinationId);
-			$stmt = pac_Core::prepare('INSERT INTO '.pac_Core::dbTable(pac_Core::ROLES_DESTINATIONS_TABLE).' (`token`, `role_id`, `destination`, `value`) VALUES (:token, :role_id, :destination, :value)');
-			foreach ($values as $token => $value) {
-				$destination->validateToken($token);
-				$value = $destination->normalizeValue($value);
-				if(!is_array($value)) {
-					$value = array($value);
-				}
-				foreach ($value as $val) {
-					$stmt->execute(array('token' => $token, 'role_id' => $roleId, 'destination' => $destinationId, 'value' => $val));
-				}
-			}
-			$pdo->commit();
-		}
-		catch (Exception $e) {
-			$pdo->rollBack();
-			throw new pac_Exception($e->getMessage().' '.$e->getCode());
-		}
-	}
-
-	/**
-	 * returns a group by its id, or return null
-	 *
-	 * @throws pac_Exception  if the role not exists.
-	 * @param  int $roleId
-	 * @return pac_Role
-	 */
-	public function getRoleById($roleId) {
-		$this->validate($roleId);
-		return $this->list[$roleId];
-	}
-
-	/**
-	 * Checks if a role exists.
-	 *
-	 * @param  int $roleId
-	 * @return boolean
-	 */
-	public function exists($roleId) {
-		return array_key_exists($roleId, $this->list);
-	}
-
-	/**
-	 * Checks if a role exists. Throws a pac_Exception is not.
-	 *
-	 * @throws pac_Exception  if the role not exists.
-	 * @param  int $roleId
-	 * @return boolean        always true
-	 */
-	public function validate($roleId) {
-		if (!$this->exists($roleId)) {
-			throw new pac_Exception('The Role with ID '.$roleId.' does not exist!');
-		}
-
-		return true;
-	}
-
-	/**
-	 * Returns all Roles in this configuration.
-	 *
-	 * @return array
-	 */
-	public function getAllRoles() {
-		return $this->list;
-	}
-
-	/**
-	 * Deletes all permission values for a role.
-	 *
-	 * @param  int $roleId
-	 * @return boolean
-	 */
-	public function deleteDestinationValues($roleId, $destinationId = null) {
-		$where = ' WHERE `role_id` = :role_id';
-		$whereData = array('role_id' => $roleId);
-		if($destinationId !== null) {
-			$where .= ' AND `destination` = :destination';
-			$whereData['destination'] = $destinationId;
-		}
-		$stmt = pac_Core::prepare('DELETE FROM `'.pac_Core::dbTable(pac_Core::ROLES_DESTINATIONS_TABLE).'`'.$where);
-		return $stmt->execute($whereData);
-	}
-
-	/**
-	 * Deletes all includes for a role.
-	 *
-	 * @param  int $roleId
-	 * @return boolean
-	 */
-	protected function deleteRoleIncludes($roleId) {
-		$stmt = pac_Core::prepare('DELETE FROM `'.pac_Core::dbTable(pac_Core::ROLES_INCLUDES_TABLE).'` WHERE `role_id` = :role_id');
-		return $stmt->execute(array('role_id' => $roleId));
-	}
-
-	/**
-	 * Deletes all permission values for a role.
-	 *
-	 * @param  int $roleId
-	 * @return boolean
-	 */
-	public function getUsersWithRole(pac_Role $role) {
-		$where     = ' WHERE `role_id` = :role_id';
-		$whereData = array('role_id' => $role->getId());
-
-		$stmt = pac_Core::prepare('SELECT COUNT(*) AS c FROM `'.pac_Core::dbTable(pac_Core::ROLES_USERS_TABLE).'`'.$where);
-		$stmt->execute($whereData);
-
-		return (int) $stmt->fetchColumn(0);
-	}
-}

vendor/pac/lib/User.php

-<?php
-/*
- * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
-
-class pac_User {
-	private $id;
-	private $roles;
-	private $cache;
-
-	public function __construct($id) {
-		$stmt = pac_Core::pdo()->prepare('SELECT role_id FROM '.pac_Core::dbTable(pac_Core::ROLES_USERS_TABLE).' WHERE user_id = :user_id');
-		$stmt->execute(array('user_id' => $id));
-
-		$this->id    = $id;
-		$this->roles = $stmt->fetchAll(PDO::FETCH_COLUMN, 'role_id');
-		$this->cache = array();
-	}
-
-	public function getId() {
-		return $this->id;
-	}
-
-	public function hasRole($roleId) {
-		return in_array($roleId, $this->roles);
-	}
-
-	public function getRoles() {
-		return $this->roles;
-	}
-
-	public function setRoles($roleIds) {
-		$pdo = pac_Core::pdo();
-		$pdo->beginTransaction();
-
-		try {
-			$stmt = pac_Core::pdo()->prepare('DELETE FROM '.pac_Core::dbTable(pac_Core::ROLES_USERS_TABLE).' WHERE user_id = :user_id');
-			$stmt->execute(array('user_id' => $this->getId()));
-
-			$stmt = pac_Core::pdo()->prepare('INSERT INTO '.pac_Core::dbTable(pac_Core::ROLES_USERS_TABLE).' (user_id, role_id) VALUES (:user_id, :role_id)');
-
-			foreach ($roleIds as $roleId) {
-				$stmt->execute(array('user_id' => $this->getId(), 'role_id' => $roleId));
-			}
-
-			$pdo->commit();
-		}
-		catch (Exception $e) {
-			$pdo->rollBack();
-			throw new pac_Exception($e->getMessage(), $e->getCode());
-		}
-	}
-
-	protected function collectAllRoles() {
-		$res     = array();
-		$service = pac_RoleService::getInstance();
-
-		//$stmt = pac_Core::pdo()->prepare('SELECT DISTINCT `included_role_id` FROM '.PAC_CORE::dbTable(pac_Core::ROLES_INCLUDES_TABLE).' WHERE `role_id` IN (?)');
-		//$res = $stmt->fetchAll(PDO::FETCH_COLUMN, 'included_role_id');
-		//sly_dump($res);
-
-		foreach ($this->roles as $roleId) {
-			$role = $service->getRoleById($roleId);
-			$res[$roleId] = $role;
-
-			foreach ($role->getAllIncludedRoles() as $xrole) {
-				if (!array_key_exists($xrole->getId(), $res)) {
-					$res[$xrole->getId()] = $xrole;
-				}
-			}
-		}
-
-		return $res;
-	}
-
-	public function hasPermission($destinationId, $token, $checkValue = true) {
-		$destination = pac_Core::getDestination($destinationId);
-		$result      = $this->getFromCache($destination, $token, $checkValue);
-
-		if($result === null) {
-			$destination->validateToken($token);
-			$roles  = $this->collectAllRoles();
-			$result = false;
-			foreach($roles as $role) {
-				$result = $role->hasPermission($destination, $token, $checkValue);
-				if($result === true) break;
-			}
-			$this->setToCache($destination, $token, $checkValue, $result);
-		}
-		return $result;
-	}
-
-	private function getFromCache(pac_Destination $destination, $token, $checkValue) {
-		if(!array_key_exists($destination->getId(), $this->cache)) return null;
-		if(!array_key_exists($token, $this->cache[$destination->getId()])) return null;
-
-		if($destination instanceof pac_Destination_Plain) {
-			return $this->cache[$destination->getId()][$token];
-		}elseif($destination instanceof pac_Destination_List) {
-			if(array_key_exists($checkValue, $this->cache[$destination->getId()][$token])) {
-				return $this->cache[$destination->getId()][$token][$checkValue];
-			}
-		}
-		return null;
-	}
-
-	protected function setToCache(pac_Destination $destination, $token, $checkValue, $result) {
-		if($destination instanceof pac_Destination_Plain) {
-			$this->cache[$destination->getId()][$token] = $result;
-		}elseif($destination instanceof pac_Destination_List) {
-			$this->cache[$destination->getId()][$token][$checkValue] = $result;
-		}
-	}
-}