Add user manager & user manipulator

This commit is contained in:
Nicolas Le Goff
2013-08-27 16:31:26 +02:00
parent f4428d2ff4
commit 1f03451bfc
18 changed files with 1442 additions and 200 deletions

View File

@@ -10,6 +10,7 @@
*/
use Alchemy\Phrasea\Application;
use Doctrine\ORM\EntityManager;
use Entities\User;
use Gedmo\Timestampable\TimestampableListener;
@@ -56,17 +57,30 @@ class patch_3902 implements patchInterface
$stmt->closeCursor();
$conn = $app['phraseanet.appbox']->get_connection();
$em = $app['EM'];
$em->getEventManager()->removeEventSubscriber(new TimestampableListener());
$this->updateUsers($em, $conn);
$this->updateModels($em, $conn);
$em->getEventManager()->addEventSubscriber(new TimestampableListener());
}
/**
* Sets user entity from usr table.
*/
private function updateUsers(EntityManager $em, $conn)
{
$sql = 'SELECT * FROM usr';
$stmt = $conn->prepare($sql);
$stmt->execute();
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();
$n = 0;
$em = $app['EM'];
$em->getEventManager()->removeEventSubscriber(new TimestampableListener());
foreach ($rs as $row) {
foreach ($rows as $row) {
$user = new User();
$user->setActivity($row['activite']);
$user->setAddress($row['adresse']);
@@ -101,7 +115,6 @@ class patch_3902 implements patchInterface
$user->setMailLocked(!!$row['mail_locked']);
$user->setMailNotificationsActivated(!!$row['mail_notifications']);
$user->setModelOf($row['model_of']);
$user->setNonce($row['nonce']);
$user->setPassword($row['usr_password']);
$user->setPushList($row['push_list']);
@@ -141,7 +154,49 @@ class patch_3902 implements patchInterface
$em->flush();
$em->clear();
}
/**
* Sets model from usr table.
*/
private function updateModels(EntityManager $em, $conn)
{
$sql = 'SELECT model_of, usr_login FROM usr WHERE model_of > 0 AND usr_login IS NOT NULL';
$stmt = $conn->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();
$em->getEventManager()->addEventSubscriber(new TimestampableListener());
$n = 0;
$repository = $em->getRepository('Entities\User');
foreach ($rows as $row) {
$user = $repository->findOneByLogin($row['usr_login']);
if (null === $user) {
continue;
}
$template = $repository->find($row['model_of']);
if (null === $template) {
continue;
}
$user->setModelOf($template);
$em->persist($user);
$n++;
if ($n % 100 === 0) {
$em->flush();
$em->clear();
}
}
$em->flush();
$em->clear();
}
}