Remove get_wrong_email_users() from User_Adapter

This commit is contained in:
Nicolas Le Goff
2013-08-22 16:00:53 +02:00
parent b93b4e7b52
commit 34b4fdde74
6 changed files with 110 additions and 48 deletions

View File

@@ -0,0 +1,64 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Setup\Version;
use Alchemy\Phrasea\Application;
/**
* In version 3.9 the user table have been removed.
* However for migration process we must be able to detect
* duplicate emails.
*/
class MailChecker
{
/**
* Returns users with duplicated emails
*
* @param \Application $app
* @param string $table The table name where to look
*
* @return array An array of User_Adapter
*/
public static function getWrongEmailUsers(Application $app, $table = 'usr')
{
$sql = 'SELECT usr_mail, usr_id, last_conn, usr_login FROM '. $table .' WHERE usr_mail IS NOT NULL';
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute();
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();
$users = array();
foreach ($rs as $row) {
if (!isset($users[$row['usr_mail']])) {
$users[$row['usr_mail']] = array();
}
$users[$row['usr_mail']][] = $row;
}
$badUsers = array();
foreach ($users as $email => $usrs) {
if (count($usrs) > 1) {
$badUsers[$email] = array();
foreach ($usrs as $usrInfo) {
$badUsers[$email][$usrInfo['usr_id']] = $usrInfo;
}
}
}
unset($users);
return $badUsers;
}
}

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Setup\Version\MailChecker;
/** /**
* UpgradeManager for Phraseanet. * UpgradeManager for Phraseanet.
@@ -64,8 +65,9 @@ class Setup_Upgrade
$this->appbox = $app['phraseanet.appbox']; $this->appbox = $app['phraseanet.appbox'];
if (count(User_Adapter::get_wrong_email_users($app)) > 0) { if (version_compare($this->appbox->get_version(), '3.9', '<')
throw new Exception_Setup_FixBadEmailAddresses('Please fix the database before starting'); && count(MailChecker::getWrongEmailUsers($app)) > 0) {
throw new \Exception_Setup_FixBadEmailAddresses('Please fix the database before starting');
} }
$this->write_lock(); $this->write_lock();

View File

@@ -1172,45 +1172,6 @@ class User_Adapter implements User_Interface, cache_cacheableInterface
return $this; return $this;
} }
public static function get_wrong_email_users(Application $app)
{
$sql = 'SELECT usr_mail, usr_id FROM usr WHERE usr_mail IS NOT NULL';
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute();
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$users = array();
foreach ($rs as $row) {
if (!isset($users[$row['usr_mail']])) {
$users[$row['usr_mail']] = array();
}
$users[$row['usr_mail']][] = $row['usr_id'];
}
$bad_users = array();
foreach ($users as $email => $usrs) {
if (count($usrs) > 1) {
$bad_users[$email] = array();
foreach ($usrs as $usr_id) {
$user = User_Adapter::getInstance($usr_id, $app);
$bad_users[$email][$user->get_id()] = $user;
}
}
}
unset($users);
return $bad_users;
}
public function setPrefs($prop, $value) public function setPrefs($prop, $value)
{ {
$this->load_preferences(); $this->load_preferences();

View File

@@ -17,6 +17,7 @@
* @link www.phraseanet.com * @link www.phraseanet.com
*/ */
use Alchemy\Phrasea\Command\Command; use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Setup\Version\MailChecker;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -41,7 +42,10 @@ class module_console_systemMailCheck extends Command
{ {
$output->writeln("Processing..."); $output->writeln("Processing...");
$bad_users = User_Adapter::get_wrong_email_users($this->container); $bad_users = array();
if (version_compare($this->getService('phraseanet.appbox')->get_version(), '3.9', '<')) {
$bad_users = MailChecker::getWrongEmailUsers($this->container);
}
foreach ($bad_users as $email => $users) { foreach ($bad_users as $email => $users) {
if ($input->getOption('list')) { if ($input->getOption('list')) {
@@ -112,12 +116,14 @@ class module_console_systemMailCheck extends Command
$output->writeln($email); $output->writeln($email);
foreach ($users as $user) { foreach ($users as $user) {
$dateConn = new \DateTime($user['last_conn']);
$output->writeln( $output->writeln(
sprintf( sprintf(
"\t %5d %40s %s" "\t %5d %40s %s"
, $user->get_id() , $user['usr_id']
, $user->get_display_name() , $user['usr_login']
, $user->get_last_connection()->format('Y m d') , $dateConn->format('Y m d')
) )
); );
} }

View File

@@ -67,12 +67,12 @@ class module_console_systemUpgrade extends Command
try { try {
$output->write('<info>Upgrading...</info>', true); $output->write('<info>Upgrading...</info>', true);
if (count(User_Adapter::get_wrong_email_users($this->container)) > 0) { try {
$upgrader = new Setup_Upgrade($this->container, $input->getOption('force'));
} catch (\Exception_Setup_FixBadEmailAddresses $e) {
return $output->writeln(sprintf('<error>You have to fix your database before upgrade with the system:mailCheck command </error>')); return $output->writeln(sprintf('<error>You have to fix your database before upgrade with the system:mailCheck command </error>'));
} }
$upgrader = new Setup_Upgrade($this->container, $input->getOption('force'));
$queries = $this->getService('phraseanet.appbox')->forceUpgrade($upgrader, $this->container); $queries = $this->getService('phraseanet.appbox')->forceUpgrade($upgrader, $this->container);
if ($input->getOption('dump')) { if ($input->getOption('dump')) {

View File

@@ -0,0 +1,29 @@
<?php
namespace Alchemy\Tests\Phrasea\Setup\Version;
use Alchemy\Phrasea\Setup\Version\MailChecker;
class MailCheckerTest extends \PhraseanetPHPUnitAbstract
{
public function testMailChecker()
{
$conn = self::$DI['app']['phraseanet.appbox']->get_connection();
$now = new \DateTime();
$stmt = $conn->prepare('CREATE TEMPORARY TABLE usr_tmp (usr_id INT, usr_mail VARCHAR(50), usr_login VARCHAR(50), last_conn DATETIME);');
$stmt->execute();
$stmt->closeCursor();
$stmt = $conn->prepare('INSERT INTO usr_tmp (usr_id, usr_mail, usr_login, last_conn) VALUES(1, "email@email.com", "login1", "'.$now->format('Y-m-D H:i:s').'");');
$stmt->execute();
$stmt->closeCursor();
$stmt = $conn->prepare('INSERT INTO usr_tmp (usr_id, usr_mail, usr_login, last_conn) VALUES(2, "email@email.com", "login2", "'.$now->format('Y-m-D H:i:s').'");');
$stmt->execute();
$stmt->closeCursor();
unset($stmt);
$users = MailChecker::getWrongEmailUsers(self::$DI['app'], 'usr_tmp');
$this->assertEquals(1, count($users));
$this->assertEquals(2, count($users['email@email.com']));
}
}