Silex框架:使用OAuth和基本身份验证

时间:2015-07-05 21:18:30

标签: php authentication login oauth silex

在我目前的Silex项目中,我希望通过Oauth和基本登录来实现登录。对于OAuth,我使用的是Silex扩展(https://github.com/gigablah/silex-oauth)。不幸的是,现在我在集成基本登录时遇到了问题。 我的想法是我必须创建一个自定义用户提供程序,它通过DB提供OAuth和密码,但我不知道如何实现它。

此时,我有一个非常丑陋的两个用户提供商的混合物。对我来说,这是逻辑,但它不会起作用。我想我不在赛道上了,所以如果你能给我一些提示,那就太好了 - 我现在试了几天......

我的用户提供商:

<?php
namespace Core;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\DBAL\Connection;

class UserProvider implements UserProviderInterface
{
private $conn;
private $oauth;

public function __construct($oauth = null)
{
    global $app;
    if($oauth) { $this->oauth = $oauth; }
    $this->conn = $app['db'];
}


/*public function loadAllUsers()
{
    $users = $this->conn->executeQuery('SELECT * FROM users')->fetchAll();
    $users_object = array();

    foreach ($users as $user) {
        if (!empty($user['username'])) {
            $users_object[$user['username']] =  array($user['password'], $user['firstname'], $user['lastname'], explode(',', $user['roles']));
        }
    }  
    $oauth = (array)$this->oauth;
    print_r($oauth->users);
    if (count($oauth['users']) > 0 ) {
                print_r($this->oauth);
    }

    if ($this->oauth) {
        if (count($oauth['users'])) {
            return $this->oauth;
        } else { 
    }     return $users_object;
        } else {
        return $users_object;
    }
}*/



public function loadUserByOAuthCredentials($token)
{
    return $this->oauth->loadUserByOAuthCredentials($token);
}

public function loadUserByUsername($username)
{
    if ($this->oauth->loadUserByUsername($username)) {
        return $this->oauth->loadUserByUsername($username);
    } else {
    $stmt = $this->conn->executeQuery('SELECT * FROM users WHERE username = ?', array(strtolower($username)));
    if (!$user = $stmt->fetch()) {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }


        return new User($user['username'], $user['password'], explode(',', $user['roles']), true, true, true, true);
    }
}

public function refreshUser(UserInterface $user)
{
    if (!$user instanceof User) {
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
    }

        return $this->loadUserByUsername($user->getUsername());
    }

   public function supportsClass($class)
    {
        return $class === 'Symfony\Component\Security\Core\User\User';
    }
}

提前感谢您,如果您需要更多信息,请告诉我。 索拉斯

0 个答案:

没有答案