UserList.class.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * User list class
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2012 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Auth
  9. */
  10. class GitPHP_UserList
  11. {
  12. /**
  13. * Stores the users
  14. *
  15. * @var array
  16. */
  17. protected $users = array();
  18. /**
  19. * Loads a user file
  20. *
  21. * @param string $userFile config file to load
  22. */
  23. public function LoadUsers($userFile)
  24. {
  25. if (!is_readable($userFile))
  26. return;
  27. if (!include($userFile))
  28. return;
  29. if (isset($gitphp_users) && is_array($gitphp_users)) {
  30. foreach ($gitphp_users as $user) {
  31. if (empty($user['username']) || empty($user['password']))
  32. continue;
  33. $this->users[$user['username']] = new GitPHP_User($user['username'], $user['password']);
  34. }
  35. }
  36. }
  37. /**
  38. * Get the user count
  39. *
  40. * @return int count
  41. */
  42. public function GetCount()
  43. {
  44. return count($this->users);
  45. }
  46. /**
  47. * Get a user
  48. *
  49. * @return GitPHP_User|null user object if found
  50. * @param string $username username
  51. */
  52. public function GetUser($username)
  53. {
  54. if (empty($username))
  55. return null;
  56. if (isset($this->users[$username]))
  57. return $this->users[$username];
  58. return null;
  59. }
  60. /**
  61. * Add a user
  62. *
  63. * @param GitPHP_User $user user
  64. */
  65. public function AddUser($user)
  66. {
  67. if (!$user)
  68. return;
  69. $username = $user->GetUsername();
  70. $password = $user->GetPassword();
  71. if (empty($username) || empty($password))
  72. return;
  73. $this->users[$username] = $user;
  74. }
  75. /**
  76. * Remove a user
  77. *
  78. * @param string $username username
  79. */
  80. public function RemoveUser($username)
  81. {
  82. if (empty($username))
  83. return;
  84. unset($this->users[$username]);
  85. }
  86. }