PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/AuthController.php

https://bitbucket.org/sarahman/mschool-project
PHP | 305 lines | 231 code | 74 blank | 0 comment | 42 complexity | a15971a4d4abd577ba6893be081e7258 MD5 | raw file
  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. include_once APPPATH . "controllers/BaseController.php";
  3. class AuthController extends BaseController
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. die("<h2 align='center' style='padding-top: 200px'>Welcome To CodeIgniter 2.0.2 (RBS-Version)</h2>");
  9. $this->load->model('user');
  10. }
  11. public function index()
  12. {
  13. $this->register();
  14. }
  15. public function register()
  16. {
  17. $this->load->model('validator');
  18. $this->validator->setupRegisterValidation();
  19. $this->data['class'] = 'signup';
  20. $this->data['selectedTab'] = 'register';
  21. if ($this->redux_auth->logged_in()) {
  22. $this->session->set_flashdata('message', 'You are already logged in. Please log out to register with a different name.');
  23. $this->session->set_flashdata('messageType', 'info');
  24. $this->redirectToHome();
  25. }
  26. if ($this->form_validation->run() == false) {
  27. $this->load->view('auth/login', $this->data);
  28. } else {
  29. $this->load->model('client');
  30. $email = $this->input->post('email');
  31. $username = $this->input->post('username');
  32. $password = $this->input->post('password');
  33. $register = $this->redux_auth->register($username, $password, $email);
  34. if ($register) {
  35. $this->client->processRegister($username);
  36. $this->session->set_flashdata('message', 'You have successfully registered. Please login below.');
  37. $this->session->set_flashdata('messageType', 'success');
  38. redirect('auth/login');
  39. } else {
  40. $this->session->set_flashdata('message', 'Something went wrong, please try again or contact the helpdesk.');
  41. $this->session->set_flashdata('messageType', 'error');
  42. redirect('auth/register');
  43. }
  44. }
  45. }
  46. public function login()
  47. {
  48. $this->load->model('validator');
  49. $this->validator->setupLoginValidation();
  50. $this->data['class'] = 'signup';
  51. $this->data['selectedTab'] = 'login';
  52. if ($this->redux_auth->logged_in()) {
  53. $this->session->set_flashdata('message', 'You are already logged in.');
  54. $this->session->set_flashdata('messageType', 'info');
  55. $this->redirectToHome();
  56. }
  57. if ($this->form_validation->run() == true) {
  58. $username = $this->input->post('username');
  59. $password = $this->input->post('password');
  60. $this->config->set_item('identity', 'username');
  61. $login = $this->redux_auth->login($username, $password);
  62. if ($login) {
  63. $this->redirectToHome();
  64. } else {
  65. $this->data['errorLogin'] = true;
  66. $this->data['login_error'] = 'The username/password was incorrect. Please try again.';
  67. }
  68. }
  69. $this->load->view('auth/login', $this->data);
  70. }
  71. public function activate($code = '')
  72. {
  73. if ($code) {
  74. $this->session->set_flashdata('message', 'Invalid Activation Code');
  75. $this->session->set_flashdata('messageType', 'error');
  76. redirect('auth/register');
  77. }
  78. $activate = $this->redux_auth->activate($code);
  79. if ($activate){
  80. $this->session->set_flashdata('message', 'Your Account is now activated, please login');
  81. $this->session->set_flashdata('messageType', 'success');
  82. redirect('auth/login');
  83. } else {
  84. $this->session->set_flashdata('message', 'Your account is already activated or doesn\'t need activating');
  85. $this->session->set_flashdata('messageType', 'error');
  86. redirect('auth/login');
  87. }
  88. }
  89. public function changePassword()
  90. {
  91. $this->data['current'] = 'edit-profile';
  92. $this->load->model('validator');
  93. $this->validator->setupChangePasswordValidation();
  94. if ($this->form_validation->run() == true) {
  95. $oldPassword = $this->input->post('old_password');
  96. $newPassword = $this->input->post('new_password');
  97. $identity = $this->session->userdata($this->config->item('identity'));
  98. $change = $this->redux_auth->change_password($identity, $oldPassword, $newPassword);
  99. if ($change) {
  100. $this->session->set_flashdata('message', 'Password Changed Succesfully. You can login with your new password.');
  101. $this->session->set_flashdata('messageType', 'success');
  102. $this->redux_auth->logout();
  103. redirect('auth/login');
  104. } else {
  105. $this->session->set_flashdata('message', 'Sorry Password Change Failed');
  106. $this->session->set_flashdata('messageType', 'error');
  107. }
  108. }
  109. $this->layout->view('auth/change_password', $this->data);
  110. }
  111. public function forgetPassword()
  112. {
  113. if ($this->redux_auth->logged_in()) {
  114. $this->session->set_flashdata('message', 'You are already logged in.');
  115. $this->session->set_flashdata('messageType', 'info');
  116. $this->redirectToHome();
  117. }
  118. $this->load->model('validator');
  119. $this->validator->setupForgetPasswordValidation();
  120. $this->data['class'] = 'signup';
  121. if ($this->form_validation->run() == true) {
  122. $email = $this->input->post('email');
  123. $forgotten = $this->redux_auth->forgotten_password($email);
  124. if ($forgotten) {
  125. $this->session->set_flashdata('message', 'A verification email has been sent, please check your inbox.');
  126. $this->session->set_flashdata('messageType', 'success');
  127. redirect('auth/forgetPassword');
  128. } else {
  129. $this->session->set_flashdata('message', 'Sorry, this email address does not belong in our system.');
  130. $this->session->set_flashdata('messageType', 'error');
  131. redirect('auth/forgetPassword');
  132. }
  133. }
  134. $this->load->view('auth/forgotten_password', $this->data);
  135. }
  136. public function recoverPassword($code = '')
  137. {
  138. if (!$code) {
  139. $this->session->set_flashdata('message', 'Invalid Verification Code');
  140. $this->session->set_flashdata('messageType', 'error');
  141. redirect('auth/register');
  142. }
  143. $forgot = $this->redux_auth->forgotten_password_complete($code);
  144. if ($forgot) {
  145. $this->session->set_flashdata('message', 'Form now your password is <strong>'.$this->redux_auth_model->new_password.'</strong>. You can change it, if you feel like.');
  146. $this->session->set_flashdata('messageType', 'success');
  147. redirect('auth/changePassword');
  148. } else {
  149. $this->session->set_flashdata('message', 'The code you entered was incorrect. Please check your email again.');
  150. $this->session->set_flashdata('messageType', 'error');
  151. redirect('auth/forgetPassword');
  152. }
  153. }
  154. public function profile()
  155. {
  156. $this->data['current'] = 'edit-profile';
  157. if ($this->redux_auth->logged_in()) {
  158. $this->data['profile'] = $this->redux_auth->profile();
  159. $this->layout->view('auth/profile', $this->data);
  160. } else {
  161. redirect('auth/login');
  162. }
  163. }
  164. public function editProfile()
  165. {
  166. $this->data['current'] = 'edit-profile';
  167. if ($this->redux_auth->logged_in() === false) {
  168. $this->session->set_flashdata('message', 'Please log in to access your backoffice.');
  169. $this->session->set_flashdata('messageType', 'error');
  170. redirect('auth/login');
  171. }
  172. $this->load->model('validator');
  173. $this->load->model('redux_auth_model');
  174. $this->validator->setupEditProfileValidation();
  175. $this->data['profile'] = $this->redux_auth->profile();
  176. if ($this->form_validation->run() == false) {
  177. $this->layout->view('auth/edit_profile', $this->data);
  178. } else {
  179. $data['full_name'] = $this->input->post('full_name');
  180. $data['phone'] = $this->input->post('phone');
  181. $update = $this->redux_auth_model->updateMeta($data, $this->data['user']->username);
  182. if ($update) {
  183. $this->session->set_flashdata('message', 'You have successfully updated profile information.');
  184. $this->session->set_flashdata('messageType', 'success');
  185. redirect('client');
  186. } else {
  187. $this->session->set_flashdata('message', 'Something went wrong, please try again or contact the helpdesk.');
  188. $this->session->set_flashdata('messageType', 'error');
  189. redirect('client');
  190. }
  191. }
  192. }
  193. public function logout()
  194. {
  195. $this->redux_auth->logout();
  196. $this->session->set_flashdata('message', 'You have successfully been logged out');
  197. $this->session->set_flashdata('messageType', 'success');
  198. redirect('auth/login');
  199. }
  200. public function username_check($username)
  201. {
  202. $check = $this->redux_auth_model->username_check($username);
  203. if ($check) {
  204. $this->form_validation->set_message('username_check', 'The username "' . $username . '" already exists.');
  205. return false;
  206. } else {
  207. return true;
  208. }
  209. }
  210. public function email_check($email)
  211. {
  212. $check = $this->redux_auth_model->email_check($email);
  213. if ($check) {
  214. $this->form_validation->set_message('email_check', 'The email "' . $email . '" already exists.');
  215. return false;
  216. } else {
  217. return true;
  218. }
  219. }
  220. private function redirectToHome()
  221. {
  222. if (!isset($this->data['user'])) {
  223. $this->data['user'] = $this->redux_auth->profile();
  224. }
  225. if ($this->data['user']->group == 'Client') {
  226. redirect('jobs');
  227. } else {
  228. redirect('jobs');
  229. }
  230. }
  231. }