EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Property not defined while extends from BaseUser at FOSUserBundle

Open reypm opened this issue 11 years ago • 41 comments

I have this User entity extends from BaseUser:

namespace PDOneBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use PDOneBundle\Classes\Form\Validator\Constraints as GdcaAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="fos_user")
 * @ORM\Entity(repositoryClass="PDOneBundle\Entity\Repository\UserRepository")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class User extends BaseUser
{
    /*
     * Hook timestampable behavior
     * updates createdAt, updatedAt fields
     */
    use TimestampableEntity;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var int
     *
     * @Assert\NotBlank()
     *
     * @ORM\ManyToOne(targetEntity="Company", inversedBy="users", cascade={"persist"})
     * @ORM\JoinColumn(name="company_id", referencedColumnName="ID")
     */
    protected $company;

    /**
     * @ORM\ManyToMany(targetEntity="Company", mappedBy="admins", cascade={"persist"})
     */
    protected $companies;

    /**
     * @ORM\ManyToMany(targetEntity="Project", mappedBy="admins", cascade={"persist"})
     */
    protected $projects;

    // methods goes here
}

I am using also EasyAdminBundle and any time I try to add a new user from EasyAdmin I got the following error:

Neither the property "expiresAt" nor one of the methods "getExpiresAt()", "expiresAt()", "isExpiresAt()", "hasExpiresAt()", "__get()" exist and have public access in class "PDOneBundle\Entity\User".

Why? It is not supposed that those methods are extended from BaseUser? Why the error? Is this related to the bundle itselft?

reypm avatar Apr 20 '15 16:04 reypm

Using FOSUserBundle is quite different, because it's not a "natural" entity.

For this, you have to override the EasyAdmin's AdminController, and override the instanciateNewEntity method like this:

protected function instantiateNewEntity()
{
    if ($this->entity['name'] === 'YourUserEntityNameInEasyAdmin') {
        return $this->get('fos_user.user_manager')->createUser();
    }
    return parent::instantiateNewEntity();
}

Can you test this and tell me if it works? If it does not, I'll try to check on one of my back-ends.

Pierstoval avatar Apr 21 '15 06:04 Pierstoval

@Pierstoval where exactly I should do that? You say override EasyAdmin controller but how?

reypm avatar Apr 21 '15 11:04 reypm

Look at this section of the doc to see how it works ;)

Pierstoval avatar Apr 21 '15 11:04 Pierstoval

Hi @paquitodev , could you experiment extending the controller?

Pierstoval avatar Apr 26 '15 17:04 Pierstoval

@Pierstoval I have not the time to test this yet but I will do the next week, keep the issue open and I'll close or comment when I can, thanks for you concern around this

reypm avatar Apr 26 '15 19:04 reypm

I also want to fix this issue. When I try to create a user in easy-admin I got this:

Neither the property "id" nor one of the methods "addId()"/"removeId()", "setId()", "id()", "__set()" or "__call()" exist and have public access in class "Groupware\UserBundle\Entity\User"

I'm using FOSUser too. I tried your fix by overriding the controller in my bundle: "Groupware\UserBundle\Controller\EasyAdminController". But sadly it doesn't work, same error message.

arukado-sama avatar May 19 '15 14:05 arukado-sama

If you use the latest stable version, create your own AdminController extending from the default one and add this method:

public function createNewUserEntity()
{
    return $this->get('fos_user.user_manager')->createUser();
}

javiereguiluz avatar May 19 '15 14:05 javiereguiluz

When using FOSUserBundle, your custom User entity must set up the $id ORM property, and if not set already, use the different getters. Plus you have to use the fos_user.user_manager service to create a new user entity, else the user won't work properly.

Pierstoval avatar May 19 '15 14:05 Pierstoval

My entity is already configured and works, only the user creation with easy-admin didn't work. Here is my id:

/**
   * @ORM\Column(name="id", type="integer", options={"unsigned":true}, nullable=false, length=10)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;

I just created setId and getId methods and now it works. I also added the createNewUserEntity function.

arukado-sama avatar May 19 '15 15:05 arukado-sama

@arukado-sama : Then, it seems that you added the id attribute to the form in your configuration, don't you ? Why would you have the id attribute as a form field ?

ogizanagi avatar May 19 '15 15:05 ogizanagi

In fact I did it, it was to test easy-admin bundle at beginning, I'll correct this mistake.

arukado-sama avatar May 19 '15 15:05 arukado-sama

@arukado-sama thanks for testing this bunde and please don't stop reporting the issues you find while using it in real backends. That's the best way for us to spot errors and potential improvements in UX.

javiereguiluz avatar May 19 '15 15:05 javiereguiluz

I'm using your bundle for a company project so don't worry I'll continue to report and suggest.

arukado-sama avatar May 19 '15 15:05 arukado-sama

Seems that the createNewUserEntity function isn't really needed, I can create a user without it and password+salt are set without any problem. The tip is to use the right lines in the "new" and "edit" forms.

arukado-sama avatar May 19 '15 15:05 arukado-sama

Yes, the $FOSUserManager->createUser() is optional but it guarantees you the best compatibility, allowing you to change the class without problem. Plus, if one day the FOSUserBundle implements new behaviors inside this createUser() method, it might create edge cases not to trigger them (even though it's still a big "if")

Pierstoval avatar May 19 '15 15:05 Pierstoval

@arukado-sama : When you say :

I can create a user without it and password+salt are set without any problem

Are you calling $this->get('fos_user.user_manager')->updateUser($user); after form validation, or are you only dealing with raw password values in the form ? :confused:

ogizanagi avatar May 19 '15 15:05 ogizanagi

Nevermind, it's a raw password without salt (although the salt is successfully created). But it seems that this command doesn't change anything, or do I have to place it somewhere? I only have the function @javiereguiluz gave to me.

screenshot from 2015-05-20 08 55 46 screenshot from 2015-05-20 08 58 07

arukado-sama avatar May 20 '15 07:05 arukado-sama

Managing entities provided by third-party bundles (like FOSUserBundle) should stick the behavior explained in the bundle, I think you should first read FOSUserBundle docs to know exactly what happens when you manage a user, and how to manage it.

I personally overrode some controller actions to change the way the user is created/updated and it works like a charm for me :confused:

Pierstoval avatar May 20 '15 07:05 Pierstoval

I already know how it works, I modified some methods to set a digesta password for Baikal. When I create a user with the fos-user console command it works like a charm as yours, but when I try to create it with easy-admin the password isn't correctly set. I added the createNewUserEntity() function in AdminController override but I think I miss something.

arukado-sama avatar May 20 '15 07:05 arukado-sama

@arukado-sama : what you missed is calling $this->get('fos_user.user_manager')->updateUser($user);. This call is very important when dealing with your user entities. See here and here what it does.

You should call this when your user new/edit form is valid:

public function preUpdateUserEntity($user)
{
    $this->get('fos_user.user_manager')->updateUser($user, false);
}

Please also note that you should use plainPassword in your form instead of password and use proper validation groups in order to check user uniqueness. (However, I'm not sure EasyAdmin is the best way to handle users creation/edition, at least for credentials, ...)

ogizanagi avatar May 20 '15 08:05 ogizanagi