Skip to content

Commit ce70e1d

Browse files
committed
Added user settings view
1 parent d1aa09f commit ce70e1d

File tree

8 files changed

+143
-22
lines changed

8 files changed

+143
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
.DS_Store
3+
*.icloud
34

45
###> symfony/framework-bundle ###
56
/.env.local

README.MD

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pastebin
2-
Simply pastebin coded for education purposes
2+
Simply pastebin coded for education purposes. It looks like it works.
33

44
## Used technologies
55
- PHP 7
@@ -9,6 +9,4 @@ Simply pastebin coded for education purposes
99
- MySQL
1010

1111
## TODO
12-
- User panel
13-
- Syntax highlight
14-
- Bug fixes and refactoring
12+
- Avatars

public/css/main.css

+23-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/main.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/main.scss

+26-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ form {
5454
align-items: flex-start;
5555
}
5656

57-
input[type=text] {
57+
input[type=text], input[type=password] {
5858
border-radius: 2px;
5959
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
6060
border: 1px solid #AAA;
@@ -206,4 +206,28 @@ table {
206206
tr {
207207
text-align: center;
208208
}
209-
}
209+
}
210+
211+
/* Panel view */
212+
#panel {
213+
@extend .flex;
214+
flex-direction: row;
215+
justify-content: space-between;
216+
padding-left: 10%;
217+
padding-right: 10%;
218+
219+
img {
220+
width: 128px;
221+
height: 128px;
222+
}
223+
224+
h2, h3 {
225+
margin: 3px;
226+
}
227+
}
228+
229+
#password-error {
230+
color: #C03;
231+
width: 100%;
232+
text-align: center;
233+
}

src/Controller/PanelController.php

+47-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace App\Controller;
44

5+
use App\Entity\User;
56
use App\Repository\PasteRepository;
67
use Doctrine\ORM\EntityManagerInterface;
78
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
89
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\Routing\Annotation\Route;
12+
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1113

