PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/auth.php

https://bitbucket.org/whiterabbitfl/ci_base
PHP | 333 lines | 247 code | 48 blank | 38 comment | 24 complexity | 012de7cbd30816b610e383f04ec75486 MD5 | raw file
  1. <?php
  2. class Auth extends CI_Controller
  3. {
  4. // Used for registering and changing password form validation
  5. var $min_username = 4;
  6. var $max_username = 20;
  7. var $min_password = 4;
  8. var $max_password = 20;
  9. function __construct()
  10. {
  11. parent::__construct();
  12. $this->load->library('Form_validation');
  13. $this->load->library('DX_Auth');
  14. $this->load->helper('url');
  15. $this->load->helper('form');
  16. }
  17. function index()
  18. {
  19. $this->login();
  20. }
  21. /* Callback function */
  22. function username_check($username)
  23. {
  24. $result = $this->dx_auth->is_username_available($username);
  25. if ( ! $result)
  26. {
  27. $this->form_validation->set_message('username_check', 'Username already exist. Please choose another username.');
  28. }
  29. return $result;
  30. }
  31. function email_check($email)
  32. {
  33. $result = $this->dx_auth->is_email_available($email);
  34. if ( ! $result)
  35. {
  36. $this->form_validation->set_message('email_check', 'Email is already used by another user. Please choose another email address.');
  37. }
  38. return $result;
  39. }
  40. function recaptcha_check()
  41. {
  42. $result = $this->dx_auth->is_recaptcha_match();
  43. if ( ! $result)
  44. {
  45. $this->form_validation->set_message('recaptcha_check', 'Your confirmation code does not match the one in the image. Try again.');
  46. }
  47. return $result;
  48. }
  49. /* End of Callback function */
  50. function login()
  51. {
  52. if ( ! $this->dx_auth->is_logged_in())
  53. {
  54. $val = $this->form_validation;
  55. // Set form validation rules
  56. $val->set_rules('username', 'Username', 'trim|required|xss_clean');
  57. $val->set_rules('password', 'Password', 'trim|required|xss_clean');
  58. $val->set_rules('remember', 'Remember me', 'integer');
  59. // Set captcha rules if login attempts exceed max attempts in config
  60. if ($this->dx_auth->is_max_login_attempts_exceeded())
  61. {
  62. $val->set_rules('captcha', 'Confirmation Code', 'trim|required|xss_clean|callback_captcha_check');
  63. }
  64. if ($val->run() AND $this->dx_auth->login($val->set_value('username'), $val->set_value('password'), $val->set_value('remember')))
  65. {
  66. // Redirect to homepage
  67. redirect('', 'location');
  68. }
  69. else
  70. {
  71. // Check if the user is failed logged in because user is banned user or not
  72. if ($this->dx_auth->is_banned())
  73. {
  74. // Redirect to banned uri
  75. $this->dx_auth->deny_access('banned');
  76. }
  77. else
  78. {
  79. // Default is we don't show captcha until max login attempts eceeded
  80. $data['show_captcha'] = FALSE;
  81. // Show captcha if login attempts exceed max attempts in config
  82. if ($this->dx_auth->is_max_login_attempts_exceeded())
  83. {
  84. // Create catpcha
  85. $this->dx_auth->captcha();
  86. // Set view data to show captcha on view file
  87. $data['show_captcha'] = TRUE;
  88. }
  89. // Load login page view
  90. $this->load->view($this->dx_auth->login_view, $data);
  91. }
  92. }
  93. }
  94. else
  95. {
  96. $data['auth_message'] = 'You are already logged in.';
  97. $this->load->view($this->dx_auth->logged_in_view, $data);
  98. }
  99. }
  100. function logout()
  101. {
  102. $this->dx_auth->logout();
  103. $data['auth_message'] = 'You have been logged out.';
  104. $this->load->view($this->dx_auth->logout_view, $data);
  105. }
  106. function register()
  107. {
  108. if ( ! $this->dx_auth->is_logged_in() AND $this->dx_auth->allow_registration)
  109. {
  110. $val = $this->form_validation;
  111. // Set form validation rules
  112. $val->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->min_username.']|max_length['.$this->max_username.']|callback_username_check|alpha_dash');
  113. $val->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']|matches[confirm_password]');
  114. $val->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean');
  115. $val->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_email_check');
  116. // Is registration using captcha
  117. if ($this->dx_auth->captcha_registration)
  118. {
  119. // Set recaptcha rules.
  120. // IMPORTANT: Do not change 'recaptcha_response_field' because it's used by reCAPTCHA API,
  121. // This is because the limitation of reCAPTCHA, not DX Auth library
  122. $val->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback_recaptcha_check');
  123. }
  124. // Run form validation and register user if it's pass the validation
  125. if ($val->run() AND $this->dx_auth->register($val->set_value('username'), $val->set_value('password'), $val->set_value('email')))
  126. {
  127. // Set success message accordingly
  128. if ($this->dx_auth->email_activation)
  129. {
  130. $data['auth_message'] = 'You have successfully registered. Check your email address to activate your account.';
  131. }
  132. else
  133. {
  134. $data['auth_message'] = 'You have successfully registered. '.anchor(site_url($this->dx_auth->login_uri), 'Login');
  135. }
  136. // Load registration success page
  137. $this->load->view($this->dx_auth->register_success_view, $data);
  138. }
  139. else
  140. {
  141. // Load registration page
  142. $this->load->view('auth/register_form');
  143. }
  144. }
  145. elseif ( ! $this->dx_auth->allow_registration)
  146. {
  147. $data['auth_message'] = 'Registration has been disabled.';
  148. $this->load->view($this->dx_auth->register_disabled_view, $data);
  149. }
  150. else
  151. {
  152. $data['auth_message'] = 'You have to logout first, before registering.';
  153. $this->load->view($this->dx_auth->logged_in_view, $data);
  154. }
  155. }
  156. function activate()
  157. {
  158. // Get username and key
  159. $username = $this->uri->segment(3);
  160. $key = $this->uri->segment(4);
  161. // Activate user
  162. if ($this->dx_auth->activate($username, $key))
  163. {
  164. $data['auth_message'] = 'Your account have been successfully activated. '.anchor(site_url($this->dx_auth->login_uri), 'Login');
  165. $this->load->view($this->dx_auth->activate_success_view, $data);
  166. }
  167. else
  168. {
  169. $data['auth_message'] = 'The activation code you entered was incorrect. Please check your email again.';
  170. $this->load->view($this->dx_auth->activate_failed_view, $data);
  171. }
  172. }
  173. function forgot_password()
  174. {
  175. $val = $this->form_validation;
  176. // Set form validation rules
  177. $val->set_rules('login', 'Username or Email address', 'trim|required|xss_clean');
  178. // Validate rules and call forgot password function
  179. if ($val->run() AND $this->dx_auth->forgot_password($val->set_value('login')))
  180. {
  181. $data['auth_message'] = 'An email has been sent to your email with instructions with how to activate your new password.';
  182. $this->load->view($this->dx_auth->forgot_password_success_view, $data);
  183. }
  184. else
  185. {
  186. $this->load->view($this->dx_auth->forgot_password_view);
  187. }
  188. }
  189. function reset_password()
  190. {
  191. // Get username and key
  192. $username = $this->uri->segment(3);
  193. $key = $this->uri->segment(4);
  194. // Reset password
  195. if ($this->dx_auth->reset_password($username, $key))
  196. {
  197. $data['auth_message'] = 'You have successfully reset you password, '.anchor(site_url($this->dx_auth->login_uri), 'Login');
  198. $this->load->view($this->dx_auth->reset_password_success_view, $data);
  199. }
  200. else
  201. {
  202. $data['auth_message'] = 'Reset failed. Your username and key are incorrect. Please check your email again and follow the instructions.';
  203. $this->load->view($this->dx_auth->reset_password_failed_view, $data);
  204. }
  205. }
  206. function change_password()
  207. {
  208. // Check if user logged in or not
  209. if ($this->dx_auth->is_logged_in())
  210. {
  211. $val = $this->form_validation;
  212. // Set form validation
  213. $val->set_rules('old_password', 'Old Password', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']');
  214. $val->set_rules('new_password', 'New Password', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']|matches[confirm_new_password]');
  215. $val->set_rules('confirm_new_password', 'Confirm new Password', 'trim|required|xss_clean');
  216. // Validate rules and change password
  217. if ($val->run() AND $this->dx_auth->change_password($val->set_value('old_password'), $val->set_value('new_password')))
  218. {
  219. $data['auth_message'] = 'Your password has successfully been changed.';
  220. $this->load->view($this->dx_auth->change_password_success_view, $data);
  221. }
  222. else
  223. {
  224. $this->load->view($this->dx_auth->change_password_view);
  225. }
  226. }
  227. else
  228. {
  229. // Redirect to login page
  230. $this->dx_auth->deny_access('login');
  231. }
  232. }
  233. function cancel_account()
  234. {
  235. // Check if user logged in or not
  236. if ($this->dx_auth->is_logged_in())
  237. {
  238. $val = $this->form_validation;
  239. // Set form validation rules
  240. $val->set_rules('password', 'Password', "trim|required|xss_clean");
  241. // Validate rules and change password
  242. if ($val->run() AND $this->dx_auth->cancel_account($val->set_value('password')))
  243. {
  244. // Redirect to homepage
  245. redirect('', 'location');
  246. }
  247. else
  248. {
  249. $this->load->view($this->dx_auth->cancel_account_view);
  250. }
  251. }
  252. else
  253. {
  254. // Redirect to login page
  255. $this->dx_auth->deny_access('login');
  256. }
  257. }
  258. // Example how to get permissions you set permission in /backend/custom_permissions/
  259. function custom_permissions()
  260. {
  261. if ($this->dx_auth->is_logged_in())
  262. {
  263. echo 'My role: '.$this->dx_auth->get_role_name().'<br/>';
  264. echo 'My permission: <br/>';
  265. if ($this->dx_auth->get_permission_value('edit') != NULL AND $this->dx_auth->get_permission_value('edit'))
  266. {
  267. echo 'Edit is allowed';
  268. }
  269. else
  270. {
  271. echo 'Edit is not allowed';
  272. }
  273. echo '<br/>';
  274. if ($this->dx_auth->get_permission_value('delete') != NULL AND $this->dx_auth->get_permission_value('delete'))
  275. {
  276. echo 'Delete is allowed';
  277. }
  278. else
  279. {
  280. echo 'Delete is not allowed';
  281. }
  282. }
  283. }
  284. }
  285. ?>