PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/Ion_auth.php

https://github.com/aletaschile/CodeIgniter-Ion-Auth
PHP | 375 lines | 228 code | 51 blank | 96 comment | 22 complexity | 275211021df07ba8ebb423e7479a1145 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Name: Ion Auth
  4. *
  5. * Author: Ben Edmunds
  6. * ben.edmunds@gmail.com
  7. * @benedmunds
  8. *
  9. * Added Awesomeness: Phil Sturgeon
  10. *
  11. * Location: http://github.com/benedmunds/CodeIgniter-Ion-Auth
  12. *
  13. * Created: 10.01.2009
  14. *
  15. * Description: Modified auth system based on redux_auth with extensive customization. This is basically what Redux Auth 2 should be.
  16. * Original Author name has been kept but that does not mean that the method has not been modified.
  17. *
  18. * Requirements: PHP5 or above
  19. *
  20. */
  21. class Ion_auth
  22. {
  23. /**
  24. * CodeIgniter global
  25. *
  26. * @var string
  27. **/
  28. protected $ci;
  29. /**
  30. * account status ('not_activated', etc ...)
  31. *
  32. * @var string
  33. **/
  34. protected $status;
  35. /**
  36. * extra where
  37. *
  38. * @var array
  39. **/
  40. public $_extra_where = array();
  41. /**
  42. * extra set
  43. *
  44. * @var array
  45. **/
  46. public $_extra_set = array();
  47. /**
  48. * __construct
  49. *
  50. * @return void
  51. * @author Ben
  52. **/
  53. public function __construct()
  54. {
  55. $this->ci =& get_instance();
  56. $this->ci->load->config('ion_auth', TRUE);
  57. $this->ci->load->library('email');
  58. $this->ci->load->library('session');
  59. $this->ci->lang->load('ion_auth');
  60. $this->ci->load->model('ion_auth_model');
  61. $this->ci->load->helper('cookie');
  62. //auto-login the user if they are remembered
  63. if (!$this->logged_in() && get_cookie('identity') && get_cookie('remember_code'))
  64. {
  65. $this->ci->ion_auth = $this;
  66. $this->ci->ion_auth_model->login_remembered_user();
  67. }
  68. $this->ci->ion_auth_model->trigger_events('library_constructor');
  69. }
  70. /**
  71. * __call
  72. *
  73. * Acts as a simple way to call model methods without loads of stupid alias'
  74. *
  75. **/
  76. public function __call($method, $arguments)
  77. {
  78. if (!method_exists( $this->ci->ion_auth_model, $method) )
  79. {
  80. throw new Exception('Undefined method Ion_auth::' . $method . '() called');
  81. }
  82. return call_user_func_array( array($this->ci->ion_auth_model, $method), $arguments);
  83. }
  84. /**
  85. * forgotten password feature
  86. *
  87. * @return void
  88. * @author Mathew
  89. **/
  90. public function forgotten_password($identity) //changed $email to $identity
  91. {
  92. if ( $this->ci->ion_auth_model->forgotten_password($identity) ) //changed
  93. {
  94. // Get user information
  95. $user = $this->where($this->ci->config->item('identity', 'ion_auth'), $identity)->users()->row(); //changed to get_user_by_identity from email
  96. $data = array(
  97. 'identity' => $user->{$this->ci->config->item('identity', 'ion_auth')},
  98. 'forgotten_password_code' => $user->forgotten_password_code
  99. );
  100. $message = $this->ci->load->view($this->ci->config->item('email_templates', 'ion_auth').$this->ci->config->item('email_forgot_password', 'ion_auth'), $data, true);
  101. $this->ci->email->clear();
  102. $this->ci->email->set_newline("\r\n");
  103. $this->ci->email->from($this->ci->config->item('admin_email', 'ion_auth'), $this->ci->config->item('site_title', 'ion_auth'));
  104. $this->ci->email->to($user->email);
  105. $this->ci->email->subject($this->ci->config->item('site_title', 'ion_auth') . ' - Forgotten Password Verification');
  106. $this->ci->email->message($message);
  107. if ($this->ci->email->send())
  108. {
  109. $this->set_message('forgot_password_successful');
  110. return TRUE;
  111. }
  112. else
  113. {
  114. $this->set_error('forgot_password_unsuccessful');
  115. return FALSE;
  116. }
  117. }
  118. else
  119. {
  120. $this->set_error('forgot_password_unsuccessful');
  121. return FALSE;
  122. }
  123. }
  124. /**
  125. * forgotten_password_complete
  126. *
  127. * @return void
  128. * @author Mathew
  129. **/
  130. public function forgotten_password_complete($code)
  131. {
  132. $this->ci->ion_auth_model->trigger_events('pre_password_change');
  133. $identity = $this->ci->config->item('identity', 'ion_auth');
  134. $profile = $this->where('forgotten_password_code', $code)->users()->row(); //pass the code to profile
  135. if (!is_object($profile))
  136. {
  137. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_unsuccessful'));
  138. $this->set_error('password_change_unsuccessful');
  139. return FALSE;
  140. }
  141. $new_password = $this->ci->ion_auth_model->forgotten_password_complete($code, $profile->salt);
  142. if ($new_password)
  143. {
  144. $data = array(
  145. 'identity' => $profile->{$identity},
  146. 'new_password' => $new_password
  147. );
  148. $message = $this->ci->load->view($this->ci->config->item('email_templates', 'ion_auth').$this->ci->config->item('email_forgot_password_complete', 'ion_auth'), $data, true);
  149. $this->ci->email->clear();
  150. $this->ci->email->set_newline("\r\n");
  151. $this->ci->email->from($this->ci->config->item('admin_email', 'ion_auth'), $this->ci->config->item('site_title', 'ion_auth'));
  152. $this->ci->email->to($profile->email);
  153. $this->ci->email->subject($this->ci->config->item('site_title', 'ion_auth') . ' - New Password');
  154. $this->ci->email->message($message);
  155. if ($this->ci->email->send())
  156. {
  157. $this->set_message('password_change_successful');
  158. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_successful'));
  159. return TRUE;
  160. }
  161. else
  162. {
  163. $this->set_error('password_change_unsuccessful');
  164. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_unsuccessful'));
  165. return FALSE;
  166. }
  167. }
  168. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_unsuccessful'));
  169. return FALSE;
  170. }
  171. /**
  172. * register
  173. *
  174. * @return void
  175. * @author Mathew
  176. **/
  177. public function register($username, $password, $email, $additional_data, $group_name = false) //need to test email activation
  178. {
  179. $this->ci->ion_auth_model->trigger_events('pre_account_creation');
  180. $email_activation = $this->ci->config->item('email_activation', 'ion_auth');
  181. if (!$email_activation)
  182. {
  183. $id = $this->ci->ion_auth_model->register($username, $password, $email, $additional_data, $group_name);
  184. if ($id !== FALSE)
  185. {
  186. $this->set_message('account_creation_successful');
  187. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_successful'));
  188. return $id;
  189. }
  190. else
  191. {
  192. $this->set_error('account_creation_unsuccessful');
  193. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_unsuccessful'));
  194. return FALSE;
  195. }
  196. }
  197. else
  198. {
  199. $id = $this->ci->ion_auth_model->register($username, $password, $email, $additional_data, $group_name);
  200. if (!$id)
  201. {
  202. $this->set_error('account_creation_unsuccessful');
  203. return FALSE;
  204. }
  205. $deactivate = $this->ci->ion_auth_model->deactivate($id);
  206. if (!$deactivate)
  207. {
  208. $this->set_error('deactivate_unsuccessful');
  209. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_unsuccessful'));
  210. return FALSE;
  211. }
  212. $activation_code = $this->ci->ion_auth_model->activation_code;
  213. $identity = $this->ci->config->item('identity', 'ion_auth');
  214. $user = $this->ci->ion_auth_model->user($id)->row();
  215. $data = array(
  216. 'identity' => $user->{$identity},
  217. 'id' => $user->id,
  218. 'email' => $email,
  219. 'activation' => $activation_code,
  220. );
  221. $message = $this->ci->load->view($this->ci->config->item('email_templates', 'ion_auth').$this->ci->config->item('email_activate', 'ion_auth'), $data, true);
  222. $this->ci->email->clear();
  223. $this->ci->email->set_newline("\r\n");
  224. $this->ci->email->from($this->ci->config->item('admin_email', 'ion_auth'), $this->ci->config->item('site_title', 'ion_auth'));
  225. $this->ci->email->to($email);
  226. $this->ci->email->subject($this->ci->config->item('site_title', 'ion_auth') . ' - Account Activation');
  227. $this->ci->email->message($message);
  228. if ($this->ci->email->send() == TRUE)
  229. {
  230. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_successful', 'activation_email_successful'));
  231. $this->set_message('activation_email_successful');
  232. return $id;
  233. }
  234. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_unsuccessful', 'activation_email_unsuccessful'));
  235. $this->set_error('activation_email_unsuccessful');
  236. return FALSE;
  237. }
  238. }
  239. /**
  240. * logout
  241. *
  242. * @return void
  243. * @author Mathew
  244. **/
  245. public function logout()
  246. {
  247. $this->ci->ion_auth_model->trigger_events('logout');
  248. $identity = $this->ci->config->item('identity', 'ion_auth');
  249. $this->ci->session->unset_userdata($identity);
  250. $this->ci->session->unset_userdata('group');
  251. $this->ci->session->unset_userdata('id');
  252. $this->ci->session->unset_userdata('user_id');
  253. //delete the remember me cookies if they exist
  254. if (get_cookie('identity'))
  255. {
  256. delete_cookie('identity');
  257. }
  258. if (get_cookie('remember_code'))
  259. {
  260. delete_cookie('remember_code');
  261. }
  262. $this->ci->session->sess_destroy();
  263. $this->set_message('logout_successful');
  264. return TRUE;
  265. }
  266. /**
  267. * logged_in
  268. *
  269. * @return bool
  270. * @author Mathew
  271. **/
  272. public function logged_in()
  273. {
  274. $this->ci->ion_auth_model->trigger_events('logged_in');
  275. $identity = $this->ci->config->item('identity', 'ion_auth');
  276. return (bool) $this->ci->session->userdata($identity);
  277. }
  278. /**
  279. * is_admin
  280. *
  281. * @return bool
  282. * @author Ben Edmunds
  283. **/
  284. public function is_admin()
  285. {
  286. $this->ci->ion_auth_model->trigger_events('is_admin');
  287. $admin_group = $this->ci->config->item('admin_group', 'ion_auth');
  288. return $this->in_group($admin_group);
  289. }
  290. /**
  291. * in_group
  292. *
  293. * @return bool
  294. * @author Phil Sturgeon
  295. **/
  296. public function in_group($check_group)
  297. {
  298. $this->ci->ion_auth_model->trigger_events('in_group');
  299. $users_groups = $this->ci->ion_auth_model->get_users_groups();
  300. $groups = array();
  301. foreach ($users_groups as $group)
  302. {
  303. $groups[] = $group->name;
  304. }
  305. if (is_array($check_group))
  306. {
  307. foreach($check_group as $key => $value)
  308. {
  309. if (in_array($value, $groups))
  310. {
  311. return TRUE;
  312. }
  313. }
  314. }
  315. else
  316. {
  317. if (in_array($check_group, $groups))
  318. {
  319. return TRUE;
  320. }
  321. }
  322. return FALSE;
  323. }
  324. }