ccc

Silex: Modificación del Vendor para obtener desde una clase el Id de Usuario

En CUsuarios:

    function getUserLoggedId(Application $app) {
        $token = $app['security.token_storage']->getToken();
        if (null !== $token) {
            $user = $token->getUser();
            $app['session']->set(
                'user', array(
                    'user-credentials' => $user
                    ));
            $userid = $app['session']->get('user')["user-credentials"]->getUserId();
        }

        return $userid;
    }

En vendor\symfony\security\Core\User\User.php:

final class User implements AdvancedUserInterface
{
    private $username;
    private $password;
    private $enabled;
    private $accountNonExpired;
    private $credentialsNonExpired;
    private $accountNonLocked;
    private $roles;
    private $userid;

    public function __construct($userid, $username, $password, array $roles = array(), $enabled = true, $userNonExpired = true, $credentialsNonExpired = true, $userNonLocked = true)
    {
        if ('' === $username || null === $username) {
            throw new \InvalidArgumentException('The username cannot be empty.');
        }

        $this->userid = $userid;
        $this->username = $username;
        $this->password = $password;
        $this->enabled = $enabled;
        $this->accountNonExpired = $userNonExpired;
        $this->credentialsNonExpired = $credentialsNonExpired;
        $this->accountNonLocked = $userNonLocked;
        $this->roles = $roles;
    }

   
    public function getUserId()
    {
        return $this->userid;
    }

...

En vendor\silex\silex\src\Silex\Provider\UserProvider.php:
public function loadUserByUsername($username)
    {
        $stmt = $this->conn->executeQuery('SELECT * FROM users WHERE email = ?', array(strtolower($username)));

        if (!$user = $stmt->fetch()) {
            throw new UsernameNotFoundException(sprintf('Email "%s" does not exist.', $username));
        }
               
        return new User($user['id_admin'], $user['email'], $user['password'], explode(',', $user['roles']), true, true, true, true);
    }

No hay comentarios:

Publicar un comentario