All pastes #521861 Raw Edit

Miscellany

public text v1 · immutable
#521861 ·published 2007-05-30 09:01 UTC
rendered paste body
<?php
/**
 * @Title: class.access.php
 * @Author: Brendan Scarvell (xenoix)
 * @Note:
 *	- Example on how to make a web based Active Directory web management with restrictions etc.
 *
 * @Todo:
 * 	- Make permissions and users to be stored in a database
 *	- Add directories and "shares" for the users to inherit the permissions to
**/
abstract class user{
	public function __construct($name)
	{
		$this->name = $name;
	}

	public function getName()
	{
		return $this->name;
	}

	// Permission Methods
	public static function hasReadPermission()
	{
		return true;
	}
	
	public static function hasModifyPermission()
	{
		return false;
	}

	public static function hasDeletePermission(){
		return false;
	}

	// Customization methods
	public static function wantsFlashInterface()
	{
		return true;
	}
	
	protected $name = NULL;
}

class GuestUser extends User{
	public static function hasModifyPermission()
	{
		return true;
	}
}

class AdminUser extends User{
	public static function hasModifyPermission()
	{
		return true;
	}
	
	public static function hasDeletePermission()
	{
		return true;
	}

	public static function wantsFlashInterface()
	{
		return false;
	}
}

Class UserFactory{
	private static $users = array("xenoix" => "admin", "reZo" => "guest", "ace" => "customer");

	public static function Create($name)
	{
		if(!isset(self::$users[$name])){
			// Error out user doesnt exist
			echo 'Error: User does not exist';
		}
		
		switch(self::$users[$name]){
			case 'guest':
				return new GuestUser($name);
				break;
			case 'customer':
				return new CustomerUser($name);
				break;
			case 'admin':
				return new AdminUser($name);
				break;
			default:
				// By default, return a guest user
				return new GuestUser($name);
				break;
		}
	}
}

function boolToStr($b)
{
	$b = ($b == true) ? "Yes\n" : "No\n";
	return $b;
}

function displayPermissions(User $obj)
{
	echo $obj->getName() . "'s permissions:\n";
	echo "<br />Read: " . boolToStr($obj->hasReadPermission());
	echo "<br />Modify: " . boolToStr($obj->hasModifyPermission());
	echo "<br />Delete: " . boolToStr($obj->hasDeletePermission());
}

function displayRequirements(){
	if($obj->wantsFlashInterface())
	{
		echo $obj->getName() . ' requires Flash<br />';
	}
}

$logins = array('xenoix','reZo','ace');

foreach($logins as $login){
	displayPermissions(UserFactory::Create($login));
	displayRequirements(UserFactory::Create($login));	
}
?>