Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Route("/user/{id}/")
- * @Method({"POST"})
- */
- public function updateAction($id = null){
- /*
- if( !$this->authenticate()){
- return $this->prepareAuthRequiredResponse();
- }
- if(!$this->isAdmin()){
- return $this->tooFewPrivilegesResponse();
- }
- */
- $errors = [];
- $request = Request::createFromGlobals();
- $dataJSON = $this->getJSONRequest();
- $em = $this->getDoctrine()->getManager();
- $new = false;
- $now = new \DateTime("now");
- if(is_null($id)){
- $id = isset($dataJSON['user']['id']) ? $dataJSON['user']['id'] : $request->get('id');
- }
- $email = isset($dataJSON['user']['email']) ? $dataJSON['user']['email'] : null;
- $password = isset($dataJSON['user']['password']) ? $dataJSON['user']['password'] : null;
- $role = isset($dataJSON['user']['role']) ? $dataJSON['user']['role'] : null;
- $token = isset($dataJSON['user']['token']) ? $dataJSON['user']['token'] : null;
- $password = $this->hashPassword($password);
- $user = null;
- if(!is_null($id)){
- $user = $em->getRepository('AppBundle\Entity\User')->find($id);
- }
- if(!is_object($user)){
- $new = true;
- $user = new User();
- /* tu niepotrzebnie sprawdzany był parametr $id czy jest nullem */
- $user->setCreated( $now );
- $user->setTokenExpTime( $now );
- //$user->setToken( "asdfasdfasdbfajhsbdjhfbasjhdbjhfabs" );
- $user->setLastHost( "127.0.0.1" );
- }
- if(!is_null($email)){
- $user->setEmail($email);
- }
- if(!is_null($password)){
- $user->setPassword($password);
- }
- if(!is_null($role)){
- $user->setRole($role);
- }
- if(!is_null($token)){
- $user -> setToken($token);
- }
- $user->setUpdated( $now );
- if(!empty($errors)){
- return $this->fastResponse([
- 'errors' => $errors
- ] , 200);
- }
- $em->persist($user);
- $em->flush();
- return $this->fastResponse([
- 'success' => 1,
- 'id' => $id,
- 'message' => array(
- ($new ? 'user added successfully' : 'user updated successfully')
- )
- ] , 200);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement