LM-Commons / LmcUser

A generic user registration and authentication module for Laminas. Supports Laminas\Db and Doctrine2. (Formerly ZfcUser)
BSD 3-Clause "New" or "Revised" License
14 stars 16 forks source link

Warning: Undefined array key "user_id" in vendor/lm-commons/lmc-user/src/Mapper/UserHydrator.php on line 77 #49

Open vrkansagara opened 4 months ago

vrkansagara commented 4 months ago

I am using LmcUser + LmcRbacMvc with DoctrineOrmModule with MySQL

Composer.json

    "lm-commons/lmc-user": "^3.6",
    "lm-commons/lmc-admin": "^1.1",
    "doctrine/migrations": "^3.7",
    "lm-commons/lmc-rbac-mvc": "^3.3",
    "laminas/laminas-authentication": "^2.16",
    "doctrine/doctrine-orm-module": "^6.1",
    "doctrine/dbal": "^3.8"

image

It has following settings

  'user_entity_class' => \LmcUser\Entity\User::class,
visto9259 commented 4 months ago

@vrkansagara, this looks like you user table in your database does not have a user_id column.

What is the schema for the user table in your database?

The /data folder provides example schemas to build the user table.

vrkansagara commented 4 months ago

@visto9259 I am trying to use LmcUser + LmcRbacMvc where I am getting this error. I have already configured using this way 'user_entity_class' => \User\Entity\User::class,

<?php

namespace User\Entity;

use LmcUser\Entity\User as LmcUserEntity;
use LmcRbacMvc\Identity\IdentityInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use LmcUser\Entity\UserInterface;
use Rbac\Role\RoleInterface;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User extends LmcUserEntity implements IdentityInterface
{

  /**
   * @var int|null
   *
   * @ORM\Id
   * @ORM\Column(name="user_id", type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;

  /**
   * @var string
   * @ORM\Column(name="username",type="string", length=128, unique=true, nullable=true)
   */
  protected $username;

  /**
   * @var string
   * @ORM\Column(name="email",type="string", length=128, unique=true, nullable=false)
   */
  protected $email;

  /**
   * @var string
   * @ORM\Column(name="display_name",type="string", length=128, unique=false, nullable=true)
   */
  protected $displayName;

  /**
   * @var string
   * @ORM\Column(name="password",type="string", length=128, unique=false, nullable=false)
   */
  protected $password;

  /**
   * @var int
   * @ORM\Column(name="state",type="integer", unique=false, nullable=true)
   */
  protected $state;

  /**
   * Get id.
   *
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * Set id.
   *
   * @param  int $id
   * @return UserInterface
   */
  public function setId($id)
  {
    $this->id = (int) $id;
    return $this;
  }

  /**
   * Get username.
   *
   * @return string
   */
  public function getUsername()
  {
    return $this->username;
  }

  /**
   * Set username.
   *
   * @param  string $username
   * @return UserInterface
   */
  public function setUsername($username)
  {
    $this->username = $username;
    return $this;
  }

  /**
   * Get email.
   *
   * @return string
   */
  public function getEmail()
  {
    return $this->email;
  }

  /**
   * Set email.
   *
   * @param  string $email
   * @return UserInterface
   */
  public function setEmail($email)
  {
    $this->email = $email;
    return $this;
  }

  /**
   * Get displayName.
   *
   * @return string
   */
  public function getDisplayName()
  {
    return $this->displayName;
  }

  /**
   * Set displayName.
   *
   * @param  string $displayName
   * @return UserInterface
   */
  public function setDisplayName($displayName)
  {
    $this->displayName = $displayName;
    return $this;
  }

  /**
   * Get password.
   *
   * @return string
   */
  public function getPassword()
  {
    return $this->password;
  }

  /**
   * Set password.
   *
   * @param  string $password
   * @return UserInterface
   */
  public function setPassword($password)
  {
    $this->password = $password;
    return $this;
  }

  /**
   * Get state.
   *
   * @return int
   */
  public function getState()
  {
    return $this->state;
  }

  /**
   * Set state.
   *
   * @param  int $state
   * @return UserInterface
   */
  public function setState($state)
  {
    $this->state = $state;
    return $this;
  }

  /**
   * @var Collection
   * @ORM\ManyToMany(targetEntity="\User\Entity\FlatRole")
   * @ORM\JoinTable(name="user_flatrole",
   *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
   * )
   */
  private $roles;

  public function __construct()
  {
    $this->roles = new ArrayCollection();
  }

  /**
   * {@inheritDoc}
   */
  public function getRoles()
  {
    return $this->roles->toArray();
  }

  /**
   * Set the list of roles
   * @param Collection $roles
   */
  public function setRoles(Collection $roles)
  {
    $this->roles->clear();
    foreach ($roles as $role) {
      $this->roles[] = $role;
    }
  }

  /**
   * Add one role to roles list
   * @param \Rbac\Role\RoleInterface $role
   */
  public function addRole(RoleInterface $role)
  {
    $this->roles[] = $role;
  }

}

