Skip to content

Commit

Permalink
PHP Enums for Image and User privacy level
Browse files Browse the repository at this point in the history
  • Loading branch information
nikrou committed Jan 4, 2025
1 parent feca890 commit c5c480e
Show file tree
Hide file tree
Showing 20 changed files with 411 additions and 373 deletions.
650 changes: 327 additions & 323 deletions admin/theme/template/users_list.html.twig

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/Controller/Admin/AdminBatchManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use App\Entity\Caddie;
use App\Entity\User;
use App\Enum\ImageSizeType;
use App\Enum\UserPrivacyLevelType;
use App\ImageLibraryGuesser;
use App\Repository\AlbumRepository;
use App\Repository\CaddieRepository;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected function appendFilter(SessionInterface $session, array $filter = []):
}

/**
* @return array<string, int|float|bool|string|null|array<int>|array<string>>
* @return array<string, int|float|bool|string|null|UserPrivacyLevelType|array<int>|array<string>>
*/
protected function getFilter(SessionInterface $session): array
{
Expand Down Expand Up @@ -983,7 +984,7 @@ public function unit(
foreach ($imageMapper->getRepository()->findBy(['id' => $collection]) as $image) {
$image->setName($request->request->get('name-' . $image->getId()));
$image->setAuthor($request->request->get('author-' . $image->getId()));
$image->setLevel($request->request->get('level-' . $image->getId()));
$image->setLevel(UserPrivacyLevelType::from($request->request->get('level-' . $image->getId())));

if ($conf['allow_html_descriptions']) {
$image->setComment($request->request->get('description-' . $image->getId()) ?? '');
Expand Down
3 changes: 2 additions & 1 deletion src/Controller/Admin/AdminPhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\DataMapper\TagMapper;
use App\DataMapper\UserMapper;
use App\Enum\ImageSizeType;
use App\Enum\UserPrivacyLevelType;
use App\ImageLibraryGuesser;
use App\Repository\ImageAlbumRepository;
use App\Repository\ImageRepository;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function edit(
if ($request->isMethod('POST')) {
$image->setName($request->request->get('name'));
$image->setAuthor($request->request->get('author'));
$image->setLevel($request->request->get('level'));
$image->setLevel(UserPrivacyLevelType::from($request->request->get('level')));

if ($conf['allow_html_descriptions']) {
$image->setComment($request->request->get('description'));
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/Admin/AdminUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\DataMapper\AlbumMapper;
use App\DataMapper\UserMapper;
use App\Entity\User;
use App\Enum\UserPrivacyLevelType;
use App\Enum\UserStatusType;
use App\Form\Model\UserProfileModel;
use App\Form\UserCreationType;
Expand Down Expand Up @@ -142,11 +143,11 @@ public function list(

// user level options
$level_options = [];
foreach ($conf['available_permission_levels'] as $level) {
$level_options[$level] = $translator->trans(sprintf('Level %d', $level), [], 'admin');
foreach (UserPrivacyLevelType::cases() as $level) {
$level_options[$level->value] = $translator->trans(sprintf('Level %d', $level->value), [], 'admin');
}
$tpl_params['level_options'] = $level_options;
$tpl_params['level_selected'] = $guestUser->getUserInfos()->getLevel();
$tpl_params['level_selected'] = $guestUser->getUserInfos()->getLevel()->value;

$tpl_params['ws'] = $this->generateUrl('ws');
$tpl_params['csrf_token'] = $csrfTokenManager->getToken('authenticate');
Expand Down
8 changes: 3 additions & 5 deletions src/Controller/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,11 @@ public function albums(
if (count($albums) > 0) {
$tpl_thumbnails_var = [];
foreach ($albums as $album) {
$userCacheAlbum = $album->getUserCacheAlbum();
$name = $albumMapper->getAlbumsDisplayNameCache($album->getUppercats());
$representative_infos = null;
if (isset($infos_of_images[$album->getRepresentativePictureId()])) {
$representative_infos = $infos_of_images[$album->getRepresentativePictureId()];
}

$userCacheAlbum = $album->getUserCacheAlbum();
$representative_infos = $infos_of_images[$album->getRepresentativePictureId()];

$tpl_var = array_merge(
$album->toArray(),
[
Expand Down
5 changes: 3 additions & 2 deletions src/DataMapper/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Entity\Album;
use App\Entity\ImageAlbum;
use App\Entity\User;
use App\Enum\UserPrivacyLevelType;
use App\Repository\AlbumRepository;
use App\Repository\ImageAlbumRepository;
use App\Repository\ImageRepository;
Expand Down Expand Up @@ -146,7 +147,7 @@ protected function getAlbumsMenu(User $user, array $selected_album = []): array
*
* @return array<int, array<string, int|string|null>>
*/
public function getComputedAlbums(int $level, array $forbidden_categories = []): array
public function getComputedAlbums(UserPrivacyLevelType $level, array $forbidden_categories = []): array
{
$albums = [];
$last_photo_date = null;
Expand Down Expand Up @@ -1125,7 +1126,7 @@ public function getInfosOfImages(User $user, array $albums, array $image_ids, Im
$bad_level_ids = [];

foreach ($imageMapper->getRepository()->findBy(['id' => $image_ids]) as $image) {
if ($image->getLevel() <= $user->getUserInfos()->getLevel()) {
if ($image->getLevel()->value <= $user->getUserInfos()->getLevel()->value) {
$infos_of_images[$image->getId()] = [$image->toArray(), 'image' => $image];
} else {
$bad_level_ids[] = $image->getId();
Expand Down
9 changes: 5 additions & 4 deletions src/Entity/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace App\Entity;

use App\Enum\UserPrivacyLevelType;
use Doctrine\DBAL\Types\Types;
use App\Repository\ImageRepository;
use DateTimeInterface;
Expand Down Expand Up @@ -75,8 +76,8 @@ class Image
#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $storage_category_id = null;

#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $level = 0;
#[ORM\Column(type: Types::INTEGER, nullable: true, enumType: UserPrivacyLevelType::class)]
private ?UserPrivacyLevelType $level = UserPrivacyLevelType::DEFAULT;

#[ORM\Column(type: Types::STRING, length: 32, nullable: true)]
private ?string $md5sum = null;
Expand Down Expand Up @@ -325,12 +326,12 @@ public function setStorageCategoryId(?int $storage_category_id): self
return $this;
}

public function getLevel(): ?int
public function getLevel(): UserPrivacyLevelType
{
return $this->level;
}

public function setLevel(int $level): self
public function setLevel(UserPrivacyLevelType $level): self
{
$this->level = $level;

Expand Down
14 changes: 7 additions & 7 deletions src/Entity/UserInfos.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace App\Entity;

use App\Enum\UserPrivacyLevelType;
use App\Enum\UserStatusType;
use Doctrine\DBAL\Types\Types;
use DateTimeInterface;
Expand All @@ -20,14 +21,13 @@
/**
*
* @phpstan-type UserInfosArray array{nb_image_page: int, language: ?string, expand: bool, show_nb_comments: bool, show_nb_hits: bool,
* recent_period: int, theme: ?string, enabled_high: bool, level: int}
* recent_period: int, theme: ?string, enabled_high: bool, level: UserPrivacyLevelType}
*/
#[ORM\Table(name: 'user_infos')]
#[ORM\Entity(repositoryClass: UserInfosRepository::class)]
class UserInfos
{
final public const DEFAULT_NB_IMAGE_PAGE = 15;
final public const DEFAULT_LEVEL = 0;
final public const DEFAULT_RECENT_PERIOD = 7;
final public const DEFAULT_SHOW_NB_COMMENTS = false;
final public const DEFAULT_SHOW_NB_HITS = false;
Expand Down Expand Up @@ -65,8 +65,8 @@ class UserInfos
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTimeInterface $registration_date = null;

#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $level = self::DEFAULT_LEVEL;
#[ORM\Column(type: Types::INTEGER, nullable: true, enumType: UserPrivacyLevelType::class)]
private ?UserPrivacyLevelType $level = UserPrivacyLevelType::DEFAULT;

#[ORM\Column(type: Types::INTEGER)]
private int $recent_period = self::DEFAULT_RECENT_PERIOD;
Expand Down Expand Up @@ -169,12 +169,12 @@ public function setRegistrationDate(?DateTimeInterface $registration_date): self
return $this;
}

public function getLevel(): int
public function getLevel(): UserPrivacyLevelType
{
return $this->level ?? self::DEFAULT_LEVEL;
return $this->level;
}

public function setLevel(int $level = self::DEFAULT_LEVEL): self
public function setLevel(UserPrivacyLevelType $level = UserPrivacyLevelType::DEFAULT): self
{
$this->level = $level;

Expand Down
29 changes: 29 additions & 0 deletions src/Enum/UserPrivacyLevelType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of Phyxo package
*
* Copyright(c) Nicolas Roudaire https://www.phyxo.net/
* Licensed under the GPL version 2.0 license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Enum;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum UserPrivacyLevelType: int implements TranslatableInterface
{
case DEFAULT = 0;
case CONTACT = 1;
case FRIENDS = 2;
case FAMILY = 4;
case ADMINS = 8;

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return $translator->trans(sprintf('Level %d', $this->value), locale: $locale);
}
}
7 changes: 4 additions & 3 deletions src/Form/Model/UserInfosModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use App\Entity\Language;
use App\Entity\Theme;
use App\Enum\UserPrivacyLevelType;
use App\Enum\UserStatusType;

class UserInfosModel
Expand All @@ -23,7 +24,7 @@ class UserInfosModel
private ?Language $language = null;
private ?Theme $theme = null;
private UserStatusType $status;
private int $level = 0;
private UserPrivacyLevelType $level = UserPrivacyLevelType::DEFAULT;
private ?bool $show_nb_comments = null;
private ?bool $show_nb_hits = null;
private ?bool $expand = null;
Expand Down Expand Up @@ -100,14 +101,14 @@ public function getStatus(): UserStatusType
return $this->status;
}

public function setLevel(int $Level): self
public function setLevel(UserPrivacyLevelType $Level): self
{
$this->level = $Level;

return $this;
}

public function getLevel(): int
public function getLevel(): UserPrivacyLevelType
{
return $this->level;
}
Expand Down
13 changes: 2 additions & 11 deletions src/Form/UserInfosType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

use App\Entity\Language;
use App\Entity\Theme;
use App\Enum\UserPrivacyLevelType;
use App\Enum\UserStatusType;
use App\Form\Model\UserInfosModel;
use App\Form\Transformer\UserToUserInfosTransformer;
use App\Repository\LanguageRepository;
use App\Repository\ThemeRepository;
use App\Repository\UserRepository;
use Phyxo\Conf;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -29,7 +29,6 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

class UserInfosType extends AbstractType
{
Expand All @@ -42,8 +41,6 @@ public function __construct(
private readonly LanguageRepository $languageRepository,
private readonly ThemeRepository $themeRepository,
private readonly UserRepository $userRepository,
private readonly TranslatorInterface $translator,
private Conf $conf,
) {
}

Expand Down Expand Up @@ -86,13 +83,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

if ($options[self::IN_ADMIN_OPTION]) {
$fields->add('status', EnumType::class, ['label' => 'Status', 'class' => UserStatusType::class, 'expanded' => false]);

$levels = [];
foreach ($this->conf['available_permission_levels'] as $level) {
$levels[$level] = $this->translator->trans(sprintf('Level %d', $level));
}

$fields->add('level', ChoiceType::class, ['label' => 'Privacy level', 'choices' => array_flip($levels), 'expanded' => false]);
$fields->add('level', EnumType::class, ['label' => 'Privacy level', 'class' => UserPrivacyLevelType::class, 'expanded' => false]);
}

$fields->add(
Expand Down
3 changes: 2 additions & 1 deletion src/Repository/AlbumRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\ORM\Query\Expr\Join;
use App\Entity\Album;
use App\Enum\UserPrivacyLevelType;
use DateTimeInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
Expand Down Expand Up @@ -362,7 +363,7 @@ public function findPrivateWithGroupAccess(int $group_id)
*
* @return array<int, array{album_id: int, id_uppercat: ?int, date_last: string, nb_images: int}>
*/
public function getComputedAlbums(int $level, array $forbidden_albums = [])
public function getComputedAlbums(UserPrivacyLevelType $level, array $forbidden_albums = [])
{
$qb = $this->createQueryBuilder('a');
$qb->select('a.id AS album_id, IDENTITY(a.parent) AS id_uppercat, MAX(i.date_available) as date_last, COUNT(i.id) AS nb_images');
Expand Down
7 changes: 4 additions & 3 deletions src/Repository/ImageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DateInterval;
use App\Entity\Album;
use App\Entity\Image;
use App\Enum\UserPrivacyLevelType;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function updateFieldForImages(array $ids, string $field, string|DateTimeI
$qb->getQuery()->getResult();
}

public function updateLevel(int $image_id, int $level = 0): void
public function updateLevel(int $image_id, UserPrivacyLevelType $level = UserPrivacyLevelType::DEFAULT): void
{
$qb = $this->createQueryBuilder('i');
$qb->update();
Expand Down Expand Up @@ -114,7 +115,7 @@ public function findWithNoStorageOrStorageForAlbums(array $album_ids = [])
*
* @return Image[]
*/
public function getForbiddenImages(array $forbidden_albums = [], int $level = 0)
public function getForbiddenImages(array $forbidden_albums = [], UserPrivacyLevelType $level = UserPrivacyLevelType::DEFAULT)
{
$qb = $this->createQueryBuilder('i');
$qb->leftJoin('i.imageAlbums', 'ia');
Expand Down Expand Up @@ -962,7 +963,7 @@ public function findDuplicates(array $fields)
/**
* @return Image[]
*/
public function filterByLevel(int $level, string $operator = '=')
public function filterByLevel(UserPrivacyLevelType $level, string $operator = '=')
{
$qb = $this->createQueryBuilder('i');
$qb->where('i.level ' . $operator . ' :level');
Expand Down
3 changes: 2 additions & 1 deletion src/Utils/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Repository\GroupRepository;
use App\Entity\User;
use App\Entity\UserInfos;
use App\Enum\UserPrivacyLevelType;
use App\Enum\UserStatusType;
use App\Repository\UserInfosRepository;

Expand Down Expand Up @@ -45,7 +46,7 @@ public function register(User $user): User
$userInfos->setRegistrationDate(new DateTime());
$userInfos->setLastModified(new DateTime());
if (in_array('ROLE_WEBMASTER', $user->getRoles())) {
$userInfos->setLevel(10); // @FIX: find a way to only inject that param instead of conf ; max($this->conf['available_permission_levels']);
$userInfos->setLevel(UserPrivacyLevelType::ADMINS);
}
$userInfos->setStatus($user->getStatusFromRoles());
$user->setUserInfos($userInfos);
Expand Down
Loading

0 comments on commit c5c480e

Please sign in to comment.