Author: sevein Date: Tue Jan 10 12:36:57 2012 New Revision: 10617 Log: Add reset password task
Added: trunk/lib/task/resetPasswordTask.class.php (contents, props changed) Added: trunk/lib/task/resetPasswordTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/resetPasswordTask.class.php Tue Jan 10 12:36:57 2012 (r10617) @@ -0,0 +1,108 @@ +<?php + +/* + * This file is part of Qubit Toolkit. + * + * Qubit Toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Qubit Toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Qubit Toolkit. If not, see <http://www.gnu.org/licenses/>. + */ + +class resetPasswordTask extends sfBaseTask +{ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('username', sfCommandArgument::REQUIRED, 'Username') + )); + + $this->addOptions(array( + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'cli'), + new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel') + )); + + $this->namespace = 'tools'; + $this->name = 'reset-password'; + $this->briefDescription = 'Generates a new password for a given username'; + $this->detailedDescription = <<<EOF +FIXME +EOF; + } + + protected function execute($arguments = array(), $options = array()) + { + $databaseManager = new sfDatabaseManager($this->configuration); + $conn = $databaseManager->getDatabase('propel')->getConnection(); + + $criteria = new Criteria; + $criteria->add(QubitUser::USERNAME, $arguments['username']); + $user = QubitUser::getOne($criteria); + + // User account exists? + if ($user !== null) + { + $password = $this->generatePassword(); + $user->setPassword($password); + $user->save(); + + $this->logSection('do', 'New password: ' . $password); + } + else + { + throw new sfException('Given username does not exist'); + } + } + + function generatePassword($length = 8) + { + // Start with a blank password + $password = ""; + + // Define possible characters - any character in this string can be + // picked for use in the password, so if you want to put vowels back in + // or add special characters such as exclamation marks, this is where + // you should do it + $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ"; + + // We refer to the length of $possible a few times, so let's grab it now + $maxlength = strlen($possible); + + // Check for length overflow and truncate if necessary + if ($length > $maxlength) + { + $length = $maxlength; + } + + // Set up a counter for how many characters are in the password so far + $i = 0; + + // add random characters to $password until $length is reached + while ($i < $length) + { + // Pick a random character from the possible ones + $char = substr($possible, mt_rand(0, $maxlength - 1), 1); + + // Have we already used this character in $password? + if (!strstr($password, $char)) + { + // No, so it's OK to add it onto the end of whatever we've already got... + $password .= $char; + // ... and increase the counter by one + $i++; + } + } + + // Done! + return $password; + } +} -- 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.
