public function oauthAction()
 {
     /* @var $request \Zend\Http\Request */
     $request = $this->getRequest();
     /* @var $userService \User\Service\User */
     $userService = $this->getServiceLocator()->get('User\\Service\\User');
     $viewModel = new ViewModel();
     if ($userService->hasIdentity()) {
         $viewModel->setVariable('redirect', '/');
         return $viewModel;
     }
     $config = $this->getServiceLocator()->get('Config');
     $clientId = $config['oauths']['id.vatgia.com']['clientId'];
     $secretKey = $config['oauths']['id.vatgia.com']['secretKey'];
     if ($request->getQuery('access_code')) {
         $access_code = $request->getQuery('access_code');
         $url = 'https://id.vatgia.com/oauth2/accessCode/' . $access_code . '?with=acc';
         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
         curl_setopt($curl, CURLOPT_USERPWD, "{$clientId}:{$secretKey}");
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
         $response = curl_exec($curl);
         curl_close($curl);
         $response = json_decode($response);
         if ($response->meta->total_count == 1 && is_array($response->objects) && count($response->objects)) {
             $acc = array_shift($response->objects)->acc;
             $email = $acc->email;
             $fullName = $acc->first_name . ' ' . $acc->last_name;
             $phone = str_replace('+84', '0', $acc->phone);
             $birthday = $acc->dob;
             $address = $acc->address;
             /* @var $userMapper \User\Model\UserMapper */
             $userMapper = $this->getServiceLocator()->get('User\\Model\\UserMapper');
             if (($user = $userMapper->get(null, null, $email)) != null) {
                 $userId = $user->getId();
                 $us = new \User\Model\User();
                 $us->setId($user->getId());
                 $us->setFullName($fullName);
                 $us->setMobile($phone);
                 $us->setBirthday($birthday);
                 $us->setAddress($address);
                 $userService->updateUser($us);
             } else {
                 $user = new \User\Model\User();
                 $user->setEmail($email);
                 $user->setFullName($fullName);
                 $user->setMobile($phone);
                 $user->setBirthday($birthday);
                 $user->setAddress($address);
                 $user->setActive(1);
                 $user->setRegisteredDate(date('Y-m-d H:i:s'));
                 $user->setRegisteredFrom($_SERVER['HTTP_HOST']);
                 $user->setRole(\User\Model\User::ROLE_MEMBER);
                 $userId = $userMapper->save($user);
             }
             $userService->getAuthService()->getStorage()->write($userId);
             $attach = $request->getQuery('attach');
             $attach = json_decode(base64_decode($attach));
             if (isset($attach->redirectUri)) {
                 $viewModel->setVariable('redirect', $attach->redirectUri);
                 if (isset($attach->target)) {
                     $viewModel->setVariable('target', $attach->target);
                 }
                 return $viewModel;
             }
         }
         $viewModel->setVariable('redirect', '/');
         return $viewModel;
     } else {
         $ui_mode = $request->getQuery('ui_mode') ? '&ui_mode=' . $request->getQuery('ui_mode') : '';
         $service = $request->getQuery('service') ? '&service=' . $request->getQuery('service') : '';
         $username = $request->getQuery('username');
         $password = $request->getQuery('password');
         $ticket = '';
         if ($username && $password) {
             $s = new \User\Service\SymetricTicket(array('username' => base64_decode($username), 'password' => base64_decode($password), 'timestamp' => time()));
             $ticket = '&signInTicket=' . urlencode($s->encrypt());
         }
         if ($social = $request->getQuery('social')) {
             $social .= '/';
         } else {
             $social = '';
         }
         $attach = ['redirectUri' => $request->getQuery('redirectUri'), 'target' => $request->getQuery('target')];
         $attach = '?attach=' . base64_encode(json_encode($attach));
         $url = 'https://id.vatgia.com/dang-nhap/' . $social . 'oauth?_cont=http://' . $_SERVER['HTTP_HOST'] . '/signin/oauth' . $attach . '&client_id=' . $clientId . $ui_mode . $service . $ticket;
         $this->redirect()->toUrl($url);
     }
     return $this->response;
 }
 public function changepasswordAction()
 {
     $sl = $this->getServiceLocator();
     /*@var $form \User\Form\ChangePassword */
     $userService = $sl->get('User\\Service\\User');
     /*@var $userService \User\Service\User */
     $user = $userService->getUser();
     $form = $sl->get('User\\Form\\ChangePassword');
     $translator = $sl->get('translator');
     $message = 0;
     if ($this->getRequest()->isPost()) {
         $form->setData($this->getRequest()->getPost());
         if ($form->isValid()) {
             $postData = (array) $this->getRequest()->getPost();
             $us = new \User\Model\User();
             $us->setId($user->getId());
             $us->setPassword($postData['oldpassword']);
             if (!$userService->validateChangeInfo($us)) {
                 $message = 1;
                 //                     '<p class="error">' . $translator->translate('Mật khẩu cũ nhập không chính xác') . '</p>';
                 //                     return new ViewModel(array('form'    => $form,
                 //                     		'message' => $message));
             } else {
                 $us->setId($user->getId());
                 $us->setPassword($postData['newpassword']);
                 $userService->updateUser($us);
                 $message = 2;
                 //                      $translator->translate('Đổi mật khẩu tài khoản thành công');
                 //                     return new ViewModel(array(
                 //                     		'message' => $message));
             }
         }
     }
     return new ViewModel(array('form' => $form, 'message' => $message));
 }