PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/application/libraries/Ion_auth.php

https://bitbucket.org/jerwinse/iagh-cms
PHP | 382 lines | 235 code | 51 blank | 96 comment | 22 complexity | d2cb0d3fe34a27de8d4cc5f518079b76 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. if ($user)
  97. {
  98. $data = array(
  99. 'identity' => $user->{$this->ci->config->item('identity', 'ion_auth')},
  100. 'forgotten_password_code' => $user->forgotten_password_code
  101. );
  102. $message = $this->ci->load->view($this->ci->config->item('email_templates', 'ion_auth').$this->ci->config->item('email_forgot_password', 'ion_auth'), $data, true);
  103. $this->ci->email->clear();
  104. $this->ci->email->set_newline("\r\n");
  105. $this->ci->email->from($this->ci->config->item('admin_email', 'ion_auth'), $this->ci->config->item('site_title', 'ion_auth'));
  106. $this->ci->email->to($user->email);
  107. $this->ci->email->subject($this->ci->config->item('site_title', 'ion_auth') . ' - Forgotten Password Verification');
  108. $this->ci->email->message($message);
  109. if ($this->ci->email->send())
  110. {
  111. $this->set_message('forgot_password_successful');
  112. return TRUE;
  113. }
  114. else
  115. {
  116. $this->set_error('forgot_password_unsuccessful');
  117. return FALSE;
  118. }
  119. }
  120. else
  121. {
  122. $this->set_error('forgot_password_unsuccessful');
  123. return FALSE;
  124. }
  125. }
  126. else
  127. {
  128. $this->set_error('forgot_password_unsuccessful');
  129. return FALSE;
  130. }
  131. }
  132. /**
  133. * forgotten_password_complete
  134. *
  135. * @return void
  136. * @author Mathew
  137. **/
  138. public function forgotten_password_complete($code)
  139. {
  140. $this->ci->ion_auth_model->trigger_events('pre_password_change');
  141. $identity = $this->ci->config->item('identity', 'ion_auth');
  142. $profile = $this->where('forgotten_password_code', $code)->users()->row(); //pass the code to profile
  143. if (!is_object($profile))
  144. {
  145. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_unsuccessful'));
  146. $this->set_error('password_change_unsuccessful');
  147. return FALSE;
  148. }
  149. $new_password = $this->ci->ion_auth_model->forgotten_password_complete($code, $profile->salt);
  150. if ($new_password)
  151. {
  152. $data = array(
  153. 'identity' => $profile->{$identity},
  154. 'new_password' => $new_password
  155. );
  156. $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);
  157. $this->ci->email->clear();
  158. $this->ci->email->set_newline("\r\n");
  159. $this->ci->email->from($this->ci->config->item('admin_email', 'ion_auth'), $this->ci->config->item('site_title', 'ion_auth'));
  160. $this->ci->email->to($profile->email);
  161. $this->ci->email->subject($this->ci->config->item('site_title', 'ion_auth') . ' - New Password');
  162. $this->ci->email->message($message);
  163. if ($this->ci->email->send())
  164. {
  165. $this->set_message('password_change_successful');
  166. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_successful'));
  167. return TRUE;
  168. }
  169. else
  170. {
  171. $this->set_error('password_change_unsuccessful');
  172. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_unsuccessful'));
  173. return FALSE;
  174. }
  175. }
  176. $this->ci->ion_auth_model->trigger_events(array('post_password_change', 'password_change_unsuccessful'));
  177. return FALSE;
  178. }
  179. /**
  180. * register
  181. *
  182. * @return void
  183. * @author Mathew
  184. **/
  185. public function register($username, $password, $email, $additional_data = array(), $group_name = array()) //need to test email activation
  186. {
  187. $this->ci->ion_auth_model->trigger_events('pre_account_creation');
  188. $email_activation = $this->ci->config->item('email_activation', 'ion_auth');
  189. if (!$email_activation)
  190. {
  191. $id = $this->ci->ion_auth_model->register($username, $password, $email, $additional_data, $group_name);
  192. if ($id !== FALSE)
  193. {
  194. $this->set_message('account_creation_successful');
  195. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_successful'));
  196. return $id;
  197. }
  198. else
  199. {
  200. $this->set_error('account_creation_unsuccessful');
  201. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_unsuccessful'));
  202. return FALSE;
  203. }
  204. }
  205. else
  206. {
  207. $id = $this->ci->ion_auth_model->register($username, $password, $email, $additional_data, $group_name);
  208. if (!$id)
  209. {
  210. $this->set_error('account_creation_unsuccessful');
  211. return FALSE;
  212. }
  213. $deactivate = $this->ci->ion_auth_model->deactivate($id);
  214. if (!$deactivate)
  215. {
  216. $this->set_error('deactivate_unsuccessful');
  217. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_unsuccessful'));
  218. return FALSE;
  219. }
  220. $activation_code = $this->ci->ion_auth_model->activation_code;
  221. $identity = $this->ci->config->item('identity', 'ion_auth');
  222. $user = $this->ci->ion_auth_model->user($id)->row();
  223. $data = array(
  224. 'identity' => $user->{$identity},
  225. 'id' => $user->id,
  226. 'email' => $email,
  227. 'activation' => $activation_code,
  228. );
  229. $message = $this->ci->load->view($this->ci->config->item('email_templates', 'ion_auth').$this->ci->config->item('email_activate', 'ion_auth'), $data, true);
  230. $this->ci->email->clear();
  231. $this->ci->email->set_newline("\r\n");
  232. $this->ci->email->from($this->ci->config->item('admin_email', 'ion_auth'), $this->ci->config->item('site_title', 'ion_auth'));
  233. $this->ci->email->to($email);
  234. $this->ci->email->subject($this->ci->config->item('site_title', 'ion_auth') . ' - Account Activation');
  235. $this->ci->email->message($message);
  236. if ($this->ci->email->send() == TRUE)
  237. {
  238. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_successful', 'activation_email_successful'));
  239. $this->set_message('activation_email_successful');
  240. return $id;
  241. }
  242. $this->ci->ion_auth_model->trigger_events(array('post_account_creation', 'post_account_creation_unsuccessful', 'activation_email_unsuccessful'));
  243. $this->set_error('activation_email_unsuccessful');
  244. return FALSE;
  245. }
  246. }
  247. /**
  248. * logout
  249. *
  250. * @return void
  251. * @author Mathew
  252. **/
  253. public function logout()
  254. {
  255. $this->ci->ion_auth_model->trigger_events('logout');
  256. $identity = $this->ci->config->item('identity', 'ion_auth');
  257. $this->ci->session->unset_userdata($identity);
  258. $this->ci->session->unset_userdata('id');
  259. $this->ci->session->unset_userdata('user_id');
  260. //delete the remember me cookies if they exist
  261. if (get_cookie('identity'))
  262. {
  263. delete_cookie('identity');
  264. }
  265. if (get_cookie('remember_code'))
  266. {
  267. delete_cookie('remember_code');
  268. }
  269. $this->ci->session->sess_destroy();
  270. $this->set_message('logout_successful');
  271. return TRUE;
  272. }
  273. /**
  274. * logged_in
  275. *
  276. * @return bool
  277. * @author Mathew
  278. **/
  279. public function logged_in()
  280. {
  281. $this->ci->ion_auth_model->trigger_events('logged_in');
  282. $identity = $this->ci->config->item('identity', 'ion_auth');
  283. return (bool) $this->ci->session->userdata($identity);
  284. }
  285. /**
  286. * is_admin
  287. *
  288. * @return bool
  289. * @author Ben Edmunds
  290. **/
  291. public function is_admin()
  292. {
  293. $this->ci->ion_auth_model->trigger_events('is_admin');
  294. $admin_group = $this->ci->config->item('admin_group', 'ion_auth');
  295. return $this->in_group($admin_group);
  296. }
  297. /**
  298. * in_group
  299. *
  300. * @return bool
  301. * @author Phil Sturgeon
  302. **/
  303. public function in_group($check_group, $id=false)
  304. {
  305. $this->ci->ion_auth_model->trigger_events('in_group');
  306. $users_groups = $this->ci->ion_auth_model->get_users_groups($id)->result();
  307. $groups = array();
  308. foreach ($users_groups as $group)
  309. {
  310. $groups[] = $group->name;
  311. }
  312. if (is_array($check_group))
  313. {
  314. foreach($check_group as $key => $value)
  315. {
  316. if (in_array($value, $groups))
  317. {
  318. return TRUE;
  319. }
  320. }
  321. }
  322. else
  323. {
  324. if (in_array($check_group, $groups))
  325. {
  326. return TRUE;
  327. }
  328. }
  329. return FALSE;
  330. }
  331. }