Skip to content
Browse files

plugin files added

  • Loading branch information...
1 parent 428cf8b commit 76b882dfef8a546a025ca8297d309da612aa0acc pawel committed Feb 27, 2012
View
67 config/app.yml
@@ -0,0 +1,67 @@
+
+all:
+
+ dh_change_request:
+
+ # nb days change request is valid
+ expires_in: 7
+
+ algorithm: sha1
+ # you have to overwrite this option !
+ salt: 'some random salt'
+
+ mail:
+ changeEmail:
+ from: noreply@example.com
+ subject: Email change verification
+ partial: changeEmailMail
+ changePassword:
+ from: noreply@example.com
+ subject: Password change verification
+ partial: changePasswordMail
+
+ dhChangeEmailForm:
+ ajax: false
+
+ dhChangePasswordForm:
+ ajax: false
+
+ updateEmail:
+ message:
+ success: Verification email has been sent to given email address. Please confirm the change
+ error: There was an error while validating the form
+ flash_name:
+ success: notice
+ error: error
+ redirect:
+ success: '@dh_change_email'
+ error: false
+ updatePassword:
+ message:
+ success: Verification email has been sent to your email address. Please confirm the change
+ error: There was an error while validating the form
+ flash_name:
+ success: notice
+ error: error
+ redirect:
+ success: '@dh_change_password'
+ error: false
+ confirmEmailChange:
+ message:
+ success: Your email has been changed successfully
+ error: There is no request matching that link
+ token_expired: Link has expired
+ redirect:
+ success: false
+ error: false
+ token_expired: false
+ confirmPasswordChange:
+ message:
+ success: Your password has been changed successfully
+ error: There is no request matching that link
+ token_expired: Link has expired
+ redirect:
+ success: false
+ error: false
+ token_expired: false
+
View
15 config/doctrine/schema.yml
@@ -0,0 +1,15 @@
+dhChangeRequest:
+ columns:
+ #serialized field value
+ new_value: { type: string(255), notnull: true }
+ field_name: { type: string(255), notnull: true }
+ token: { type: string(128), notnull: true, unique: true }
+ token_expire: { type: timestamp }
+ user_id: { type: integer, notnull: true }
+ relations:
+ User:
+ class: sfGuardUser
+ local: user_id
+ onDelete: CASCADE
+ foreignAlias: ChangeRequests
+
View
31 config/routing.yml
@@ -0,0 +1,31 @@
+#change confirmation
+
+dh_confirm_email_change:
+ url: /confirm-email-change/:token
+ param: { module: dhConfirmChange, action: confirmEmailChange }
+
+dh_confirm_password_change:
+ url: /confirm-password-change/:token
+ param: { module: dhConfirmChange, action: confirmPasswordChange }
+
+# change email and password forms
+
+dh_change_email:
+ url: /change-email
+ param: { module: dhChangeRequest, action: changeEmail }
+
+dh_change_email_update:
+ url: /change-email/update
+ param: { module: dhChangeRequest, action: updateEmail }
+ requirements:
+ sf_method: [post]
+
+dh_change_password:
+ url: /change-password
+ param: { module: dhChangeRequest, action: changePassword }
+
+dh_change_password_update:
+ url: /change-password/update
+ param: { module: dhChangeRequest, action: updatePassword }
+ requirements:
+ sf_method: [post]
View
25 lib/action/dhBaseActions.class.php
@@ -0,0 +1,25 @@
+<?php
+
+class dhBaseActions extends sfActions
+{
+
+ protected function resolveRedirect($result)
+ {
+ $redirect = dhChangeRequestConfig::getActionRedirect($this->getActionName(), $result);
+
+ if ($redirect == 'referer')
+ $this->redirect($this->getRequest()->getReferer());
+
+ if ($redirect)
+ $this->redirect($redirect);
+ }
+
+ protected function setFlash($result)
+ {
+ $this->getUser()->setFlash(
+ dhChangeRequestConfig::getActionFlashName($this->getActionName(), $result),
+ $this->getContext()->getI18N()->__(dhChangeRequestConfig::getActionMessage($this->getActionName(), $result))
+ );
+ }
+
+}
View
119 lib/action/dhBaseChangeRequestActions.class.php
@@ -0,0 +1,119 @@
+<?php
+
+
+class dhBaseChangeRequestActions extends dhBaseActions
+{
+ const RESULT_SUCCESS = 'success';
+ const RESULT_ERROR = 'error';
+
+ public function executeChangeEmail(sfWebRequest $request)
+ {
+ $this->processEditAction('dhChangeEmailForm', dhChangeRequest::FIELD_NAME_EMAIL);
+ }
+
+ public function executeUpdateEmail(sfWebRequest $request)
+ {
+ return $this->processUpdateAction('dhChangeEmailForm', dhChangeRequest::FIELD_NAME_EMAIL, 'changeEmail');
+ }
+
+ public function executeChangePassword(sfWebRequest $request)
+ {
+ $this->processEditAction('dhChangePasswordForm', dhChangeRequest::FIELD_NAME_PASSWORD);
+ }
+
+ public function executeUpdatePassword(sfWebRequest $request)
+ {
+ return $this->processUpdateAction('dhChangePasswordForm', dhChangeRequest::FIELD_NAME_PASSWORD, 'changePassword');
+ }
+
+ protected function processEditAction($form_class, $field_name)
+ {
+ $form = new $form_class($this->getChangeRequest($field_name));
+
+ $this->resolveIsAjax($form);
+
+ $this->form = $form;
+ }
+
+ protected function resolveIsAjax($form)
+ {
+ if($form instanceof dhChangeEmailForm && dhChangeRequestConfig::isFormAjax('dhChangeEmailForm'))
+ {
+ $this->getResponse()->addJavascript('/dhDoctrineGuardChangeRequestPlugin/js/dh.change.email.form.js', 'last');
+ }
+ else if($form instanceof dhChangePasswordForm && dhChangeRequestConfig::isFormAjax('dhChangePasswordForm'))
+ {
+ $this->getResponse()->addJavascript('/dhDoctrineGuardChangeRequestPlugin/js/dh.change.password.form.js', 'last');
+ }
+ }
+
+ protected function processUpdateAction($form_class, $field_name, $template)
+ {
+ $form = new $form_class($this->getChangeRequest($field_name));
+
+ $result = $this->processForm($form);
+ $this->setFlash($result);
+
+ if($this->getRequest()->isXmlHttpRequest())
+ {
+ return $this->renderPartial($this->getModuleName().'/formFields', array('form' => $form));
+ }
+ else
+ {
+ $this->resolveRedirect($result);
+ $this->form = $form;
+ $this->setTemplate($template);
+ }
+
+ return null;
+ }
+
+ protected function processForm($form)
+ {
+ if($form->bindAndValidate($this->getRequest()))
+ {
+ $change_request = $form->save();
+
+ $this->sendVerificationMail($change_request);
+
+ return self::RESULT_SUCCESS;
+ }
+
+ return self::RESULT_ERROR;
+ }
+
+ protected function sendVerificationMail(dhChangeRequest $change_request)
+ {
+ if($change_request->getFieldName() === dhChangeRequest::FIELD_NAME_EMAIL)
+ {
+ $this->sendMail($change_request, $change_request->getNewValue(), 'changeEmail');
+ }
+ else if($change_request->getFieldName() === dhChangeRequest::FIELD_NAME_PASSWORD)
+ {
+ $this->sendMail($change_request, $change_request->getUser()->getEmailAddress(), 'changePassword');
+ }
+ }
+
+ protected function sendMail(dhChangeRequest $change_request, $to, $config)
+ {
+ $config = dhChangeRequestConfig::getMailConfig($config);
+
+ $message = $this->getMailer()->compose()
+ ->setFrom($config['from'])
+ ->setTo($to)
+ ->setSubject($config['subject'])
+ ->setBody($this->getPartial($config['partial'], array('change_request' => $change_request)));
+
+ return $this->getMailer()->send($message);
+ }
+
+ /**
+ *
+ * @param type $field_name
+ * @return dhChangeRequest
+ */
+ protected function getChangeRequest($field_name)
+ {
+ return dhChangeRequest::getChangeRequest($this->getUser()->getGuardUser(), $field_name);
+ }
+}
View
49 lib/action/dhBaseConfirmChangeActions.class.php
@@ -0,0 +1,49 @@
+<?php
+
+
+class dhBaseConfirmChangeActions extends dhBaseActions
+{
+ const RESULT_SUCCESS = 'success';
+ const RESULT_ERROR = 'error';
+ const RESULT_TOKEN_EXPIRED = 'token_expired';
+
+ public function executeConfirmEmailChange(sfWebRequest $request)
+ {
+ return $this->changeField($request->getParameter('token'), dhChangeRequest::FIELD_NAME_EMAIL);
+ }
+
+ public function executeConfirmPasswordChange(sfWebRequest $request)
+ {
+ return $this->changeField($request->getParameter('token'), dhChangeRequest::FIELD_NAME_PASSWORD);
+ }
+
+ protected function changeField($token, $field_name)
+ {
+ $change_request = dhChangeRequestTable::getInstance()->findOneByTokenAndFieldName($token, $field_name);
+
+ if(!$change_request)
+ {
+ $result = self::RESULT_ERROR;
+ }
+ else
+ {
+ if($change_request->isRequestValid()) $result = self::RESULT_TOKEN_EXPIRED;
+ else
+ {
+ $change_request->confirm();
+ $result = self::RESULT_SUCCESS;
+ }
+ }
+
+ return $this->processResult($result);
+ }
+
+ protected function processResult($result)
+ {
+ $this->resolveRedirect($result);
+
+ //in case of no redirect
+ $this->message = dhChangeRequestConfig::getActionMessage($this->getActionName(), $result);
+ $this->setTemplate('confirmChange');
+ }
+}
View
50 lib/config/dhChangeRequestConfig.class.php
@@ -0,0 +1,50 @@
+<?php
+
+
+class dhChangeRequestConfig
+{
+ static public function getActionConfig($action_name)
+ {
+ return sfConfig::get('app_dh_change_request_'.$action_name);
+ }
+
+ static public function getMailConfig($config_key)
+ {
+ $config = sfConfig::get('app_dh_change_request_mail', array());
+
+ return $config[$config_key];
+ }
+
+ static public function getFormConfig($form_class)
+ {
+ return sfConfig::get('app_dh_change_request_'.$form_class);
+ }
+
+ static public function getActionMessage($action_name, $result)
+ {
+ $config = self::getActionConfig($action_name);
+
+ return $config['message'][$result];
+ }
+
+ static public function getActionFlashName($action_name, $result)
+ {
+ $config = self::getActionConfig($action_name);
+
+ return $config['flash_name'][$result];
+ }
+
+ static public function getActionRedirect($action_name, $result)
+ {
+ $config = self::getActionConfig($action_name);
+
+ return $config['redirect'][$result];
+ }
+
+ static public function isFormAjax($form_class)
+ {
+ $config = self::getFormConfig($form_class);
+
+ return $config['ajax'];
+ }
+}
View
13 lib/filter/doctrine/PlugindhChangeRequestFormFilter.class.php
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * PlugindhChangeRequest form.
+ *
+ * @package ##PROJECT_NAME##
+ * @subpackage filter
+ * @author ##AUTHOR_NAME##
+ * @version SVN: $Id: sfDoctrineFormFilterPluginTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
+ */
+abstract class PlugindhChangeRequestFormFilter extends BasedhChangeRequestFormFilter
+{
+}
View
21 lib/form/doctrine/PlugindhChangeRequestForm.class.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * PlugindhChangeRequest form.
+ *
+ * @package ##PROJECT_NAME##
+ * @subpackage form
+ * @author ##AUTHOR_NAME##
+ * @version SVN: $Id: sfDoctrineFormPluginTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
+ */
+abstract class PlugindhChangeRequestForm extends BasedhChangeRequestForm
+{
+
+ public function bindAndValidate(sfWebRequest $request)
+ {
+ $this->bind($request->getParameter($this->getName()), $request->getFiles($this->getName()));
+
+ return $this->isValid();
+ }
+
+}
View
23 lib/form/doctrine/dhChangeEmailForm.class.php
@@ -0,0 +1,23 @@
+<?php
+
+class dhChangeEmailForm extends dhChangeFieldForm
+{
+
+ protected function getFieldName()
+ {
+ return dhChangeRequest::FIELD_NAME_EMAIL;
+ }
+
+ public function configure()
+ {
+ parent::configure();
+
+ $this->widgetSchema[dhChangeRequest::FIELD_NAME_EMAIL] = new sfWidgetFormInputText();
+ $this->validatorSchema[dhChangeRequest::FIELD_NAME_EMAIL] = new sfValidatorString(array('max_length' => 255));
+
+ $this->useFields(array(dhChangeRequest::FIELD_NAME_EMAIL));
+
+ $this->mergePostValidator(new sfValidatorDoctrineUnique(array('model' => 'sfGuardUser', 'column' => 'email_address')));
+ }
+
+}
View
20 lib/form/doctrine/dhChangeFieldForm.class.php
@@ -0,0 +1,20 @@
+<?php
+
+abstract class dhChangeFieldForm extends PlugindhChangeRequestForm
+{
+
+ abstract protected function getFieldName();
+
+ public function configure()
+ {
+ $this->widgetSchema['user_id'] = new sfWidgetFormInputHidden();
+ }
+
+ protected function doUpdateObject($values)
+ {
+ parent::doUpdateObject($values);
+
+ $this->getObject()->updateRequest(sfConfig::get('app_dh_change_request_expires_in'));
+ }
+
+}
View
28 lib/form/doctrine/dhChangePasswordForm.class.php
@@ -0,0 +1,28 @@
+<?php
+
+class dhChangePasswordForm extends dhChangeFieldForm
+{
+
+ protected function getFieldName()
+ {
+ return dhChangeRequest::FIELD_NAME_PASSWORD;
+ }
+
+ public function configure()
+ {
+ parent::configure();
+
+ $this->widgetSchema[dhChangeRequest::FIELD_NAME_PASSWORD] = new sfWidgetFormInputPassword();
+ $this->validatorSchema[dhChangeRequest::FIELD_NAME_PASSWORD] = new sfValidatorString(array('max_length' => 128, 'required' => true));
+ $this->widgetSchema['password_again'] = new sfWidgetFormInputPassword();
+ $this->validatorSchema['password_again'] = clone $this->validatorSchema[dhChangeRequest::FIELD_NAME_PASSWORD];
+ $this->validatorSchema['password_again']->setOption('required', true);
+
+ $this->getWidgetSchema()->setLabel(dhChangeRequest::FIELD_NAME_PASSWORD, 'Password');
+
+ $this->mergePostValidator(new sfValidatorSchemaCompare(dhChangeRequest::FIELD_NAME_PASSWORD, sfValidatorSchemaCompare::EQUAL, 'password_again', array(), array('invalid' => 'The two passwords must be the same.')));
+
+ $this->useFields(array(dhChangeRequest::FIELD_NAME_PASSWORD, 'password_again'));
+ }
+
+}
View
122 lib/model/doctrine/PlugindhChangeRequest.class.php
@@ -0,0 +1,122 @@
+<?php
+
+/**
+ * PlugindhChangeRequest
+ *
+ * This class has been auto-generated by the Doctrine ORM Framework
+ *
+ * @package ##PACKAGE##
+ * @subpackage ##SUBPACKAGE##
+ * @author ##NAME## <##EMAIL##>
+ * @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
+ */
+abstract class PlugindhChangeRequest extends BasedhChangeRequest
+{
+
+ const FIELD_NAME_PASSWORD = 'hashed_password';
+ const FIELD_NAME_EMAIL = 'email_address';
+
+ public function setHashedPassword($password)
+ {
+ $this->setFieldName(self::FIELD_NAME_PASSWORD);
+ $this->setNewValue($this->getHashedPassword($password));
+ }
+
+ public function setEmailAddress($email_address)
+ {
+ $this->setFieldName(self::FIELD_NAME_EMAIL);
+ $this->setNewValue($email_address);
+ }
+
+ protected function getHashedPassword($password)
+ {
+ if(!$this->getUserId() || $this->getUser()->isNew())
+ throw new Exception('You have to set an existing User before generating a password');
+
+ $user = $this->getUser()->copy();
+
+ $user->setPassword($password);
+
+ return $user->getPassword();
+ }
+
+ public function updateRequest($days_valid = 7)
+ {
+ $this->setToken($this->generateUniqueToken());
+ $this->setTokenExpire($this->getExpireDate($days_valid));
+ }
+
+ protected function generateUniqueToken()
+ {
+ $algorithm = sfConfig::get('app_dh_change_request_algorithm');
+ $salt = sfConfig::get('app_dh_change_request_salt');
+
+ do
+ {
+ $token = call_user_func($algorithm, ((string) time()) . $salt);
+ }
+ while(dhChangeRequestTable::getInstance()->findOneByToken($token));
+
+ return $token;
+ }
+
+ protected function getExpireDate($days_valid)
+ {
+ $dt = new DateTime();
+ $dt->modify("+$days_valid day");
+
+ return $dt->format('Y-m-d H:i:s');
+ }
+
+ public function isRequestValid()
+ {
+ $current = new DateTime();
+ $token_expire = $this->getDateTimeObject('token_expire');
+
+ return $current->getTimestamp() > $token_expire->getTimestamp();
+ }
+
+ public function confirm()
+ {
+ if(!$this->getFieldName() || !$this->getNewValue())
+ throw new sfException('There is nothing to confirm');
+
+ $this->updateUser();
+
+ $this->delete();
+ }
+
+ protected function updateUser()
+ {
+ $user = $this->getUser();
+ $user->set($this->getFieldName(), $this->getNewValue());
+ $user->save();
+ }
+
+ /**
+ *
+ * @param sfGuardUser $user
+ * @param type $field_name
+ * @return dhChangeRequest
+ */
+ static public function getChangeRequest(sfGuardUser $user, $field_name)
+ {
+ $change_requests = $user->getChangeRequests();
+
+ foreach($change_requests as $change_request) /* @var $change_request dhChangeRequest */
+ {
+ if($change_request->getFieldName() === $field_name)
+ {
+ return $change_request;
+ }
+ }
+
+ //if(!$change_request)
+ $change_request = $change_requests->get($field_name);
+ $change_request->setUser($user);
+
+ return $change_request;
+ }
+
+}
+
View
19 lib/model/doctrine/PlugindhChangeRequestTable.class.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * PlugindhChangeRequestTable
+ *
+ * This class has been auto-generated by the Doctrine ORM Framework
+ */
+class PlugindhChangeRequestTable extends Doctrine_Table
+{
+ /**
+ * Returns an instance of this class.
+ *
+ * @return object PlugindhChangeRequestTable
+ */
+ public static function getInstance()
+ {
+ return Doctrine_Core::getTable('PlugindhChangeRequest');
+ }
+}
View
8 modules/dhChangeRequest/actions/actions.class.php
@@ -0,0 +1,8 @@
+<?php
+
+
+
+class dhChangeRequestActions extends dhBaseChangeRequestActions
+{
+
+}
View
2 modules/dhChangeRequest/config/security.yml
@@ -0,0 +1,2 @@
+all:
+ is_secure: true
View
5 modules/dhChangeRequest/templates/_changeEmailMail.php
@@ -0,0 +1,5 @@
+Someone made a request to change your email address.
+
+If it was you click the link below to confirm the change:
+
+<?php echo url_for('dh_confirm_email_change', array('token' => $change_request->getToken()), true); ?>
View
5 modules/dhChangeRequest/templates/_changePasswordMail.php
@@ -0,0 +1,5 @@
+Someone made a request to change your password.
+
+If it was you click the link below to confirm the change:
+
+<?php echo url_for('dh_confirm_password_change', array('token' => $change_request->getToken()), true); ?>
View
1 modules/dhChangeRequest/templates/_formFields.php
@@ -0,0 +1 @@
+<?php echo $form; ?>
View
11 modules/dhChangeRequest/templates/changeEmailSuccess.php
@@ -0,0 +1,11 @@
+<form id="dh-change-email-form" action="<?php echo url_for('@dh_change_email_update'); ?>" method="post">
+
+ <div class="dh-form-fields-container">
+
+ <?php include_partial('dhChangeRequest/formFields', array('form' => $form)); ?>
+
+ </div>
+
+ <input type="submit" value="Change" />
+
+</form>
View
11 modules/dhChangeRequest/templates/changePasswordSuccess.php
@@ -0,0 +1,11 @@
+<form id="dh-change-password-form" action="<?php echo url_for('@dh_change_password_update'); ?>" method="post">
+
+ <div class="dh-form-fields-container">
+
+ <?php include_partial('dhChangeRequest/formFields', array('form' => $form)); ?>
+
+ </div>
+
+ <input type="submit" value="Change" />
+
+</form>
View
8 modules/dhConfirmChange/actions/actions.class.php
@@ -0,0 +1,8 @@
+<?php
+
+
+
+class dhConfirmChangeActions extends dhBaseConfirmChangeActions
+{
+
+}
View
1 modules/dhConfirmChange/templates/confirmChangeSuccess.php
@@ -0,0 +1 @@
+<p class="dh-confirm-change-message"><?php echo $message; ?></p>
View
9 web/js/dh.change.email.form.js
@@ -0,0 +1,9 @@
+$(document).ready(function() {
+
+ var change_email_options = {
+ target : '#dh-change-email-form .dh-form-fields-container'
+ };
+
+ $('#dh-change-email-form').ajaxForm(change_email_options);
+
+});
View
9 web/js/dh.change.password.form.js
@@ -0,0 +1,9 @@
+$(document).ready(function() {
+
+ var change_password_options = {
+ target : '#dh-change-password-form .dh-form-fields-container'
+ };
+
+ $('#dh-change-password-form').ajaxForm(change_password_options);
+
+});

0 comments on commit 76b882d

Please sign in to comment.
Something went wrong with that request. Please try again.