owncourses /
courses-server
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Admin; |
||
| 6 | |||
| 7 | use App\Model\UserInterface; |
||
| 8 | use Sonata\AdminBundle\Admin\AbstractAdmin; |
||
| 9 | use Sonata\AdminBundle\Datagrid\DatagridMapper; |
||
| 10 | use Sonata\AdminBundle\Datagrid\ListMapper; |
||
| 11 | use Sonata\AdminBundle\Form\FormMapper; |
||
| 12 | use Sonata\AdminBundle\Show\ShowMapper; |
||
| 13 | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
||
| 14 | use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
||
| 15 | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
||
| 16 | |||
| 17 | final class UserAdmin extends AbstractAdmin |
||
| 18 | { |
||
| 19 | private ?UserPasswordEncoderInterface $encoder = null; |
||
| 20 | |||
| 21 | public function preUpdate($object) |
||
| 22 | { |
||
| 23 | parent::preUpdate($object); |
||
| 24 | |||
| 25 | $this->handleUserPassword($object); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function prePersist($object) |
||
| 29 | { |
||
| 30 | parent::prePersist($object); |
||
| 31 | |||
| 32 | $this->handleUserPassword($object); |
||
| 33 | } |
||
| 34 | |||
| 35 | private function handleUserPassword(UserInterface $user) |
||
| 36 | { |
||
| 37 | if (null === ($plainPassword = $user->getPlainPassword())) { |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | $encoded = $this->encoder->encodePassword($user, $plainPassword); |
||
|
0 ignored issues
–
show
|
|||
| 42 | $user->setPassword($encoded); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function setEncoder(UserPasswordEncoderInterface $encoder) |
||
| 46 | { |
||
| 47 | $this->encoder = $encoder; |
||
| 48 | } |
||
| 49 | |||
| 50 | protected function configureDatagridFilters(DatagridMapper $datagridMapper): void |
||
| 51 | { |
||
| 52 | $datagridMapper |
||
| 53 | ->add('id') |
||
| 54 | ->add('email') |
||
| 55 | ->add('firstName') |
||
| 56 | ->add('lastName') |
||
| 57 | ->add('roles') |
||
| 58 | ; |
||
| 59 | } |
||
| 60 | |||
| 61 | protected function configureListFields(ListMapper $listMapper): void |
||
| 62 | { |
||
| 63 | $listMapper |
||
| 64 | ->add('email') |
||
| 65 | ->add('firstName') |
||
| 66 | ->add('lastName') |
||
| 67 | ->add('courses') |
||
| 68 | ->add('created') |
||
| 69 | ->add('updated') |
||
| 70 | ->add('lastLoginDate') |
||
| 71 | ->add('_action', null, [ |
||
| 72 | 'actions' => [ |
||
| 73 | 'show' => [], |
||
| 74 | 'edit' => [], |
||
| 75 | 'delete' => [], |
||
| 76 | ], |
||
| 77 | ]); |
||
| 78 | } |
||
| 79 | |||
| 80 | protected function configureFormFields(FormMapper $formMapper): void |
||
| 81 | { |
||
| 82 | $container = $this->getConfigurationPool()->getContainer(); |
||
| 83 | $roles = $container->getParameter('security.role_hierarchy.roles'); |
||
| 84 | |||
| 85 | $formMapper |
||
| 86 | ->add('email') |
||
| 87 | ->add('firstName') |
||
| 88 | ->add('lastName') |
||
| 89 | ->add('courses') |
||
| 90 | ->add('plainPassword', PasswordType::class, ['label' => 'Password', 'required' => false]) |
||
| 91 | ->add('roles', ChoiceType::class, [ |
||
| 92 | 'choices' => self::flattenRoles($roles), |
||
| 93 | 'multiple' => true, |
||
| 94 | ] |
||
| 95 | ) |
||
| 96 | ; |
||
| 97 | } |
||
| 98 | |||
| 99 | protected function configureShowFields(ShowMapper $showMapper): void |
||
| 100 | { |
||
| 101 | $showMapper |
||
| 102 | ->add('id') |
||
| 103 | ->add('firstName') |
||
| 104 | ->add('lastName') |
||
| 105 | ->add('courses') |
||
| 106 | ->add('email') |
||
| 107 | ->add('roles') |
||
| 108 | ; |
||
| 109 | } |
||
| 110 | |||
| 111 | protected static function flattenRoles(array $rolesHierarchy): array |
||
| 112 | { |
||
| 113 | $flatRoles = []; |
||
| 114 | foreach ($rolesHierarchy as $roles) { |
||
| 115 | if (empty($roles)) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | |||
| 119 | foreach ($roles as $role) { |
||
| 120 | if (!isset($flatRoles[$role])) { |
||
| 121 | $flatRoles[$role] = $role; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | return $flatRoles; |
||
| 127 | } |
||
| 128 | } |
||
| 129 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.