public function create($data)
 {
     $this->getEntityManager();
     $user = new \User\Entity\User($data);
     $user->validate($this->em);
     $this->getEntityManager()->persist($user);
     $this->getEntityManager()->flush();
     return new JsonModel($user->toArray());
 }
Example #2
0
 public function load(ObjectManager $manager)
 {
     $bcrypt = new Bcrypt();
     $bcrypt->setCost(16);
     $admin = new \User\Entity\User();
     $admin->setUsername('admin');
     $admin->setDisplayName('Admin');
     $admin->setEmail('*****@*****.**');
     $admin->setState(1);
     $admin->setPassword($bcrypt->create('password'));
     $admin->addRole($this->getReference('role_admin'));
     $userOne = new \User\Entity\User();
     $userOne->setUsername('User A');
     $userOne->setDisplayName('Anton');
     $userOne->setEmail('*****@*****.**');
     $userOne->setState(1);
     $userOne->setPassword($bcrypt->create('password'));
     $userOne->addRole($this->getReference('role_user'));
     $userTwo = new \User\Entity\User();
     $userTwo->setUsername('User B');
     $userTwo->setDisplayName('Berty');
     $userTwo->setEmail('*****@*****.**');
     $userTwo->setState(1);
     $userTwo->setPassword($bcrypt->create('password'));
     $userTwo->addRole($this->getReference('role_user'));
     $manager->persist($admin);
     $manager->persist($userOne);
     $manager->persist($userTwo);
     $this->addReference('user_admin', $admin);
     $this->addReference('user_a', $userOne);
     $this->addReference('user_b', $userTwo);
     $manager->flush();
 }
 /**
  * Register user
  * 
  * @author Stoyan Rangelov
  * @param array $data
  * @return integer|array
  */
 public function registerUser(array $data)
 {
     //set default group
     $data['group'] = $this->getDefaultGroup();
     //set avatar
     if (isset($_FILES['file'])) {
         $data['avatar'] = $_FILES['file'];
     }
     //Get entity manager
     $em = $this->getEntityManager();
     //Input filters
     $inputFilter = new \User\InputFilter\User();
     $customFilter = $inputFilter->registerUser($this->getUsersRepo(), $this->getServiceLocator()->get('group.service')->getGroupsRepo());
     $inputFilter->setInputFilter($customFilter);
     $filter = $inputFilter->getInputFilter();
     $filter->setData($data);
     if ($filter->isValid()) {
         //Upload avatar
         $avatarName = $this->uploadAvatar($data);
         //Generate the password hash
         $bcrypt = new Bcrypt();
         $securePass = $bcrypt->create($data['password']);
         //Get the entity
         $user = new \User\Entity\User();
         //Populate the User's entity
         $user->setLogin($data['email'])->setHash($securePass)->setCreatedAt(new \DateTime())->setUpdatedAt(new \DateTime())->setActivationCode($this->generateActivationCode())->setGroup($this->getServiceLocator()->get('group.service')->getGroupById($data['group']));
         $em->persist($user);
         $em->flush();
         //Populate the user information
         $userInformation = new \User\Entity\UserInformation();
         $userInformation->setFirstName($data['first_name'])->setLastName($data['last_name'])->setUser($this->getUserById($user->getId()));
         $em->persist($userInformation);
         //            //Populate the user's website
         //            $userWebsite = new \User\Entity\UserWebsite();
         //            $userWebsite->setWebsite($data['website'])
         //                    ->setUser($this->getUserById($user->getId()));
         //            $em->persist($userWebsite);
         //
         //            //Populate the user's phone
         //            $userPhone = new \User\Entity\UserPhone();
         //            $userPhone->setPhone($data['phone'])
         //                    ->setUser($this->getUserById($user->getId()));
         //            $em->persist($userPhone);
         $em->flush();
         //Send email to the user
         try {
             $this->sendConfirmationEmail($user);
         } catch (Exception $e) {
         }
         $result = array();
         $result['status_code'] = 201;
         $result['user_id'] = $user->getId();
         return $result;
     } else {
         return $this->getErrorMessages($filter);
     }
 }
Example #4
0
 /**
  * @return \Zend\Http\Response
  * @throws \Facebook\FacebookRequestException
  */
 public function facebookCallbackAction()
 {
     $config = $this->getServiceLocator()->get('config')['facebook'];
     $config['callbackUrl'] = $this->url()->fromRoute('user/default', ['controller' => 'auth', 'action' => 'facebook-callback'], ['force_canonical' => true]);
     FacebookSession::setDefaultApplication($config['appId'], $config['appSecret']);
     $helper = new FacebookRedirectLoginHelper($config['callbackUrl']);
     try {
         $session = $helper->getSessionFromRedirect();
     } catch (\Exception $ex) {
         $this->flashMessenger()->addErrorMessage("Invalid callback request. Oops. Sorry.");
         return $this->redirect()->toRoute('home');
     }
     if ($session) {
         // Logged in
         $request = new FacebookRequest($session, 'GET', '/me');
         $response = $request->execute();
         $graphObject = $response->getGraphObject();
         /**
          * @var \Doctrine\ORM\EntityManager $objectManager
          */
         $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
         /**
          * @var \User\Entity\Auth $auth
          */
         $auth = $objectManager->getRepository('User\\Entity\\Auth')->getAuthRow(Auth::PROVIDER_FACEBOOK, $graphObject->getProperty('id'));
         if ($auth) {
             $user = $auth->getUser();
             if (!$user->isActive()) {
                 $this->flashMessenger()->addSuccessMessage("'User is not active'");
                 return $this->redirect()->toRoute('home');
             }
             $auth->setToken($session->getAccessToken());
             $auth->setTokenSecret(0);
             $auth->setTokenType(Auth::TYPE_ACCESS);
             $message = "You've successfully logged in via facebook";
         } else {
             if (!$this->identity()) {
                 //create new user
                 $user = new \User\Entity\User();
                 $displayName = $graphObject->getProperty('first_name') . ' ' . $graphObject->getProperty('last_name');
                 $user->setDisplayName($displayName);
                 $user->setRole($user::ROLE_USER);
                 $user->activate();
                 $objectManager->persist($user);
                 $objectManager->flush();
             } else {
                 //get current authorized user
                 $user = $this->identity()->getUser();
             }
             $auth = new \User\Entity\Auth();
             $auth->setToken($session->getAccessToken());
             $auth->setTokenSecret(0);
             $auth->setForeignKey($graphObject->getProperty('id'));
             $auth->setProvider(Auth::PROVIDER_FACEBOOK);
             $auth->setTokenType(Auth::TYPE_ACCESS);
             $auth->setUserId($user->getId());
             $user->getAuths()->add($auth);
             $auth->setUser($user);
             $message = "You've successfully registered via facebook";
         }
         $objectManager->persist($user);
         $objectManager->persist($auth);
         $objectManager->flush();
         $auth->login($this->getServiceLocator());
         $this->flashMessenger()->addSuccessMessage($message);
         $session = new Container('location');
         $location = $session->location;
         if ($location) {
             $session->getManager()->getStorage()->clear('location');
             return $this->redirect()->toUrl($location);
         }
         return $this->redirect()->toRoute('home');
     }
 }