/** * @Route("/api/createUser", name="createUser"), defaults={"_format"="xml"}, name="_createUser") */ public function createUserAction(Request $request) { try { $verificationCode = $request->get('verificationcode', ''); if ($verificationCode == '') { $oOperation = new Operation(); $oOperation->setBSucceeded(false); $oOperation->setICode(409); $oOperation->setSMessage("Phone validation failed"); return $this->xmlResponse($oOperation, SerializationContext::create()->setGroups(array('Error')), 409); } $username = $request->get('username', null); $phone = $request->get('phone', null); $deviceId = $request->get('deviceid', null); $contactsPhones = $request->get('contactsphones', null); $em = $this->getDoctrine()->getManager(); //Get current device $deviceRepository = $em->getRepository('AppBundle:UserDevice'); $device = $deviceRepository->findOneBy(array('deviceId' => $deviceId)); if (!isset($device)) { $oOperation = new Operation(); $oOperation->setBSucceeded(false); $oOperation->setICode(409); $oOperation->setSMessage("Device not found"); return $this->xmlResponse($oOperation, SerializationContext::create()->setGroups(array('Error')), 409); } $userRepository = $em->getRepository('AppBundle:User'); $user = $userRepository->findOneBy(array('phone' => $phone)); if (!isset($user)) { $user = new User(); $user->setCdate((new DateTime())->setTimezone(new \DateTimeZone('UTC'))); $user->setTKey(sha1(date_format(new DateTime(), 'Y-m-d H:i:s') . $phone)); $user->setAvatar(self::DEFAULT_AVATAR_URL); } /** TODO ensure normalized Phone */ $user->setPhone($phone); $user->setUsername($username); //If we have a registered User append his already existing contacts //Store the new contact list $updatedContactsPhones $contactsPhonesArray = explode(',', $contactsPhones); if (empty($user->getContactsphones())) { $user->setContactsphones(json_encode(array_values($contactsPhonesArray))); } if (isset($device) && isset($user)) { $contactsPhonesArray = $this->appendUserContacts($user, $contactsPhonesArray); } $updatedContactsPhones = json_encode(array_values($contactsPhonesArray)); //Get lastest contacts Users and store their ids via $contactsUserIds $contactsUserIds = array(); if (isset($contactsPhones)) { $contactsUsers = $userRepository->findBy(array('phone' => $contactsPhonesArray)); foreach ($contactsUsers as $contactsUser) { $contactsUserIds[] = $contactsUser->getId(); if (!$user->getMyContacts()->Contains($contactsUser)) { $user->addMyContact($contactsUser); } } } $contactsUserIds = json_encode($contactsUserIds); $user->setConnections($contactsUserIds); $user->setContactsPhones($updatedContactsPhones); $user->setLastUpdate((new DateTime())->setTimezone(new \DateTimeZone('UTC'))); $em->persist($user); $em->flush(); //Update the device $device->setUser($user); $device->setIsUserRegistered(true); $em->persist($device); $em->flush(); return $this->xmlResponse($device, SerializationContext::create()->enableMaxDepthChecks()->setGroups(array('Device'))); } catch (Exception $ex) { $oOperation = new Operation(); $oOperation->setBSucceeded(false); $oOperation->setICode(409); $oOperation->setSMessage("Phone validation failed"); $oError = new Error(); $oError->setICode($ex->getCode()); $oError->setSMessage($ex->getMessage()); $oOperation->addError($oError); return $this->xmlResponse($oOperation, SerializationContext::create()->setGroups(array('Error')), 409); } }
/** * @param File $file * @param User $user */ private function uploadFile(File $file, User $user) { $filename = 'emergya-' . $user->getNick() . '.' . $file->getClientOriginalExtension(); $file->move($this->getParameter('uploads_directory'), $filename); $user->setAvatar($filename); }
public function loadUserByOAuthUserResponse(UserResponseInterface $response) { $uri = $this->request->getUri(); $isMailru = false; if (strpos($uri, '/login_mailru') !== false) { $isMailru = true; } if ($isMailru === false) { throw new \Exception("Invalid social network login attempt"); } $social = ""; if ($isMailru) { $social = "mailru"; } //check to see if the user is logged in and if she is logged in with the same social network $isLoggedInAlready = $this->session->has('user'); $isLoggedInAlreadyId = $this->session->get('user')['id']; if ($isLoggedInAlready && $this->session->get('user')['social'] == $social) { return $this->loadUserByUsername($isLoggedInAlreadyId); } $social_id = $response->getUsername(); $username = $response->getUsername(); $realName = $response->getRealName(); $email = $response->getEmail(); $avatar = $response->getProfilePicture(); //set data in session. upon logging out we just erase the whole array. $sessionData = array(); $sessionData['social_id'] = $social_id; $sessionData['username'] = $username; $sessionData['realName'] = $realName; $sessionData['email'] = $email; $sessionData['avatar'] = $avatar; $sessionData['social'] = $social; $user = null; if ($isLoggedInAlready) { $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneById($isLoggedInAlreadyId); } else { if ($isMailru) { $user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByMid($social_id); } } if ($user == null) { $user = new User(); //change these only the user hasn't been registered before. $user->setUsername($username); $user->setRealname($realName); $user->setAvatar($avatar); } if ($isMailru) { $user->setMid($social_id); } $user->setLastLogin(new \DateTime('now')); $user->setSocial($social); // SET E-MAIL //if all emails are empty, set the first one to this one. if ($user->getEmail() == "") { $user->setEmail($email); } else { //if it really is an e-mail, try putting it in email2 or email3 if ($email != "") { //is the e-mail different than the previous one? if ($email != $user->getEmail()) { //if there an e-mail in email2? no: if ($user->getEmail2() == "") { $user->setEmail2($email); } else { //there is an e-mail in email2 and it's different. fall back to setting the user3 to w/e. if ($user->getEmail2() != $email) { $user->setEmail3($email); } } } } } //save all changes $em = $this->doctrine->getManager(); $em->persist($user); $em->flush(); $id = $user->getId(); //set id $sessionData['id'] = $id; $sessionData['is_admin'] = $this->adminChecker->check($user); $this->session->set('user', $sessionData); return $this->loadUserByUsername($user->getId()); }