Skip to content

Instantly share code, notes, and snippets.

@sevein
Created January 10, 2012 20:41
Show Gist options
  • Save sevein/1591065 to your computer and use it in GitHub Desktop.
Save sevein/1591065 to your computer and use it in GitHub Desktop.
Symfony task in Qubit to reset user passwods
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment