2019-03-20 23:16:07 +01:00
|
|
|
<?php
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
|
|
|
* 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
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-08-12 15:47:57 +02:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-03-14 18:01:41 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
namespace App\Entity\UserSystem;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
2019-09-24 13:39:49 +02:00
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement;
|
|
|
|
use App\Entity\Attachments\UserAttachment;
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
2019-09-18 18:17:17 +02:00
|
|
|
use App\Entity\PriceInformations\Currency;
|
2019-03-18 19:05:41 +01:00
|
|
|
use App\Security\Interfaces\HasPermissionsInterface;
|
2019-08-12 21:47:25 +02:00
|
|
|
use App\Validator\Constraints\Selectable;
|
2019-09-11 13:37:51 +02:00
|
|
|
use App\Validator\Constraints\ValidPermission;
|
2023-02-01 23:50:54 +01:00
|
|
|
use App\Validator\Constraints\ValidTheme;
|
2023-02-20 23:04:20 +01:00
|
|
|
use Hslavich\OneloginSamlBundle\Security\User\SamlUserInterface;
|
2022-10-04 00:08:58 +02:00
|
|
|
use Jbtronics\TFAWebauthn\Model\LegacyU2FKeyInterface;
|
2021-10-02 20:41:14 +02:00
|
|
|
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
2022-10-03 23:09:50 +02:00
|
|
|
use Webauthn\PublicKeyCredentialUserEntity;
|
2020-01-05 22:49:00 +01:00
|
|
|
use function count;
|
|
|
|
use DateTime;
|
2019-12-14 16:35:19 +01:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2019-09-24 13:39:49 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2019-03-14 18:01:41 +01:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Exception;
|
|
|
|
use function in_array;
|
2019-12-14 16:35:19 +01:00
|
|
|
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
|
|
|
|
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
|
2019-12-29 17:36:41 +01:00
|
|
|
use Scheb\TwoFactorBundle\Model\PreferredProviderInterface;
|
2019-12-14 16:35:19 +01:00
|
|
|
use Scheb\TwoFactorBundle\Model\TrustedDeviceInterface;
|
2019-09-11 19:04:09 +02:00
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
2019-03-14 18:01:41 +01:00
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
2019-03-15 18:04:15 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2022-10-03 23:09:50 +02:00
|
|
|
use Jbtronics\TFAWebauthn\Model\TwoFactorInterface as WebauthnTwoFactorInterface;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-15 13:18:47 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2019-03-14 18:01:41 +01:00
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
|
2022-09-25 18:33:13 +02:00
|
|
|
* @ORM\Table("`users`", indexes={
|
|
|
|
* @ORM\Index(name="user_idx_username", columns={"name"})
|
|
|
|
* })
|
2020-01-08 20:50:37 +01:00
|
|
|
* @ORM\EntityListeners({"App\EntityListeners\TreeCacheInvalidationListener"})
|
2019-09-11 19:04:09 +02:00
|
|
|
* @UniqueEntity("name", message="validator.user.username_already_used")
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2023-02-20 23:04:20 +01:00
|
|
|
class User extends AttachmentContainingDBElement implements UserInterface, HasPermissionsInterface, TwoFactorInterface,
|
2023-02-24 00:12:44 +01:00
|
|
|
BackupCodeInterface, TrustedDeviceInterface, WebauthnTwoFactorInterface, PreferredProviderInterface, PasswordAuthenticatedUserInterface
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
2020-02-02 14:05:36 +01:00
|
|
|
//use MasterAttachmentTrait;
|
2019-12-14 16:35:19 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
2020-02-01 16:17:20 +01:00
|
|
|
* The User id of the anonymous user.
|
2020-01-05 22:49:00 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public const ID_ANONYMOUS = 1;
|
2019-03-19 17:17:04 +01:00
|
|
|
|
2019-09-24 13:39:49 +02:00
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var bool Determines if the user is disabled (user can not log in)
|
|
|
|
* @ORM\Column(type="boolean")
|
2019-09-24 13:39:49 +02:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected bool $disabled = false;
|
2019-09-24 13:39:49 +02:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The theme
|
|
|
|
* @ORM\Column(type="string", name="config_theme", nullable=true)
|
2023-02-01 23:50:54 +01:00
|
|
|
* @ValidTheme()
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2023-01-28 23:24:45 +01:00
|
|
|
protected ?string $theme = null;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-08-21 21:36:22 +02:00
|
|
|
* @var string|null the hash of a token the user must provide when he wants to reset his password
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\Column(type="string", nullable=true)
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $pw_reset_token = null;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\Column(type="text", name="config_instock_comment_a")
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $instock_comment_a = '';
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\Column(type="text", name="config_instock_comment_w")
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $instock_comment_w = '';
|
2020-01-05 22:49:00 +01:00
|
|
|
|
|
|
|
/** @var int The version of the trusted device cookie. Used to invalidate all trusted device cookies at once.
|
|
|
|
* @ORM\Column(type="integer")
|
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected int $trustedDeviceCookieVersion = 0;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
2019-03-20 12:27:11 +01:00
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string[]|null A list of backup codes that can be used, if the user has no access to its Google Authenticator device
|
|
|
|
* @ORM\Column(type="json")
|
2019-03-20 12:27:11 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?array $backupCodes = [];
|
2019-03-20 12:27:11 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\Id()
|
|
|
|
* @ORM\GeneratedValue()
|
|
|
|
* @ORM\Column(type="integer")
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?int $id = null;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var Group|null the group this user belongs to
|
2022-10-09 21:50:56 +02:00
|
|
|
* DO NOT PUT A fetch eager here! Otherwise you can not unset the group of a user! This seems to be some kind of bug in doctrine. Maybe this is fixed in future versions.
|
|
|
|
* @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
|
|
|
|
* @Selectable()
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?Group $group = null;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The secret used for google authenticator
|
|
|
|
* @ORM\Column(name="google_authenticator_secret", type="string", nullable=true)
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $googleAuthenticatorSecret = null;
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The timezone the user prefers
|
|
|
|
* @ORM\Column(type="string", name="config_timezone", nullable=true)
|
|
|
|
* @Assert\Timezone()
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $timezone = '';
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-15 18:04:15 +01:00
|
|
|
* @var string|null The language/locale the user prefers
|
|
|
|
* @ORM\Column(type="string", name="config_language", nullable=true)
|
2019-09-12 17:50:33 +02:00
|
|
|
* @Assert\Language()
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $language = '';
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The email address of the user
|
|
|
|
* @ORM\Column(type="string", length=255, nullable=true)
|
|
|
|
* @Assert\Email()
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $email = '';
|
2019-03-14 18:01:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The department the user is working
|
|
|
|
* @ORM\Column(type="string", length=255, nullable=true)
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $department = '';
|
2019-03-15 13:18:47 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The last name of the User
|
|
|
|
* @ORM\Column(type="string", length=255, nullable=true)
|
2019-03-15 13:18:47 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $last_name = '';
|
2019-03-14 18:01:41 +01:00
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var string|null The first name of the User
|
|
|
|
* @ORM\Column(type="string", length=255, nullable=true)
|
2019-12-14 16:35:19 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $first_name = '';
|
2019-12-14 16:35:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 22:49:00 +01:00
|
|
|
* @var bool True if the user needs to change password after log in
|
|
|
|
* @ORM\Column(type="boolean")
|
2019-12-14 16:35:19 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected bool $need_pw_change = true;
|
2019-12-14 16:35:19 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
|
|
|
* @var string|null The hashed password
|
|
|
|
* @ORM\Column(type="string", nullable=true)
|
2019-12-14 16:35:19 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?string $password = null;
|
2019-12-14 16:35:19 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
|
|
|
* @ORM\Column(type="string", length=180, unique=true)
|
|
|
|
* @Assert\NotBlank
|
2020-01-08 20:57:46 +01:00
|
|
|
* @Assert\Regex("/^[\w\.\+\-\$]+$/", message="user.invalid_username")
|
2020-01-04 20:24:09 +01:00
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $name = '';
|
2019-12-14 16:35:19 +01:00
|
|
|
|
2019-09-13 17:13:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @ORM\Column(type="json")
|
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?array $settings = [];
|
2019-09-13 17:13:58 +02:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
2020-03-29 23:13:25 +02:00
|
|
|
* @var Collection<int, UserAttachment>
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\UserAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
|
2020-04-01 17:00:50 +02:00
|
|
|
* @ORM\OrderBy({"name" = "ASC"})
|
2020-01-05 22:49:00 +01:00
|
|
|
*/
|
|
|
|
protected $attachments;
|
|
|
|
|
2020-02-01 19:42:28 +01:00
|
|
|
/** @var DateTime|null The time when the backup codes were generated
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\Column(type="datetime", nullable=true)
|
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?DateTime $backupCodesGenerationDate = null;
|
2020-01-05 22:49:00 +01:00
|
|
|
|
2022-10-04 00:08:58 +02:00
|
|
|
/** @var Collection<int, LegacyU2FKeyInterface>
|
2020-01-05 22:49:00 +01:00
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\UserSystem\U2FKey", mappedBy="user", cascade={"REMOVE"}, orphanRemoval=true)
|
|
|
|
*/
|
|
|
|
protected $u2fKeys;
|
|
|
|
|
2022-10-04 00:08:58 +02:00
|
|
|
/**
|
|
|
|
* @var Collection<int, WebauthnKey>
|
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\UserSystem\WebauthnKey", mappedBy="user", cascade={"REMOVE"}, orphanRemoval=true)
|
|
|
|
*/
|
|
|
|
protected $webauthn_keys;
|
|
|
|
|
2019-09-18 18:17:17 +02:00
|
|
|
/**
|
|
|
|
* @var Currency|null The currency the user wants to see prices in.
|
2019-11-09 00:47:20 +01:00
|
|
|
* 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)
|
2019-09-18 18:17:17 +02:00
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency")
|
|
|
|
* @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
|
|
|
|
* @Selectable()
|
|
|
|
*/
|
2020-03-29 22:47:25 +02:00
|
|
|
protected $currency;
|
2019-09-18 18:17:17 +02:00
|
|
|
|
2022-10-30 21:51:24 +01:00
|
|
|
/**
|
|
|
|
* @var PermissionData
|
2022-10-31 21:12:01 +01:00
|
|
|
* @ValidPermission()
|
|
|
|
* @ORM\Embedded(class="PermissionData", columnPrefix="permissions_")
|
2022-10-30 21:51:24 +01:00
|
|
|
*/
|
2022-11-28 00:03:42 +01:00
|
|
|
protected ?PermissionData $permissions = null;
|
2022-10-30 21:51:24 +01:00
|
|
|
|
2019-08-06 13:18:29 +02:00
|
|
|
/**
|
2020-08-21 21:36:22 +02:00
|
|
|
* @var DateTime the time until the password reset token is valid
|
2023-02-21 00:29:50 +01:00
|
|
|
* @ORM\Column(type="datetime", nullable=true, columnDefinition="DEFAULT NULL")
|
2019-09-24 13:39:49 +02:00
|
|
|
*/
|
2020-03-29 22:47:25 +02:00
|
|
|
protected $pw_reset_expires;
|
2019-09-24 13:39:49 +02:00
|
|
|
|
2023-02-21 00:29:50 +01:00
|
|
|
/**
|
|
|
|
* @var bool True if the user was created by a SAML provider (and therefore cannot change its password)
|
|
|
|
* @ORM\Column(type="boolean")
|
|
|
|
*/
|
|
|
|
protected bool $saml_user = false;
|
|
|
|
|
2019-09-10 17:12:56 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2019-09-24 13:39:49 +02:00
|
|
|
parent::__construct();
|
2022-10-30 21:51:24 +01:00
|
|
|
$this->permissions = new PermissionData();
|
2019-12-14 16:35:19 +01:00
|
|
|
$this->u2fKeys = new ArrayCollection();
|
2022-10-04 00:08:58 +02:00
|
|
|
$this->webauthn_keys = new ArrayCollection();
|
2019-09-10 17:12:56 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
/**
|
|
|
|
* Returns a string representation of this user (the full name).
|
|
|
|
* E.g. 'Jane Doe (j.doe) [DISABLED].
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
$tmp = $this->isDisabled() ? ' [DISABLED]' : '';
|
|
|
|
|
|
|
|
return $this->getFullName(true).$tmp;
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:17:04 +01:00
|
|
|
/**
|
|
|
|
* Checks if the current user, is the user which represents the not logged in (anonymous) users.
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
|
|
|
* @return bool true if this user is the anonymous user
|
2019-03-19 17:17:04 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function isAnonymousUser(): bool
|
2019-03-19 17:17:04 +01:00
|
|
|
{
|
2019-03-20 23:16:07 +01:00
|
|
|
return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
|
2019-03-19 17:17:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
/**
|
|
|
|
* A visual identifier that represents this user.
|
|
|
|
*
|
|
|
|
* @see UserInterface
|
|
|
|
*/
|
|
|
|
public function getUsername(): string
|
|
|
|
{
|
2022-12-18 19:45:04 +01:00
|
|
|
return $this->name;
|
2019-03-14 18:01:41 +01:00
|
|
|
}
|
|
|
|
|
2021-10-02 20:41:14 +02:00
|
|
|
public function getUserIdentifier(): string
|
|
|
|
{
|
|
|
|
return $this->getUsername();
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
/**
|
|
|
|
* @see UserInterface
|
|
|
|
*/
|
|
|
|
public function getRoles(): array
|
|
|
|
{
|
|
|
|
$roles = [];
|
|
|
|
//$roles = $this->roles;
|
|
|
|
// guarantee every user at least has ROLE_USER
|
|
|
|
$roles[] = 'ROLE_USER';
|
|
|
|
|
2023-02-21 00:42:03 +01:00
|
|
|
if ($this->saml_user) {
|
|
|
|
$roles[] = 'ROLE_SAML_USER';
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return array_unique($roles);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRoles(array $roles): self
|
|
|
|
{
|
|
|
|
//$this->roles = $roles;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see UserInterface
|
2019-03-15 18:38:45 +01:00
|
|
|
* Gets the password hash for this entity.
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
|
|
|
public function getPassword(): string
|
|
|
|
{
|
|
|
|
return (string) $this->password;
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:45 +01:00
|
|
|
/**
|
|
|
|
* Sets the password hash for this user.
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-15 18:38:45 +01:00
|
|
|
* @return User
|
|
|
|
*/
|
2019-03-14 18:01:41 +01:00
|
|
|
public function setPassword(string $password): self
|
|
|
|
{
|
|
|
|
$this->password = $password;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see UserInterface
|
|
|
|
*/
|
2020-02-01 19:42:28 +01:00
|
|
|
public function getSalt(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
2020-02-01 19:42:28 +01:00
|
|
|
return null;
|
2019-03-14 18:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see UserInterface
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
public function eraseCredentials(): void
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
// If you store any temporary, sensitive data on the user, clear it here
|
|
|
|
// $this->plainPassword = null;
|
|
|
|
}
|
|
|
|
|
2019-09-18 18:17:17 +02:00
|
|
|
/**
|
|
|
|
* Gets the currency the user prefers when showing him prices.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return Currency|null the currency the user prefers, or null if the global currency should be used
|
2019-09-18 18:17:17 +02:00
|
|
|
*/
|
|
|
|
public function getCurrency(): ?Currency
|
|
|
|
{
|
|
|
|
return $this->currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the currency the users prefers to see prices in.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-18 18:17:17 +02:00
|
|
|
* @return User
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setCurrency(?Currency $currency): self
|
2019-09-18 18:17:17 +02:00
|
|
|
{
|
|
|
|
$this->currency = $currency;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-18 18:17:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-10-26 23:22:27 +02:00
|
|
|
/**
|
|
|
|
* Checks if this user is disabled (user cannot login any more).
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return bool true, if the user is disabled
|
2019-10-26 23:22:27 +02:00
|
|
|
*/
|
|
|
|
public function isDisabled(): bool
|
|
|
|
{
|
|
|
|
return $this->disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the status if a user is disabled.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param bool $disabled true if the user should be disabled
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-10-26 23:22:27 +02:00
|
|
|
* @return User
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setDisabled(bool $disabled): self
|
2019-10-26 23:22:27 +02:00
|
|
|
{
|
|
|
|
$this->disabled = $disabled;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-10-26 23:22:27 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-10-30 21:51:24 +01:00
|
|
|
public function getPermissions(): PermissionData
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
2022-11-28 00:03:42 +01:00
|
|
|
if ($this->permissions === null) {
|
|
|
|
$this->permissions = new PermissionData();
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
return $this->permissions;
|
|
|
|
}
|
|
|
|
|
2019-09-19 12:35:28 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Check if the user needs a password change.
|
2019-09-19 12:35:28 +02:00
|
|
|
*/
|
|
|
|
public function isNeedPwChange(): bool
|
|
|
|
{
|
|
|
|
return $this->need_pw_change;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the status, if the user needs a password change.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-19 12:35:28 +02:00
|
|
|
* @return User
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setNeedPwChange(bool $need_pw_change): self
|
2019-09-19 12:35:28 +02:00
|
|
|
{
|
|
|
|
$this->need_pw_change = $need_pw_change;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-19 12:35:28 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:49:22 +01:00
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Returns the encrypted password reset token.
|
2019-11-24 22:49:22 +01:00
|
|
|
*/
|
|
|
|
public function getPwResetToken(): ?string
|
|
|
|
{
|
|
|
|
return $this->pw_reset_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Sets the encrypted password reset token.
|
|
|
|
*
|
2019-11-24 22:49:22 +01:00
|
|
|
* @return User
|
|
|
|
*/
|
2020-01-04 20:24:09 +01:00
|
|
|
public function setPwResetToken(?string $pw_reset_token): self
|
2019-11-24 22:49:22 +01:00
|
|
|
{
|
|
|
|
$this->pw_reset_token = $pw_reset_token;
|
2020-01-04 20:24:09 +01:00
|
|
|
|
2019-11-24 22:49:22 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Gets the datetime when the password reset token expires.
|
2019-11-24 22:49:22 +01:00
|
|
|
*/
|
2020-01-05 22:49:00 +01:00
|
|
|
public function getPwResetExpires(): DateTime
|
2019-11-24 22:49:22 +01:00
|
|
|
{
|
|
|
|
return $this->pw_reset_expires;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Sets the datetime when the password reset token expires.
|
|
|
|
*
|
2019-11-24 22:49:22 +01:00
|
|
|
* @return User
|
|
|
|
*/
|
2020-01-05 22:49:00 +01:00
|
|
|
public function setPwResetExpires(DateTime $pw_reset_expires): self
|
2019-11-24 22:49:22 +01:00
|
|
|
{
|
|
|
|
$this->pw_reset_expires = $pw_reset_expires;
|
2020-01-04 20:24:09 +01:00
|
|
|
|
2019-11-24 22:49:22 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
/************************************************
|
|
|
|
* Getters
|
|
|
|
************************************************/
|
|
|
|
|
2019-03-20 12:27:11 +01:00
|
|
|
/**
|
|
|
|
* Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
|
2019-03-20 23:16:07 +01:00
|
|
|
* 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
|
2019-03-20 12:27:11 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getFullName(bool $including_username = false): string
|
2019-03-20 12:27:11 +01:00
|
|
|
{
|
2020-04-08 16:18:53 +02:00
|
|
|
$tmp = $this->getFirstName();
|
|
|
|
//Dont add a space, if the name has only one part (it would look strange)
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($this->getFirstName()) && !empty($this->getLastName())) {
|
2020-04-08 16:18:53 +02:00
|
|
|
$tmp .= ' ';
|
|
|
|
}
|
|
|
|
$tmp .= $this->getLastName();
|
|
|
|
|
2019-03-20 12:27:11 +01:00
|
|
|
if ($including_username) {
|
2020-04-08 16:18:53 +02:00
|
|
|
$tmp .= sprintf(' (@%s)', $this->getName());
|
2019-03-20 12:27:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:18:53 +02:00
|
|
|
return $tmp;
|
2019-03-20 12:27:11 +01:00
|
|
|
}
|
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Change the username of this user.
|
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param string $new_name the new username
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function setName(string $new_name): AbstractNamedDBElement
|
2019-03-19 17:17:04 +01:00
|
|
|
{
|
|
|
|
// Anonymous user is not allowed to change its username
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$this->isAnonymousUser()) {
|
2019-03-19 17:17:04 +01:00
|
|
|
$this->name = $new_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Get the first name of the user.
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getFirstName(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->first_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Change the first name of the user.
|
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param string|null $first_name The new first name
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setFirstName(?string $first_name): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->first_name = $first_name;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Get the last name of the user.
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getLastName(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->last_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Change the last name of the user.
|
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param string|null $last_name The new last name
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setLastName(?string $last_name): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->last_name = $last_name;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Gets the department of this user.
|
|
|
|
*
|
2019-03-14 18:01:41 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getDepartment(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->department;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Change the department of the user.
|
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param string|null $department The new department
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-03-14 18:01:41 +01:00
|
|
|
* @return User
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setDepartment(?string $department): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->department = $department;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Get the email of the user.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-03-14 18:01:41 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getEmail(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Change the email of the user.
|
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param string|null $email The new email adress
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setEmail(?string $email): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->email = $email;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Gets the language the user prefers (as 2 letter ISO code).
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return string|null The 2 letter ISO code of the preferred language (e.g. 'en' or 'de').
|
2020-01-04 20:24:09 +01:00
|
|
|
* If null is returned, the user has not specified a language and the server wide language should be used.
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getLanguage(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->language;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Change the language the user prefers.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @param string|null $language The new language as 2 letter ISO code (e.g. 'en' or 'de').
|
2020-01-04 20:24:09 +01:00
|
|
|
* Set to null, to use the system wide language.
|
|
|
|
*
|
2019-03-14 18:01:41 +01:00
|
|
|
* @return User
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setLanguage(?string $language): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->language = $language;
|
2020-01-04 20:24:09 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Gets the timezone of the user.
|
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return string|null The timezone of the user (e.g. 'Europe/Berlin') or null if the user has not specified
|
2020-01-04 20:24:09 +01:00
|
|
|
* a timezone (then the global one should be used)
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getTimezone(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->timezone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Change the timezone of this user.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setTimezone(?string $timezone): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->timezone = $timezone;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Gets the theme the users wants to see. See self::AVAILABLE_THEMES for valid values.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return string|null the name of the theme the user wants to see, or null if the system wide should be used
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
public function getTheme(): ?string
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
return $this->theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-14 16:35:19 +01:00
|
|
|
* Change the theme the user wants to see.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @param string|null $theme The name of the theme (See See self::AVAILABLE_THEMES for valid values). Set to null
|
2020-01-04 20:24:09 +01:00
|
|
|
* if the system wide theme should be used.
|
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
2019-03-14 18:01:41 +01:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setTheme(?string $theme): self
|
2019-03-14 18:01:41 +01:00
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 18:01:41 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
/**
|
|
|
|
* Gets the group to which this user belongs to.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return Group|null The group of this user. Null if this user does not have a group.
|
|
|
|
*/
|
2019-03-15 13:18:47 +01:00
|
|
|
public function getGroup(): ?Group
|
|
|
|
{
|
|
|
|
return $this->group;
|
|
|
|
}
|
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
/**
|
|
|
|
* Sets the group of this user.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @param Group|null $group The new group of this user. Set to null if this user should not have a group.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
2019-03-15 13:18:47 +01:00
|
|
|
public function setGroup(?Group $group): self
|
|
|
|
{
|
|
|
|
$this->group = $group;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-15 13:18:47 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-04-28 14:18:11 +02:00
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
/**
|
|
|
|
* Return true if the user should do two-factor authentication.
|
|
|
|
*/
|
|
|
|
public function isGoogleAuthenticatorEnabled(): bool
|
|
|
|
{
|
2022-08-14 19:32:53 +02:00
|
|
|
return (bool)$this->googleAuthenticatorSecret;
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the user name that should be shown in Google Authenticator.
|
|
|
|
*/
|
|
|
|
public function getGoogleAuthenticatorUsername(): string
|
|
|
|
{
|
|
|
|
return $this->getUsername();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the Google Authenticator secret
|
|
|
|
* When an empty string is returned, the Google authentication is disabled.
|
|
|
|
*/
|
|
|
|
public function getGoogleAuthenticatorSecret(): ?string
|
|
|
|
{
|
|
|
|
return $this->googleAuthenticatorSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the secret used for Google Authenticator. Set to null to disable Google Authenticator.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): self
|
|
|
|
{
|
|
|
|
$this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
|
2020-01-04 20:24:09 +01:00
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given code is a valid backup code.
|
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param string $code the code that should be checked
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return bool true if the backup code is valid
|
2019-12-14 16:35:19 +01:00
|
|
|
*/
|
|
|
|
public function isBackupCode(string $code): bool
|
|
|
|
{
|
2022-09-25 00:54:31 +02:00
|
|
|
return in_array($code, $this->getBackupCodes(), true);
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate a backup code.
|
|
|
|
*
|
|
|
|
* @param string $code The code that should be invalidated
|
|
|
|
*/
|
|
|
|
public function invalidateBackupCode(string $code): void
|
|
|
|
{
|
2022-09-25 00:54:31 +02:00
|
|
|
$key = array_search($code, $this->getBackupCodes(), true);
|
2020-01-04 20:24:09 +01:00
|
|
|
if (false !== $key) {
|
2019-12-14 16:35:19 +01:00
|
|
|
unset($this->backupCodes[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-04 20:24:09 +01:00
|
|
|
* Returns the list of all valid backup codes.
|
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return string[] An array with all backup codes
|
|
|
|
*/
|
2020-01-04 20:24:09 +01:00
|
|
|
public function getBackupCodes(): array
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
2019-12-27 18:21:12 +01:00
|
|
|
return $this->backupCodes ?? [];
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the backup codes for this user. Existing backup codes are overridden.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
|
|
|
* @param string[] $codes An array containing the backup codes
|
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return $this
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @throws Exception If an error with the datetime occurs
|
2019-12-14 16:35:19 +01:00
|
|
|
*/
|
2020-01-04 20:24:09 +01:00
|
|
|
public function setBackupCodes(array $codes): self
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
|
|
|
$this->backupCodes = $codes;
|
2020-01-04 20:24:09 +01:00
|
|
|
if (empty($codes)) {
|
2019-12-27 15:20:06 +01:00
|
|
|
$this->backupCodesGenerationDate = null;
|
|
|
|
} else {
|
2020-01-05 22:49:00 +01:00
|
|
|
$this->backupCodesGenerationDate = new DateTime();
|
2019-12-27 15:20:06 +01:00
|
|
|
}
|
2020-01-04 20:24:09 +01:00
|
|
|
|
2019-12-14 16:35:19 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the date when the backup codes were generated.
|
|
|
|
*/
|
2020-01-05 22:49:00 +01:00
|
|
|
public function getBackupCodesGenerationDate(): ?DateTime
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
|
|
|
return $this->backupCodesGenerationDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return version for the trusted device token. Increase version to invalidate all trusted token of the user.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2019-12-14 16:35:19 +01:00
|
|
|
* @return int The version of trusted device token
|
|
|
|
*/
|
|
|
|
public function getTrustedTokenVersion(): int
|
|
|
|
{
|
|
|
|
return $this->trustedDeviceCookieVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate all trusted device tokens at once, by incrementing the token version.
|
|
|
|
* You have to flush the changes to database afterwards.
|
|
|
|
*/
|
2020-01-04 20:24:09 +01:00
|
|
|
public function invalidateTrustedDeviceTokens(): void
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
2020-01-04 20:24:09 +01:00
|
|
|
++$this->trustedDeviceCookieVersion;
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
public function getPreferredTwoFactorProvider(): ?string
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
2022-10-03 23:09:50 +02:00
|
|
|
//If U2F is available then prefer it
|
|
|
|
//if ($this->isU2FAuthEnabled()) {
|
|
|
|
// return 'u2f_two_factor';
|
|
|
|
//}
|
|
|
|
|
2022-10-05 22:32:01 +02:00
|
|
|
if ($this->isWebAuthnAuthenticatorEnabled()) {
|
|
|
|
return 'webauthn_two_factor_provider';
|
|
|
|
}
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
//Otherwise use other methods
|
|
|
|
return null;
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
public function isWebAuthnAuthenticatorEnabled(): bool
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
2022-10-04 00:08:58 +02:00
|
|
|
return count($this->u2fKeys) > 0
|
|
|
|
|| count($this->webauthn_keys) > 0;
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
public function getLegacyU2FKeys(): iterable
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
2022-10-03 23:09:50 +02:00
|
|
|
return $this->u2fKeys;
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
public function getWebAuthnUser(): PublicKeyCredentialUserEntity
|
2019-12-14 16:35:19 +01:00
|
|
|
{
|
2022-10-03 23:09:50 +02:00
|
|
|
return new PublicKeyCredentialUserEntity(
|
|
|
|
$this->getUsername(),
|
|
|
|
(string) $this->getId(),
|
|
|
|
$this->getFullName(),
|
|
|
|
);
|
2019-12-14 16:35:19 +01:00
|
|
|
}
|
2019-12-29 17:36:41 +01:00
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
public function getWebauthnKeys(): iterable
|
2019-12-29 17:36:41 +01:00
|
|
|
{
|
2022-10-04 00:08:58 +02:00
|
|
|
return $this->webauthn_keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addWebauthnKey(WebauthnKey $webauthnKey): void
|
|
|
|
{
|
|
|
|
$this->webauthn_keys->add($webauthnKey);
|
2019-12-29 17:36:41 +01:00
|
|
|
}
|
2023-02-20 23:04:20 +01:00
|
|
|
|
2023-02-21 00:29:50 +01:00
|
|
|
/**
|
|
|
|
* Returns true, if the user was created by the SAML authentication.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSamlUser(): bool
|
|
|
|
{
|
|
|
|
return $this->saml_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the saml_user flag.
|
|
|
|
* @param bool $saml_user
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function setSamlUser(bool $saml_user): User
|
|
|
|
{
|
|
|
|
$this->saml_user = $saml_user;
|
|
|
|
return $this;
|
|
|
|
}
|
2019-03-14 18:01:41 +01:00
|
|
|
}
|