Skip to content

Commit 1872a91

Browse files
committed
Added Posts
1 parent 6cb4cbc commit 1872a91

42 files changed

Lines changed: 1685 additions & 180 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.htaccess

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
# Uncomment the following to prevent the httpoxy vulnerability
2+
# See: https://httpoxy.org/
3+
#<IfModule mod_headers.c>
4+
# RequestHeader unset Proxy
5+
#</IfModule>
6+
17
<IfModule mod_rewrite.c>
2-
RewriteEngine on
3-
# Uncomment if you have a .well-known directory in the root folder, e.g. for the Let's Encrypt challenge
4-
# https://tools.ietf.org/html/rfc5785
5-
#RewriteRule ^(\.well-known/.*)$ $1 [L]
6-
RewriteRule ^$ public/ [L]
7-
RewriteRule (.*) public/$1 [L]
8+
RewriteEngine on
9+
RewriteRule ^$ public/ [L]
10+
RewriteRule (.*) public/$1 [L]
811
</IfModule>

app/Controllers/Auth/AuthController.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,40 @@ public function getSignout(){
2121

2222
}
2323

24-
/**
25-
* Display sign in page
26-
*
27-
* @return
28-
*/
29-
public function getSignin($request , $response){
30-
31-
return $this->view->render($response,'auth/signin.twig');
32-
}
24+
3325

