115 lines
No EOL
4.3 KiB
PHP
115 lines
No EOL
4.3 KiB
PHP
<?php
|
|
class User {
|
|
private $db;
|
|
public function __construct() {
|
|
$this->db = new Database();
|
|
}
|
|
public static function parseProjects($projects): array {
|
|
if (is_array($projects)) {
|
|
$list = $projects;
|
|
} elseif (is_string($projects) && !empty($projects)) {
|
|
$decoded = json_decode($projects, true);
|
|
if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
|
|
$list = $decoded;
|
|
} else {
|
|
$list = explode(',', $projects);
|
|
}
|
|
} else {
|
|
return [];
|
|
}
|
|
|
|
$clean = [];
|
|
foreach ($list as $item) {
|
|
if (is_string($item)) {
|
|
$val = trim($item, " \t\n\r\0\x0B[]\"'");
|
|
if (!empty($val)) {
|
|
$clean[] = $val;
|
|
}
|
|
}
|
|
}
|
|
return array_values(array_unique($clean));
|
|
}
|
|
|
|
// Auth-Logic
|
|
public function authenticate($email, $password) {
|
|
$this->db->query("SELECT * FROM home_users WHERE email = :email AND status = 'Active'");
|
|
$this->db->bind(':email', $email);
|
|
$user = $this->db->single();
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$user['projects'] = self::parseProjects($user['projects'] ?? []);
|
|
if (in_array(strtolower($user['email']), ['philipp.urbschat@gmail.com', 'hi@philippurbschat.de'])) {
|
|
$user['is_admin'] = true;
|
|
} else {
|
|
$user['is_admin'] = (bool)$user['is_admin'];
|
|
}
|
|
return $user;
|
|
}
|
|
return false;
|
|
}
|
|
// Data-Retrieval
|
|
public function getAll() {
|
|
$this->db->query("SELECT id, name, email, is_admin, status, projects FROM home_users ORDER BY id DESC");
|
|
$users = $this->db->resultSet();
|
|
foreach ($users as &$user) {
|
|
$user['projects'] = self::parseProjects($user['projects'] ?? []);
|
|
}
|
|
return $users;
|
|
}
|
|
public function getById($id) {
|
|
$this->db->query("SELECT * FROM home_users WHERE id = :id");
|
|
$this->db->bind(':id', $id);
|
|
$user = $this->db->single();
|
|
if ($user) {
|
|
$user['projects'] = self::parseProjects($user['projects'] ?? []);
|
|
}
|
|
return $user;
|
|
}
|
|
// Persistence
|
|
public function save($data, $id = null) {
|
|
$projectsList = self::parseProjects($data['projects'] ?? []);
|
|
$projectsJson = json_encode($projectsList);
|
|
if ($id) {
|
|
$sql = "UPDATE home_users SET name = :name, email = :email, projects = :projects, is_admin = :is_admin, status = :status ";
|
|
if (!empty($data['password'])) {
|
|
$sql .= ", password = :password ";
|
|
}
|
|
$sql .= "WHERE id = :id";
|
|
$this->db->query($sql);
|
|
$this->db->bind(':id', $id);
|
|
} else {
|
|
$sql = "INSERT INTO home_users (name, email, password, projects, is_admin, status) VALUES (:name, :email, :password, :projects, :is_admin, :status)";
|
|
$this->db->query($sql);
|
|
}
|
|
$this->db->bind(':name', $data['name']);
|
|
$this->db->bind(':email', $data['email']);
|
|
$this->db->bind(':projects', $projectsJson);
|
|
$this->db->bind(':is_admin', !empty($data['is_admin']) ? 1 : 0);
|
|
$this->db->bind(':status', $data['status']);
|
|
if (!$id || !empty($data['password'])) {
|
|
$this->db->bind(':password', password_hash($data['password'], PASSWORD_DEFAULT));
|
|
}
|
|
return $this->db->execute();
|
|
}
|
|
public function delete($id) {
|
|
$this->db->query("DELETE FROM home_users WHERE id = :id");
|
|
$this->db->bind(':id', $id);
|
|
return $this->db->execute();
|
|
}
|
|
|
|
public function findByEmail(string $email): ?array {
|
|
$this->db->query("SELECT * FROM home_users WHERE LOWER(email) = LOWER(:email)");
|
|
$this->db->bind(':email', trim($email));
|
|
$user = $this->db->single();
|
|
if ($user) {
|
|
$user['projects'] = self::parseProjects($user['projects'] ?? []);
|
|
}
|
|
return $user ?: null;
|
|
}
|
|
|
|
public function updatePassword(int $id, string $newPasswordHash): bool {
|
|
$this->db->query("UPDATE home_users SET password = :password WHERE id = :id");
|
|
$this->db->bind(':password', $newPasswordHash);
|
|
$this->db->bind(':id', $id);
|
|
return $this->db->execute();
|
|
}
|
|
} |