/**
* TASK
*
* This is production code you found in the codebase. It was clearly written by very junior developer.
* It turns out you have some time to work on that and improve it to meet the best quality and industry standards.
*
*/
class AuthenticationManager {
// private $users = [];
public $sql;
public function __construct() {
$this->sql = new Sql('http://localhost:3306', 'mysql_user', '4sfynvasdere');
}
public function authenticate($username, $password) {
$user = $this->sql->query("SELECT * FROM users WHERE login = $username AND password = $password");
if ($user) {
return true;
}
return false;
}
public function logAccess($username) {
// Log access
file_put_contents('access.log', "$username accessed the system.\n", FILE_APPEND);
}
public function isAdmin($username) {
return $username === 'admin';
}
public function registerUser($name, $lastName, $email, $password)
{
$this->sql->insert('INSERT INTO users VALUES('. $name .', '. $lastName .', '. $email .', '. $password .')');
$smtp = new SmptClient('http://mail-service.com', 'smtp_centra', 'fdr49nqwF$3r,>@3');
$smtp->send(
'Welcome to Centra Playground!',
"Hey $name!
Nice to have you onboard! This is your password in case you forget it: $password"
);
}
public function grantAccess($username, $resource) {
if ($this->isAdmin($username)) {
// Grant access to resource
echo "Access granted to $username for $resource.\n";
} else {
echo "Access denied for $username.\n";
}
}
}