Guest User

Untitled

a guest
Jun 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2.  
  3. class Auth_User_Model extends ORM {
  4.  
  5. // Relationships
  6. protected $has_many = array('user_tokens');
  7. protected $has_and_belongs_to_many = array('roles');
  8.  
  9. // Columns to ignore
  10. protected $ignored_columns = array('password_confirm');
  11.  
  12. public function __set($key, $value)
  13. {
  14. if ($key === 'password')
  15. {
  16. // Use Auth to hash the password
  17. $value = Auth::instance()->hash_password($value);
  18. }
  19.  
  20. parent::__set($key, $value);
  21. }
  22.  
  23. /**
  24. * Validates and optionally saves a new user record from an array.
  25. *
  26. * @param array values to check
  27. * @param boolean save the record when validation succeeds
  28. * @return boolean
  29. */
  30. public function validate(array & $array, $save = FALSE)
  31. {
  32. $array = Validation::factory($array)
  33. ->pre_filter('trim')
  34. ->add_rules('nome', 'required')
  35. ->add_rules('email', 'required', 'length[4,127]', 'valid::email')
  36. ->add_rules('username', 'required', 'length[4,32]', 'chars[a-zA-Z0-9_.]', array($this, 'username_exists'))
  37. ->add_rules('password', 'required', 'length[5,42]')
  38. ->add_rules('password_confirm', 'matches[password]');
  39.  
  40. return parent::validate($array, $save);
  41. }
  42.  
  43. /**
  44. * Validates login information from an array, and optionally redirects
  45. * after a successful login.
  46. *
  47. * @param array values to check
  48. * @param string URI or URL to redirect to
  49. * @return boolean
  50. */
  51. public function login(array & $array, $redirect = FALSE)
  52. {
  53. $array = Validation::factory($array)
  54. ->pre_filter('trim')
  55. ->add_rules('username', 'required', 'length[4,127]')
  56. ->add_rules('password', 'required', 'length[5,42]');
  57.  
  58. // Login starts out invalid
  59. $status = FALSE;
  60.  
  61. if ($array->validate())
  62. {
  63. // Attempt to load the user
  64. $this->find($array['username']);
  65.  
  66. if ($this->loaded AND Auth::instance()->login($this, $array['password']))
  67. {
  68. if (is_string($redirect))
  69. {
  70. // Redirect after a successful login
  71. url::redirect($redirect);
  72. }
  73.  
  74. // Login is successful
  75. $status = TRUE;
  76. }
  77. else
  78. {
  79. $array->add_error('username', 'invalid');
  80. }
  81. }
  82.  
  83. return $status;
  84. }
  85.  
  86. /**
  87. * Validates an array for a matching password and password_confirm field.
  88. *
  89. * @param array values to check
  90. * @param string save the user if
  91. * @return boolean
  92. */
  93. public function change_password(array & $array, $save = FALSE)
  94. {
  95. $array = Validation::factory($array)
  96. ->pre_filter('trim')
  97. ->add_rules('password', 'required', 'length[5,127]')
  98. ->add_rules('password_confirm', 'matches[password]');
  99.  
  100. if ($status = $array->validate())
  101. {
  102. // Change the password
  103. $this->password = $array['password'];
  104.  
  105. if ($save !== FALSE AND $status = $this->save())
  106. {
  107. if (is_string($save))
  108. {
  109. // Redirect to the success page
  110. url::redirect($save);
  111. }
  112. }
  113. }
  114.  
  115. return $status;
  116. }
  117.  
  118. /**
  119. * Tests if a username exists in the database. This can be used as a
  120. * Valdidation rule.
  121. *
  122. * @param mixed id to check
  123. * @return boolean
  124. */
  125. public function username_exists($id)
  126. {
  127. return ! (bool) $this->db
  128. ->where($this->unique_key($id), $id)
  129. ->count_records($this->table_name);
  130. }
  131.  
  132. /**
  133. * Allows a model to be loaded by username or email address.
  134. */
  135. public function unique_key($id)
  136. {
  137. if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id))
  138. {
  139. return valid::email($id) ? 'email' : 'username';
  140. }
  141.  
  142. return parent::unique_key($id);
  143. }
  144.  
  145. } // End Auth User Model
Add Comment
Please, Sign In to add comment