Пример #1
0
 public function createAction($format)
 {
     try {
         $em = $this->getDoctrine()->getManager();
         $user = new User();
         $params = array();
         $content = $this->get("request")->getContent();
         if (!empty($content)) {
             $params = json_decode($content, true);
             $user->setConfirmationToken($params['confirmationToken']);
             $user->setCredentialsExpireAt($params['credentialsExpireAt']);
             $user->setCredentialsExpired($params['credentialsExpired']);
             $user->setEmail($params['email']);
             $user->setEmailCanonical($params['emailCanonical']);
             $user->setEnabled($params['enabled']);
             $user->setExpired($params['expired']);
             $user->setExpiresAt($params['expiresAt']);
             $user->setLastLogin($params['lastLogin']);
             $user->setLocked($params['locked']);
             $user->setPassword($params['password']);
             $user->setPasswordRequestedAt($params['passwordRequestedAt']);
             $user->setRoles($params['roles']);
             $user->setSalt($params['salt']);
             $user->setUsername($params['username']);
             $user->setUsernameCanonical($params['usernameCanonical']);
         }
         $em->persist($user);
         $em->flush();
         return $this->formatResponse("ok", $format);
     } catch (Exception $ex) {
         return $this->formatResponse("error", $format);
     }
 }
 public function getUser($authorizationCode, UserProviderInterface $userProvider)
 {
     $provider = $this->getGoogleOAuthProvider();
     try {
         // the credentials are really the access token
         $accessToken = $provider->getAccessToken('authorization_code', ['code' => $authorizationCode]);
     } catch (IdentityProviderException $ex) {
         $response = $e->getResponseBody();
         $errorCode = $response['error']['code'];
         $message = $response['error']['message'];
         //var_dump($response);
         //TODO throw a custom error to handle ???
     }
     $googleUser = $provider->getResourceOwner($accessToken);
     $email = $googleUser->getEmail();
     $googleId = $googleUser->getId();
     $firstname = $googleUser->getFirstname();
     $lastname = $googleUser->getLastname();
     $displayName = $googleUser->getName();
     $em = $this->container->get('doctrine')->getManager();
     $user = $em->getRepository('AppBundle:User')->findOneBy(['email' => $email]);
     //If there is no user, we need to create one
     if (!$user) {
         $user = new User();
         $user->setUsername($email);
         $user->setEmail($email);
         $user->setLastname($lastname);
         $user->setFirstname($firstname);
         $user->setGoogleDisplayName($displayName);
         $user->setLocale('en_US');
         //Set to unencoded password.
         //Since passwords are encode when checked, users should not be able to login using it
         $user->setPassword('GOOGLE LOGIN');
         //Make sure that a user has at least the role of ROLE_USER when created
         $roles = $user->getRoles();
         $user->setRoles($roles);
     }
     $user->setLastLogin(new \DateTime());
     $user->setGoogleId($googleId);
     $em->persist($user);
     $em->flush();
     return $user;
 }
 public function load(ObjectManager $manager)
 {
     $user = new User();
     $user->setUsername('karen');
     //$user->setPassword($this->encodePassword($user, 'karenpass'));
     $user->setPlainPassword('karenpass');
     $user->setApiToken('123abc');
     $user->setLocale('en_US');
     $user->setEmail('*****@*****.**');
     $user->setLastLogin(new \DateTime());
     $manager->persist($user);
     $admin = new User();
     $admin->setUsername('scott');
     //$admin->setPassword($this->encodePassword($admin, 'karenpass'));
     $admin->setPlainPassword('scottpass');
     $admin->setApiToken('DkE3KWIXPt6bnzZl6lcTt682WLhWYnLYjTeNyiZqgPJiHoEkjTtx03ECCnWP');
     $admin->setLocale('en_US');
     $admin->setRoles(array('ROLE_ADMIN'));
     $admin->setEmail('*****@*****.**');
     $admin->setLastLogin(new \DateTime());
     $manager->persist($admin);
     // the queries aren't done until now
     $manager->flush();
 }
 /**
  * {@inheritDoc}
  */
 public function setLastLogin(\DateTime $time = NULL)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setLastLogin', [$time]);
     return parent::setLastLogin($time);
 }
Пример #5
0
 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());
 }