Author: sevein
Date: Sun Jan 29 19:22:05 2012
New Revision: 10770

Log:
Require strong passwords (optional), refers to issue 2227. TODO: add help text 
when required.

Modified:
   trunk/apps/qubit/modules/settings/actions/listAction.class.php
   trunk/apps/qubit/modules/user/actions/editAction.class.php
   trunk/data/fixtures/settings.yml
   trunk/lib/form/SettingsGlobalForm.class.php
   trunk/lib/validator/QubitValidatorPassword.class.php

Modified: trunk/apps/qubit/modules/settings/actions/listAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/settings/actions/listAction.class.php      Sun Jan 
29 00:02:55 2012        (r10769)
+++ trunk/apps/qubit/modules/settings/actions/listAction.class.php      Sun Jan 
29 19:22:05 2012        (r10770)
@@ -220,6 +220,7 @@
     $defaultPubStatus = QubitSetting::getSettingByName('defaultPubStatus');
     $swordDepositDir = QubitSetting::getSettingByName('sword_deposit_dir');
     $requireSslAdmin = QubitSetting::getSettingByName('require_ssl_admin');
+    $requireStrongPasswords = 
QubitSetting::getSettingByName('require_strong_passwords');
 
     // Set defaults for global form
     $this->globalForm->setDefaults(array(
@@ -239,6 +240,7 @@
       'defaultPubStatus' => (isset($defaultPubStatus)) ? 
$defaultPubStatus->getValue(array('sourceCulture'=>true)) : 
QubitTerm::PUBLICATION_STATUS_DRAFT_ID,
       'sword_deposit_dir' => (isset($swordDepositDir)) ? 
$swordDepositDir->getValue(array('sourceCulture'=>true)) : null,
       'require_ssl_admin' => (isset($requireSslAdmin)) ? 
intval($requireSslAdmin->getValue(array('sourceCulture'=>true))) : 1,
+      'require_strong_passwords' => (isset($requireStrongPasswords)) ? 
intval($requireStrongPasswords->getValue(array('sourceCulture'=>true))) : 1
     ));
   }
 
@@ -422,6 +424,16 @@
       $setting->save();
     }
 
+    // Require strong passwords
+    if (null !== $requireStrongPasswords = 
$thisForm->getValue('require_strong_passwords'))
+    {
+      $setting = QubitSetting::getSettingByName('require_strong_passwords');
+
+      // Force sourceCulture update to prevent discrepency in settings between 
cultures
+      $setting->setValue($requireStrongPasswords, array('sourceCulture' => 
true));
+      $setting->save();
+    }
+
     return $this;
   }
 

