fetch user preferences via API (#2905)

This commit is contained in:
Kevin Papst
2021-11-15 21:14:45 +01:00
committed by GitHub
parent 5896ae26c6
commit a1992494d3
5 changed files with 63 additions and 1 deletions

View File

@@ -373,6 +373,45 @@ class User implements UserInterface, EquatableInterface, \Serializable
return $this;
}
/**
* Read-only list of of all visible user preferences.
*
* @Serializer\VirtualProperty
* @Serializer\SerializedName("preferences"),
* @Serializer\Groups({"User_Entity"})
* @SWG\Property(type="array", @SWG\Items(ref="#/definitions/UserPreference"))
*
* @internal only for API usage
* @return UserPreference[]
*/
public function getVisiblePreferences(): array
{
// hide all internal preferences, which are either available in other fields
// or which are only used within the Kimai UI
$skip = [
UserPreference::TIMEZONE,
UserPreference::LOCALE,
UserPreference::SKIN,
'calendar.initial_view',
'login.initial_view',
'reporting.initial_view',
'theme.collapsed_sidebar',
'theme.layout',
'theme.update_browser_title',
'timesheet.daily_stats',
'timesheet.export_decimal',
];
$all = [];
foreach ($this->preferences as $preference) {
if ($preference->isEnabled() && !\in_array($preference->getName(), $skip)) {
$all[] = $preference;
}
}
return $all;
}
/**
* @return Collection<UserPreference>
*/

View File

@@ -11,6 +11,7 @@ namespace App\Entity;
use App\Form\Type\YesNoType;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Validator\Constraint;
@@ -23,6 +24,8 @@ use Symfony\Component\Validator\Constraints as Assert;
* @ORM\UniqueConstraint(columns={"user_id", "name"})
* }
* )
*
* @Serializer\ExclusionPolicy("all")
*/
class UserPreference
{
@@ -53,6 +56,9 @@ class UserPreference
/**
* @var string
*
* @Serializer\Expose()
* @Serializer\Groups({"Default"})
*
* @ORM\Column(name="name", type="string", length=50, nullable=false)
* @Assert\NotNull()
* @Assert\Length(min=2, max=50, allowEmptyString=false)
@@ -61,6 +67,9 @@ class UserPreference
/**
* @var string
*
* @Serializer\Expose()
* @Serializer\Groups({"Default"})
*
* @ORM\Column(name="value", type="string", length=255, nullable=true)
*/
private $value;