/** * @ORM\PrePersist */ public function prePersist() { parent::prePersist(); $saltGenerator = new SaltGenerator(); $this->salt = $saltGenerator->generateSalt(); /* * If no plain password is present, we must generate one */ if ($this->getPlainPassword() == '') { $passwordGenerator = new PasswordGenerator(); $this->setPlainPassword($passwordGenerator->generatePassword(12)); } $this->getHandler()->encodePassword(); }
protected function execute(InputInterface $input, OutputInterface $output) { $this->questionHelper = $this->getHelperSet()->get('question'); $this->entityManager = $this->getHelperSet()->get('em')->getEntityManager(); $text = ""; $name = $input->getArgument('username'); if ($name) { $user = $this->entityManager->getRepository('RZ\\Roadiz\\Core\\Entities\\User')->findOneBy(['username' => $name]); if ($user !== null) { if ($input->getOption('enable')) { if ($user !== null && $user->setEnabled(true)) { $this->entityManager->flush(); $text = '<info>User enabled…</info>' . PHP_EOL; } else { $text = '<error>Requested user is not setup yet…</error>' . PHP_EOL; } } elseif ($input->getOption('disable')) { if ($user !== null && $user->setEnabled(false)) { $this->entityManager->flush(); $text = '<info>User disabled…</info>' . PHP_EOL; } else { $text = '<error>Requested user is not setup yet…</error>' . PHP_EOL; } } elseif ($input->getOption('delete')) { $confirmation = new ConfirmationQuestion('<question>Do you really want to delete user “' . $user->getUsername() . '”?</question>', false); if ($user !== null && $this->questionHelper->ask($input, $output, $confirmation)) { $this->entityManager->remove($user); $this->entityManager->flush(); $text = '<info>User deleted…</info>' . PHP_EOL; } else { $text = '<error>Requested user is not setup yet…</error>' . PHP_EOL; } } elseif ($input->getOption('picture')) { if ($user !== null) { $facebook = new FacebookPictureFinder($user->getFacebookName()); if (false !== ($url = $facebook->getPictureUrl())) { $user->setPictureUrl($url); $this->entityManager->flush(); $text = '<info>User profile pciture updated…</info>' . PHP_EOL; } } else { $text = '<error>Requested user is not setup yet…</error>' . PHP_EOL; } } elseif ($input->getOption('regenerate')) { if ($user !== null && $this->questionHelper->askConfirmation($output, '<question>Do you really want to regenerate user “' . $user->getUsername() . '” password?</question> : ', false)) { $passwordGenerator = new PasswordGenerator(); $user->setPlainPassword($passwordGenerator->generatePassword(12)); $user->getHandler()->encodePassword(); $this->entityManager->flush(); $text = '<info>User password regenerated…</info>' . PHP_EOL; $text .= 'Password: <info>' . $user->getPlainPassword() . '</info>' . PHP_EOL; } else { $text = '<error>Requested user is not setup yet…</error>' . PHP_EOL; } } elseif ($input->getOption('add-roles') && $user !== null) { $text = '<info>Adding roles to ' . $user->getUsername() . '</info>' . PHP_EOL; foreach ($input->getOption('add-roles') as $role) { $user->addRole(RolesBag::get($role)); $text .= '<info>Role: ' . $role . '</info>' . PHP_EOL; } $this->entityManager->flush(); } else { $text = '<info>' . $user . '</info>' . PHP_EOL; } } else { if ($input->getOption('create')) { $this->executeUserCreation($name, $input, $output); } else { $text = '<error>User “' . $name . '” does not exist… use --create to add a new user.</error>' . PHP_EOL; } } } else { $text = '<info>Installed users…</info>' . PHP_EOL; $users = $this->entityManager->getRepository('RZ\\Roadiz\\Core\\Entities\\User')->findAll(); if (count($users) > 0) { $text .= ' | ' . PHP_EOL; foreach ($users as $user) { $text .= ' |_ ' . $user->getUsername() . ' — <info>' . ($user->isEnabled() ? 'enabled' : 'disabled') . '</info>' . ' — <comment>' . implode(', ', $user->getRoles()) . '</comment>' . PHP_EOL; } } else { $text = '<info>No available users</info>' . PHP_EOL; } } $output->writeln($text); }
/** * @dataProvider generatePasswordProvider */ public function testGeneratePassword($passwordLength) { $passGen = new PasswordGenerator(); $pass = $passGen->generatePassword($passwordLength); $this->assertEquals($passwordLength, strlen($pass)); }