-->

TYPO3: Add custom set functions in extension contr

2019-05-31 15:51发布

问题:

I'm creating an extension for visitors to sign up to the page.
When signing up, it should create a FE user in the backend which is disabled (and will be manually enabled by an admin). So I'll need to set the disable field to 1 when creating the FE user.

This is the function inside my controller:

/**
 * action create
 *
 * @param \Vendor\FeReg\Domain\Model\Dummy $newDummy
 * @return void
 */
public function createAction(\Vendor\FeReg\Domain\Model\Dummy $newDummy)
{
    // vars
    $title = $newDummy->getTitle();
    $atitle = $newDummy->getAtitle();
    $fname = $newDummy->getFname();
    $lname = $newDummy->getLname();
    $street = $newDummy->getStreet();
    $city = $newDummy->getCity();
    $post = $newDummy->getPost();
    $phone = $newDummy->getPhone();
    $fax = $newDummy->getFax();
    $email = $newDummy->getEmail();
    $org = $newDummy->getOrg();
    $cat = $newDummy->getCat();
    $field = $newDummy->getField();
    $uname = $newDummy->getUname();
    $ppass = $newDummy->getPpass();
    $cpass = $newDummy->getCpass();
    $fulltitle = ($atitle ? $title." ".$atitle : $title);


    $frontendUser = new FrontendUser();

    $frontendUser->setUsername($uname);
    $frontendUser->setPassword($ppass);
    $frontendUser->setFirstname($fname);
    $frontendUser->setLastname($lname);
    $frontendUser->setAddress($street);
    $frontendUser->setTelephone($phone);
    $frontendUser->setFax($fax);
    $frontendUser->setEmail($email);
    $frontendUser->setTitle($fulltitle);
    $frontendUser->setZip($post);
    $frontendUser->setCity($city);
    $frontendUser->setCompany($org);

    $this->frontendUserRepository->add($frontendUser);

    // $this->dummyRepository->add($newDummy);
    // $this->addFlashMessage($title, '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);

    $this->redirect('new');
}

To make this work I'm also loading the $frontendUserRepository.
I need something like $frontendUser->setDisable(1).

Environment: TYPO3 7.6.8 / PHP 5.6.24 / mysqlnd 5.0.11

回答1:

Disabling a user is not default enabled on the standard user object. I've tackled this issue myself by creating a model extending the FrontendUser from TYPO3 and adding a property disable.

class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser {
    /**
     * @var boolean
     */
    protected $disable;

    /**
     * Gets the Disable
     *
     * @return boolean
     */
    public function getDisable() {
        return (bool)$this->disable;
    }

    /**
     * Sets the Disable
     *
     * @param boolean $disable
     * @return void
     */
    public function setDisable($disable) {
        $this->disable = (bool)$disable;
    }
}

You might need a bit of typoscript to map it to the proper property

config.tx_extbase {
    persistence{
        classes {
            VendorName\ExtensionName\Domain\Model\FrontendUser {
                mapping {
                    tableName = fe_users
                    columns {
                        disable.mapOnProperty = disable
                    }
                }
            }
        }
    }
}

You'll need the FrontendUserRepository as well

/**
* A Frontend User repository
*/
class FrontendUserRepository extends \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository {
}

After this you can use the newly created FrontendUser model as your model for the FrontendUser, inject it, and happily use

$userModel->setDisable(1);
// and
$userModel->getDisable();

(All namespaces are fully written, this is not necessary obviously, but simply done for ease of reading)