/lib/Foundry/Core/Auth/Service/InMemory.php

https://github.com/justjohn/foundry-core · PHP · 258 lines · 110 code · 25 blank · 123 comment · 29 complexity · e1309bd7734b44ae6f3788c26371dea0 MD5 · raw file

  1. <?php
  2. /**
  3. * In-memory Authentication Service Implementation
  4. *
  5. * This file contains the logic required to authenticate against an
  6. * in-memory (temporary or session limited) service. Mainly used for
  7. * testing other classes that depend on an auth service.
  8. *
  9. * @category Foundry-Core
  10. * @package Foundry\Core\Auth\Service
  11. * @author John Roepke <john@justjohn.us>
  12. * @copyright 2010-2011 John Roepke
  13. * @license http://phpfoundry.com/license/bsd New BSD license
  14. * @version 1.0.0
  15. */
  16. namespace Foundry\Core\Auth\Service;
  17. /**
  18. * In-memory Authentication Service
  19. *
  20. * @category Foundry-Core
  21. * @package Foundry\Core\Auth\Service
  22. * @author John Roepke <john@justjohn.us>
  23. * @copyright 2010-2011 John Roepke
  24. * @license http://phpfoundry.com/license/bsd New BSD license
  25. * @since 1.0.0
  26. */
  27. class InMemory {
  28. /**
  29. * The user database keyed by username.
  30. * @var array
  31. */
  32. private $users = array();
  33. /**
  34. * User passwords keyed by array.
  35. * @var array
  36. */
  37. private $user_passwords = array();
  38. /**
  39. * Groups keyed bu group name.
  40. * @var array
  41. */
  42. private $groups = array();
  43. /**
  44. * Authenticate a user.
  45. * @param string $username
  46. * @param string $password
  47. */
  48. public function authenticate($username, $password) {
  49. if (empty($username) || !isset($this->users[$username])) return false;
  50. return $this->user_passwords[$username] === $password;
  51. }
  52. /**
  53. * Change a user password.
  54. * @param string $username
  55. * @param string $password
  56. */
  57. public function changePassword($username, $password) {
  58. if (empty($username) || !isset($this->users[$username])) return false;
  59. $this->user_passwords[$username] = $password;
  60. return true;
  61. }
  62. /**
  63. * Check to see if a user exists.
  64. * @param string $username The username to check.
  65. * @return boolean
  66. */
  67. public function userExists($username) {
  68. return isset($this->users[$username]);
  69. }
  70. /**
  71. * Returns an array of all the users or arrays keyed by username.
  72. * @return array|boolean
  73. */
  74. public function getUsers() {
  75. return $this->users;
  76. }
  77. /**
  78. * Get a user's information as an array.
  79. * @param string $username
  80. * @return User
  81. */
  82. public function getUser($username) {
  83. if (!isset($this->users[$username])) return false;
  84. return $this->users[$username];
  85. }
  86. /**
  87. * Add a user.
  88. * @param User $user The attributes of the user to add.
  89. * @param string $password The user's password.
  90. * @return boolean true on sucess, false on failure.
  91. */
  92. public function addUser($user, $password) {
  93. $username = $user->getUsername();
  94. if (empty($username) || isset($this->users[$username])) return false;
  95. $this->users[$username] = $user;
  96. $this->user_passwords[$username] = $password;
  97. return true;
  98. }
  99. /**
  100. * Update a user.
  101. * @param User $user The attributes of the user to update.
  102. * @return boolean true on sucess, false on failure.
  103. */
  104. public function updateUser($user) {
  105. $username = $user->getUsername();
  106. if (!isset($this->users[$username])) return false;
  107. $this->users[$username] = $user;
  108. return true;
  109. }
  110. /**
  111. * Delete a user.
  112. * @param string $username The username to delete.
  113. * @return boolean true on success, false on failure
  114. */
  115. public function deleteUser($username) {
  116. if (empty($username) || !isset($this->users[$username])) return false;
  117. unset($this->users[$username]);
  118. unset($this->user_passwords[$username]);
  119. return true;
  120. }
  121. // Group Methods
  122. /**
  123. * Check to see if a group exists.
  124. * @param string $groupname The group name to check.
  125. * @return boolean
  126. */
  127. public function groupExists($groupname) {
  128. return isset($this->groups[$groupname]);
  129. }
  130. /**
  131. * Returns an array of all the groups keyed by group name.
  132. *
  133. * @return array
  134. */
  135. public function getGroups() {
  136. return $this->groups;
  137. }
  138. /**
  139. * Returns an array of Group names keyed by group name.
  140. * @return array an group names (strings) keyed by group names.
  141. */
  142. public function getGroupNames() {
  143. $group_names = array();
  144. if (!empty($this->groups)) {
  145. foreach (array_keys($this->groups) as $groupname) {
  146. $group_names[$groupname] = $groupname;
  147. }
  148. }
  149. return $group_names;
  150. }
  151. /**
  152. * Get a group's members.
  153. * @param array $groupname
  154. * @return Group
  155. */
  156. public function getGroup($groupname) {
  157. if (empty($groupname) || !isset($this->groups[$groupname])) return false;
  158. return $this->groups[$groupname];
  159. }
  160. /**
  161. * Add a group.
  162. * @param Group $group The group to add.
  163. * @return boolean true on sucess, false on failure.
  164. */
  165. public function addGroup($group) {
  166. $groupname = $group->getName();
  167. if (empty($groupname) || isset($this->groups[$groupname])) return false;
  168. $this->groups[$groupname] = $group;
  169. return true;
  170. }
  171. /**
  172. * Delete a group.
  173. * @param string $groupname The name of the group to delete.
  174. * @return boolean true on success, false on failure.
  175. */
  176. public function deleteGroup($groupname) {
  177. if (empty($groupname) || !isset($this->groups[$groupname])) return false;
  178. unset($this->groups[$groupname]);
  179. return true;
  180. }
  181. /**
  182. * Get an array of groups the given user is a member of.
  183. * @param string $username
  184. * @return array
  185. */
  186. public function getUserGroups($username) {
  187. if (empty($username) || !isset($this->users[$username])) return false;
  188. $user_groups = array();
  189. foreach ($this->groups as $groupname=>$group) {
  190. $users = $group->getUsers();
  191. if (isset($users[$username])) {
  192. $user_groups[$groupname] = $groupname;
  193. }
  194. }
  195. return $user_groups;
  196. }
  197. /**
  198. * Add a user to a group.
  199. * @param string $username The username to add to the group.
  200. * @param string $groupname The name of the group to add the user to.
  201. * @return boolean
  202. */
  203. public function addUserToGroup($username, $groupname) {
  204. if (empty($username) || empty($groupname)
  205. || !isset($this->groups[$groupname])
  206. || !isset($this->users[$username])) return false;
  207. $group = $this->groups[$groupname];
  208. $users = $group->getUsers();
  209. $users[$username] = $username;
  210. $group->setUsers($users);
  211. $this->groups[$groupname] = $group;
  212. return true;
  213. }
  214. /**
  215. * Remove a user from a group.
  216. * @param string $username The username to remove from the group.
  217. * @param string $groupname The name of the group to remove the user from.
  218. * @return boolean
  219. */
  220. public function removeUserFromGroup($username, $groupname) {
  221. if (empty($username) || empty($groupname)
  222. || !isset($this->groups[$groupname])
  223. || !isset($this->users[$username])) return false;
  224. $group = $this->groups[$groupname];
  225. $users = $group->getUsers();
  226. if (!isset($users[$username])) return false; // User is not in group
  227. unset($users[$username]);
  228. $group->setUsers($users);
  229. $this->groups[$groupname] = $group;
  230. return true;
  231. }
  232. }
  233. ?>