API-PLATFORM User Update Password Or user profile

i need help :-D, i’m a beginner on Symfony and Api Platform. I have an entity User, i want create an items Operation for update my user with an object { email: string; firstname: string; lastname: string } without password, and a second itemsOperation just for my password.

I have testing, but i can’t do it.

this is my entity user and annotation api.

/**
* @ApiResource(
*      itemOperations={
*          "get",
*          "update_password"={"method"="PUT", "path"="user/{id}/update-password", 
*          "validation_groups"={"updatePassword"}},
*          "update_user"={"method"="PUT", "path"="user/{id}/update-user", 
*          "validation_groups"={"updateUser"}}
*      }
* )
* @ORMTable(name="users")
* @ORMEntity
*/

class User implements UserInterface
{
    /**
     * @ORMColumn(type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
     private $id;

     /**
      * @ORMColumn(type="string", length=25, unique=true)
      */
     private $username;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertLength(min="8", max="22")
     * @AssertNotNull(groups={"updatePassword"})
     * @AssertIsNull(groups={"updateUser"})
     */
    private $password;

    /**
     * @ORMColumn(type="string", length=45)
     * @Groups({"updateUser"})
     */
    private $email;

    /**
     * @ORMColumn(type="array")
     */
    private $roles = [];

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"updateUser"})
     */
    private $firstname;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"updateUser"})
     */
    private $lastname;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUsername(): ?string
    {
        return $this->username;
    }

    public function setUsername(string $username): self
    {
        $this->username = $username;

        return $this;
    }

    public function getSalt()
    {
        return null;
    }

    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getRoles(): array
    {
        $roles = $this->roles;
    
        return $roles;
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        $this->password = null;
    }

    public function getFirstname(): ?string
    {
        return $this->firstname;
    }

    public function setFirstname(?string $firstname): self
    {
        $this->firstname = $firstname;

        return $this;
    }

    public function getLastname(): ?string
    {
        return $this->lastname;
    }

    public function setLastname(?string $lastname): self
    {
        $this->lastname = $lastname;

        return $this;
    }
}

If i used update-user endpoint, failed error because i not send my password.

Can you help me ?

Thanx all.

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *