# Authentication override module example

1. <?php
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. */
14. // Let's presume that this is an external session.
15. session_start();
17. // We can authenticate the user against our own user database.
18. // This demo hook will authenticate the user with username "foo"
19. // and password "bar" as the Phorum user with user_id = 1.
20. function test_user_authenticate($data)
21. {
22. // Only do this for the forum session. We do not touch the admin session.
23. if ($data['type'] == PHORUM_FORUM_SESSION) {
24. if ($data['username'] == 'foo' && $data['password'] == 'bar') {
25. $data['user_id'] = 1;
26. } else {
27. $data['user_id'] = FALSE;
28. }
29. }
31. return $data;
32. }
34. // This hook overrides creating a Phorum user session. Instead of running
35. // a Phorum session, we use the PHP session system to track the logged
36. // in user. We do this by storing the active user_id in the $_SESSION
37. // variable.
38. function test_user_session_create($type)
39. {
40. // Only do this for the forum session. We do not touch the admin session.
41. if ($type == PHORUM_FORUM_SESSION) {
42. $_SESSION['loggedin_user'] = $GLOBALS["PHORUM"]["user"]["user_id"];
43. return NULL;
44. } else {
45. return $type;
46. }
47. }
49. // This hook overrides the Phorum user session restore process. We use
50. // the user id that we stored in the PHP $_SESSION variable as the
51. // active Phorum user.
52. function test_user_session_restore($data)
53. {
54. if ($_SESSION['loggedin_user']) {
55. $user_id = $_SESSION['loggedin_user'];
56. $data[PHORUM_SESSION_LONG_TERM] = $user_id;
57. $data[PHORUM_SESSION_SHORT_TERM] = $user_id;
58. } else {
59. $data[PHORUM_SESSION_LONG_TERM] = FALSE;
60. $data[PHORUM_SESSION_SHORT_TERM] = FALSE;
61. }
63. return $data;
64. }
66. // This hook overrides destroying a Phorum user session. Instead of destroying
67. // a Phorum session, we clear the user_id that is stored in the $_SESSION
68. // variable.
69. function test_user_session_destroy($type)
70. {
71. // Only do this for the forum session. We do not touch the admin session.
72. if ($type == PHORUM_FORUM_SESSION) {
73. $_SESSION['loggedin_user'] = FALSE;
74. return NULL;
75. } else {
76. return $type;
77. }
78. }
80. ?>

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