Пример #1
0
 /** @return \Kdyby\Facebook\Dialog\LoginDialog */
 protected function createComponentFbLogin()
 {
     $dialog = $this->facebook->createDialog('login');
     /** @var \Kdyby\Facebook\Dialog\LoginDialog $dialog */
     $dialog->onResponse[] = function (\Kdyby\Facebook\Dialog\LoginDialog $dialog) {
         $fb = $dialog->getFacebook();
         if (!$fb->getUser()) {
             $this->flashMessage("Facebook authentication failed.");
             return;
         }
         /**
          * If we get here, it means that the user was recognized
          * and we can call the Facebook API
          */
         try {
             $me = $fb->api('/me');
             if (!($existing = $this->usersModel->findByFacebookId($fb->getUser()))) {
                 /**
                  * Variable $me contains all the public information about the user
                  * including facebook id, name and email, if he allowed you to see it.
                  */
                 $existing = $this->usersModel->registerFromFacebook($fb->getUser(), $me, $this->user);
             }
             /**
              * You should save the access token to database for later usage.
              *
              * You will need it when you'll want to call Facebook API,
              * when the user is not logged in to your website,
              * with the access token in his session.
              */
             $this->usersModel->updateFacebookAccessToken($fb->getUser(), $fb->getAccessToken());
             /**
              * Nette\Security\User accepts not only textual credentials,
              * but even an identity instance!
              */
             $this->user->setExpiration('365 days', FALSE);
             $this->user->login(new \Nette\Security\Identity($existing->id, NULL, $existing));
             /**
              * You can celebrate now! The user is authenticated :)
              */
         } catch (\Kdyby\Facebook\FacebookApiException $e) {
             /**
              * You might wanna know what happened, so let's log the exception.
              *
              * Rendering entire bluescreen is kind of slow task,
              * so might wanna log only $e->getMessage(), it's up to you
              */
             \Tracy\Debugger::log($e, 'facebook');
             $this->flashMessage("Facebook authentication failed.");
         }
         $this->redirect('this');
     };
     return $dialog;
 }
Пример #2
0
 /**
  * Unlink account
  *
  * @param int $key Account session key
  */
 public static function unlink($key = 0)
 {
     // Revoke Token
     \Rest::get('https://accounts.google.com/o/oauth2/revoke', array('token' => $_SESSION['usernames'][$key]['credentials']['access_token']));
     // Remove Data
     UsersModel::remove($_SESSION['usernames'][$key]['username']['id']);
     if (isset($_SESSION['usernames'][$key])) {
         $toRemove = $_SESSION['usernames'][$key];
         unset($_SESSION['usernames'][$key]);
         if (isset($_SESSION['wizard']['source']) && $_SESSION['wizard']['source']['username']['id'] == $toRemove['username']['id']) {
             $_SESSION['wizard']['source'] = \Auth::userBeside($_SESSION['wizard']['destination']);
         }
         if (isset($_SESSION['wizard']['destination']) && $_SESSION['wizard']['destination']['username']['id'] == $toRemove['username']['id']) {
             $_SESSION['wizard']['destination'] = \Auth::userBeside($_SESSION['sync']['source']);
         }
         if ($_SESSION['current']['username']['id'] == $key && count($_SESSION['usernames'])) {
             end($_SESSION['usernames']);
             $_SESSION['current'] = current($_SESSION['usernames']);
         }
     }
     if (!count($_SESSION['usernames'])) {
         \Auth::logout();
     }
     \Util::notice(array('type' => 'success', 'text' => 'The account has been successfully removed and its API tokens revoked.'));
     \Router::redirect('accounts');
 }
Пример #3
0
 public static function save()
 {
     if (isset($_POST) && isset($_POST['error']) || !isset($_POST['access_token'])) {
         if (isset($_POST['error']) && $_POST['error'] != 'immediate_failed') {
             \Util::notice(array('type' => 'danger', 'text' => 'Sorry, operation failed with the following error: ' . $_POST['error']));
         } else {
             echo json_encode(array('error' => 'immediate_failed'));
         }
     } else {
         $share = SharesModel::first(array('link' => $_POST['task']));
         if ($share) {
             $share = $share->toArray();
             $data = json_decode($share['data'], true);
             if (strtotime($share['created_at']) + $share['expires'] > time()) {
                 $task = TasksModel::first($share['task_id'])->toArray();
                 $service = ServicesModel::first($share['service_id'])->toArray();
                 $source = UsersModel::profile($task['user_id']);
                 $destination['username']['id'] = uniqid();
                 $destination['credentials'] = $_POST;
                 call_user_func_array(array('app\\libraries\\' . $service['library'], 'backup'), array($destination, 0, $task['id']));
                 call_user_func_array(array('app\\libraries\\' . $service['library'], 'migrate'), array($source, $destination, $task['id'], false, $data));
                 \Util::notice(array('type' => 'success', 'text' => 'The data is being imported. Please check your account in a couple of minutes.'));
             } else {
                 \Util::notice(array('type' => 'danger', 'text' => 'The requested link has expired.'));
             }
         }
     }
 }
