PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/Ion_auth.php

https://github.com/asalce/CodeIgniter-Ion-Auth
PHP | 470 lines | 216 code | 56 blank | 198 comment | 14 complexity | d26dea751fb99653ea3b7f7b059978fd 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. Original redux license is below.
  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. /*
  22. * ----------------------------------------------------------------------------
  23. * "THE BEER-WARE LICENSE" :
  24. * <thepixeldeveloper@googlemail.com> wrote this file. As long as you retain this notice you
  25. * can do whatever you want with this stuff. If we meet some day, and you think
  26. * this stuff is worth it, you can buy me a beer in return Mathew Davies
  27. * ----------------------------------------------------------------------------
  28. */
  29. class Ion_auth
  30. {
  31. /**
  32. * CodeIgniter global
  33. *
  34. * @var string
  35. **/
  36. protected $ci;
  37. /**
  38. * account status ('not_activated', etc ...)
  39. *
  40. * @var string
  41. **/
  42. protected $status;
  43. /**
  44. * extra where
  45. *
  46. * @var array
  47. **/
  48. public $_extra_where = array();
  49. /**
  50. * extra set
  51. *
  52. * @var array
  53. **/
  54. public $_extra_set = array();
  55. /**
  56. * __construct
  57. *
  58. * @return void
  59. * @author Mathew
  60. **/
  61. public function __construct()
  62. {
  63. $this->ci =& get_instance();
  64. $this->ci->load->config('ion_auth');
  65. $email = $this->ci->config->item('email');
  66. $this->ci->load->library('email', $email);
  67. $this->ci->load->model('ion_auth_model');
  68. }
  69. /**
  70. * Activate user.
  71. *
  72. * @return void
  73. * @author Mathew
  74. **/
  75. public function activate($id, $code=false)
  76. {
  77. return $this->ci->ion_auth_model->activate($id, $code);
  78. }
  79. /**
  80. * Deactivate user.
  81. *
  82. * @return void
  83. * @author Mathew
  84. **/
  85. public function deactivate($id)
  86. {
  87. return $this->ci->ion_auth_model->deactivate($id);
  88. }
  89. /**
  90. * Change password.
  91. *
  92. * @return void
  93. * @author Mathew
  94. **/
  95. public function change_password($identity, $old, $new)
  96. {
  97. return $this->ci->ion_auth_model->change_password($identity, $old, $new);
  98. }
  99. /**
  100. * forgotten password feature
  101. *
  102. * @return void
  103. * @author Mathew
  104. **/
  105. public function forgotten_password($email)
  106. {
  107. if ( $this->ci->ion_auth_model->forgotten_password($email) )
  108. {
  109. // Get user information.
  110. $profile = $this->ci->ion_auth_model->profile($email);
  111. $data = array('identity' => $profile->{$this->ci->config->item('identity')},
  112. 'forgotten_password_code' => $profile->forgotten_password_code
  113. );
  114. $message = $this->ci->load->view($this->ci->config->item('email_templates').$this->ci->config->item('email_forgot_password'), $data, true);
  115. $this->ci->email->clear();
  116. $config['mailtype'] = "html";
  117. $this->ci->email->initialize($config);
  118. $this->ci->email->set_newline("\r\n");
  119. $this->ci->email->from($this->ci->config->item('admin_email'), $this->ci->config->item('site_title'));
  120. $this->ci->email->to($profile->email);
  121. $this->ci->email->subject($this->ci->config->item('site_title') . ' - Forgotten Password Verification');
  122. $this->ci->email->message($message);
  123. return $this->ci->email->send();
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. /**
  131. * forgotten_password_complete
  132. *
  133. * @return void
  134. * @author Mathew
  135. **/
  136. public function forgotten_password_complete($code)
  137. {
  138. $identity = $this->ci->config->item('identity');
  139. $profile = $this->ci->ion_auth_model->profile($code);
  140. $new_password = $this->ci->ion_auth_model->forgotten_password_complete($code);
  141. if ($new_password)
  142. {
  143. $data = array('identity' => $profile->{$identity},
  144. 'new_password' => $new_password
  145. );
  146. $message = $this->ci->load->view($this->ci->config->item('email_templates').$this->ci->config->item('email_forgot_password_complete'), $data, true);
  147. $this->ci->email->clear();
  148. $config['mailtype'] = "html";
  149. $this->ci->email->initialize($config);
  150. $this->ci->email->set_newline("\r\n");
  151. $this->ci->email->from($this->ci->config->item('admin_email'), $this->ci->config->item('site_title'));
  152. $this->ci->email->to($profile->email);
  153. $this->ci->email->subject($this->ci->config->item('site_title') . ' - New Password');
  154. $this->ci->email->message($message);
  155. return $this->ci->email->send();
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. }
  162. /**
  163. * register
  164. *
  165. * @return void
  166. * @author Mathew
  167. **/
  168. public function register($username, $password, $email, $additional_data, $group_name = false) //need to test email activation
  169. {
  170. $email_activation = $this->ci->config->item('email_activation');
  171. if (!$email_activation)
  172. {
  173. return $this->ci->ion_auth_model->register($username, $password, $email, $additional_data, $group_name);
  174. }
  175. else
  176. {
  177. $id = $this->ci->ion_auth_model->register($username, $password, $email, $additional_data, $group_name);
  178. if (!$id)
  179. {
  180. return false;
  181. }
  182. $deactivate = $this->ci->ion_auth_model->deactivate($id);
  183. if (!$deactivate)
  184. {
  185. return false;
  186. }
  187. $activation_code = $this->ci->ion_auth_model->activation_code;
  188. $identity = $this->ci->config->item('identity');
  189. $user = $this->ci->ion_auth_model->get_user($id)->row();
  190. $data = array('identity' => $user->{$identity},
  191. 'id' => $user->id,
  192. 'email' => $email,
  193. 'activation' => $activation_code,
  194. );
  195. $message = $this->ci->load->view($this->ci->config->item('email_templates').$this->ci->config->item('email_activate'), $data, true);
  196. $this->ci->email->clear();
  197. $config['mailtype'] = "html";
  198. $this->ci->email->initialize($config);
  199. $this->ci->email->set_newline("\r\n");
  200. $this->ci->email->from($this->ci->config->item('admin_email'), $this->ci->config->item('site_title'));
  201. $this->ci->email->to($email);
  202. $this->ci->email->subject($this->ci->config->item('site_title') . ' - Account Activation');
  203. $this->ci->email->message($message);
  204. return $this->ci->email->send();
  205. }
  206. }
  207. /**
  208. * login
  209. *
  210. * @return void
  211. * @author Mathew
  212. **/
  213. public function login($identity, $password)
  214. {
  215. return $this->ci->ion_auth_model->login($identity, $password);
  216. }
  217. /**
  218. * logout
  219. *
  220. * @return void
  221. * @author Mathew
  222. **/
  223. public function logout()
  224. {
  225. $identity = $this->ci->config->item('identity');
  226. $this->ci->session->unset_userdata($identity);
  227. $this->ci->session->unset_userdata('group');
  228. $this->ci->session->unset_userdata('id');
  229. $this->ci->session->unset_userdata('user_id');
  230. $this->ci->session->sess_destroy();
  231. }
  232. /**
  233. * logged_in
  234. *
  235. * @return bool
  236. * @author Mathew
  237. **/
  238. public function logged_in()
  239. {
  240. $identity = $this->ci->config->item('identity');
  241. return (bool) $this->ci->session->userdata($identity);
  242. }
  243. /**
  244. * is_admin
  245. *
  246. * @return bool
  247. * @author Ben Edmunds
  248. **/
  249. public function is_admin()
  250. {
  251. $admin_group = $this->ci->config->item('admin_group');
  252. $user_group = $this->ci->session->userdata('group');
  253. return $user_group == $admin_group;
  254. }
  255. /**
  256. * is_group
  257. *
  258. * @return bool
  259. * @author Phil Sturgeon
  260. **/
  261. public function is_group($check_group)
  262. {
  263. $user_group = $this->ci->session->userdata('group');
  264. if(is_array($check_group))
  265. {
  266. return in_array($user_group, $check_group);
  267. }
  268. return $user_group == $check_group;
  269. }
  270. /**
  271. * Profile
  272. *
  273. * @return void
  274. * @author Mathew
  275. **/
  276. public function profile()
  277. {
  278. $session = $this->ci->config->item('identity');
  279. $identity = $this->ci->session->userdata($session);
  280. return $this->ci->ion_auth_model->profile($identity);
  281. }
  282. /**
  283. * Get Users
  284. *
  285. * @return object Users
  286. * @author Ben Edmunds
  287. **/
  288. public function get_users($group_name = false)
  289. {
  290. return $this->ci->ion_auth_model->get_users($group_name)->result();
  291. }
  292. /**
  293. * Get Users Array
  294. *
  295. * @return array Users
  296. * @author Ben Edmunds
  297. **/
  298. public function get_users_array($group_name = false)
  299. {
  300. return $this->ci->ion_auth_model->get_users($group_name)->result_array();
  301. }
  302. /**
  303. * Get Active Users
  304. *
  305. * @return object Users
  306. * @author Ben Edmunds
  307. **/
  308. public function get_active_users($group_name = false)
  309. {
  310. return $this->ci->ion_auth_model->get_active_users($group_name)->result();
  311. }
  312. /**
  313. * Get Active Users Array
  314. *
  315. * @return object Users
  316. * @author Ben Edmunds
  317. **/
  318. public function get_active_users_array($group_name = false)
  319. {
  320. return $this->ci->ion_auth_model->get_active_users($group_name)->result_array();
  321. }
  322. /**
  323. * Get User
  324. *
  325. * @return object User
  326. * @author Ben Edmunds
  327. **/
  328. public function get_user($id=false)
  329. {
  330. return $this->ci->ion_auth_model->get_user($id)->row();
  331. }
  332. /**
  333. * Get User as Array
  334. *
  335. * @return array User
  336. * @author Ben Edmunds
  337. **/
  338. public function get_user_array($id=false)
  339. {
  340. return $this->ci->ion_auth_model->get_user($id)->row_array();
  341. }
  342. /**
  343. * Get Users Group
  344. *
  345. * @return object Group
  346. * @author Ben Edmunds
  347. **/
  348. public function get_users_group($id=false)
  349. {
  350. return $this->ci->ion_auth_model->get_users_group($id);
  351. }
  352. /**
  353. * update_user
  354. *
  355. * @return void
  356. * @author Phil Sturgeon
  357. **/
  358. public function update_user($id, $data)
  359. {
  360. return $this->ci->ion_auth_model->update_user($id, $data);
  361. }
  362. /**
  363. * update_user
  364. *
  365. * @return void
  366. * @author Phil Sturgeon
  367. **/
  368. public function delete_user($id)
  369. {
  370. return $this->ci->ion_auth_model->delete_user($id);
  371. }
  372. /**
  373. * extra_where
  374. *
  375. * Crazy function that allows extra where field to be used for user fetching/unique checking etc.
  376. * Basically this allows users to be unique based on one other thing than the identifier which is helpful
  377. * for sites using multiple domains on a single database.
  378. *
  379. * @return void
  380. * @author Phil Sturgeon
  381. **/
  382. public function extra_where()
  383. {
  384. $where =& func_get_args();
  385. if(count($where) == 1)
  386. {
  387. $this->_extra_where = $where[0];
  388. }
  389. else
  390. {
  391. $this->_extra_where = array($where[0] => $where[1]);
  392. }
  393. }
  394. /**
  395. * extra_set
  396. *
  397. * Set your extra field for registration
  398. *
  399. * @return void
  400. * @author Phil Sturgeon
  401. **/
  402. public function extra_set()
  403. {
  404. $set =& func_get_args();
  405. if(count($set) == 1)
  406. {
  407. $this->_extra_set = $set[0];
  408. }
  409. else
  410. {
  411. $this->_extra_set = array($set[0] => $set[1]);
  412. }
  413. }
  414. }