Changed login/logout process
This commit is contained in:
@@ -20,84 +20,4 @@ class Access extends \Core\Model
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function showAll()
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT id, username, full_name, registered_at FROM user";
|
||||
|
||||
if ($stmt = $db->query($sql)) {
|
||||
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return $result;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function showSingle($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT id, username, full_name, registered_at, privilage FROM user WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
if ($query->rowCount() === 1) {
|
||||
$result = $query->fetch(\PDO::FETCH_ASSOC);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function login($args = [])
|
||||
{
|
||||
try {
|
||||
$username = htmlspecialchars($args['username']);
|
||||
$password = $args['password'];
|
||||
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT id, password, salt FROM user WHERE username = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(1, $username);
|
||||
|
||||
if ($query->execute()) {
|
||||
if ($query->rowCount() === 1) {
|
||||
$result = $query->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
$id = $result['id'];
|
||||
$salt = $result['salt'];
|
||||
$hash = $result['password'];
|
||||
|
||||
if (\Core\Hash::compare($password, $salt, $hash)) {
|
||||
$user = $this->showSingle($id);
|
||||
|
||||
return $user;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function logout($id)
|
||||
{
|
||||
if ($this->update('user', ['status' => 0], $id)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,76 +35,6 @@ class Post extends \Core\Model
|
||||
);
|
||||
}
|
||||
|
||||
public function showAll($conditions = [])
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT * FROM pengumuman";
|
||||
|
||||
if ($conditions) {
|
||||
$sql .= " WHERE";
|
||||
foreach ($conditions as $condition) {
|
||||
|
||||
$keys[] = $condition[0];
|
||||
$operators[] = $condition[1];
|
||||
$values[] = $condition[2];
|
||||
}
|
||||
|
||||
$x = 1;
|
||||
$i = 0;
|
||||
foreach ($keys as $key) {
|
||||
$sql .= " $key $operators[$i] ?";
|
||||
$i++;
|
||||
|
||||
$x++;
|
||||
if ($x <= count($keys)) {
|
||||
$sql .= " AND";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($conditions) {
|
||||
$x = 1;
|
||||
foreach ($values as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
if ($query->rowCount() != 0) {
|
||||
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage, 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function showSingle($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT * FROM pengumuman WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
if ($query->rowCount() === 1) {
|
||||
$result = $query->fetch(\PDO::FETCH_ASSOC);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function showCategories()
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user