Extend Account-Class by a function

Hi!

I would like to add the function “setActive” to the Account-Class. “setActive” should set the expirationDate to null if true, to a past date if false:

class Account extends \Neos\Flow\Security\Account
{
    public function setActive(bool $active)
    {
        $this->expirationDate = ($active) ? null : (new \DateTime())->sub(new \DateInterval("P1D"));
    }
}

I’m getting the following error:

Could not convert target type “XYZ\XYZ\Domain\Model\Account”: It is not allowed to map property “__identity”.

What exactly do I do: I take the account instances from the normal account repository and send them to Flow. With the update action, however, I expect the extended model and save this again via the normal account repository.

I have a feeling that this is not clean and that’s where the error comes from, but I’m not sure what the clean way would be here.

Do you guys have some tips for me? Thanks a lot!

Tobias

Hi Tobias,

Extending core entities is a dangerous thing to do.
Can you elaborate a little on your usecase?

The Account model already has an expirationDate that you can set in order to disable it. Maybe that helps?

Dear Bastian,

thank you very much for your reply. I am using a generic table model in which I want to deactivate an account by a single click. I.e. I would have liked to address a setter function (setActive(false)) of the account model by a simple updateAction. So now I have to create a separate setActiveAction().

Anyway - I have thought a little bit. I’m going to go a slightly different way and create a “User” model which is connected to Security/Account using OneToOne. Then I can simply add more properties to a user account (bpsw. a mail address or similar). Then I can also use appropriate logic there in the UserRepository, which then sets a date in the past to deactivate the actual SecurityAccount.

/**
 * @Flow\Entity
 */
class User
{
    /**
     * @var Account
     * @ORM\OneToOne()
     */
    protected $securityAccount;
1 Like

That should be possible with

public function deactivateAccount(Account $account): void
{
    if (!$account->isActive()) {
        return;
    }
    $account->setExpirationDate(new \DateTime('now'));
    $this->accountRepository->update($account);
}

public function activateAccount(Account $account): void
{
    if ($account->isActive()) {
        return;
    }
    $account->setExpirationDate(null);
    $this->accountRepository->update($account);
}

If you need custom behavior/fields it definitely makes sense to have a custom user entity (personally I would not use ORM relations for that, but this is a matter of taste really).
Still, it might make sense to trigger the code above from your users disable()/enable() methods such that the user in question can no longer authenticate (if that is what you want).