Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1.     /**
  2.      * @Route("/user/{id}/")
  3.      * @Method({"POST"})
  4.      */
  5.  
  6.     public function updateAction($id = null){
  7.         /*
  8.         if( !$this->authenticate()){
  9.  
  10.             return $this->prepareAuthRequiredResponse();
  11.         }
  12.  
  13.         if(!$this->isAdmin()){
  14.  
  15.             return $this->tooFewPrivilegesResponse();
  16.         }
  17.         */
  18.         $errors = [];
  19.         $request = Request::createFromGlobals();
  20.  
  21.         $dataJSON = $this->getJSONRequest();
  22.  
  23.         $em = $this->getDoctrine()->getManager();
  24.         $new = false;
  25.         $now = new \DateTime("now");
  26.  
  27.         if(is_null($id)){
  28.             $id = isset($dataJSON['user']['id']) ? $dataJSON['user']['id'] : $request->get('id');
  29.         }
  30.  
  31.         $email = isset($dataJSON['user']['email']) ? $dataJSON['user']['email'] : null;
  32.         $password = isset($dataJSON['user']['password']) ? $dataJSON['user']['password'] : null;
  33.         $role = isset($dataJSON['user']['role']) ? $dataJSON['user']['role'] : null;
  34.         $token = isset($dataJSON['user']['token']) ? $dataJSON['user']['token'] : null;
  35.         $password = $this->hashPassword($password);
  36.  
  37.         $user = null;
  38.  
  39.         if(!is_null($id)){
  40.  
  41.             $user = $em->getRepository('AppBundle\Entity\User')->find($id);
  42.  
  43.         }
  44.  
  45.         if(!is_object($user)){
  46.             $new = true;
  47.             $user = new User();
  48.  
  49.             /* tu niepotrzebnie sprawdzany był parametr $id czy jest nullem */
  50.             $user->setCreated( $now );
  51.             $user->setTokenExpTime( $now );
  52.             //$user->setToken( "asdfasdfasdbfajhsbdjhfbasjhdbjhfabs" );
  53.             $user->setLastHost( "127.0.0.1" );
  54.  
  55.         }
  56.  
  57.         if(!is_null($email)){
  58.  
  59.             $user->setEmail($email);
  60.         }
  61.  
  62.         if(!is_null($password)){
  63.  
  64.             $user->setPassword($password);
  65.         }
  66.  
  67.         if(!is_null($role)){
  68.  
  69.             $user->setRole($role);
  70.         }
  71.  
  72.         if(!is_null($token)){
  73.  
  74.             $user -> setToken($token);
  75.         }
  76.         $user->setUpdated( $now );
  77.  
  78.         if(!empty($errors)){
  79.  
  80.             return $this->fastResponse([
  81.                 'errors' => $errors
  82.             ] , 200);
  83.         }
  84.        
  85.         $em->persist($user);
  86.         $em->flush();
  87.        
  88.  
  89.         return $this->fastResponse([
  90.             'success' => 1,
  91.             'id' => $id,
  92.             'message' => array(
  93.                 ($new ? 'user added successfully' : 'user updated successfully')
  94.             )
  95.         ] , 200);
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement