Configure the project
Your UserService is installed but you still need to explain it how to query the user's database.
It's necessary to implement UserDaoInterface
and UserInterface
in your project.
The UserService
needs to be connected to a UserDao
object that you will develop.
This UserDao
must implement the UserDaoInterface
.
Each object representing a user returned by the UserDao
class must implement the UserInterface
.
Setting up the DAO
For the following exemple, I'll use TDBM (see TDBM documentation for more information). I'll use the user table like:
TDBM will create automatically for us a UserDao
class. However, this class does not contain all the methods the UserService
needs.
The UserDao
must implement the UserDaoInterface
.
First : implement UserDaoInterface
:
UserDaoInterface
contains five functions to be implemented:
interface UserDaoInterface {
public function getUserByCredentials($login, $password);
public function getUserByToken($token);
public function discardToken($token);
public function getUserById($id);
public function getUserByLogin($login);
}
In this example, the login is the email column and the password is the password column:
UserDao.php
class UserDao extends UserDaoBase implements UserDaoInterface {
public function getUserByCredentials($login, $password){
$user = $this->getByFilter(array(
new EqualFilter("user", "email", $login)
));
if ($user){
// User exists.
// Is the password correct?
// Note: this is using PHP >= 5.5 password_hash function.
if (password_verify($password, $user->getPassword()) {
return $user;
} else {
return NULL;
}
}else{
// User does not exist
return NULL;
}
}
public function getUserByToken($token){
// You should implement this if you want to be able to autolog a user by token (useful
// for forgotten passwords)
throw new \Exception("To be implemented");
}
public function discardToken($token){
// You should implement this if you want to be able to autolog a user by token (useful
// for forgotten passwords)
throw new \Exception("To be implemented");
}
public function getUserById($id){
return $this->getById($id);
}
public function getUserByLogin($login){
$user = $this->getByFilter( new EqualFilter("user", "email", $login));
return $user;
}
}
Then, implements the UserInterface
:
UserInterface
contains two methods:
interface UserInterface {
public function getId();
public function getLogin();
}
It's necessary to implement the UserInterface
and its methods in my UserBean
:
UserBean.php
class UserBean extends UserBaseBean implements UserInterface{
public function getLogin(){
return $this->getEmail();
}
// getId() is already implemented by UserBaseBean
}
Next steps
Found a typo? Something is wrong in this documentation? Just fork and edit it!