") * * @var array * @ORM\Column(name="roles", type="array") * @Constraints\Role(groups={"RolesUpdate"}) */ private $roles = []; use ColorTrait; public function __construct() { $this->registeredAt = new DateTime(); $this->preferences = new ArrayCollection(); $this->teams = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getRegisteredAt(): ?DateTime { return $this->registeredAt; } public function setRegisteredAt(DateTime $registeredAt): User { $this->registeredAt = $registeredAt; return $this; } public function setAlias(?string $alias): User { $this->alias = StringHelper::ensureMaxLength($alias, 60); return $this; } public function getAlias(): ?string { return $this->alias; } public function getTitle(): ?string { return $this->title; } public function setTitle(?string $title): User { $this->title = StringHelper::ensureMaxLength($title, 50); return $this; } public function getAvatar(): ?string { return $this->avatar; } public function setAvatar(?string $avatar): User { $this->avatar = $avatar; return $this; } public function getApiToken(): ?string { return $this->apiToken; } public function setApiToken(?string $apiToken): User { $this->apiToken = $apiToken; return $this; } public function getPlainApiToken(): ?string { return $this->plainApiToken; } public function setPlainApiToken(?string $plainApiToken): User { $this->plainApiToken = $plainApiToken; return $this; } /** * @return Collection */ public function getPreferences(): Collection { return $this->preferences; } /** * @param iterable $preferences * @return User */ public function setPreferences(iterable $preferences): User { $this->preferences = new ArrayCollection(); foreach ($preferences as $preference) { $this->addPreference($preference); } return $this; } /** * @param string $name * @param bool|int|string|null $value */ public function setPreferenceValue(string $name, $value = null) { $pref = $this->getPreference($name); if (null === $pref) { $pref = new UserPreference(); $pref->setName($name); $this->addPreference($pref); } $pref->setValue($value); } public function getPreference(string $name): ?UserPreference { // this code will be triggered, if a currently logged-in user will be deleted and the refreshed from the session // via one of the UserProvider - e.g. see LdapUserProvider::refreshUser() which calls $user->getPreferenceValue() if (empty($this->preferences)) { return null; } foreach ($this->preferences as $preference) { if ($preference->getName() === $name) { return $preference; } } return null; } public function getLocale(): string { return $this->getPreferenceValue(UserPreference::LOCALE, User::DEFAULT_LANGUAGE); } public function getTimezone(): string { return $this->getPreferenceValue(UserPreference::TIMEZONE, date_default_timezone_get()); } public function getLanguage(): string { return $this->getLocale(); } public function setLanguage(?string $language) { if ($language === null) { $language = User::DEFAULT_LANGUAGE; } $this->setPreferenceValue(UserPreference::LOCALE, $language); } public function isFirstDayOfWeekSunday(): bool { return $this->getFirstDayOfWeek() === 'sunday'; } public function getFirstDayOfWeek(): string { return $this->getPreferenceValue(UserPreference::FIRST_WEEKDAY, User::DEFAULT_FIRST_WEEKDAY); } public function isSmallLayout(): bool { return $this->getPreferenceValue('theme.layout', 'fixed') === 'boxed'; } public function isExportDecimal(): bool { return (bool) $this->getPreferenceValue('timesheet.export_decimal', false); } public function setTimezone(?string $timezone) { if ($timezone === null) { $timezone = date_default_timezone_get(); } $this->setPreferenceValue(UserPreference::TIMEZONE, $timezone); } /** * @param string $name * @param mixed $default * @return bool|int|null|string */ public function getPreferenceValue(string $name, $default = null) { $preference = $this->getPreference($name); if (null === $preference) { return $default; } return $preference->getValue(); } /** * @param UserPreference $preference * @return User */ public function addPreference(UserPreference $preference): User { if (null === $this->preferences) { $this->preferences = new ArrayCollection(); } $this->preferences->add($preference); $preference->setUser($this); return $this; } public function addTeam(Team $team): User { if ($this->teams->contains($team)) { return $this; } $this->teams->add($team); $team->addUser($this); return $this; } public function removeTeam(Team $team) { if (!$this->teams->contains($team)) { return; } $this->teams->removeElement($team); $team->removeUser($this); } public function hasTeamAssignment(): bool { return !$this->getTeams()->isEmpty(); } public function hasTeamMember(User $user): bool { /** @var Team $team */ foreach ($this->getTeams() as $team) { if ($team->hasUser($user)) { return true; } } return false; } /** * @return Collection */ public function getTeams(): Collection { return $this->teams; } public function isInTeam(Team $team): bool { return $this->teams->contains($team); } public function isTeamleadOf(Team $team): bool { return $team->getTeamLead() === $this; } public function canSeeAllData(): bool { return $this->isSuperAdmin() || true === $this->isAllowedToSeeAllData; } /** * This method should not be called by plugins and returns true on success or false on a failure. * * @internal immutable property that cannot be set by plugins * @param bool $canSeeAllData * @return bool * @throws Exception */ public function initCanSeeAllData(bool $canSeeAllData): bool { // prevent manipulation from plugins if (null !== $this->isAllowedToSeeAllData) { return false; } $this->isAllowedToSeeAllData = $canSeeAllData; return true; } public function isTeamlead(): bool { return $this->hasRole(static::ROLE_TEAMLEAD); } public function isAdmin(): bool { return $this->hasRole(static::ROLE_ADMIN); } public function getDisplayName(): ?string { if (!empty($this->getAlias())) { return $this->getAlias(); } return $this->getUsername(); } public function getAuth(): ?string { return $this->auth; } public function setAuth(string $auth): User { $this->auth = $auth; return $this; } public function isSamlUser(): bool { return $this->auth === self::AUTH_SAML; } public function isLdapUser(): bool { return $this->auth === self::AUTH_LDAP; } public function isInternalUser(): bool { return $this->auth === null || $this->auth === self::AUTH_INTERNAL; } /** * {@inheritdoc} */ public function addRole($role) { $role = strtoupper($role); if ($role === static::DEFAULT_ROLE) { return $this; } if (!\in_array($role, $this->roles, true)) { $this->roles[] = $role; } return $this; } /** * {@inheritdoc} */ public function eraseCredentials() { $this->plainPassword = null; } /** * {@inheritdoc} */ public function getUsername() { return $this->username; } /** * {@inheritdoc} */ public function getEmail() { return $this->email; } /** * {@inheritdoc} */ public function getPassword() { return $this->password; } public function getPlainPassword(): ?string { return $this->plainPassword; } public function getLastLogin(): ?DateTime { return $this->lastLogin; } public function getConfirmationToken(): ?string { return $this->confirmationToken; } /** * {@inheritdoc} */ public function getRoles() { $roles = $this->roles; // we need to make sure to have at least one role $roles[] = static::DEFAULT_ROLE; return array_values(array_unique($roles)); } public function hasRole($role): bool { return \in_array(strtoupper($role), $this->getRoles(), true); } public function setSuperAdmin(bool $isSuper): void { if (true === $isSuper) { $this->addRole(static::ROLE_SUPER_ADMIN); } else { $this->removeRole(static::ROLE_SUPER_ADMIN); } } public function isSuperAdmin(): bool { return $this->hasRole(static::ROLE_SUPER_ADMIN); } public function removeRole($role): User { if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { unset($this->roles[$key]); $this->roles = array_values($this->roles); } return $this; } public function setUsername($username): User { $this->username = $username; return $this; } public function setEmail($email): User { $this->email = $email; return $this; } public function isEnabled(): bool { return $this->enabled; } public function setEnabled(bool $enabled): User { $this->enabled = $enabled; return $this; } public function setPassword($password): User { $this->password = $password; return $this; } public function setPlainPassword($password): User { $this->plainPassword = $password; return $this; } public function setLastLogin(\DateTime $time = null): User { $this->lastLogin = $time; return $this; } public function setConfirmationToken($confirmationToken): User { $this->confirmationToken = $confirmationToken; return $this; } public function setPasswordRequestedAt(\DateTime $date = null): User { $this->passwordRequestedAt = $date; return $this; } /** * Gets the timestamp that the user requested a password reset. * * @return DateTime|null */ public function getPasswordRequestedAt(): ?DateTime { return $this->passwordRequestedAt; } public function isPasswordRequestNonExpired(int $seconds): bool { $date = $this->getPasswordRequestedAt(); if ($date === null || !($date instanceof DateTime)) { return false; } return $date->getTimestamp() + $seconds > time(); } public function setRoles(array $roles): User { $this->roles = []; foreach ($roles as $role) { $this->addRole($role); } return $this; } public function isEqualTo(UserInterface $user) { if (!$user instanceof self) { return false; } if ($this->password !== $user->getPassword()) { return false; } if ($this->username !== $user->getUsername()) { return false; } return true; } /** * {@inheritdoc} */ public function serialize() { return serialize([ $this->password, $this->username, $this->enabled, $this->id, $this->email, ]); } /** * {@inheritdoc} */ public function unserialize($serialized) { $data = unserialize($serialized); // unserialize a user object from <= 1.14 if (8 === \count($data)) { unset($data[1], $data[2], $data[7]); $data = array_values($data); } list( $this->password, $this->username, $this->enabled, $this->id, $this->email) = $data; } /** * {@inheritdoc} */ public function getSalt() { return null; } /** * @return string */ public function __toString() { return $this->getDisplayName(); } public function getInitials(): string { $length = 2; $name = $this->getDisplayName(); $initial = ''; if (filter_var($name, FILTER_VALIDATE_EMAIL)) { // turn my.email@gmail.com into "My Email" $result = mb_strstr($name, '@', true); $name = $result === false ? $name : $result; $name = str_replace('.', ' ', $name); } $words = explode(' ', $name); // if name contains single word, use first N character if (\count($words) === 1) { $initial = $words[0]; if (mb_strlen($name) >= $length) { $initial = mb_substr($name, 0, $length, 'UTF-8'); } } else { // otherwise, use initial char from each word foreach ($words as $word) { $initial .= mb_substr($word, 0, 1, 'UTF-8'); } $initial = mb_substr($initial, 0, $length, 'UTF-8'); } $initial = mb_strtoupper($initial); return $initial; } }