1214
/**
1315
* @Route("/panel")
@@ -18,9 +20,10 @@ class PanelController extends AbstractController {
1820
* @IsGranted("ROLE_USER")
1921
* @Route("/", name="app_panel")
2022
*/
21-
public function root(PasteRepository $pasteRepository) {
23+
public function root(Request $request, PasteRepository $pasteRepository) {
2224
return $this->render("panel/panel.html.twig", [
23-
'sidebar' => $pasteRepository->getSidebar()
25+
'sidebar' => $pasteRepository->getSidebar(),
26+
'password' => $request->get('password')
2427
]);
2528
}
2629

@@ -30,6 +33,9 @@ public function root(PasteRepository $pasteRepository) {
3033
*/
3134
public function pastes(Request $request, PasteRepository $pasteRepository) {
3235
$user = $this->get('security.token_storage')->getToken()->getUser();
36+
if ($user == 'anon.') {
37+
return $this->redirectToRoute('app_root');
38+
}
3339

3440
return $this->render('panel/pastes.html.twig', [
3541
'sidebar' => $pasteRepository->getSidebar(),
@@ -45,6 +51,9 @@ public function pastes(Request $request, PasteRepository $pasteRepository) {
4551
public function delete(string $name, PasteRepository $pasteRepository, EntityManagerInterface $entityManager) {
4652
$paste = $pasteRepository->findOneByName($name);
4753
$user = $this->get('security.token_storage')->getToken()->getUser();
54+
if ($user == 'anon.') {
55+
return $this->redirectToRoute('app_root');
56+
}
4857

4958
if ($paste == null) {
5059
return $this->redirectToRoute('app_pastes', [
@@ -64,4 +73,40 @@ public function delete(string $name, PasteRepository $pasteRepository, EntityMan
6473
'removed' => true
6574
]);
6675
}
76+
77+
/**
78+
* @IsGranted("ROLE_USER")
79+
* @Route("/password/{id}", name="app_password")
80+
*/
81+
public function password(Request $request, int $id, EncoderFactoryInterface $encoderFactory, EntityManagerInterface $entityManager) {
82+
$user = $this->get('security.token_storage')->getToken()->getUser();
83+
if ($user == 'anon.' || $user->getId() != $id) {
84+
return $this->redirectToRoute('app_panel');
85+
}
86+
87+
$old = $request->get('old');
88+
$new = $request->get('new');
89+
$repeat = $request->get('repeat');
90+
91+
if ($old == null || $new == null || $repeat == null) {
92+
return $this->redirectToRoute('app_panel');
93+
}
94+
95+
if ($new != $repeat) {
96+
return $this->redirectToRoute('app_panel', ['password' => 2]);
97+
}
98+
99+
if (!$encoderFactory->getEncoder(User::class)->isPasswordValid($user->getPassword(), $old, $user->getSalt())) {
100+
return $this->redirectToRoute('app_panel', ['password' => 3]);
101+
}
102+
103+
if ($old != $new) {
104+
return $this->redirectToRoute('app_panel', ['password' => 4]);
105+
}
106+
$user->setPassword($encoderFactory->getEncoder(User::class)->encodePassword($new, $user->getSalt()));
107+
$entityManager->persist($user);
108+
$entityManager->flush();
109+
110+
return $this->redirectToRoute('app_panel', ['password' => 1]);
111+
}
67112
}

templates/base.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
<a href="{{ path('app_panel') }}"><i class="fas fa-cog"></i></a>
2424

2525
{% if app.user %}
26-
<a href="{{ path('app_panel') }}" id="username">{{ app.user.username }}</a>
27-
<a href="{{ path('app_panel') }}"><img src="{{ asset('images/guest.png') }}" alt=""/></a> <!-- TODO: User avatars -->
26+
<a href="{{ path('app_pastes') }}" id="username">{{ app.user.username }}</a>
27+
<a href="{{ path('app_pastes') }}"><img src="{{ asset('images/guest.png') }}" alt=""/></a> <!-- TODO: User avatars -->
2828
<a href="{{ path('app_logout') }}"><i class="fas fa-sign-out-alt"></i></a>
2929
{% else %}
3030
<a href="{{ path('app_login') }}" id="username">Guest user</a>

templates/panel/panel.html.twig

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
<head>
4-
<title>pastebin</title>
5-
</head>
6-
<body>
7-
Panel
8-
</body>
9-
</html>
1+
{% extends 'base.html.twig' %}
2+
{% block head %}
3+
<link rel="stylesheet" href="{{ asset('css/main.css') }}" />
4+
<title>Pastes list</title>
5+
{% endblock %}
6+
7+
{% block body %}
8+
<h1>Settings</h1>
9+
{% if password == 1 %}
10+
<h3 id="password-error">Password change successfully</h3>
11+
{% elseif password == 2 %}
12+
<h3 id="password-error">Password does not match</h3>
13+
{% elseif password == 3 %}
14+
<h3 id="password-error">Invalid old password</h3>
15+
{% elseif password == 4 %}
16+
<h3 id="password-error">Old password cant be the same as new</h3>
17+
{% endif %}
18+
<div id="panel">
19+
<div>
20+
<img src="{{ asset('images/guest.png') }}" alt="" />
21+
<h2>{{ app.user.username }}</h2>
22+
<h3>Join date: {{ app.user.registerDate.format('d M Y') }}</h3>
23+
<h3>Mail: {{ app.user.mail }}</h3>
24+
</div>
25+
<form method="post" action="{{ path('app_password', {id: app.user.id}) }}" >
26+
<div id="inputs" class="test">
27+
<div id="labels">
28+
<label for="old">Old password</label>
29+
<label for="new">New password</label>
30+
<label for="repeat">Repeat new password</label>
31+
</div>
32+
<div id="widgets">
33+
<input id="old" name="old" type="password" />
34+
<input id="new" name="new" type="password" />
35+
<input id="repeat" name="repeat" type="password" />
36+
<input type="submit" value="Change password" />
37+
</div>
38+
</div>
39+
</form>
40+
</div>
41+
{% endblock %}

0 commit comments

Comments
 (0)