[TYPO3-mvc] How to cope with anonymous FE user entries
Thomas Hucke
thucke at web.de
Sat Mar 10 19:11:40 CET 2012
Hi all,
I write an extension (th_rating) to do ratings of any object.
Originally my model restricts any rating to be connected to a FE user.
No I reached a request to allow anonymous ratings (
http://forge.typo3.org/issues/34436 ).
How can I model that?
Obviously extbase needs a valid FE user to handle a create-action.
Everytime I call the CREATE action with "voter=0" the getErrorFlashMessage
method is being called in my controller.
This is how I modeled the connection to FE user:
/**
* The voter of this object
*
* @var Tx_Extbase_Domain_Model_FrontendUser
* @lazy
*/
protected $voter;
/**
* Sets the frontenduser of this vote
*
* @param Tx_Extbase_Domain_Model_FrontendUser $voter An object storage
containing the frontenduser
* @return void
*/
public function setVoter(Tx_Extbase_Domain_Model_FrontendUser $voter) {
$this->voter = $voter;
}
/**
* Returns the frontenduser of this vote
*
* @return Tx_Extbase_Domain_Model_FrontendUser The frontenduser of this
vote
*/
public function getVoter() {
if ($this->voter instanceof Tx_Extbase_Persistence_LazyLoadingProxy) {
$this->voter = $this->voter->_loadRealInstance();
}
return $this->voter;
}
This is the CREATE-action:
/**
* Creates a new vote
*
* @param Tx_ThRating_Domain_Model_Vote $vote A fresh vote object which has
not yet been added to the repository
* @return void
* dontverifyrequesthash
*/
//http://localhost:8503/index.php?id=71&tx_thrating_pi1[controller]=Vote&tx_thrating_pi1[action]=create&tx_thrating_pi1[vote][rating]=1&tx_thrating_pi1[vote][voter]=1&tx_thrating_pi1[vote][vote]=1
public function createAction(Tx_ThRating_Domain_Model_Vote $vote) {
//t3lib_utility_Debug::debug($vote,'Debug');
if ($this->settings['allowAnonymous'] ||
$this->accessControllService->backendAdminIsLoggedIn() ||
$this->accessControllService->isLoggedIn($vote->getVoter())) {
$matchVote =
$this->voteRepository->findMatchingRatingAndVoter($vote->getRating(),$vote->getVoter());
if (!($matchVote instanceof Tx_ThRating_Domain_Model_Vote) ||
!$this->voteValidator->isValid($matchVote)) {
$vote->getRating()->addVote($vote);
//persist newly added object to enable redirect to show action
Tx_Extbase_Dispatcher::getPersistenceManager()->persistAll();
$this->flashMessageContainer->add(Tx_Extbase_Utility_Localization::translate('error.vote.create.newCreated',
'ThRating'));
} else {
$vote = $matchVote;
$this->flashMessageContainer->add(Tx_Extbase_Utility_Localization::translate('error.vote.create.alreadyRated',
'ThRating', t3lib_FlashMessage::NOTICE));
}
} else {
$this->flashMessageContainer->add(Tx_Extbase_Utility_Localization::translate('error.vote.create.noPermission',
'ThRating', t3lib_FlashMessage::ERROR));
}
$referrer = $this->request->getArgument('__referrer');
$this->forward($referrer['actionName'],$referrer['controllerName'],$referrer['extensionName'],$this->request->getArguments());
}
Thanks in advance
Thomas
More information about the TYPO3-project-typo3v4mvc
mailing list