|
| 1 | +<?php |
| 2 | +namespace Sandstorm\UserManagement\Controller; |
| 3 | + |
| 4 | +use Neos\Flow\Annotations as Flow; |
| 5 | +use Neos\Flow\Security\Account; |
| 6 | +use Neos\Flow\Security\Context; |
| 7 | +use Neos\Flow\Security\AccountRepository; |
| 8 | +use Neos\Flow\Security\Authentication\AuthenticationManagerInterface; |
| 9 | +use Neos\Flow\Security\Authentication\Token\UsernamePassword; |
| 10 | +use Neos\Flow\Security\Authentication\TokenInterface; |
| 11 | +use Neos\Flow\Security\Cryptography\HashService; |
| 12 | +use Neos\Flow\Mvc\Controller\ActionController; |
| 13 | +use Neos\Party\Domain\Repository\PartyRepository; |
| 14 | +use Sandstorm\UserManagement\Domain\Model\User; |
| 15 | +use Sandstorm\UserManagement\Domain\Repository\UserRepository; |
| 16 | + |
| 17 | +/** |
| 18 | + */ |
| 19 | +class ProfileController extends ActionController |
| 20 | +{ |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Context |
| 24 | + * @Flow\Inject |
| 25 | + */ |
| 26 | + protected $securityContext; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var PartyRepository |
| 30 | + * @Flow\Inject |
| 31 | + */ |
| 32 | + protected $partyRepository; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var UserRepository |
| 36 | + * @Flow\Inject |
| 37 | + */ |
| 38 | + protected $userRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @Flow\Inject |
| 42 | + * @var AuthenticationManagerInterface |
| 43 | + */ |
| 44 | + protected $authenticationManager; |
| 45 | + |
| 46 | + /** |
| 47 | + * @Flow\Inject |
| 48 | + * @var AccountRepository |
| 49 | + */ |
| 50 | + protected $accountRepository; |
| 51 | + |
| 52 | + /** |
| 53 | + * @Flow\Inject |
| 54 | + * @var HashService |
| 55 | + */ |
| 56 | + protected $hashService; |
| 57 | + |
| 58 | + |
| 59 | + public function indexAction() |
| 60 | + { |
| 61 | + $pluginArguments = $this->request->getInternalArguments(); |
| 62 | + $account = $this->securityContext->getAccount(); |
| 63 | + $user = $this->userRepository->findOneByAccount($account); |
| 64 | + $this->view->assign('account', $account); |
| 65 | + $this->view->assign('user', $user); |
| 66 | + $this->view->assign('pluginArguments', $pluginArguments); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param User $user |
| 71 | + */ |
| 72 | + public function editProfileAction(User $user) |
| 73 | + { |
| 74 | + $this->userRepository->update($user); |
| 75 | + $this->redirect('index'); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param Account $account |
| 80 | + * @param array $password Expects an array in the format array('<password>', '<password confirmation>') |
| 81 | + * @Flow\Validate(argumentName="password", type="\Neos\Neos\Validation\Validator\PasswordValidator", options={ "allowEmpty"=1, "minimum"=1, "maximum"=255 }) |
| 82 | + */ |
| 83 | + public function setNewPasswordAction(Account $account, array $password = array()) { |
| 84 | + $user = $this->userRepository->findOneByAccount($account); |
| 85 | + $password = array_shift($password); |
| 86 | + if (strlen(trim(strval($password))) > 0) { |
| 87 | + $this->setPassword($account, $password); |
| 88 | + } |
| 89 | + $this->redirect('index'); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Disable the technical error flash message |
| 95 | + * |
| 96 | + * @return boolean |
| 97 | + */ |
| 98 | + protected function getErrorFlashMessage() |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Sets a new password for the given account |
| 105 | + * |
| 106 | + * @param Account $account The user to set the password for |
| 107 | + * @param string $password A new password |
| 108 | + * @return void |
| 109 | + * @api |
| 110 | + */ |
| 111 | + protected function setPassword(Account $account, $password) |
| 112 | + { |
| 113 | + $tokens = $this->authenticationManager->getTokens(); |
| 114 | + $indexedTokens = array(); |
| 115 | + foreach ($tokens as $token) { |
| 116 | + /** @var TokenInterface $token */ |
| 117 | + $indexedTokens[$token->getAuthenticationProviderName()] = $token; |
| 118 | + } |
| 119 | + |
| 120 | + /** @var Account $account */ |
| 121 | + $authenticationProviderName = $account->getAuthenticationProviderName(); |
| 122 | + if (isset($indexedTokens[$authenticationProviderName]) && $indexedTokens[$authenticationProviderName] instanceof UsernamePassword) { |
| 123 | + $account->setCredentialsSource($this->hashService->hashPassword($password)); |
| 124 | + $this->accountRepository->update($account); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments