0

I have started a project with silex… I did the authentication and everything works fine. But now, I want to get some of my user's data and the User class I use in my custom UserProvider (from this example Silex doc - Defining a custom User Provider) is final, so I can't override it. I intended to use the query in loadUserByUsername() to get all the information in order to limit access to the database.

Is overriding Symfony\Component\Security\Core\User the right way or do I have to make another usermanager, and if so, how to do it the right way?

Thanks

Ophiuchus
  • 515
  • 7
  • 17

1 Answers1

3

Overriding your custom UserProvider is a right way by adding methods like

  • find($UserId)
  • findAll()
  • save($user)
  • delete($UserId)

and

public function buildObject(array $rowFromDatabase) {
    $user = new MyUser();
    $user->setId($row["id"]);
    $user->setUsername($row["username"]);
    $user->setSalt($row["salt"]);
    $user->setRole($row["role"]);
    $user->setPassword($row["password"]);
    return $user;
}

Where MyUser is your POJO implementing UserInterface

then your loadUserByUsername would be

public function loadUserByUsername($username) {
    $sql = "SELECT * FROM MyUserTable WHERE username = ?";
    $row = $this->getDB()->fetchAssoc($sql, array($username));

    if($row) {
        return $this->buildDomainObject($row);
    }else{
        throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
    }
}

and to limit connection to the database you can use the DAO Pattern.

doncredas
  • 161
  • 7