Modified: trunk/apps/qubit/modules/user/actions/editAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/user/actions/editAction.class.php  Sun Jan 29 
00:02:55 2012        (r10769)
+++ trunk/apps/qubit/modules/user/actions/editAction.class.php  Sun Jan 29 
19:22:05 2012        (r10770)
@@ -32,7 +32,7 @@
   protected function earlyExecute()
   {
     $this->form->getValidatorSchema()->setOption('allow_extra_fields', true);
-    $this->form->getValidatorSchema()->setPostValidator(new 
sfValidatorSchemaCompare(
+    $this->form->getValidatorSchema()->setPreValidator(new 
sfValidatorSchemaCompare(
       'password', '==', 'confirmPassword',
       array(),
       array('invalid' => $this->context->i18n->__('Your password confirmation 
did not match you password.'))));
@@ -81,11 +81,28 @@
         break;
 
       case 'password':
+        $this->form->setDefault('password', null);
+
+        // Use QubitValidatorPassword only when strong passwords are required
+        if (sfConfig::get('app_require_strong_passwords'))
+        {
+          $this->form->setValidator('password', new QubitValidatorPassword(
+            array('required' => !isset($this->getRoute()->resource)),
+            array('invalid' => $this->context->i18n->__('Your password is not 
strong enough.'),
+                  'min_length' => $this->context->i18n->__('Your password is 
not strong enough (too short).'))));
+        }
+        else
+        {
+          $this->form->setValidator('confirmPassword', new 
sfValidatorString(array('required' => !isset($this->getRoute()->resource))));
+        }
+
+        $this->form->setWidget('password', new sfWidgetFormInputPassword);
+
       case 'confirmPassword':
-        $this->form->setDefault($name, null);
+        $this->form->setDefault('confirmPassword', null);
         // Required field only if a new user is being created
-        $this->form->setValidator($name, new 
sfValidatorString(array('required' => !isset($this->getRoute()->resource))));
-        $this->form->setWidget($name, new sfWidgetFormInputPassword);
+        $this->form->setValidator('confirmPassword', new 
sfValidatorString(array('required' => !isset($this->getRoute()->resource))));
+        $this->form->setWidget('confirmPassword', new 
sfWidgetFormInputPassword);
 
         break;
 

Modified: trunk/data/fixtures/settings.yml
==============================================================================
--- trunk/data/fixtures/settings.yml    Sun Jan 29 00:02:55 2012        (r10769)
+++ trunk/data/fixtures/settings.yml    Sun Jan 29 19:22:05 2012        (r10770)
@@ -369,6 +369,9 @@
   QubitSetting_swordDepositDir:
     name: sword_deposit_dir
     value: '/tmp'
-  QubitSetting_sslRequirementAdmin:
+  QubitSetting_requireSslAdmin:
     name: require_ssl_admin
     value: 0
+  QubitSetting_requireStrongPasswords:
+    name: require_strong_password
+    value: 0

Modified: trunk/lib/form/SettingsGlobalForm.class.php
==============================================================================
--- trunk/lib/form/SettingsGlobalForm.class.php Sun Jan 29 00:02:55 2012        
(r10769)
+++ trunk/lib/form/SettingsGlobalForm.class.php Sun Jan 29 19:22:05 2012        
(r10770)
@@ -54,7 +54,8 @@
       'show_tooltips' => new 
sfWidgetFormSelectRadio(array('choices'=>array(1=>'yes', 0=>'no')), 
array('class'=>'radio')),
       'defaultPubStatus' => new 
sfWidgetFormSelectRadio(array('choices'=>array(QubitTerm::PUBLICATION_STATUS_DRAFT_ID=>__('Draft'),
 QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID=>__('Published'))), 
array('class'=>'radio')),
       'sword_deposit_dir' => new sfWidgetFormInput,
-      'require_ssl_admin' => new 
sfWidgetFormSelectRadio(array('choices'=>array(1=>'yes', 0=>'no')), 
array('class'=>'radio'))
+      'require_ssl_admin' => new 
sfWidgetFormSelectRadio(array('choices'=>array(1=>'yes', 0=>'no')), 
array('class'=>'radio')),
+      'require_strong_passwords' => new 
sfWidgetFormSelectRadio(array('choices'=>array(1=>'yes', 0=>'no')), 
array('class'=>'radio'))
     ));
 
     // Add labels
@@ -74,7 +75,8 @@
       'show_tooltips' => __('Show tooltips'),
       'defaultPubStatus' => __('Default publication status'),
       'sword_deposit_dir' => __('SWORD deposit directory'),
-      'require_ssl_admin' => __('Require SSL for all administrator 
funcionality')
+      'require_ssl_admin' => __('Require SSL for all administrator 
funcionality'),
+      'require_strong_passwords' => __('Require strong passwords')
     ));
 
     // Add helper text
@@ -95,6 +97,7 @@
       // 'show_tooltips' => __('')
       // 'sword_deposit_dir' => __('')
       // 'require_ssl_admin' => __('')
+      // 'require_strong_passwords' => __('')
     ));
 
     // Reference image max. width validator
@@ -141,6 +144,7 @@
     $this->validatorSchema['defaultPubStatus'] = new 
sfValidatorChoice(array('choices' => 
array(QubitTerm::PUBLICATION_STATUS_DRAFT_ID, 
QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID)));
     $this->validatorSchema['sword_deposit_dir'] = new 
sfValidatorString(array('required' => false));
     $this->validatorSchema['require_ssl_admin'] = new 
sfValidatorInteger(array('required' => false));
+    $this->validatorSchema['require_strong_passwords'] = new 
sfValidatorInteger(array('required' => false));
 
     // Set decorator
     $decorator = new QubitWidgetFormSchemaFormatterList($this->widgetSchema);

Modified: trunk/lib/validator/QubitValidatorPassword.class.php
==============================================================================
--- trunk/lib/validator/QubitValidatorPassword.class.php        Sun Jan 29 
00:02:55 2012        (r10769)
+++ trunk/lib/validator/QubitValidatorPassword.class.php        Sun Jan 29 
19:22:05 2012        (r10770)
@@ -17,9 +17,51 @@
  * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-class QubitValidatorPassword extends sfValidatorBase
+class QubitValidatorPassword extends sfValidatorString
 {
   protected function configure($options = array(), $messages = array())
   {
+    parent::configure($options, $messages);
+
+    $this->setOption('min_length', 8);
+  }
+
+  protected function doClean($value)
+  {
+    $value = parent::doClean($value);
+
+    $score = 0;
+
+    // Check 1: contains upper case letters
+    if (preg_match('/[A-Z]/', $value))
+    {
+      $score++;
+    }
+
+    // Check 2: contains lower case letters
+    if (preg_match('/[a-z]/', $value))
+    {
+      $score++;
+    }
+
+    // Check 3: contains numbers
+    if (preg_match('/[0-9]/', $value))
+    {
+      $score++;
+    }
+
+    // Check 4: contains everything but 1), 2) and 3) (special characters)
+    if (preg_match('/[^A-Za-z0-9]/', $value))
+    {
+      $score++;
+    }
+
+    // If less than three checks were passed
+    if ($score < 3)
+    {
+      throw new sfValidatorError($this, 'invalid', array('value' => null));
+    }
+
+    return $value;
   }
 }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to