Cześć, specjalnie dla Ciebie zainstalowałem sobie SF3 aby rozwiązać Twój problem (IMG:
style_emoticons/default/tongue.gif)
Nie powiem Ci gdzie masz błąd bo nie dałeś wszystkich plików (gdzie akcja z logowaniem?) ale pokaże Ci sposób w jaki sposób stworzyć prosty system autoryzacji
Jedziemy:1. Zainstalowałem sobie symfony 3 (my_project)
2. Stworzyłem bazę w phpmyadmin (symfony3) i dodałem dane dostępowe do konfiguracji (
app/config/parameters.yml)
3. Dodałem entity (
src/AppBundle/Entity/User.php):
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER'); }
public function eraseCredentials()
{
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/** @see \Serializable::serialize() */
{
$this->id,
$this->username,
$this->password,
$this->isActive,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
{
list (
$this->id,
$this->username,
$this->password,
$this->isActive,
// see section on salt below
// $this->salt
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
}
4. Następnie użyłem:
php bin/console doctrine:schema:update --force i dodatkowo wygenerowalem gettery i settery:
php bin/console doctrine:generate:entities AppBundle5. Następnie stworzyłem kontroller do logowania,rejestracji (dane ręcznie zadeklarowane do testów), sprawdzenia czy jest zalogowany (
src/AppBundle/Controller/SecurityController.php):
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
use AppBundle\Entity\User;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
return $this->render('security/login.html.twig');
}
/**
* @Route("/admin")
*/
public function adminAction()
{
$auth_checker = $this->get('security.authorization_checker');
$token = $this->get('security.token_storage')->getToken();
return new Response('<html><body>Admin page!</body></html>');
}
/**
* @Route("/register")
*/
public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$user = new User;
$password = $passwordEncoder->encodePassword($user, 'admin');
$user->setPassword($password);
$user->setEmail('admin@admin.lc');
$user->setUsername('admin');
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
}
6. Dodałem widok dla logowania (
app/Resources/views/security/login.html.twig):
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
7. Dodałem kilka linijek do security (gdzie logowanie, gdzie ma dostep):
app/config/security.yml :
security:
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: AppBundle:User
property: username
firewalls:
main:
anonymous: ~
form_login:
login_path: login
check_path: login
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_USER }
Po wejściu na /admin przenosi Cię do logowania.
Jeśli jesteś zalogowany to po wejściu na /admin pokazuje Ci obiekt użytkownika zalogowanego
Daj znać czy działa (IMG:
style_emoticons/default/smile.gif)