Skip to content

Commit 8547dac

Browse files
committed
Use a login form instead of HTTP basic
1 parent 3f83050 commit 8547dac

File tree

6 files changed

+127
-10
lines changed

6 files changed

+127
-10
lines changed

app/Resources/views/base.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html>
33
<head>
44
<meta name="viewport" content=" minimum-scale=1.0, maximum-scale=1.0, width=device-width, user-scalable=no">
@@ -44,6 +44,10 @@
4444
<div class="navbar-right">
4545
<ul class="nav navbar-nav">
4646
<li><a href="{{ path('sonata_admin_dashboard') }}">Admin Dashboard</a></li>
47+
48+
{% if is_granted('ROLE_ADMIN') %}
49+
<li><a href="{{ path('logout') }}">Logout</a></li>
50+
{% endif %}
4751
</ul>
4852

4953
{% block language_selector %}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content=" minimum-scale=1.0, maximum-scale=1.0, width=device-width, user-scalable=no">
5+
<meta charset="utf-8" />
6+
7+
<title>Admin area login</title>
8+
9+
<link rel="stylesheet" href="{{ asset("assets/vendor/bootstrap.min.css") }}">
10+
<link rel="stylesheet" href="{{ asset("assets/css/style.css") }}">
11+
<style>
12+
body { background:#fafafa; }
13+
.login-form { margin-bottom:20px; }
14+
.login-form-box{
15+
width:430px;
16+
margin-top:200px;
17+
padding:20px;
18+
background:rgba(255, 255, 255, .8);
19+
border:1px solid #cee3ab;
20+
border-top:5px solid #a3ca5f;
21+
}
22+
</style>
23+
</head>
24+
<body>
25+
<div class="login-form-box center-block">
26+
<div class="alert alert-info clearfix">
27+
<p>This login form is using the Security component.</p>
28+
29+
<a class="docref" href="https://symfony.com/doc/current/security/form_login_setup"><i class="glyphicon glyphicon-chevron-right"></i>Read about this feature in the documentation.</a>
30+
</div>
31+
32+
<h3>Admin area</h3>
33+
34+
{% if error %}
35+
<div class="alert alert-danger">
36+
<span class="glyphicon glyphicon-exclamation-sign"></span>
37+
{{ error.messageKey|trans(error.messageData, 'security') }}
38+
</div>
39+
{% endif %}
40+
41+
<form action="{{ path('login') }}" method="post" class="form-horizontal clearfix login-form">
42+
<div class="form-group">
43+
<label for="username" class="col-sm-4">Username:</label>
44+
<div class="col-sm-8">
45+
<input type="text" class="form-control" id="username" name="_username" value="{{ last_username }}" />
46+
<span class="help-block">The demo username is "anna_admin".</span>
47+
</div>
48+
</div>
49+
50+
<div class="form-group">
51+
<label for="password" class="col-sm-4">Password:</label>
52+
<div class="col-sm-8">
53+
<input type="password" class="form-control" id="password" name="_password" />
54+
<span class="help-block">The demo password is "kitten".</span>
55+
</div>
56+
</div>
57+
58+
<div class="col-sm-offset-4 col-sm-8">
59+
<button type="submit" class="btn btn-primary">Login</button>
60+
</div>
61+
</form>
62+
</div>
63+
</body>
64+
</html>

app/config/config_test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ framework:
99
session:
1010
storage_id: session.storage.filesystem
1111

12+
security:
13+
firewalls:
14+
main:
15+
http_basic: ~
16+
1217
web_profiler:
1318
toolbar: false
1419
intercept_redirects: false

app/config/parameters.yml.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,3 @@ parameters:
2222
mailer_host: localhost
2323
mailer_user: ~
2424
mailer_password: ~
25-
26-
security_users:
27-
user: { password: user, roles: [ 'ROLE_USER' ] }
28-
admin: { password: admin, roles: [ 'ROLE_ADMIN' ] }

app/config/security.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
security:
2+
# *NEVER* store passwords as plaintext in production, this is purely for
3+
# demo purposes.
24
encoders:
35
Symfony\Component\Security\Core\User\User: plaintext
46

@@ -9,19 +11,24 @@ security:
911
providers:
1012
in_memory:
1113
memory:
12-
users: %security_users%
14+
# Only for demo purposes. Of course, use a more
15+
# secure password on production.
16+
users:
17+
anna_admin: { password: kitten, roles: ROLE_ADMIN }
18+
admin: { password: admin, roles: ROLE_ADMIN }
1319

1420
firewalls:
1521
dev:
1622
pattern: ^/(_(profiler|wdt)|css|images|js)/
1723
security: false
1824

1925
main:
20-
pattern: ^/
2126
anonymous: ~
22-
http_basic:
23-
realm: 'Secured Demo Area (username: admin, password: admin)'
27+
form_login:
28+
login_path: login
29+
check_path: login
30+
logout: ~
2431

2532
access_control:
2633
- { path: ^(/(de|fr|en))?/admin, roles: ROLE_ADMIN }
27-
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
34+
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace AppBundle\Controller;
13+
14+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
class SecurityController extends Controller
19+
{
20+
/**
21+
* @Route("/login", name="login")
22+
*/
23+
public function loginAction(Request $request)
24+
{
25+
$authenticationUtils = $this->get('security.authentication_utils');
26+
$error = $authenticationUtils->getLastAuthenticationError();
27+
$lastUsername = $authenticationUtils->getLastUsername();
28+
29+
return $this->render('security/login.html.twig', array(
30+
'last_username' => $lastUsername,
31+
'error' => $error,
32+
));
33+
}
34+
35+
/**
36+
* @Route("/logout", name="logout")
37+
*/
38+
public function logoutAction()
39+
{
40+
}
41+
}

0 commit comments

Comments
 (0)