Esempio n. 1
0
 public function load(ObjectManager $manager)
 {
     // load the main required roles
     // create the ROLE_ADMIN role
     $roleAdmin = new Role();
     $roleAdmin->setName('ROLE_ADMIN');
     $manager->persist($roleAdmin);
     // create the ROLE_NOTACTIVE role
     $roleNotActive = new Role();
     $roleNotActive->setName('ROLE_NOTACTIVE');
     $manager->persist($roleNotActive);
     // create the ROLE_USER role
     $roleUser = new Role();
     $roleUser->setName('ROLE_USER');
     $manager->persist($roleUser);
     // create the ROLE_UPDATABLE_USERNAME role
     $roleUserName = new Role();
     $roleUserName->setName('ROLE_UPDATABLE_USERNAME');
     $manager->persist($roleUserName);
     // create admin user
     $user = new User();
     $user->setLoginName('Objects');
     $user->setUserPassword('0bjects123');
     $user->setEmail('*****@*****.**');
     $user->setFirstName('Objects');
     $user->getUserRoles()->add($roleAdmin);
     $manager->persist($user);
     // create admin user
     $user1 = new User();
     $user1->setLoginName('mahmoud');
     $user1->setUserPassword('123');
     $user1->setEmail('*****@*****.**');
     $user1->setFirstName('mahmoud');
     $user1->getUserRoles()->add($roleAdmin);
     $manager->persist($user1);
     // create active user
     $user2 = new User();
     $user2->setLoginName('Ahmed');
     $user2->setUserPassword('123');
     $user2->setEmail('*****@*****.**');
     $user2->setFirstName('Ahmed');
     $user2->getUserRoles()->add($roleUser);
     $manager->persist($user2);
     //create a user
     $user3 = new User();
     $user3->setLoginName('mirehan');
     $user3->setUserPassword('123');
     $user3->setEmail('*****@*****.**');
     $user3->setFirstName('mirehan');
     $user3->getUserRoles()->add($roleUser);
     $user3->getUserRoles()->add($roleUserName);
     $manager->persist($user3);
     //create a NotActivated user
     $user4 = new User();
     $user4->setLoginName('notactive');
     $user4->setUserPassword('123');
     $user4->setEmail('*****@*****.**');
     $user4->setFirstName('notactive');
     $user4->getUserRoles()->add($roleNotActive);
     $manager->persist($user4);
     $manager->flush();
 }
Esempio n. 2
0
 /**
  * signup or login using google oauth
  * @author Mahmoud
  */
 public function googleEnterAction()
 {
     $session = $this->get('session');
     $googleUser = $session->get('googleUserInfo');
     $googleId = $googleUser['id'];
     $em = $this->getDoctrine()->getManager();
     $user = $em->getRepository('ObjectsUserBundle:User')->getUserWithRolesByGoogleId($googleId);
     if ($user) {
         try {
             $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
             $this->get('security.context')->setToken($token);
             return $this->redirectUserAction();
         } catch (\Exception $e) {
             $this->get('security.context')->setToken(null);
             $session->invalidate();
             return $this->redirect($this->generateUrl('login'));
         }
     }
     $container = $this->container;
     $user = new User();
     $user->setEmail($googleUser['email']);
     $user->setGoogleId($googleId);
     $name = '';
     if (isset($googleUser['name'])) {
         $name = $googleUser['name'];
     } elseif (isset($googleUser['given_name'])) {
         $name = $googleUser['given_name'];
     } else {
         $name = 'user';
     }
     $nameParts = explode(' ', $name);
     if (!empty($nameParts[0])) {
         $user->setFirstName($nameParts[0]);
     }
     if (!empty($nameParts[1])) {
         $user->setLastName($nameParts[1]);
     }
     if ($container->getParameter('login_name_required')) {
         $user->setLoginName($this->suggestLoginName($name));
     }
     if (isset($googleUser['link'])) {
         $user->setUrl($googleUser['link']);
     }
     if (isset($googleUser['gender'])) {
         if ($googleUser['gender'] === 'male') {
             $user->setGender(1);
         } elseif ($googleUser['gender'] === 'female') {
             $user->setGender(0);
         }
     }
     if (isset($googleUser['locale'])) {
         $user->setSuggestedLanguage($googleUser['locale']);
     }
     if (isset($googleUser['birthday'])) {
         $user->setDateOfBirth(new \DateTime($googleUser['birthday']));
     }
     if (isset($googleUser['picture'])) {
         $user->setImageFromUrl($googleUser['picture']);
     }
     return $this->finishSignUp($user, true);
 }