public function registrationAction() { $auth = new AuthenticationService(); if ($auth->hasIdentity()) { return $this->redirect()->toRoute('home'); } // process the form $form = new RegistrationForm(); $request = $this->getRequest(); if ($this->getRequest()->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); if ($data['password'] == $data['password_confirm']) { /** * Check if a user with the given username or email already * exists */ $sm = $this->getServiceLocator(); $mapper = $sm->get('User\\Model\\UserMapper'); $params = array('where' => 'username = "******"'); $user = $mapper->select($params); $params = array('where' => 'email = "' . $data['email'] . '"'); $email = $mapper->select($params); if (!$user && !$email) { $user = new User($data); // Hash the password with a random salt $user->setPassword_salt(mcrypt_create_iv(64)); $user->setPassword_hash(hash('sha256', $user->getPassword_salt() . $data['password'])); $user->setActive(0); // Insert the account into the database $mapper->save($user); $params = array('where' => 'username = "******"'); $user = $mapper->select($params); if ($user) { $user = $user[0]; // prompt the user to activate the account return $this->redirect()->toRoute('registration', array('action' => 'confirm', 'id' => $user->getId())); } } else { if ($user) { print "A user with this user name already exists."; } if ($email) { print "A user with this email already exists."; } } } else { print "The password was not confirmed."; } } } return new ViewModel(array('form' => $form)); }
public function googleAction() { $request = $this->getRequest(); if (trim($request->getQuery('redirect'))) { $_SESSION['redirect'] = trim($request->getQuery('redirect')); } $viewModel = new ViewModel(); $viewModel->setTerminal(true); $response = $this->getResponse(); /* @var $googleLogin \User\Service\GoogleLogin */ require_once 'Google/Client.php'; require_once 'Google/Service/Oauth2.php'; require_once 'Google/Http/REST.php'; require_once 'Google/Http/Request.php'; require_once 'Google/Service/Resource.php'; require_once 'Google/Auth/OAuth2.php'; $config = $this->getServiceLocator()->get('Config'); $clientId = $config['login']['google']['clientId']; $clientSecret = $config['login']['google']['clientSecret']; $url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://'; $url .= $_SERVER['HTTP_HOST'] . '/user/signin/google'; $client = new \Google_Client(); $client->setApplicationName('MyFirstTest'); $client->setClientId($clientId); $client->setClientSecret($clientSecret); $client->setRedirectUri($url); $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me')); $error = $this->getRequest()->getQuery('error'); if ($error) { echo '<script>window.close()</script>'; } $code = $this->getRequest()->getQuery('code'); if ($code) { $client->authenticate($code); $_SESSION['access_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } else { $authUrl = $client->createAuthUrl(); return $this->redirect()->toUrl(filter_var($authUrl, FILTER_SANITIZE_URL)); } //TODO: xoa vi cam giac dau co can if (isset($_SESSION['access_token'])) { $client->setAccessToken($_SESSION['access_token']); } if ($client->isAccessTokenExpired()) { $authUrl = $client->createAuthUrl(); return $this->redirect()->toUrl(filter_var($authUrl, FILTER_SANITIZE_URL)); } //End TODO if ($client->getAccessToken()) { $oauth2 = new \Google_Service_Oauth2($client); $userInfoPlus = $oauth2->userinfo->get(); $_SESSION['access_token'] = $client->getAccessToken(); if (!$userInfoPlus->getEmail()) { echo '<script>window.opener.alert("Không tồn tại thông tin Email của tài khoản trên");window.close()</script>'; } $arrResponse = array('email' => $userInfoPlus->getEmail(), 'name' => $userInfoPlus->getName()); $user = new User(); $userMapper = $this->getServiceLocator()->get('User\\Model\\UserMapper'); $serviceUser = $this->getServiceLocator()->get('User\\Service\\User'); if (!$serviceUser->authenticateGoogle($arrResponse['email'])) { $user->setActive('1'); $user->setEmail($arrResponse['email']); $user->setRole(\User\Model\User::ROLE_MEMBER); $user->setFullName($arrResponse['name']); $user->setCreatedDate(DateBase::getCurrentDate()); $user->setCreatedDateTime(DateBase::getCurrentDateTime()); $userMapper->save($user); // check nếu user đó đã dc mời tham gia dự án sẽ gắn luôn nó với dự án $projectUser = new \Work\Model\ProjectUser(); $projectUser->setUserEmail($user->getEmail()); $projectUser->setUserId($user->getId()); $projectUserMapper = $this->getServiceLocator()->get('\\Work\\Model\\ProjectUserMapper'); $projectUserMapper->updateUserId($projectUser); $this->redirect()->toUrl($url); } else { // check nếu user đó đã dc mời tham gia dự án sẽ gắn luôn nó với dự án $projectUser = new \Work\Model\ProjectUser(); $projectUser->setUserEmail($serviceUser->getUser()->getEmail()); $projectUser->setUserId($serviceUser->getUser()->getId()); $projectUserMapper = $this->getServiceLocator()->get('\\Work\\Model\\ProjectUserMapper'); $projectUserMapper->updateUserId($projectUser); if (!$_SESSION['redirect']) { return $this->redirect()->toUrl('/'); } else { $redirect = $_SESSION['redirect']; unset($_SESSION['redirect']); return $this->redirect()->toUrl($redirect); } } } else { $this->redirect()->toUrl($client->createAuthUrl()); } return $response; }
/** * @param \User\Model\User $user */ public function signup(\User\Model\User $user) { $user->setSalt(substr(md5(time() . rand(2000, 5000)), 0, 20)); $user->setCreatedDate(DateBase::getCurrentDate()); $user->setCreatedDateTime(DateBase::getCurrentDateTime()); $user->setPassword(md5($user->getSalt() . $user->getPassword())); $user->setRegisteredDate(date('Y-m-d')); $user->setRegisteredFrom(str_replace('www.', '', strtolower($_SERVER['HTTP_HOST']))); // todo hien tai mac dinh dang ki thuong la active luon sau nay tinh sau $user->setActive(\User\Model\User::STATUS_ACTIVE); // $user->setActiveKey((md5($user->getUsername() . $user->getPassword() . time()))); // $user->setActiveLink('http://' . $_SERVER['HTTP_HOST'] . '/user/active?u=' . $user->getUsername() . '&c=' . $user->getActiveKey()); $user->setRole(\User\Model\User::ROLE_MEMBER); $sl = $this->getServiceLocator(); $translator = $this->getServiceLocator()->get('translator'); /** @var $mapper \User\Model\UserMapper */ $mapper = $sl->get('User\\Model\\Usermapper'); $mapper->save($user); }
public function addAction() { /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); $sl = $this->getServiceLocator(); /* @var $cityMapper \Address\Model\CityMapper */ $cityMapper = $sl->get('Address\\Model\\CityMapper'); $city = new \Address\Model\City(); /* @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $sl->get('Address\\Model\\DistrictMapper'); $district = new \Address\Model\District(); $districts = array(); if (!!($cityId = $request->getPost('cityId'))) { $district->setCityId($cityId); $districts = $districtMapper->fetchAll($district); } $form = new \System\Form\User\Add($this->getServiceLocator()); $form->setCities($city->toSelectBoxArray($cityMapper->fetchAll())); $form->setDistricts($district->toSelectBoxArray($districts)); if ($this->getRequest()->isPost()) { $data = $this->getRequest()->getPost(); $form->setData($data); if ($form->isValid()) { $user = new User(); $user->exchangeArray($form->getData()); $user->setSalt($user->generateSalt()); $user->setPassword($user->createPassword()); if (!$user->getRole()) { $user->setRole(User::ROLE_GUEST); } if ($user->getBirthdate()) { $user->setBirthdate(DateBase::toCommonDate($user->getBirthdate())); } $user->setActive(1); $user->setCreatedById($this->user()->getIdentity()); $user->setCreatedDate(DateBase::getCurrentDate()); $user->setCreatedDateTime(DateBase::getCurrentDateTime()); $userMapper = $this->getServiceLocator()->get('\\User\\Model\\UserMapper'); $userMapper->save($user); if ($form->get('afterSubmit')->getValue()) { return $this->redirect()->toUrl($form->get('afterSubmit')->getValue()); } } } $viewModel = new ViewModel(); $viewModel->setVariable('form', $form); return $viewModel; }