Skip to content

Commit c85e2b7

Browse files
committed
rewriting some stufff
1 parent 384a84f commit c85e2b7

File tree

12 files changed

+278
-25
lines changed

12 files changed

+278
-25
lines changed

src/classes/controllers/AuthController.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,66 @@
11
<?php
22
class AuthController extends Controller {
33
private $authmodel;
4+
private $userModel;
45

56
public function __construct() {
67
$this->authModel = $this->model('Auth');
8+
$this->userModel = $this->model('User');
79
}
10+
811
public function index() {
912
$this->login();
1013
}
1114

15+
public function register() {
16+
if(!isset($_SESSION['loggedIn'])) {
17+
header('Location: ' . ROOT_PATH);
18+
exit();
19+
}
20+
21+
$msg = null;
22+
$username = '';
23+
$password = '';
24+
25+
if(isset($_POST['submitInput'])) {
26+
if(empty($_POST['usernameInput'])) {
27+
$msg[] = "Field username is empty.";
28+
} else {
29+
$username = $_POST['usernameInput'];
30+
}
31+
if(empty($_POST['passwordInput'])) {
32+
$msg[] = "Field password is empty.";
33+
} else {
34+
$password = $_POST['passwordInput'];
35+
}
36+
if($this->userModel->existsUname($username)) {
37+
$msg[] = 'User already exists';
38+
}
39+
if($msg == null) {
40+
$password = password_hash($password, PASSWORD_DEFAULT);
41+
$username = htmlentities($username, ENT_QUOTES, 'utf-8');
42+
$username = trim($username);
43+
$username = str_replace(' ', '', $username);
44+
45+
$this->authModel->register($username, $password);
46+
header('Location: ' . ROOT_PATH . 'users/users');
47+
}
48+
}
49+
50+
$data = array(
51+
'username' => $username,
52+
'msg' => $msg,
53+
'backPath' => 'users/users',
54+
'actionName' => 'Register'
55+
);
56+
57+
$this->render('auth/register', $data);
58+
59+
}
60+
1261
public function login() {
1362
if(isset($_SESSION['loggedIn'])) {
14-
Linker::link('index', 'index');
63+
header('Location: /index/index');
1564
exit();
1665
}
1766

@@ -34,7 +83,7 @@ public function login() {
3483
$login = $this->authModel->login($username, $password);
3584
if($login !== false) {
3685
$_SESSION['loggedIn'] = $login;
37-
header('Location: ' . Linker::link('index', 'index'));
86+
header('Location: /index/index');
3887
exit();
3988
} else {
4089
$msg[] = "Login failed, password or username incorrect.";
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,116 @@
11
<?php
22
class UsersController extends Controller {
3+
private $userModel;
4+
private $postModel;
5+
6+
public function __construct() {
7+
$this->userModel = $this->model('User');
8+
$this->postModel = $this->model('Post');
9+
}
10+
311
public function index() {
12+
if(!isset($_SESSION['loggedIn'])) {
13+
header('Location: ' . ROOT_PATH);
14+
exit();
15+
}
16+
17+
$users = $this->userModel->getAllUsers();
18+
19+
$data = array(
20+
'usersData' => $users
21+
);
422

23+
$this->render('users/index', $data);
24+
}
25+
26+
public function edit($id) {
27+
if(!isset($_SESSION['loggedIn'])) {
28+
header('Location: ' . ROOT_PATH);
29+
exit();
30+
}
31+
if($id === null || !is_numeric($id)) {
32+
header('Location: ' . ROOT_PATH . 'users/users');
33+
exit();
34+
}
35+
$id = filter_var($id, FILTER_SANITIZE_NUMBER_INT);
36+
37+
$msg = null;
38+
$username = '';
39+
$password = '';
40+
41+
if(!$this->userModel->existsId($id)) {
42+
header('Location: ' . ROOT_PATH . 'users/users');
43+
exit();
44+
}
45+
46+
$user = $this->userModel->getUserById($id);
47+
48+
if(isset($_POST['submitInput'])) {
49+
if(empty($_POST['usernameInput'])) {
50+
$msg[] = "Field username is empty.";
51+
} else {
52+
$username = $_POST['usernameInput'];
53+
}
54+
if(empty($_POST['passwordInput'])) {
55+
$password = '';
56+
} else {
57+
$password = $_POST['passwordInput'];
58+
}
59+
60+
if($msg == null) {
61+
$username = htmlentities($username, ENT_QUOTES, "UTF-8");
62+
63+
$password = $password == '' ? $user->password : password_hash($password, PASSWORD_DEFAULT);
64+
65+
$this->userModel->update($username, $password, $id);
66+
header('Location: ' . ROOT_PATH . 'users/users');
67+
exit();
68+
}
69+
}
70+
71+
$data = array(
72+
'msg' => $msg,
73+
'id' => $user->id,
74+
'username' => $user->username,
75+
'backPath' => 'users/users',
76+
'actionName' => 'Edit'
77+
);
78+
79+
$this->render('users/edit', $data);
80+
}
81+
82+
public function delete($id) {
83+
if(!isset($_SESSION['loggedIn'])) {
84+
header('Location: ' . ROOT_PATH);
85+
exit();
86+
}
87+
if($id === null || !is_numeric($id)) {
88+
header('Location: ' . ROOT_PATH);
89+
exit();
90+
}
91+
$id = filter_var($id, FILTER_SANITIZE_NUMBER_INT);
92+
93+
if(!$this->userModel->existsId($id)) {
94+
header('Location: ' . ROOT_PATH);
95+
exit();
96+
}
97+
if($id == $_SESSION['loggedIn'] || $id == 1) {
98+
header('Location: ' . ROOT_PATH . 'users/users');
99+
exit();
100+
}
101+
102+
if(isset($_POST['sure'])) {
103+
$this->postModel->deleteAllPostsFromUserId($id);
104+
$this->userModel->delete($id);
105+
106+
header('Location: ' . ROOT_PATH);
107+
}
108+
109+
$data = array(
110+
'id' => $id,
111+
'backPath' => 'users/users'
112+
);
113+
114+
$this->render('users/delete', $data);
5115
}
6116
}

src/classes/models/AuthModel.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<?php
22
class AuthModel extends Model {
33
public function login($uname, $passwd) {
4-
$user = $this->getUser($uname);
4+
$this->db->query('SELECT id, password FROM users WHERE username LIKE :uname;');
5+
$this->db->bind(':uname', $uname);
6+
$user = $this->db->single();
7+
8+
if($this->db->rowCount() <= 0) {
9+
return false;
10+
}
511

612
if($user == null) return false;
713

@@ -14,17 +20,13 @@ public function login($uname, $passwd) {
1420
}
1521
}
1622

17-
// PRIV
18-
19-
private function getUser($uname) {
20-
$this->db->query('SELECT id, password FROM users WHERE username LIKE :uname;');
21-
$this->db->bind(':uname', $uname);
22-
$row = $this->db->single();
23+
public function register($uname, $passwd) {
24+
$this->db->query('INSERT INTO users (username, password) VALUES (:username, :password);');
25+
$this->db->bind(':username', $uname);
26+
$this->db->bind(':password', $passwd);
27+
$this->db->execute();
2328

24-
if($this->db->rowCount() <= 0) {
25-
return false;
26-
}
27-
28-
return $row;
29+
return true;
2930
}
31+
3032
}

src/classes/models/PostModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public function create($title, $body, $userId) {
7373
return $this->db->lastId();
7474
}
7575

76+
public function deleteAllPostsFromUserId($id) {
77+
$this->db->query('DELETE FROM posts WHERE user_fk LIKE :id;');
78+
$this->db->bind(':id', $id);
79+
$this->db->execute();
80+
81+
return true;
82+
}
83+
7684
public function update($title, $body, $id) {
7785
$this->db->query('UPDATE posts SET title = :title, body = :body WHERE id LIKE :id');
7886
$this->db->bind(':title', $title);

src/classes/models/UserModel.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,52 @@
11
<?php
22
class UserModel extends Model {
3+
public function getAllUsers() {
4+
$this->db->query('SELECT id, username FROM users');
5+
$results = $this->db->resultSet();
6+
7+
return $this->db->rowCount() > 0 ? $results : null;
8+
}
9+
310
public function getUserById($id) {
4-
$user = $this->getUserByIdFromDb($id);
5-
unset($user['password']);
11+
$this->db->query('SELECT id, username, password FROM users WHERE id LIKE :id;');
12+
$this->db->bind(':id', $id);
13+
$user = $this->db->single();
614

715
return $user;
816
}
917

10-
// PRIV
18+
public function existsUname($uname) {
19+
$this->db->query('SELECT id FROM users WHERE username LIKE :username;');
20+
$this->db->bind(':username', $uname);
21+
$this->db->execute();
22+
23+
return $this->db->rowCount() >= 1 ? true : false;
24+
}
1125

12-
private function getUserByIdFromDb($id) {
13-
$this->db->query('SELECT id, username, password FROM users WHERE id LIKE :id;');
26+
public function existsId($id) {
27+
$this->db->query('SELECT id FROM users WHERE id LIKE :id;');
1428
$this->db->bind(':id', $id);
15-
$user = $this->db->single();
29+
$this->db->execute();
30+
31+
return $this->db->rowCount() >= 1 ? true : false;
32+
}
1633

17-
return $user;
34+
public function delete($id) {
35+
$this->db->query('DELETE FROM users WHERE id LIKE :id;');
36+
$this->db->bind(':id', $id);
37+
$this->db->execute();
38+
39+
return true;
40+
}
41+
42+
43+
public function update($uname, $passwd, $id) {
44+
$this->db->query('UPDATE users SET username = :username, password = :password WHERE id LIKE :id;');
45+
$this->db->bind(':username', $uname);
46+
$this->db->bind(':password', $passwd);
47+
$this->db->bind(':id', $id);
48+
$this->db->execute();
49+
50+
return true;
1851
}
1952
}

src/templates/loginForm.php renamed to src/templates/userForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="col-md-4 offset-md-4">
33
<div class="login-form bg-dark mt-4 p-4">
44
<form method="POST" class="row g-3">
5-
<h4>Login</h4>
5+
<h4><?= $action?></h4>
66
<?php Template::load('alert', ['type' => 'danger', 'msg' => $msg]);?>
77
<div class="col-12">
88
<label>Username</label>
@@ -13,7 +13,7 @@
1313
<input type="password" name="passwordInput" class="form-control" placeholder="Password">
1414
</div>
1515
<div class="col-12">
16-
<button type="submit" name="submitInput" class="btn btn-primary float-end">Login</button>
16+
<button type="submit" name="submitInput" class="btn btn-primary float-end"><?= $action?></button>
1717
</div>
1818
</form>
1919
</div>

src/views/auth/login.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?= Template::load('header', ['title' => 'Login']);?>
22

3-
<?= Template::load('loginForm', ['username' => $data['username'], 'msg' => $data['msg']]);?>
3+
<?= Template::load('userForm', ['username' => $data['username'], 'msg' => $data['msg'], 'action' => 'Login']);?>
44

55
<?= Template::load('footer');?>

src/views/auth/register.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?= Template::load('header', ['title' => 'Register']);?>
2+
3+
<?= Template::load('userForm', ['username' => $data['username'], 'msg' => $data['msg'], 'action' => 'Register']);?>
4+
5+
<?= Template::load('footer');?>

src/views/index/imprint.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/views/users/delete.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?= Template::load('header', ['title' => 'Home']);?>
2+
3+
<?= Template::load('backBtn', ['controller' => 'users', 'method' => 'index', 'args' => ''])?>
4+
<?= Template::load('delete', ['actionName' => 'User', 'id' => $data['id']]);?>
5+
6+
<?= Template::load('footer');?>

src/views/users/edit.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?= Template::load('header', ['title' => 'Edit']);?>
2+
3+
<?= Template::load('userForm', ['username' => $data['username'], 'msg' => $data['msg'], 'action' => 'Edit']);?>
4+
5+
<?= Template::load('footer');?>

0 commit comments

Comments
 (0)