| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* Copyright (C) 2017 Michael Giesler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Dembelo. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Dembelo is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * it under the terms of the GNU Affero General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Dembelo is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * GNU Affero General Public License 3 for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * You should have received a copy of the GNU Affero General Public License 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * along with Dembelo. If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | namespace AdminBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Doctrine\ORM\QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use DembeloMain\Model\Repository\UserRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * Class UserController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @Route(service="app.admin_controller_user") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class UserController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var UserRepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $userRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var \Swift_Mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * UserController constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param UserRepositoryInterface $userRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param \Swift_Mailer           $mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         UserRepositoryInterface $userRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         \Swift_Mailer $mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         $this->userRepository = $userRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |         $this->mailer = $mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @Route("/users", name="admin_users") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |     public function usersAction(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |         $filters = $request->query->get('filter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         /* @var $query QueryBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         $query = $this->userRepository->createQueryBuilder(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         if (null !== $filters) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 72 | 2 | View Code Duplication |             foreach ($filters as $field => $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 if (empty($value) && $value !== '0') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 if ($field === 'status') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     //$value = $value === 'aktiv' ? 1 : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $query->field($field)->equals((int) $value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     $query->field($field)->equals(new \MongoRegex('/.*'.$value.'.*/i')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |         $users = $query->getQuery()->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |         $output = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         /* @var $user \DembeloMain\Document\User */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |         foreach ($users as $user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |             $obj = new \StdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             $obj->id = $user->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |             $obj->email = $user->getEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             $obj->roles = implode(', ', $user->getRoles()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |             $obj->licenseeId = $user->getLicenseeId() ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             $obj->gender = $user->getGender(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             $obj->status = $user->getStatus(); // === 0 ? 'inaktiv' : 'aktiv'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             $obj->source = $user->getSource(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |             $obj->reason = $user->getReason(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |             $obj->created = date('Y-m-d H:i:s', $user->getMetadata()['created']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             $obj->updated = date('Y-m-d H:i:s', $user->getMetadata()['updated']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |             $output[] = $obj; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         return new Response(\json_encode($output)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @Route("/useractivationmail", name="admin_user_activation_mail") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     public function useractivationmailAction(Request $request): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         $userId = $request->request->get('userId'); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         /* @var $user \DembeloMain\Document\User */ | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $user = $this->userRepository->find($userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if (null === $user) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             return new Response(\json_encode(['error' => false])); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         $user->setActivationHash(sha1($user->getEmail().$user->getPassword().\time())); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         $this->userRepository->save($user); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         $message = (new \Swift_Message('waszulesen - Bestätigung der Email-Adresse')) | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             ->setFrom('[email protected]') | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             ->setTo($user->getEmail()) | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             ->setBody( | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 $this->renderView( | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |                     'AdminBundle::Emails/registration.txt.twig', | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |                     array('hash' => $user->getActivationHash()) | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |                 ), | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |                 'text/html' | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $this->mailer->send($message); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         return new Response(\json_encode(['error' => false])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |