Skip to content

Commit 7af06d5

Browse files
committed
Fix redirection errors
1 parent 41dd866 commit 7af06d5

7 files changed

Lines changed: 49 additions & 17 deletions

File tree

app/Controllers/PostsController.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function edit($request, $response, $args){
101101
$post = Post::find( $args['id']);
102102

103103
//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) ){
104+
if(($this->auth->user()->id != $post->user_id) AND ($this->auth->user()->role_id > 2 ) ){
105105

106106
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
107107

@@ -150,6 +150,17 @@ public function edit($request, $response, $args){
150150
*/
151151
public function delete($request, $response, $args){
152152
$user = Post::find( $args['id']);
153+
154+
//only owner and admin can delete
155+
if(($this->auth->user()->id != $post->user_id) AND ($this->auth->user()->role_id > 2 ) ){
156+
157+
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
158+
159+
return $this->view->render($response,'posts/view.twig', ['post'=>$post]);
160+
161+
}
162+
163+
153164
if($user->delete()){
154165
$this->flash->addMessage('success', 'Post Deleted Successfully');
155166
return $response->withRedirect($this->router->pathFor('posts.index', ['user_id'=>$this->auth->user()->id]));

app/Controllers/RolesController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public function view($request, $response, $args){
4848
*/
4949
public function add($request, $response){
5050

51+
if($this->auth->user()->role_id > 2 ){
52+
53+
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
54+
55+
return $this->view->render($response,'/');
56+
57+
}
58+
5159
if($request->isPost()){
5260

5361
/**
@@ -133,6 +141,16 @@ public function edit($request, $response, $args){
133141
*/
134142
public function delete($request, $response, $args){
135143
$user = Role::find( $args['id']);
144+
//only admin can delete
145+
if($this->auth->user()->role_id > 2 ){
146+
147+
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
148+
149+
return $this->view->render($response,'/');
150+
151+
}
152+
153+
136154
if($user->delete()){
137155
$this->flash->addMessage('success', 'Role Deleted Successfully');
138156
return $response->withRedirect($this->router->pathFor('roles.index'));

app/Controllers/UsersController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function edit($request, $response, $args){
4545
$user = User::find( $args['id']);
4646

4747
//only admin and the person that created the post can edit or delete this profile.
48-
if(($this->auth->user()->id != $args['id']) OR ($this->auth->user()->role_id < 3) ){
48+
if(($this->auth->user()->id != $args['id']) AND ($this->auth->user()->role_id > 2) ){
4949

5050
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
5151
return $this->view->render($response,'users/view.twig', ['id'=>$args['id']]);
@@ -97,7 +97,17 @@ public function edit($request, $response, $args){
9797
* @return
9898
*/
9999
public function delete($request, $response, $args){
100-
$user = User::find( $args['id']);
100+
$user = User::find( $args['id']);
101+
102+
//only owner and admin can delete
103+
if(($this->auth->user()->id != $args['id']) AND ($this->auth->user()->role_id > 2) ){
104+
105+
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
106+
return $this->view->render($response,'users/view.twig', ['id'=>$args['id']]);
107+
108+
}
109+
110+
101111
if($user->delete()){
102112
$this->flash->addMessage('success', 'User Account Deleted Successfully');
103113
return $response->withRedirect($this->router->pathFor('home'));

bin/src/templates/Controllers.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class BooksController extends Controller{
1010

1111
/**
12-
* List all users
12+
* List all books
1313
*
1414
* @return
1515
*/
@@ -53,7 +53,7 @@ public function view($request, $response, $args){
5353
*/
5454
public function add($request, $response, $args){
5555

56-
if($request->isBook()){
56+
if($request->isPost()){
5757

5858
/**
5959
* validate input before submission
@@ -149,10 +149,10 @@ public function edit($request, $response, $args){
149149
* @return
150150
*/
151151
public function delete($request, $response, $args){
152-
$user = Book::find( $args['id']);
152+
$book = Book::find( $args['id']);
153153
if($user->delete()){
154154
$this->flash->addMessage('success', 'Book Deleted Successfully');
155-
return $response->withRedirect($this->router->pathFor('books.index', ['user_id'=>$this->auth->user()->id]));
155+
return $response->withRedirect($this->router->pathFor('books.index'));
156156
}
157157
}
158158

bin/src/templates/Views/add.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<div class="panel-body">
2424
<div class="login-form" >
25-
<form action="{{ path_for('books.add') }}" method="book" autocomplete="off">
25+
<form action="{{ path_for('books.add') }}" method="post" autocomplete="off">
2626
<div class="form-group {{ errors.title ? 'has-error' : '' }}">
2727
<input type="text" class="form-control login-field" name="title" placeholder="Enter your title" >
2828
<label class="login-field-icon fui-user" for="login-first-name"></label>

bin/src/templates/Views/edit.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<div class="panel-body">
2424
<div class="login-form" >
25-
<form action="{{ path_for('books.edit', {'id': book.id}) }}" method="book" autocomplete="off">
25+
<form action="{{ path_for('books.edit', {'id': book.id}) }}" method="post" autocomplete="off">
2626
<div class="form-group {{ errors.title ? 'has-error' : '' }}">
2727
<input type="text" class="form-control login-field" value="{{ book.title }}" name="title" placeholder="Enter your title" >
2828
<label class="login-field-icon fui-user" for="login-first-name"></label>

resources/views/users/edit.twig

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,14 @@
5252
{% endif %}
5353
</div>
5454
<div class="form-group {{ errors.phone ? 'has-error' : '' }}">
55-
<input type="phone" class="form-control login-field" value="{{ old.phone }}" name="phone" placeholder="Enter your email" id="login-email">
55+
<input type="phone" class="form-control login-field" value="{{ old.phone }}" name="phone" placeholder="Enter your phone" id="login-email">
5656
<label class="login-field-icon fui-user" for="phone"></label>
5757

5858
{% if errors.email %}
5959
<span class="help-block">{{ errors.phone | first }}</span>
6060
{% endif %}
6161
</div>
6262

63-
<div class="form-group">
64-
<input type="password" class="form-control login-field" value="" name="password" placeholder="Password" id="login-pass">
65-
<label class="login-field-icon fui-lock" for="login-pass"></label>
66-
{% if errors.password %}
67-
<span class="help-block">{{ errors.password | first }}</span>
68-
{% endif %}
69-
</div>
7063

7164
<button class="btn btn-primary btn-lg btn-block" type="submit">Sign up</button>
7265

0 commit comments

Comments
 (0)