Authenticate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * The Authenticate package is used to define helper methods related to authentication.
  4. *
  5. * Authentication will only work if sessions are enabled. Currently there is no
  6. * testing error reporting of this and probably won't be until the PHP5 version. Complex
  7. * error handling is just too cumbersome in php < 5.
  8. *
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @copyright (c) 2003 amfphp.org
  11. * @package flashservices
  12. * @subpackage util
  13. * @version $Id: Authenticate.php,v 1.11 2005/03/24 22:19:48 pmineault Exp $
  14. */
  15. class Authenticate {
  16. /**
  17. * isAuthenticated hides the session implementation for tracking user access.
  18. *
  19. * @return bool Whether the current user has been authenticated
  20. */
  21. function isAuthenticated () {
  22. if (isset($_SESSION['amfphp_username'])) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. /**
  29. * getAuthUser returns the current user name of the user that is logged in with the session.
  30. * @return string the name of the authenticated user
  31. */
  32. function getAuthUser ()
  33. {
  34. if(isset($_SESSION['amfphp_username']))
  35. {
  36. return $_SESSION['amfphp_username'];
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. /**
  44. * Returns true if the client is authenticated and the requested roles
  45. * passed match.
  46. *
  47. * Every service method can have a comman delimited list of roles that are
  48. * required to access a service. Every user can also be assigned to a seperate
  49. * comma delimited list to roles they belong to. This method compares those two
  50. * strings (lists) and makes sure there is atleast one match.
  51. *
  52. * @param string $roles comma delimited list of the methods roles
  53. * @return bool Whether the user is in the proper role.
  54. */
  55. function isUserInRole($roles) {
  56. $methodRoles = explode(",", $roles); // split the method roles into an array
  57. foreach($methodRoles as $key => $role) {
  58. $methodRoles[$key] = strtolower(trim($role));
  59. }
  60. if(!isset($_SESSION['amfphp_roles']))
  61. {
  62. $_SESSION['amfphp_roles'] = "";
  63. }
  64. $userRoles = explode(",", $_SESSION['amfphp_roles']); // split the users session roles into an array
  65. foreach($userRoles as $key => $role) {
  66. $userRoles[$key] = strtolower(trim($role));
  67. if (in_array($userRoles[$key], $methodRoles)) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * login assumes that the user has verified the credentials and logs in the user.
  75. *
  76. * The login method hides the session implementation for storing the user credentials
  77. *
  78. * @param string $name The user name
  79. * @param string $roles The comma delimited list of roles for the user
  80. */
  81. function login($name, $roles) {
  82. if(!session_id())
  83. {
  84. session_start();
  85. }
  86. $_SESSION['amfphp_username'] = $name;
  87. $_SESSION['amfphp_roles'] = $roles;
  88. }
  89. /**
  90. * logout kills the user session and terminates the login properties
  91. */
  92. function logout() {
  93. $_SESSION['amfphp_username'] = null;
  94. $_SESSION['amfphp_roles'] = null;
  95. if(isset($_SESSION['amfphp_username']))
  96. {
  97. unset($_SESSION['amfphp_username']);
  98. }
  99. if(isset($_SESSION['amfphp_roles']))
  100. {
  101. unset($_SESSION['amfphp_roles']);
  102. }
  103. return true;
  104. }
  105. }
  106. ?>