diff --git a/features/organization/users.feature b/features/organization/users.feature index 33a44729..76ec35ad 100644 --- a/features/organization/users.feature +++ b/features/organization/users.feature @@ -185,7 +185,7 @@ Feature: And the response status code should be 403 Scenario: As a super-admin, I can impersonate a user - Given I am authenticated as "michel.roca@resop.com" + Given I am authenticated as "super.admin@resop.com" When I go to "/organizations/201/users/" Then I should see "Usurper l'identité" When I follow "Usurper l'identité" diff --git a/fixtures/users.yaml b/fixtures/users.yaml index ba09c0b4..8e4a996d 100644 --- a/fixtures/users.yaml +++ b/fixtures/users.yaml @@ -92,14 +92,14 @@ App\Entity\User: properties: {"occupation": "", "organizationOccupation": "Secouriste", "vulnerable": , "fullyEquipped": , "drivingLicence": } organizations: ['@Organization.DT77'] - # Michel Roca is a super-admin, he can do whatever he wants. - User.michel_roca: + # Super ADMIN is a super-admin, he can do whatever he wants. + User.super_admin: id: 107 - firstName: Michel - lastName: ROCA + firstName: Super + lastName: ADMIN organization: '@Organization.DT75' identificationNumber: 990008A - emailAddress: michel.roca@resop.com + emailAddress: super.admin@resop.com plainPassword: covid19 phoneNumber: '' birthday: '1990-01-01' diff --git a/src/DataFixtures/ApplicationFixtures.php b/src/DataFixtures/ApplicationFixtures.php index a2858287..b8dc2376 100644 --- a/src/DataFixtures/ApplicationFixtures.php +++ b/src/DataFixtures/ApplicationFixtures.php @@ -35,6 +35,10 @@ final class ApplicationFixtures extends Fixture private const PERCENT_ASSET_AVAILABLE = 0.30; private const PERCENT_ASSET_PARTIALLY_AVAILABLE = 0.30; + private const USER_TYPE = 'user'; + private const ADMIN_TYPE = 'admin'; + private const SUPER_ADMIN_TYPE = 'super_admin'; + private const ORGANIZATIONS = [ 'DT75' => [ 'UL 01-02', @@ -97,7 +101,7 @@ final class ApplicationFixtures extends Fixture private array $missionTypes = []; private SkillSetDomain $skillSetDomain; - private int $nbUsers; + private int $nbUsers = 15; private int $nbAvailabilities; private int $availabilitiesId = 1; @@ -116,10 +120,10 @@ public function __construct( string $slotInterval, int $nbUsers = null, int $nbAvailabilities = null - ) { + ) + { $this->validator = $validator; $this->skillSetDomain = $skillSetDomain; - $this->nbUsers = $nbUsers ?: random_int(10, 20); $this->nbAvailabilities = $nbAvailabilities ?: random_int(2, 6); $this->slotBookingGuesser = $slotBookingGuesser; $this->slotAvailabilityGuesser = $slotAvailabilityGuesser; @@ -296,44 +300,62 @@ private function loadCommissionableAssets(ObjectManager $manager): void $manager->flush(); } - private function loadUsers(ObjectManager $manager): void + private function createUser(int $organizationUserNumber, Organization $organization = null, string $type = self::USER_TYPE): User { - $startIdNumber = 990000; + $organizationId = $organization ? $organization->getId() : 0; $firstNames = ['Audrey', 'Arnaud', 'Bastien', 'Beatrice', 'Benoit', 'Camille', 'Claire', 'Hugo', 'Fabien', 'Florian', 'Francis', 'Lilia', 'Lisa', 'Marie', 'Marine', 'Mathias', 'Mathieu', 'Michel', 'Nassim', 'Nathalie', 'Olivier', 'Pierre', 'Philippe', 'Sybille', 'Thomas', 'Tristan']; $lastNames = ['Bryant', 'Butler', 'Curry', 'Davis', 'Doncic', 'Durant', 'Embiid', 'Fournier', 'Grant', 'Gobert', 'Harden', 'Irving', 'James', 'Johnson', 'Jordan', 'Lilliard', 'Morant', 'Noah', 'Oneal', 'Parker', 'Pippen', 'Skywalker', 'Thompson', 'Westbrook']; - $occupations = ['Pharmacien', 'Pompier', 'Ambulancier.e', 'Logisticien', 'Infirmier.e']; - - $x = 1; + $occupations = [null, 'Pharmacien', 'Pompier', 'Ambulancier.e', 'Logisticien', 'Infirmier.e']; + $organizationOcccupations = [null, 'Secouriste', 'DLUS', 'DLAS']; $availableSkillSet = $this->skillSetDomain->getSkillSet(); + + $user = new User(); + $user->id = $organizationId * 100 + $organizationUserNumber; + $user->firstName = $firstNames[array_rand($firstNames)]; + $user->lastName = $lastNames[array_rand($lastNames)]; + $user->organization = $organization; + + // e.g. 990001A + $user->setIdentificationNumber($user->id.'A'); + $user->setEmailAddress($type.$user->id.'@resop.com'); + $user->phoneNumber = $this->phoneNumberUtil->parse('0102030405', 'FR'); + $user->birthday = '1990-01-01'; + $user->properties = [ + 'organizationOccupation' => $organizationOcccupations[array_rand($organizationOcccupations)], + 'fullyEquipped' => (bool) random_int(0, 1), + 'drivingLicence' => (bool) random_int(0, 1), + 'vulnerable' => (bool) random_int(0, 1), + 'occupation' => $occupations[array_rand($occupations)], + ]; + $user->skillSet = (array) array_rand($availableSkillSet, random_int(1, 3)); + + if (self::ADMIN_TYPE === $type || self::SUPER_ADMIN_TYPE === $type) { + // Set encoded password directly for performances on fixtures loading + // Plain password is: covid19 + $user->password = '$argon2id$v=19$m=65536,t=4,p=1$cEjk39WnLC+QRVJfNI5nmw$eM0J3UZ75hwFJRGQmph2OiBGRzJU6/NGVWcj0j+WVYw'; + + if (null !== $organization) { + $user->addOrganization($organization); + } + } + + return $user; + } + + private function loadUsers(ObjectManager $manager): void + { + $user = $this->createUser(1, null, self::SUPER_ADMIN_TYPE); + $user->roles[] = 'ROLE_SUPER_ADMIN'; + $this->validateAndPersist($manager, $user); + foreach ($this->organizations as $organization) { - for ($i = 0; $i < $this->nbUsers; ++$i) { - $user = new User(); - $user->id = $i + 1; - $user->firstName = $firstNames[array_rand($firstNames)]; - $user->lastName = $lastNames[array_rand($lastNames)]; - $user->organization = $organization; - // Set encoded password directly for performances on fixtures loading - // Plain password is: covid19 - $user->password = '$argon2id$v=19$m=65536,t=4,p=1$cEjk39WnLC+QRVJfNI5nmw$eM0J3UZ75hwFJRGQmph2OiBGRzJU6/NGVWcj0j+WVYw'; - - // e.g. 990001A - $user->setIdentificationNumber(str_pad(''.++$startIdNumber.'', 10, '0', \STR_PAD_LEFT).'A'); - $user->setEmailAddress('user'.$x.'@resop.com'); - $user->phoneNumber = $this->phoneNumberUtil->parse('0102030405', 'FR'); - $user->birthday = '1990-01-01'; - $user->properties = [ - 'organizationOccupation' => 'Secouriste', - 'fullyEquipped' => (bool) random_int(0, 1), - 'drivingLicence' => (bool) random_int(0, 1), - 'vulnerable' => (bool) random_int(0, 1), - 'occupation' => $occupations[array_rand($occupations)], - ]; - $user->skillSet = (array) array_rand($availableSkillSet, random_int(1, 3)); + $user = $this->createUser(1, $organization, self::ADMIN_TYPE); + $this->validateAndPersist($manager, $user); + for ($i = 0; $i < $this->nbUsers; ++$i) { + $user = $this->createUser($i+2, $organization); $this->users[$organization->getParentOrganization()->id][] = $user; - $this->validateAndPersist($manager, $user); - ++$x; } } diff --git a/src/Entity/User.php b/src/Entity/User.php index d8da92ca..2b1daeb1 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -90,7 +90,7 @@ class User implements UserPasswordInterface, AvailabilitableInterface, UserSeria /** * @ORM\ManyToOne(targetEntity="App\Entity\Organization", fetch="EAGER") - * @Assert\NotNull() + * @Assert\Expression("'ROLE_SUPER_ADMIN' in this.roles or value != null") */ public ?Organization $organization = null; @@ -234,7 +234,7 @@ public function unserialize($serialized): void $this->identificationNumber, $this->emailAddress, $this->birthday, - $this->password) = unserialize($serialized); + $this->password) = unserialize($serialized, ['allowed_classes' => [__CLASS__]]); } public function getId(): ?int diff --git a/src/Form/Type/UserPasswordType.php b/src/Form/Type/UserPasswordType.php index e03c3ccc..a725a665 100644 --- a/src/Form/Type/UserPasswordType.php +++ b/src/Form/Type/UserPasswordType.php @@ -21,10 +21,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'type' => PasswordType::class, 'required' => true, 'first_options' => [ - 'label' => 'Mot de passe', + 'label' => 'user.password', ], 'second_options' => [ - 'label' => 'Confirmation', + 'label' => 'user.confirmPassword', ], ]); @@ -34,6 +34,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void if (!empty($user->getPassword())) { $event->getForm()->add('currentPassword', PasswordType::class, [ 'required' => true, + 'label' => 'user.currentPassword', ]); } }); diff --git a/templates/_navbar.html.twig b/templates/_navbar.html.twig index 9d2d2341..df542b02 100644 --- a/templates/_navbar.html.twig +++ b/templates/_navbar.html.twig @@ -9,21 +9,12 @@