Пример #4
0
 public function login()
 {
     if (App::create()->session->isLoggedIn()) {
         header('Location: /profile');
     }
     $postData = App::create()->request->post();
     if ($postData) {
         $user = new UsersModel();
         $validation = $user->fromArray($postData)->validate('login');
         $validationErrors = $validation->getErrors();
         if (!$validationErrors) {
             $email = $postData['email'];
             $password = md5($postData['password']);
             $user = $user->findOne(['email' => "='{$email}'", 'AND', 'password' => "='{$password}'"]);
             if ($user->getId()) {
                 App::create()->session->setData(['userId' => $user->getId()]);
                 header('Location: /profile');
             } else {
                 $validationErrors[]['user']['message'] = 'Your entered data for login are wrong';
             }
         }
     }
     App::create()->template->setData(['title' => 'Login', 'validationErrors' => $validationErrors ? $validationErrors : [], 'postData' => App::create()->request->post()])->render('auth/login');
 }
Пример #5
0
 public static function oAuthTokensPermissions($tokens = false)
 {
     $profile = Rest::get('https://www.googleapis.com/oauth2/v1/userinfo', array('alt' => 'json', 'access_token' => $tokens['access_token']));
     $username = UsersModel::first(array('google_id' => $profile['id'], 'status' => UsersModel::STATUS_ACTIVE)) ?: UsersModel::create();
     if (!static::showWizard()) {
         $redirectUrl = BASE_URL . 'accounts/permissions';
     } else {
         $redirectUrl = BASE_URL . 'accounts/add';
     }
     Util::notice(array('type' => 'success', 'text' => 'You have successfully updated your ' . $username->email . ' account permissions.'));
     $username->last_login = $_SESSION['current']['username']['last_login'];
     $username->save();
     // Credentials update
     $credentials = UsersCredentialsModel::first(array('user_id' => $username->id));
     // Get refresh token
     $data = array('client_id' => OAUTH_CLIENT_ID, 'client_secret' => OAUTH_CLIENT_SECRET, 'redirect_uri' => 'postmessage', 'code' => $tokens['code'], 'grant_type' => 'authorization_code');
     $tokens = Rest::post('https://accounts.google.com/o/oauth2/token', $data);
     $credentials->refresh_token = $tokens['refresh_token'];
     $credentials->access_token = $tokens['access_token'];
     $credentials->expires_at = date(DATE_TIME, time() + $tokens['expires_in']);
     $credentials->save();
     $services = UsersServicesModel::all(array('user_id' => $username->id))->toArray();
     $credentials = $credentials->toArray();
     // User profile
     $userProfile = UsersProfilesModel::first(array('user_id' => $username->id))->toArray();
     $username = $username->toArray();
     $setSession = true;
     // Session thingie
     static::setUsername(compact('username', 'services', 'credentials', 'userProfile', 'setSession'));
     return array('success' => 'true', 'redirectUrl' => $redirectUrl);
 }
Пример #6
0
 public static function revoke()
 {
     $users = UsersModel::withCredentials();
     if ($users) {
         foreach ($users as $user) {
             if (isset($user['credentials']['access_token'])) {
                 \Rest::get('https://accounts.google.com/o/oauth2/revoke', array('token' => $user['credentials']['access_token']));
             }
             if (isset($user['credentials']['refresh_token'])) {
                 \Rest::get('https://accounts.google.com/o/oauth2/revoke', array('token' => $user['credentials']['refresh_token']));
             }
         }
     }
 }
Пример #7
0
 public function check()
 {
     if (Authentication::getInstance()->isAuthenticated()) {
         $userModel = new UsersModel();
         $master = $userModel->getMasterKey(Authentication::getInstance()->getUserName());
         $master = $master[0]['master_key'];
         $response = new AJAXAnswer(true, $master);
     } else {
         $response = new AJAXAnswer(false, '');
     }
     $response->answer();
 }