2019-04-04 15:18:27 +02:00
|
|
|
<?php
|
|
|
|
class User_Sql extends User {
|
|
|
|
|
2019-04-18 19:03:35 +02:00
|
|
|
|
|
|
|
public static function create_user(PDO $db,$table_users,$login, $display_name,
|
|
|
|
$password,$admin = false,$active = true)
|
|
|
|
{
|
|
|
|
$stmt = $db->prepare(
|
|
|
|
"INSERT INTO " . $table_users . "
|
|
|
|
(login, display_name, auth_method,password,admin,active)
|
|
|
|
VALUES
|
|
|
|
(:login, :display_name, 'local', :password, :admin, :active)"
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmt->bindParam(':login', $login);
|
|
|
|
$stmt->bindParam(':display_name', $display_name);
|
|
|
|
$stmt->bindParam(':password',$hashed_password);
|
|
|
|
$stmt->bindParam(':admin', $adminInt);
|
|
|
|
$stmt->bindParam(':active', $activeInt);
|
|
|
|
|
2019-04-18 23:40:05 +02:00
|
|
|
|
2019-04-18 19:03:35 +02:00
|
|
|
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
|
2019-04-18 23:40:05 +02:00
|
|
|
|
2019-04-18 19:03:35 +02:00
|
|
|
$adminInt = $admin? 1 : 0;
|
2019-04-18 23:40:05 +02:00
|
|
|
$activeInt = $active? 1 : 0;
|
2019-04-18 19:03:35 +02:00
|
|
|
$stmt->execute();
|
|
|
|
|
|
|
|
return $db->lastInsertId();
|
|
|
|
}
|
|
|
|
|
2019-04-04 15:18:27 +02:00
|
|
|
public function authentificate($login,$password)
|
|
|
|
{
|
|
|
|
|
2019-04-18 23:40:05 +02:00
|
|
|
$stmt = $this->db->prepare(
|
2019-06-05 15:11:56 +02:00
|
|
|
"SELECT id,display_name,password,admin
|
2019-04-18 23:40:05 +02:00
|
|
|
FROM users
|
|
|
|
WHERE login=:login
|
|
|
|
AND active=1
|
|
|
|
AND auth_method='local'"
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmt->bindParam(':login', $login);
|
|
|
|
$stmt->execute();
|
|
|
|
if($r = $stmt->fetch()){
|
|
|
|
|
|
|
|
//check password
|
|
|
|
if(password_verify($password,$r["password"])){
|
2019-06-05 15:11:56 +02:00
|
|
|
|
2019-04-18 23:40:05 +02:00
|
|
|
$this->is_connected = true;
|
|
|
|
$this->display_name = $r["display_name"];
|
|
|
|
$this->id = $r['id'];
|
|
|
|
$this->auth_method = 'sql';
|
2019-06-05 15:11:56 +02:00
|
|
|
$this->is_admin = ($r["admin"] == '1')? true : false;
|
2019-04-18 23:40:05 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2019-04-04 15:18:27 +02:00
|
|
|
}
|
2019-04-18 23:40:05 +02:00
|
|
|
$this->is_connected = false;
|
|
|
|
return $this;
|
2019-04-04 15:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|