Authentication override module example

  1. <?php
  2.  
  3. /* phorum module info
  4. hook:  user_authenticate|test_user_authenticate
  5. hook:  user_session_create|test_user_session_create
  6. hook:  user_session_restore|test_user_session_restore
  7. hook:  user_session_destroy|test_user_session_destroy
  8. title: User API hook demo
  9. desc:  This module is a demo for demonstrating the user API hooks, which can be used for implementing external user authentication and session.
  10. author: Phorum Dev Team
  11. url: http://www.phorum.org/
  12. */
  13.  
  14. // Let's presume that this is an external session.
  15.  
  16. // We can authenticate the user against our own user database.
  17. // This demo hook will authenticate the user with username "foo"
  18. // and password "bar" as the Phorum user with user_id = 1.
  19. function test_user_authenticate($data)
  20. {
  21.     // Only do this for the forum session. We do not touch the admin session.
  22.     if ($data['type'== PHORUM_FORUM_SESSION{
  23.         if ($data['username'== 'foo' && $data['password'== 'bar'{
  24.             $data['user_id'1;
  25.         else {
  26.             $data['user_id'FALSE;
  27.         }
  28.     }
  29.  
  30.     return $data;
  31. }
  32.  
  33. // This hook overrides creating a Phorum user session. Instead of running
  34. // a Phorum session, we use the PHP session system to track the logged
  35. // in user. We do this by storing the active user_id in the $_SESSION
  36. // variable.
  37. function test_user_session_create($type)
  38. {
  39.     // Only do this for the forum session. We do not touch the admin session.
  40.     if ($type == PHORUM_FORUM_SESSION{
  41.         $_SESSION['loggedin_user'$GLOBALS["PHORUM"]["user"]["user_id"];
  42.         return NULL;
  43.     else {
  44.         return $type;
  45.     }
  46. }
  47.  
  48. // This hook overrides the Phorum user session restore process. We use
  49. // the user id that we stored in the PHP $_SESSION variable as the
  50. // active Phorum user.
  51. function test_user_session_restore($data)
  52. {
  53.     if ($_SESSION['loggedin_user']{
  54.         $user_id $_SESSION['loggedin_user'];
  55.         $data[PHORUM_SESSION_LONG_TERM]  $user_id;
  56.         $data[PHORUM_SESSION_SHORT_TERM$user_id;
  57.     else {
  58.         $data[PHORUM_SESSION_LONG_TERM]  FALSE;
  59.         $data[PHORUM_SESSION_SHORT_TERMFALSE;
  60.     }
  61.  
  62.     return $data;
  63. }
  64.  
  65. // This hook overrides destroying a Phorum user session. Instead of destroying
  66. // a Phorum session, we clear the user_id that is stored in the $_SESSION
  67. // variable.
  68. function test_user_session_destroy($type)
  69. {
  70.     // Only do this for the forum session. We do not touch the admin session.
  71.     if ($type == PHORUM_FORUM_SESSION{
  72.         $_SESSION['loggedin_user'FALSE;        
  73.         return NULL;
  74.     else {
  75.         return $type;
  76.     }
  77. }
  78.  
  79. ?>

Documentation generated on Fri, 24 Aug 2012 01:45:40 -0500 by phpDocumentor 1.4.3