Part-DB.Part-DB-server/src/Entity/UserSystem/User.php

520 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
declare(strict_types=1);
/**
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/.
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
namespace App\Entity\UserSystem;
use App\Entity\Base\NamedDBElement;
use App\Entity\PriceInformations\Currency;
use App\Security\Interfaces\HasPermissionsInterface;
use App\Validator\Constraints\Selectable;
use App\Validator\Constraints\ValidPermission;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This entity represents a user, which can log in and have permissions.
* Also this entity is able to save some informations about the user, like the names, email-address and other info.
*
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table("`users`")
* @UniqueEntity("name", message="validator.user.username_already_used")
*/
class User extends NamedDBElement implements UserInterface, HasPermissionsInterface
{
/** The User id of the anonymous user */
public const ID_ANONYMOUS = 1;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\NotBlank
*/
2019-08-20 18:39:57 +02:00
protected $name = '';
/**
* //@ORM\Column(type="json").
*/
//protected $roles = [];
/**
* @var string|null The hashed password
* @ORM\Column(type="string", nullable=true)
*/
protected $password;
/**
* @var bool True if the user needs to change password after log in
* @ORM\Column(type="boolean")
*/
2019-04-28 14:18:11 +02:00
protected $need_pw_change = true;
/**
* @var string|null The first name of the User
* @ORM\Column(type="string", length=255, nullable=true)
*/
2019-03-20 22:53:06 +01:00
protected $first_name = '';
/**
* @var string|null The last name of the User
* @ORM\Column(type="string", length=255, nullable=true)
*/
2019-03-20 22:53:06 +01:00
protected $last_name = '';
/**
* @var string|null The department the user is working
* @ORM\Column(type="string", length=255, nullable=true)
*/
2019-03-20 22:53:06 +01:00
protected $department = '';
/**
* @var string|null The email address of the user
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email()
*/
2019-03-20 22:53:06 +01:00
protected $email = '';
/**
* @var string|null The language/locale the user prefers
* @ORM\Column(type="string", name="config_language", nullable=true)
* @Assert\Language()
*/
2019-03-20 22:53:06 +01:00
protected $language = '';
/**
* @var string|null The timezone the user prefers
* @ORM\Column(type="string", name="config_timezone", nullable=true)
* @Assert\Timezone()
*/
2019-03-20 22:53:06 +01:00
protected $timezone = '';
/**
* @var string|null The theme
* @ORM\Column(type="string", name="config_theme", nullable=true)
*/
2019-03-20 22:53:06 +01:00
protected $theme = '';
/**
* @var Group|null the group this user belongs to
* @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* @Selectable()
*/
protected $group;
/**
* @var array
* @ORM\Column(type="json")
*/
protected $settings = [];
/**
* @var Currency|null The currency the user wants to see prices in.
* Dont use fetch=EAGER here, this will cause problems with setting the currency setting.
* TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call).
* TODO: Find a way to use fetch EAGER (this improves performance a bit)
* @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency")
* @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
* @Selectable()
*/
protected $currency = null;
/** @var PermissionsEmbed
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
* @ValidPermission()
*/
protected $permissions;
/**
* @ORM\Column(type="text", name="config_image_path")
*/
2019-09-16 22:04:59 +02:00
protected $image_path = '';
/**
* @ORM\Column(type="text", name="config_instock_comment_w")
*/
2019-09-16 22:04:59 +02:00
protected $instock_comment_w = '';
/**
* @ORM\Column(type="text", name="config_instock_comment_a")
*/
2019-09-16 22:04:59 +02:00
protected $instock_comment_a = '';
public function __construct()
{
$this->permissions = new PermissionsEmbed();
}
/**
* Checks if the current user, is the user which represents the not logged in (anonymous) users.
*
* @return bool true if this user is the anonymous user
*/
public function isAnonymousUser(): bool
{
return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->name;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = [];
//$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
//$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
* Gets the password hash for this entity.
*/
public function getPassword(): string
{
return (string) $this->password;
}
/**
* Sets the password hash for this user.
*
* @param string $password
*
* @return User
*/
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* Gets the currency the user prefers when showing him prices.
* @return Currency|null The currency the user prefers, or null if the global currency should be used.
*/
public function getCurrency(): ?Currency
{
return $this->currency;
}
/**
* Sets the currency the users prefers to see prices in.
* @param Currency|null $currency
* @return User
*/
public function setCurrency(?Currency $currency): User
{
$this->currency = $currency;
return $this;
}
/**
* Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14.
*
* @return string The ID as a string;
*/
public function getIDString(): string
{
return 'U'.sprintf('%06d', $this->getID());
}
public function getPermissions(): PermissionsEmbed
{
return $this->permissions;
}
/************************************************
* Getters
************************************************/
/**
* Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
* Example: Max Muster (m.muster).
*
* @param bool $including_username include the username in the full name
*
* @return string a string with the full name of this user
*/
public function getFullName(bool $including_username = false): string
{
$str = $this->getFirstName().' '.$this->getLastName();
if ($including_username) {
$str .= ' ('.$this->getName().')';
}
return $str;
}
public function setName(string $new_name): NamedDBElement
{
// Anonymous user is not allowed to change its username
if (!$this->isAnonymousUser()) {
$this->name = $new_name;
}
return $this;
}
/**
* @return string
*/
public function getFirstName(): ?string
{
return $this->first_name;
}
/**
* @param string $first_name
*
* @return User
*/
public function setFirstName(?string $first_name): User
{
$this->first_name = $first_name;
return $this;
}
/**
* @return string
*/
public function getLastName(): ?string
{
return $this->last_name;
}
/**
* @param string $last_name
*
* @return User
*/
public function setLastName(?string $last_name): User
{
$this->last_name = $last_name;
return $this;
}
/**
* @return string
*/
public function getDepartment(): ?string
{
return $this->department;
}
/**
* @param string $department
*
* @return User
*/
public function setDepartment(?string $department): User
{
$this->department = $department;
return $this;
}
/**
* @return string
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string $email
*
* @return User
*/
public function setEmail(?string $email): User
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getLanguage(): ?string
{
return $this->language;
}
/**
* @param string $language
*
* @return User
*/
public function setLanguage(?string $language): User
{
$this->language = $language;
return $this;
}
/**
* @return string
*/
public function getTimezone(): ?string
{
return $this->timezone;
}
/**
* @param string $timezone
*
* @return User
*/
public function setTimezone(?string $timezone): User
{
$this->timezone = $timezone;
return $this;
}
/**
* @return string
*/
public function getTheme(): ?string
{
return $this->theme;
}
/**
* @param string $theme
*
* @return User
*/
public function setTheme(?string $theme): User
{
$this->theme = $theme;
return $this;
}
public function getGroup(): ?Group
{
return $this->group;
}
public function setGroup(?Group $group): self
{
$this->group = $group;
return $this;
}
2019-04-28 14:18:11 +02:00
public function __toString()
{
return $this->getFullName(true);
}
}