Skip to content

Instantly share code, notes, and snippets.

@fsuter
Created October 2, 2012 11:38
Show Gist options
  • Save fsuter/3818368 to your computer and use it in GitHub Desktop.
Save fsuter/3818368 to your computer and use it in GitHub Desktop.
Account Controller
<?php
namespace Cobweb\Monitoring\Controller;
/* *
* This script belongs to the FLOW3 package "Cobweb.Monitoring". *
* *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* Default controller for the Cobweb.Monitoring package
*
* @FLOW3\Scope("singleton")
*/
class AccountController extends \TYPO3\FLOW3\Mvc\Controller\ActionController {
/**
* @FLOW3\Inject
* @var \TYPO3\FLOW3\Security\AccountFactory
*/
protected $accountFactory;
/**
* @FLOW3\Inject
* @var \TYPO3\FLOW3\Security\AccountRepository
*/
protected $accountRepository;
/**
* @FLOW3\Inject
* @var \Cobweb\Monitoring\Domain\Repository\UserRepository
*/
protected $userRepository;
/**
* @FLOW3\Inject
* @var \Cobweb\Monitoring\Domain\Repository\InstanceGroupRepository
*/
protected $instanceGroupRepository;
/**
* @FLOW3\Inject
* @var \TYPO3\FLOW3\Security\Cryptography\HashService
*/
protected $hashService;
/**
* @return void
*/
protected function initializeAction() {
parent::initializeAction();
// This seems necessary to match an "account" variable with an Account object
// Not quite sure what it does and why it's necessary
// Code copied from TYPO3.TYPO3
if ($this->arguments->hasArgument('account')) {
$propertyMappingConfigurationForAccount = $this->arguments->getArgument('account')->getPropertyMappingConfiguration();
$propertyMappingConfigurationForAccountParty = $propertyMappingConfigurationForAccount->forProperty('party');
$propertyMappingConfigurationForAccountPartyName = $propertyMappingConfigurationForAccount->forProperty('party.name');
$propertyMappingConfigurationForClient = $propertyMappingConfigurationForAccount->forProperty('party.client');
foreach (array($propertyMappingConfigurationForAccountParty, $propertyMappingConfigurationForAccountPartyName, $propertyMappingConfigurationForClient) as $propertyMappingConfiguration) {
$propertyMappingConfiguration->setTypeConverterOption('TYPO3\FLOW3\Property\TypeConverter\PersistentObjectConverter', \TYPO3\FLOW3\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, TRUE);
$propertyMappingConfiguration->setTypeConverterOption('TYPO3\FLOW3\Property\TypeConverter\PersistentObjectConverter', \TYPO3\FLOW3\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED, TRUE);
}
}
}
/**
* Lists all existing user accounts
*
* @return void
*/
public function indexAction() {
$this->view->assign('accounts', $this->accountRepository->findAll());
}
/**
* Displays form to create a new account
*
* @param \TYPO3\FLOW3\Security\Account $account
* @return void
*/
public function newAction(\TYPO3\FLOW3\Security\Account $account = NULL) {
$this->view->assign('account', $account);
$this->view->assign('clients', $this->getListOfInstanceGroups());
}
/**
* Creates an account and its related person
*
* @param string $identifier
* @FLOW3\Validate(argumentName="identifier", type="NotEmpty")
* @FLOW3\Validate(argumentName="identifier", type="StringLength", options={ "minimum"=3, "maximum"=255 })
* @FLOW3\Validate(argumentName="identifier", type="\Cobweb\Monitoring\Domain\Validator\AccountExistsValidator")
* @param array $password
* @FLOW3\Validate(argumentName="password", type="\Cobweb\Monitoring\Domain\Validator\PasswordValidator", options={ "minimum"=8, "maximum"=255, "allowEmpty"=0 })
* @param string $firstName
* @FLOW3\Validate(argumentName="firstName", type="NotEmpty")
* @FLOW3\Validate(argumentName="firstName", type="StringLength", options={ "minimum"=1, "maximum"=255 })
* @param string $lastName
* @FLOW3\Validate(argumentName="lastName", type="NotEmpty")
* @FLOW3\Validate(argumentName="lastName", type="StringLength", options={ "minimum"=1, "maximum"=255 })
* @param string $client
* @param array $roles
* @FLOW3\Validate(argumentName="roles", type="\Cobweb\Monitoring\Domain\Validator\RolesValidator")
* @return void
*/
public function createAction($identifier, $password, $firstName, $lastName, $client, $roles) {
// Create the account with the given data
$account = $this->accountFactory->createAccountWithPassword($identifier, $password[0], $this->filterRoles($roles));
$this->accountRepository->add($account);
// Create the person and link it to the account
$personName = new \TYPO3\Party\Domain\Model\PersonName('', $firstName, '', $lastName);
$user = new \Cobweb\Monitoring\Domain\Model\User();
$user->setName($personName);
$user->addAccount($account);
if (!empty($client)) {
$clientObject = $this->instanceGroupRepository->findByIdentifier($client);
if (!empty($clientObject)) {
$user->setClient($clientObject);
}
}
$this->userRepository->add($user);
// Issue success message and redirect to list view
$this->addFlashMessage('New account created.');
$this->redirect('index');
}
/**
* Displays the form for editing an existing account
*
* @param \TYPO3\FLOW3\Security\Account $account
* @return void
*/
public function editAction(\TYPO3\FLOW3\Security\Account $account) {
$this->view->assign('account', $account);
$this->view->assign('clients', $this->getListOfInstanceGroups());
}
/**
* Updates an account and its related person
*
* @param \TYPO3\FLOW3\Security\Account $account
* @param array $password
* @FLOW3\Validate(argumentName="password", type="\Cobweb\Monitoring\Domain\Validator\PasswordValidator", options={ "minimum"=8, "maximum"=255, "allowEmpty"=1 })
* @param string $firstName
* @FLOW3\Validate(argumentName="firstName", type="NotEmpty")
* @FLOW3\Validate(argumentName="firstName", type="StringLength", options={ "minimum"=1, "maximum"=255 })
* @param string $lastName
* @FLOW3\Validate(argumentName="lastName", type="NotEmpty")
* @FLOW3\Validate(argumentName="lastName", type="StringLength", options={ "minimum"=1, "maximum"=255 })
* @param \Cobweb\Monitoring\Domain\Model\InstanceGroup $client
* @param array $roles
* @FLOW3\Validate(argumentName="roles", type="\Cobweb\Monitoring\Domain\Validator\RolesValidator")
* @return void
*/
public function updateAction(\TYPO3\FLOW3\Security\Account $account, $password, $firstName, $lastName, \Cobweb\Monitoring\Domain\Model\InstanceGroup $client, $roles) {
// Encrypt the password
$password = array_shift($password);
if (strlen(trim(strval($password))) > 0) {
$account->setCredentialsSource($this->hashService->hashPassword($password, 'default'));
}
// Update the other account information
$account->setRoles($this->filterRoles($roles));
// Update the related person's information
/** @var $user \Cobweb\Monitoring\Domain\Model\User */
$user = $account->getParty();
$user->getName()->setFirstName($firstName);
$user->getName()->setLastName($lastName);
if (!empty($client)) {
$user->setClient($client);
}
// Push the changed object to their relative repositories
$this->userRepository->update($user);
$this->accountRepository->update($account);
// Issue success message and redirect to list view
$this->addFlashMessage('Account updated.');
$this->redirect('index');
}
/**
* Deletes the given account
*
* @param \TYPO3\FLOW3\Security\Account $account
* @return void
*/
public function deleteAction(\TYPO3\FLOW3\Security\Account $account) {
$this->userRepository->remove($account->getParty());
$this->accountRepository->remove($account);
// Issue success message and redirect to list view
$this->addFlashMessage('Account deleted.');
$this->redirect('index');
}
/**
* Overrides getErrorFlashMessage to present nice flash error messages.
*
* @return \TYPO3\FLOW3\Error\Message
*/
protected function getErrorFlashMessage() {
switch ($this->actionMethodName) {
case 'createAction' :
return new \TYPO3\FLOW3\Error\Error('Error creating the new account');
case 'updateAction' :
return new \TYPO3\FLOW3\Error\Error('Error updating the account');
default :
return parent::getErrorFlashMessage();
}
}
/**
* Filters out empty roles in the list
*
* @param array $roles List of roles
* @return array
*/
protected function filterRoles($roles) {
$finalRoles = array();
foreach ($roles as $aRole) {
if (!empty($aRole)) {
$finalRoles[] = $aRole;
}
}
return $finalRoles;
}
/**
* Assembles a list of instance groups for the selector displayed in the input form
*
* Adds an empty entry at the start
*
* @return array
*/
protected function getListOfInstanceGroups() {
$instanceGroupList = array('' => '');
$instances = $this->instanceGroupRepository->findAll();
/** @var $anInstanceGroup \Cobweb\Monitoring\Domain\Model\InstanceGroup */
foreach ($instances as $anInstanceGroup) {
// The identifier must be retrieved via the persistence manager
$id = $this->persistenceManager->getIdentifierByObject($anInstanceGroup);
$instanceGroupList[$id] = $anInstanceGroup->getName();
}
return $instanceGroupList;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment