change_password.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright 2011, Mark Scherer
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @version 1.0
  9. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  10. */
  11. if (!defined('PWD_MIN_LENGTH')) {
  12. define('PWD_MIN_LENGTH', 3);
  13. }
  14. if (!defined('PWD_MAX_LENGTH')) {
  15. define('PWD_MAX_LENGTH', 20);
  16. }
  17. /**
  18. * A cakephp1.3 behavior to change passwords the easy way
  19. * - complete validation
  20. * - hashing of password
  21. * - requires fields (no tempering even without security component)
  22. *
  23. * usage: do NOT add it via $actAs = array()
  24. * attach it dynamically in only those actions where you actually change the password like so:
  25. * $this->User->Behaviors->attach('Tools.ChangePassword', array(SETTINGSARRAY));
  26. * as first line in any action where you want to allow the user to change his password
  27. * also add the two form fields in the form (pwd, pwd_confirm)
  28. * the rest is cake automagic :)
  29. *
  30. * TODO: form_field_current
  31. * TODO: test cases
  32. * feel free to help me out - this was written in less then 2 hours
  33. *
  34. * 2011-07-04 ms
  35. */
  36. class ChangePasswordBehavior extends ModelBehavior {
  37. var $settings = array();
  38. /**
  39. * @access protected
  40. */
  41. var $_defaultSettings = array(
  42. 'field' => 'password',
  43. 'confirm' => true, # set to false if in admin view and no confirmation (pwd_repeat) is required
  44. 'allowEmpty' => false,
  45. 'current' => false, # expect the current password for security purposes
  46. 'form_field' => 'pwd',
  47. 'form_field_repeat' => 'pwd_repeat',
  48. 'form_field_current' => 'pwd_current',
  49. 'hashType' => null,
  50. 'hashSalt' => true,
  51. 'auth' => 'Auth',
  52. );
  53. var $_validationRules = array(
  54. 'pwd' => array(
  55. 'between' => array(
  56. 'rule' => array('between', PWD_MIN_LENGTH, PWD_MAX_LENGTH),
  57. 'message' => array('valErrBetweenCharacters %s %s', PWD_MIN_LENGTH, PWD_MAX_LENGTH),
  58. )
  59. ),
  60. 'pwd_repeat' => array(
  61. 'between' => array(
  62. 'rule' => array('between', PWD_MIN_LENGTH, PWD_MAX_LENGTH),
  63. 'message' => array('valErrBetweenCharacters %s %s', PWD_MIN_LENGTH, PWD_MAX_LENGTH),
  64. ),
  65. 'validateIdentical' => array(
  66. 'rule' => array('validateIdentical', 'pwd'),
  67. 'message' => 'valErrPwdNotMatch',
  68. ),
  69. ),
  70. 'pwd_current' => array(
  71. 'notEmpty' => array(
  72. 'rule' => array('notEmpty'),
  73. 'message' => 'valErrProvideCurrentPwd',
  74. ),
  75. 'validateCurrentPwd' => array(
  76. 'rule' => 'validateCurrentPwd',
  77. 'message' => 'valErrCurrentPwdIncorrect',
  78. )
  79. ),
  80. );
  81. /**
  82. * if not implemented in app_model
  83. * 2011-07-22 ms
  84. */
  85. public function validateCurrentPwd(Model $Model, $data) {
  86. if (is_array($data)) {
  87. $pwd = array_shift($data);
  88. } else {
  89. $pwd = $data;
  90. }
  91. $uid = null;
  92. if ($Model->id) {
  93. $uid = $Model->id;
  94. } elseif (!empty($Model->data[$Model->alias]['id'])) {
  95. $uid = $Model->data[$Model->alias]['id'];
  96. } else {
  97. return false;
  98. }
  99. //TODO
  100. //$this->Auth = new AuthComponent();
  101. //return $this->Auth->verifyUser($uid, $pwd)) {
  102. return true;
  103. }
  104. /**
  105. * if not implemented in app_model
  106. * 2011-07-22 ms
  107. */
  108. public function validateIdentical(Model $Model, $data, $compareWith = null) {
  109. if (is_array($data)) {
  110. $value = array_shift($data);
  111. } else {
  112. $value = $data;
  113. }
  114. $compareValue = $Model->data[$Model->alias][$compareWith];
  115. return ($compareValue === $value);
  116. }
  117. /**
  118. * adding validation rules
  119. * 2011-07-22 ms
  120. */
  121. public function setup(Model $Model, $config = array()) {
  122. $this->settings[$Model->alias] = Set::merge($this->_defaultSettings, $config);
  123. $formField = $this->settings[$Model->alias]['form_field'];
  124. $formFieldRepeat = $this->settings[$Model->alias]['form_field_repeat'];
  125. # add the validation rules if not already attached
  126. if (!isset($Model->validate[$formField])) {
  127. $Model->validate[$formField] = $this->_validationRules[$formField];
  128. }
  129. if (!isset($Model->validate[$formFieldRepeat])) {
  130. $Model->validate[$formFieldRepeat] = $this->_validationRules[$formFieldRepeat];
  131. $Model->validate[$formFieldRepeat]['validateIdentical']['rule'][1] = $formField;
  132. }
  133. }
  134. /**
  135. * whitelisting
  136. * 2011-07-22 ms
  137. */
  138. function beforeValidate(Model $Model) {
  139. # add fields to whitelist!
  140. $whitelist = array($this->settings[$Model->alias]['form_field'], $this->settings[$Model->alias]['form_field_repeat']);
  141. if ($this->settings[$Model->alias]['current']) {
  142. $whitelist[] = $this->settings[$Model->alias]['form_field_current'];
  143. }
  144. if (!empty($Model->whitelist)) {
  145. $Model->whitelist = am($Model->whitelist, $whitelist);
  146. }
  147. # make sure fields are set and validation rules are triggered - prevents tempering of form data
  148. $formField = $this->settings[$Model->alias]['form_field'];
  149. $formFieldRepeat = $this->settings[$Model->alias]['form_field_repeat'];
  150. $formFieldCurrent = $this->settings[$Model->alias]['form_field_current'];
  151. if (!isset($Model->data[$Model->alias][$formField])) {
  152. $Model->data[$Model->alias][$formField] = '';
  153. }
  154. if ($this->settings[$Model->alias]['confirm'] && !isset($Model->data[$Model->alias][$formFieldRepeat])) {
  155. $Model->data[$Model->alias][$formFieldRepeat] = '';
  156. }
  157. if ($this->settings[$Model->alias]['current'] && !isset($Model->data[$Model->alias][$formFieldCurrent])) {
  158. $Model->data[$Model->alias][$formFieldCurrent] = '';
  159. }
  160. return true;
  161. }
  162. /**
  163. * hashing the password now
  164. * 2011-07-22 ms
  165. */
  166. function beforeSave(Model $Model) {
  167. $formField = $this->settings[$Model->alias]['form_field'];
  168. $formFieldRepeat = $this->settings[$Model->alias]['form_field_repeat'];
  169. if (!empty($Model->data[$Model->alias][$formField])) {
  170. $field = $this->settings[$Model->alias]['field'];
  171. $type = $this->settings[$Model->alias]['hashType'];
  172. $salt = $this->settings[$Model->alias]['hashSalt'];
  173. $Model->data[$Model->alias][$field] = Security::hash($Model->data[$Model->alias][$formField], $type, $salt);
  174. unset($Model->data[$Model->alias][$formField]);
  175. if ($this->settings[$Model->alias]['confirm']) {
  176. unset($Model->data[$Model->alias][$formFieldRepeat]);
  177. }
  178. # update whitelist
  179. if (!empty($Model->whitelist)) {
  180. $Model->whitelist = am($Model->whitelist, array($field));
  181. }
  182. }
  183. return true;
  184. }
  185. }