Add user and userlist classes
Add user and userlist classes

--- a/include/AutoLoader.class.php
+++ b/include/AutoLoader.class.php
@@ -79,6 +79,8 @@
 			$path = 'cache/';
 		} else if (strncmp($classname, 'Route', 5) === 0) {
 			$path = 'router/';
+		} else if (strncmp($classname, 'User', 4) === 0) {
+			$path = 'auth/';
 		} else if (in_array($classname, array(
 				'Config',
 				'DebugLog',

--- /dev/null
+++ b/include/auth/User.class.php
@@ -1,1 +1,78 @@
+<?php
+/**
+ * User class
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Auth
+ */
+class GitPHP_User
+{
+	/**
+	 * Username
+	 *
+	 * @var string
+	 */
+	protected $username;
 
+	/**
+	 * Password
+	 *
+	 * @var string
+	 */
+	protected $password;
+
+	/**
+	 * Constructor
+	 *
+	 * @param string $username username
+	 * @param string $password password
+	 */
+	public function __construct($username, $password)
+	{
+		$this->username = $username;
+		$this->password = $password;
+	}
+
+	/**
+	 * Get the username
+	 *
+	 * @return string username
+	 */
+	public function GetUsername()
+	{
+		return $this->username;
+	}
+
+	/**
+	 * Set the username
+	 *
+	 * @param string $username username
+	 */
+	public function SetUsername($username)
+	{
+		$this->username = $username;
+	}
+
+	/**
+	 * Get the password
+	 *
+	 * @return string password
+	 */
+	public function GetPassword()
+	{
+		return $this->password;
+	}
+
+	/**
+	 * Set the password
+	 *
+	 * @param string $password password
+	 */
+	public function SetPassword($password)
+	{
+		$this->password = $password;
+	}
+}
+

--- /dev/null
+++ b/include/auth/UserList.class.php
@@ -1,1 +1,100 @@
+<?php
+/**
+ * User list class
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Auth
+ */
+class GitPHP_UserList
+{
+	/**
+	 * Stores the users
+	 *
+	 * @var array
+	 */
+	protected $users = array();
 
+	/**
+	 * Loads a user file
+	 *
+	 * @param string $userFile config file to load
+	 */
+	public function LoadUsers($userFile)
+	{
+		if (!is_readable($userFile))
+			return;
+
+		if (!include($userFile))
+			return;
+
+		if (isset($gitphp_users) && is_array($gitphp_users)) {
+			foreach ($gitphp_users as $user) {
+				if (empty($user['username']) || empty($user['password']))
+					continue;
+
+				$this->users[$user['username']] = new GitPHP_User($user['username'], $user['password']);
+			}
+		}
+	}
+
+	/**
+	 * Get the user count
+	 *
+	 * @return int count
+	 */
+	public function GetCount()
+	{
+		return count($this->users);
+	}
+
+	/**
+	 * Get a user
+	 *
+	 * @return GitPHP_User|null user object if found
+	 * @param string $username username
+	 */
+	public function GetUser($username)
+	{
+		if (empty($username))
+			return null;
+
+		if (isset($this->users[$username]))
+			return $this->users[$username];
+
+		return null;
+	}
+
+	/**
+	 * Add a user
+	 *
+	 * @param GitPHP_User $user user
+	 */
+	public function AddUser($user)
+	{
+		if (!$user)
+			return;
+
+		$username = $user->GetUsername();
+		$password = $user->GetPassword();
+		if (empty($username) || empty($password))
+			return;
+
+		$this->users[$username] = $user;
+	}
+
+	/**
+	 * Remove a user
+	 *
+	 * @param string $username username
+	 */
+	public function RemoveUser($username)
+	{
+		if (empty($username))
+			return;
+
+		unset($this->users[$username]);
+	}
+}
+

--- /dev/null
+++ b/test/auth/UserListTest.php
@@ -1,1 +1,33 @@
+<?php
+/**
+ * Userlist test class
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Test\Auth
+ */
+class GitPHP_UserListTest extends PHPUnit_Framework_TestCase
+{
+	public function testUserList()
+	{
+		$userlist = new GitPHP_UserList();
 
+		$this->assertEquals(0, $userlist->GetCount());
+
+		$user = new GitPHP_User('username', 'password');
+		$userlist->AddUser($user);
+
+		$this->assertEquals(1, $userlist->GetCount());
+
+		$user2 = $userlist->GetUser('username');
+		$this->assertInstanceOf('GitPHP_User', $user2);
+		$this->assertEquals('username', $user2->GetUsername());
+		$this->assertEquals('password', $user2->GetPassword());
+
+		$userlist->RemoveUser('username');
+		$this->assertNull($userlist->GetUser('username'));
+		$this->assertEquals(0, $userlist->GetCount());
+	}
+}
+

--- /dev/null
+++ b/test/auth/UserTest.php
@@ -1,1 +1,24 @@
+<?php
+/**
+ * User test class
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Test\Auth
+ */
+class GitPHP_UserTest extends PHPUnit_Framework_TestCase
+{
+	public function testUser()
+	{
+		$user = new GitPHP_User('user','pass');
+		$this->assertEquals('user', $user->GetUsername());
+		$this->assertEquals('pass', $user->GetPassword());
+		
+		$user->SetUsername('user2');
+		$this->assertEquals('user2', $user->GetUsername());
+		$user->SetPassword('pass2');
+		$this->assertEquals('pass2', $user->GetPassword());
+	}
+}
 

comments