|
From: <gem...@li...> - 2011-11-18 17:08:35
|
Revision: 238
http://gemstracker.svn.sourceforge.net/gemstracker/?rev=238&view=rev
Author: matijsdejong
Date: 2011-11-18 17:08:28 +0000 (Fri, 18 Nov 2011)
Log Message:
-----------
Organization code is now used
FIrst version of PasswordChecker.php, not yet integrated with working code.
Modified Paths:
--------------
trunk/library/classes/Gems/Default/OrganizationAction.php
trunk/library/classes/Gems/Project/ProjectSettings.php
trunk/library/classes/Gems/User/Organization.php
trunk/library/classes/Gems/User/User.php
trunk/library/classes/Gems/User/UserDefinitionAbstract.php
trunk/library/classes/Gems/User/UserLoader.php
Added Paths:
-----------
trunk/library/classes/Gems/User/PasswordChecker.php
Modified: trunk/library/classes/Gems/Default/OrganizationAction.php
===================================================================
--- trunk/library/classes/Gems/Default/OrganizationAction.php 2011-11-18 15:11:59 UTC (rev 237)
+++ trunk/library/classes/Gems/Default/OrganizationAction.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -159,6 +159,7 @@
$model->set('gor_name', 'description', 'ENGLISH please! Use translation file to translate.');
$model->set('gor_url', 'description', 'ENGLISH link preferred. Use translation file to translate.');
$model->set('gor_task', 'description', 'ENGLISH please! Use translation file to translate.');
+ $model->set('gor_code', 'label', $this->_('Code name'), 'size', 10, 'description', $this->_('Only for programmers.'));
}
Gems_Model::setChangeFieldsByPrefix($model, 'gor');
Modified: trunk/library/classes/Gems/Project/ProjectSettings.php
===================================================================
--- trunk/library/classes/Gems/Project/ProjectSettings.php 2011-11-18 15:11:59 UTC (rev 237)
+++ trunk/library/classes/Gems/Project/ProjectSettings.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -91,6 +91,20 @@
$this->checkRequiredValues();
}
+ protected function _getPasswordRules(array $current, array $keys, array &$rules)
+ {
+ foreach ($current as $key => $value) {
+ if (is_array($value)) {
+ // Only act when this is in the set of key values
+ if (isset($keys[strtolower($key)])) {
+ $this->_getPasswordRules($value, $keys, $rules);
+ }
+ } else {
+ $rules[$key] = $value;
+ }
+ }
+ }
+
/**
* This function checks for the required project settings.
*
@@ -181,6 +195,26 @@
}
/**
+ *
+ * @param string $userDefinition
+ * @param string $role
+ * @return array
+ */
+ public function getPasswordRules($userDefinition, $role)
+ {
+ $args = MUtil_Ra::flatten(func_get_args());
+ $args = array_change_key_case(array_flip(array_filter($args)));
+ //MUtil_Echo::track($args);
+
+ $rules = array();
+ if (isset($this->passwords) && is_array($this->passwords)) {
+ $this->_getPasswordRules($this->passwords, $args, $rules);
+ }
+
+ return $rules;
+ }
+
+ /**
* Returns the super admin name, if any
*
* @return string
Modified: trunk/library/classes/Gems/User/Organization.php
===================================================================
--- trunk/library/classes/Gems/User/Organization.php 2011-11-18 15:11:59 UTC (rev 237)
+++ trunk/library/classes/Gems/User/Organization.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -154,6 +154,15 @@
return is_array($this->_organizationData) && parent::checkRegistryRequestsAnswers();
}
+ /**
+ * Get the style attribute.
+ *
+ * @return string
+ */
+ public function getCode()
+ {
+ return $this->_organizationData['gor_code'];
+ }
/**
* Get the style attribute.
Added: trunk/library/classes/Gems/User/PasswordChecker.php
===================================================================
--- trunk/library/classes/Gems/User/PasswordChecker.php (rev 0)
+++ trunk/library/classes/Gems/User/PasswordChecker.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -0,0 +1,225 @@
+<?php
+
+/**
+ * Copyright (c) 2011, Erasmus MC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Erasmus MC nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * @package Gems
+ * @subpackage User
+ * @author Matijs de Jong <mj...@ma...>
+ * @copyright Copyright (c) 2011 Erasmus MC
+ * @license New BSD License
+ * @version $Id: Sample.php 203 2011-07-07 12:51:32Z matijs $
+ */
+
+/**
+ *
+ *
+ * @package Gems
+ * @subpackage User
+ * @copyright Copyright (c) 2011 Erasmus MC
+ * @license New BSD License
+ * @since Class available since version 1.5
+ */
+class Gems_User_PasswordChecker extends MUtil_Registry_TargetAbstract
+{
+ /**
+ *
+ * @var array
+ */
+ protected $_errors = array();
+
+ /**
+ *
+ * @var Gems_Project_ProjectSettings
+ */
+ protected $project;
+
+ /**
+ *
+ * @var Zend_Translate
+ */
+ protected $translate;
+
+ /**
+ *
+ * @var Gems_User_User $user
+ */
+ protected $user;
+
+ /**
+ *
+ * @param type $errorMsg
+ */
+ protected function _addError($errorMsg)
+ {
+ $this->_errors[] = $errorMsg;
+ }
+
+
+ /**
+ * Test the password for minimum number of upper case characters.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function capsCount($parameter, $password)
+ {
+ $len = intval($parameter);
+ $results = array();
+ if (preg_match_all('/[A-Z]/', $password, $results) < $len) {
+ $this->_addError(sprintf(
+ $this->translate->plural('A password should contain at least one uppercase character.', 'A password should contain at least %d uppercase characters.', $len),
+ $len));
+ }
+ }
+
+ /**
+ * Test the password for minimum number of lower case characters.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function lowerCount($parameter, $password)
+ {
+ $len = intval($parameter);
+ $results = array();
+ if (preg_match_all('/[a-z]/', $password, $results) < $len) {
+ $this->_addError(sprintf(
+ $this->translate->plural('A password should contain at least one lowercase character.', 'A password should contain at least %d lowercase characters.', $len),
+ $len));
+ }
+ }
+
+ /**
+ * Test the password for minimum length.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function minLength($parameter, $password)
+ {
+ $len = intval($parameter);
+ if (strlen($password) < $len) {
+ $this->_addError(sprintf($this->translate->_('A password should be at least %d characters long.'), $len));
+ }
+ }
+
+ /**
+ * Test the password for minimum number non letter characters.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function notAlphaCount($parameter, $password)
+ {
+ $len = intval($parameter);
+ $results = array();
+ $count = preg_match_all('/[A-Za-z]/', $password, $results);
+ if (strlen($password) - $count < $len) {
+ $this->_addError(sprintf(
+ $this->translate->plural('A password should contain at least one not alphabetic character.', 'A password should contain at least %d not alphabetic characters.', $len),
+ $len));
+ }
+ }
+
+ /**
+ * Test the password for minimum number not alphanumeric characters.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function notAlphaNumCount($parameter, $password)
+ {
+ $len = intval($parameter);
+ $results = array();
+ $count = preg_match_all('/[A-Za-z]/', $password, $results);
+ if (strlen($password) - $count < $len) {
+ $this->_addError(sprintf(
+ $this->translate->plural('A password should contain at least one not alphanumeric character.', 'A password should contain at least %d not alphanumeric characters.', $len),
+ $len));
+ }
+ }
+
+ /**
+ * The password should not contain the name of the user or the login name.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function notTheName($parameter, $password)
+ {
+ $on = $parameter != 0;
+ if ($on) {
+ $lpwd = strtolower($password);
+
+ if (false !== strpos($lpwd, strtolower($this->user->getLoginName()))) {
+ $this->_addError($this->translate->_('A password should not contain the login name.'));
+ }
+ }
+ }
+
+ /**
+ * Test the password for minimum number of numeric characters.
+ *
+ * @param mixed $parameter
+ * @param string $password
+ */
+ protected function numCount($parameter, $password)
+ {
+ $len = intval($parameter);
+ $results = array();
+ if (preg_match_all('/[0-9]/', $password, $results) < $len) {
+ $this->_addError(sprintf(
+ $this->translate->plural('A password should contain at least one number.', 'A password should contain at least %d numbers.', $len),
+ $len));
+ }
+ }
+
+ /**
+ * Check for password weakness.
+ *
+ * @param Gems_User_User $user The user for e.g. name checks
+ * @param string $password
+ * @return mixed String or array of strings containing warning messages
+ */
+ public function reportPasswordWeakness(Gems_User_User $user, $password)
+ {
+ $this->_errors = array();
+ $this->user = $user;
+
+ $rules = $this->project->getPasswordRules($user->getOrganizationCode(), $user->getRoles());
+
+ // MUtil_Echo::track($rules);
+ foreach ($rules as $rule => $parameter) {
+ if (method_exists($this, $rule)) {
+ $this->$rule($parameter, $password);
+ }
+ }
+
+ return $this->_errors;
+ }
+}
Modified: trunk/library/classes/Gems/User/User.php
===================================================================
--- trunk/library/classes/Gems/User/User.php 2011-11-18 15:11:59 UTC (rev 237)
+++ trunk/library/classes/Gems/User/User.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -60,6 +60,12 @@
/**
*
+ * @var MUtil_Acl
+ */
+ protected $acl;
+
+ /**
+ *
* @var Gems_User_UserDefinitionInterface
*/
protected $definition;
@@ -202,7 +208,7 @@
$authResult = $auth->authenticate($adapter, $formValues);
$this->_authResult = $authResult;
-
+
return $authResult;
}
@@ -349,6 +355,18 @@
}
/**
+ * Gets the (optional) organization code.
+ *
+ * @return string
+ */
+ public function getOrganizationCode()
+ {
+ $organizationId = $this->getOrganizationId();
+
+ return $this->userLoader->getOrganization($organizationId)->getCode();
+ }
+
+ /**
* Return a password reset key
*
* @return string
@@ -369,6 +387,16 @@
}
/**
+ * Returns the current user role.
+ *
+ * @return string
+ */
+ public function getRoles()
+ {
+ return $this->acl->getRoleAndParents($this->getRole());
+ }
+
+ /**
* Returns the user id, that identifies this user within this installation.
*
* One user id might be connected to multiple logins for multiple organizations.
Modified: trunk/library/classes/Gems/User/UserDefinitionAbstract.php
===================================================================
--- trunk/library/classes/Gems/User/UserDefinitionAbstract.php 2011-11-18 15:11:59 UTC (rev 237)
+++ trunk/library/classes/Gems/User/UserDefinitionAbstract.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -111,17 +111,6 @@
}
/**
- * Check for password weakness.
- *
- * @param string $password
- * @return mixed String or array of strings containing warning messages
- */
- public function isPasswordWeak($password)
- {
-
- }
-
- /**
* Set the password, if allowed for this user type.
*
* @param Gems_User_User $user The user whose password to change
Modified: trunk/library/classes/Gems/User/UserLoader.php
===================================================================
--- trunk/library/classes/Gems/User/UserLoader.php 2011-11-18 15:11:59 UTC (rev 237)
+++ trunk/library/classes/Gems/User/UserLoader.php 2011-11-18 17:08:28 UTC (rev 238)
@@ -277,6 +277,20 @@
}
/**
+ * Check for password weakness.
+ *
+ * @param Gems_User_User $user The user for e.g. name checks
+ * @param string $password
+ * @return mixed String or array of strings containing warning messages
+ */
+ public function reportPasswordWeakness(Gems_User_User $user, $password)
+ {
+ $checker = $this->_getClass('passwordChecker');
+
+ return $checker->reportPasswordWeakness($user, $password);
+ }
+
+ /**
* Sets a new user as the current user.
*
* @param Gems_User_User $user
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|