use Cake\Validation\Validator; use Cake\ORM\TableRegistry; ! class ChangePasswordForm extends Form { protected $table; protected $user; ! public function __construct($id) { // ͲͷϢʔβʔʹରͯ͠มߋΛߦ͏ͷ͔ίϯετϥΫλͰઃఆ $this->table = TableRegistry::get('Users'); $this->user = $this->table->get($id); } ! protected function _buildSchema(Schema $schema) { // ϑΟʔϧυఆٛ return $schema ->addField('password', ['type' => 'string']) ->addField('new_password', ['type' => 'string']) ->addField('confirm_password', ['type' => 'string']); } ! protected function _buildValidator(Validator $validator) { // UsersTableʹఆٛͯ͋͠ΔvalidationΛར༻͢Δ // ݱࡏͷύεϫʔυͱҰக͢Δ $this->table->validationRequireCurrentPassword($validator); ! // ৽͍͠ύεϫʔυͱ֬ೝύεϫʔυͷೖྗ͕Ұக͢Δ $validator ->notEmpty('new_password', __('ύεϫʔυඞͣೖྗ͍ͯͩ͘͠͞ɻ')) ->notEmpty('confirm_password', __('ύεϫʔυඞͣೖྗ͍ͯͩ͘͠͞ɻ')); $validator->add('new_password', 'minLength', [ 'rule' => ['minLength', 6], 'message' => __('ύεϫʔυ6จࣈҎ্Ͱೖྗ͍ͯͩ͘͠͞ɻ'), ]) ->add('new_password', 'maxLength', [ 'rule' => ['maxLength', 255], 'message' => __('ύεϫʔυ255จࣈҎͰೖྗ͍ͯͩ͘͠͞ɻ'), ]); $validator->add('confirm_password', 'maxLength', [ 'rule' => ['maxLength', 255], 'message' => __('ύεϫʔυ255จࣈҎͰೖྗ͍ͯͩ͘͠͞ɻ'), ]) ->add('confirm_password', 'compareWith', [ 'rule' => ['compareWith', 'new_password'], 'message' => __('ύεϫʔυ͕Ұக͠·ͤΜɻ'), ]); ! return $validator; } ! protected function _execute(array $data) { // ύεϫʔυߋ৽ $newpassword = ['password' => $data['new_password']]; $this->table->patchEntity($this->user, $newpassword); if (!$this->table->save($this->user)) { return false; } ! // send email $mailer = new Email('default'); $mailer->to($this->user) ->subject(__('ύεϫʔυ͕มߋ͞Ε·ͨ͠')) ->template('change_password') ->viewVars(['user' => $user]) ->send(); ! return true; } ! public function getUser() { return $this->user; } }