PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/application/libraries/login_manager.php

https://bitbucket.org/ramo_carlo/datamapper
PHP | 119 lines | 92 code | 10 blank | 17 comment | 15 complexity | 0a909c4e8c53d991e81f2d86e0e12300 MD5 | raw file
  1. <?php
  2. /**
  3. * Simple utility class to handle logins.
  4. */
  5. class Login_Manager {
  6. var $logged_in_user = NULL;
  7. function __construct($params = array())
  8. {
  9. $this->CI =& get_instance();
  10. $this->session =& $this->CI->session;
  11. if( ! isset($params['autologin']) || $params['autologin'] !== FALSE)
  12. {
  13. $required_group = -1;
  14. if(isset($params['required_group']))
  15. {
  16. $required_group = $params['required_group'];
  17. }
  18. $this->check_login($required_group);
  19. }
  20. }
  21. function check_login($required_group = -1)
  22. {
  23. // Special auto-setup routine
  24. if( ! $this->CI->db->table_exists('users'))
  25. {
  26. redirect('admin/reset_warning');
  27. }
  28. else
  29. {
  30. // see if there are any users in the system
  31. $u = new User();
  32. if($u->count() == 0)
  33. {
  34. redirect('admin/init');
  35. }
  36. }
  37. // if not logged in, automatically redirect
  38. $u = $this->get_user();
  39. if($u === FALSE)
  40. {
  41. $this->session->set_userdata('login_redirect', uri_string());
  42. redirect('login');
  43. }
  44. if($required_group > 0)
  45. {
  46. if($u->group->id > $required_group)
  47. {
  48. show_error('You do not have access to this section.');
  49. }
  50. }
  51. }
  52. /**
  53. * process_login
  54. * Validates that a username and password are correct.
  55. *
  56. * @param object $user The user containing the login information.
  57. * @return FALSE if invalid, TRUE or a redirect string if valid.
  58. */
  59. function process_login($user)
  60. {
  61. // attempt the login
  62. $success = $user->login();
  63. if($success)
  64. {
  65. // store the userid if the login was successful
  66. $this->session->set_userdata('logged_in_id', $user->id);
  67. // store the user for this request
  68. $this->logged_in_user = $user;
  69. // if a redirect is necessary, return it.
  70. $redirect = $this->session->userdata('login_redirect');
  71. if( ! empty($redirect))
  72. {
  73. $success = $redirect;
  74. }
  75. }
  76. return $success;
  77. }
  78. function logout()
  79. {
  80. $this->session->sess_destroy();
  81. $this->logged_in_user = NULL;
  82. }
  83. function get_user()
  84. {
  85. if(is_null($this->logged_in_user))
  86. {
  87. if( ! $this->CI->db->table_exists('users'))
  88. {
  89. return FALSE;
  90. }
  91. $id = $this->session->userdata('logged_in_id');
  92. if(is_numeric($id))
  93. {
  94. $u = new User();
  95. $u->get_by_id($id);
  96. if($u->exists()) {
  97. $u->group->get();
  98. $this->logged_in_user = $u;
  99. return $this->logged_in_user;
  100. }
  101. }
  102. return FALSE;
  103. }
  104. else
  105. {
  106. return $this->logged_in_user;
  107. }
  108. }
  109. }