3426
/**
3527
* Display sign in page
3628
*
3729
* @return
3830
*/
39-
public function postSignin($request, $response){
31+
public function signin($request, $response){
4032

41-
$auth = $this->auth->attempt(
42-
$request->getParam('email'),
43-
$request->getParam('password')
44-
);
45-
46-
if(!$auth){
47-
$this->flash->addMessage('error', 'Login Failed!'); //You can also use error, info, warning
48-
49-
return $response->withRedirect($this->router->pathFor('auth.signin'));
50-
};
51-
52-
$this->flash->addMessage('success', 'Logiin successful!'); //You can also use error, info, warning
33+
if($request->isPost()){
34+
35+
//Attempt to log user in
36+
$auth = $this->auth->attempt(
37+
$request->getParam('email'),
38+
$request->getParam('password')
39+
);
40+
41+
//if login fails
42+
if(!$auth){
43+
$this->flash->addMessage('error', 'Login Failed!'); //You can also use error, info, warning
44+
return $response->withRedirect($this->router->pathFor('auth.signin'));
45+
};
46+
47+
$this->flash->addMessage('success', 'Login successful!'); //You can also use error, info, warning
48+
49+
return $response->withRedirect($this->router->pathFor('home'));
50+
}
51+
52+
return $this->view->render($response,'auth/signin.twig');
5353

54-
return $response->withRedirect($this->router->pathFor('home'));
5554
}
5655

5756
/**
58-
* Render Signin view
57+
* Render Signin up
5958
* @param get $request
6059
*
6160
*
@@ -89,7 +88,7 @@ public function postSignup($request , $response){
8988

9089
//redirect if validation fails
9190
if($validation->failed()){
92-
$this->flash->addMessage('error', 'Signup Failed'); //You can also use error, info, warning
91+
$this->flash->addMessage('error', 'Signup Failed!'); //You can also use error, info, warning
9392

9493
return $response->withRedirect($this->router->pathFor('auth.signup'));
9594
}
@@ -100,7 +99,7 @@ public function postSignup($request , $response){
10099
'last_name' => $request->getParam('last_name'),
101100
'email' => $request->getParam('email'),
102101
//harsh password with PHPs inbuilt password harsher
103-
'password' => password_hash($request->getParam('email'), PASSWORD_DEFAULT) //PASSWORD_BCRYPT is also available
102+
'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT) //PASSWORD_BCRYPT is also available
104103
]);
105104

106105
$this->flash->addMessage('success', 'Signup successful'); //You can also use error, info, warning

app/Controllers/Auth/PasswordController.php

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
*/
1111
class PasswordController extends Controller{
1212

13-
public function getChangePassword($request , $response){
14-
15-
return $this->view->render($response, 'auth/password/change.twig');
16-
}
13+
1714

1815
/**
1916
*
@@ -22,35 +19,48 @@ public function getChangePassword($request , $response){
2219
*
2320
* @return
2421
*/
25-
public function postChangePassword(($request , $response){
22+
public function changePassword($request , $response){
2623

27-
/**
28-
* validate input before submission
29-
* @var
30-
*
31-
*/
32-
$validation = $this->validator->validate($request, [
33-
'old_password' => v::notEmpty()->matchesPassword($this->auth->user()->password), //from the custom validation rule defined in App\Validation\Rules and Exception
34-
'password' => v::notEmpty(),
35-
]);
36-
37-
//redirect if validation fails
38-
if($validation->failed()){
39-
$this->flash->addMessage('error', 'Password Change Attempt Failed'); //You can also use error, info, warning
40-
41-
return $response->withRedirect($this->router->pathFor('auth.password.change'));
42-
}
43-
24+
25+
if($request->isPost()){
26+
/**
27+
* validate input before submission
28+
* @var
29+
*
30+
*/
31+
$validation = $this->validator->validate($request, [
32+
'old_password' => v::notEmpty()->matchesPassword($this->auth->user()->password), //from the custom validation rule defined in App\Validation\Rules and Exception
33+
'new_password1' => v::notEmpty()->fieldsMatch($request->getParam('new_password2'), $request->getParam('new_password1')),
34+
'new_password2' => v::notEmpty(),
35+
]);
36+
37+
//redirect if validation fails
38+
if($validation->failed()){
39+
$this->flash->addMessage('error', 'Password Change Attempt Failed'); //You can also use error, info, warning
40+
41+
return $response->withRedirect($this->router->pathFor('auth.password.change'));
42+
}
43+
44+
$this->auth->user()->setPassword($request->getParam('new_password1'));
45+
46+
$this->flash->addMessage('success', 'Password change successful');
47+
48+
49+
return $response->withRedirect($this->router->pathFor('home'));
4450

45-
$this->auth->user()->setPassword($request->getParam('password'));
51+
}
4652

47-
$this->flash->addMessage('success', 'Password change successful'); //You can also use error, info, warning
48-
49-
50-
51-
52-
return $response->withRedirect($this->router->pathFor('home'));
53-
53+
return $this->view->render($response, 'auth/password/change.twig');
5454

5555
}
56+
57+
/**
58+
* Method to reset password for users who are logged out
59+
*/
60+
public function resetPassword(){
61+
// There are many ways to do this, but the way we will use here is
62+
// 1. Generate Random Password
63+
// 2. Encrypt it and update the database
64+
// 2. Send email containing the new password to the user
65+
}
5666
}

app/Controllers/HomeController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public function index($request, $response){
1414
return $this->view->render($response,'home.twig');
1515
}
1616

17+
18+
public function about($request, $response){
19+
20+
return $this->view->render($response,'about.twig');
21+
}
1722
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
use App\Controllers\Controller;
5+
use App\Models\User;
6+
use Respect\Validation\Validator as v;
7+
use App\Models\Post;
8+
9+
class PostsController extends Controller{
10+
11+
/**
12+
* List all users
13+
*
14+
* @return
15+
*/
16+
public function index($request, $response, $args){
17+
18+
//find all posts
19+
if(isset($args['user_id'])){
20+
$posts = Post::where('user_id',$args['user_id'] )->get();
21+
//get the user's details
22+
$user = User::find($args['user_id']);
23+
24+
return $this->view->render($response,'posts/index.twig', ['posts'=>$posts, 'user'=>$user]);
25+
}else{
26+
$posts = Post::all();
27+
return $this->view->render($response,'posts/index.twig', ['posts'=>$posts]);
28+
}
29+
30+
}
31+
32+
33+
34+
/**
35+
* Display a post
36+
*
37+
* @return
38+
*/
39+
public function view($request, $response, $args){
40+
41+
$post = Post::find( $args['id']);
42+
43+
return $this->view->render($response,'posts/view.twig', ['post'=>$post]);
44+
45+
}
46+
47+
48+
49+
/**
50+
* Create A New Post
51+
*
52+
* @return
53+
*/
54+
public function add($request, $response, $args){
55+
56+
if($request->isPost()){
57+
58+
/**
59+
* validate input before submission
60+
* @var
61+
*
62+
*/
63+
$validation = $this->validator->validate($request, [
64+
'title' => v::notEmpty(),
65+
'body' => v::notEmpty(),
66+
]);
67+
68+
69+
//redirect if validation fails
70+
if($validation->failed()){
71+
$this->flash->addMessage('error', 'Validation Failed!');
72+
73+
return $response->withRedirect($this->router->pathFor('posts/add.twig'));
74+
}
75+
76+
$post = Post::create([
77+
'title' => $request->getParam('title'),
78+
'body' => $request->getParam('body'),
79+
'user_id' => $this->auth->user()->id,
80+
]);
81+
82+
$this->flash->addMessage('success', 'Post Added Successfully');
83+
//redirect to eg. posts/view/8
84+
return $response->withRedirect($this->router->pathFor('posts.view', ['id'=>$post->id]));
85+
86+
}
87+
return $this->view->render($response,'posts/add.twig');
88+
89+
}
90+
91+
92+
93+
/**
94+
* Edit post
95+
*
96+
* @return
97+
*/
98+
public function edit($request, $response, $args){
99+
100+
//find the post
101+
$post = Post::find( $args['id']);
102+
103+
//only admin and the person that created the post can edit or delete it.
104+
if(($this->auth->user()->id != $post->user_id) OR ($this->auth->user()->role_id < 3) ){
105+
106+
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
107+
108+
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
109+
110+
}
111+
112+
//if form was submitted
113+
if($request->isPost()){
114+
115+
$validation = $this->validator->validate($request, [
116+
'title' => v::notEmpty(),
117+
'body' => v::notEmpty(),
118+
]);
119+
//redirect if validation fails
120+
if($validation->failed()){
121+
$this->flash->addMessage('error', 'Validation Failed!');
122+
123+
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
124+
}
125+
126+
//save Data
127+
$post = Post::where('id', $args['id'])
128+
->update([
129+
'title' => $request->getParam('title'),
130+
'body' => $request->getParam('body')
131+
]);
132+
133+
if($post){
134+
$this->flash->addMessage('success', 'Post Updated Successfully');
135+
//redirect to eg. posts/view/8
136+
return $response->withRedirect($this->router->pathFor('posts.view', ['id'=>$args['id']]));
137+
}
138+
}
139+
140+
141+
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
142+
143+
}
144+
145+
146+
/**
147+
* Delete a post
148+
*
149+
* @return
150+
*/
151+
public function delete($request, $response, $args){
152+
$user = Post::find( $args['id']);
153+
if($user->delete()){
154+
$this->flash->addMessage('success', 'Post Deleted Successfully');
155+
return $response->withRedirect($this->router->pathFor('posts.index', ['user_id'=>$this->auth->user()->id]));
156+
}
157+
}
158+
159+
}

0 commit comments

Comments
 (0)