<?php
/*
 * Copyright (c) 2013, 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 WV42_Filter_Roles extends WV_Filter_And {
	/**
	 * @param string         $metainfo   name of the metainfo holding the list of required roles for the article
	 * @param sly_Model_User $user       list of rights the current (or any other) user has
	 */
	public function __construct($metainfo, sly_Model_User $user) {
		$filters = array(new WV2_Filter_Metainfo($metainfo));
		$or      = array(
			new WV2_Filter_Value(null, '='),
			new WV2_Filter_Value('', '=')
		);

		if ($user->isAdmin()) {
			$or[] = new WV_Filter_True();
		}
		else {
			$roles = $user->getRoles();

			foreach ($roles as $role) {
				$or[] = new WV2_Filter_Value("%|$role|%", 'LIKE');
			}
		}

		$filters[] = new WV_Filter_Or($or);

		parent::__construct($filters);
	}
}