User table structure look like this.

image
vrkansagara commented 4 months ago

I also running with the another issue when I switched to the above User.php entity as bellow

image
visto9259 commented 4 months ago

For the roles issue, do you have a roles column in your table? I don't see it the screen capture of the table structure that you shared above.

vrkansagara commented 4 months ago

That roles come from the relation , I think @visto9259

vrkansagara commented 4 months ago

Problems looks like the guest role is not assigned while user creation is fired.

image

This role should be assigned as default while user is being created using LmcRbacMvc

I do also suspect that user_id is not mapped while user is being registered.

vrkansagara commented 4 months ago

image

This patch should be needed here

    public function hydrate(array $data, $object)
    {
        if (!$object instanceof UserEntityInterface) {
            throw new Exception\InvalidArgumentException('$object must be an instance of LmcUser\Entity\UserInterface');
        }

        if (isset($data['id'])) {
          $data = $this->mapField('user_id', 'id', $data);
        }

        return $this->hydrator->hydrate($data, $object);
    }

looks like there is issue here https://github.com/LM-Commons/LmcUser/blob/master/src/Mapper/UserHydrator.php#L64

visto9259 commented 4 months ago

Hi @vrkansagara , I am still trying to reproduce the issues in my own dev setup. Have you found a resolution or do you have more info to provide? Are you using ORM for the user entities? Off the shelf, LmcUser does not use ORM for the user table. There is another module to use ORM for user https://github.com/LM-Commons/LmcUserDoctrineORM. LmcUser when fetching users does not use ORM (I think) and since the roles come from the relation defined in your entity, roles do not get pulled into the user entity.

vrkansagara commented 4 months ago

yes, @visto9259 , I do confirm that I was not using LmcUserDoctrineORM. so facing this issue.

I also facing much difficult to setup the FlatRole using LmcUserMVC so can you consider to as proposal for the demo repository where it can show the working model for following packages.

this combination is useful to show the basic Dashboard application so any one can easily start using this combination package and start creating application.

visto9259 commented 4 months ago

That would be a good idea but we lack the bandwidth to do so. This is all based on volunteer work. I wrote a tutorial on the basics of how to use LmcUser: https://www.vistomedia.com/laminas-user-authentication-using-lmcuser/ Take a look. I intended to write a tutorial on LmcRbacMvc as a follow up but did not have the time to get to it.

There are thousands of installs of LmcUser and LmcRbacMvc without issues. There could be bugs in the code but I suspect that there is something not set up properly in your application.

Can you share details on how you define the database adapter in your config? LmcUser uses the class \Laminas\Db\Adapter\Adapter::class as the database adapter. You need a database config somewhere in your config files which I assume you have. Please share your lmc_user and lmc_rbac configs.

vrkansagara commented 4 months ago

@visto9259 , I figured out the issue as i said here. https://github.com/LM-Commons/LmcUser/issues/49#issuecomment-1959318435

So this issue can be closed.

I have couple of improvement , I will make it PR

visto9259 commented 4 months ago

This is just the explanation for the warning and that I knew that the issue was where you pointed out. You can submit a PR for it. What about the other issues that you reported?