This topic contains 3 replies, has 2 voices, and was last updated by oliverusselldev 5 years, 1 month ago.
- Topic
Good day, everyone.
I need to extend authentication mechanism for my needs.
To do this i created Custom Form Password Authenticator1) I changed firewall settings:
123456789main:...#organization-form-login:simple_form:authenticator: my_authenticatorcsrf_provider: form.csrf_providercheck_path: oro_user_security_checklogin_path: oro_user_security_login...2) I created service for my_authenticator
1234567services:...my_authenticator:class: OQ\SecurityBundle\Security\MyAuthenticatorarguments:- @oro_organization.organization_manager...3) And here is the code of MyAuthenticator
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364namespace OQ\SecurityBundle\Security;use Symfony\Component\Config\Definition\Exception\Exception;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;use Symfony\Component\Security\Core\Exception\AuthenticationException;use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;use Symfony\Component\Security\Core\User\UserProviderInterface;Use Oro\Bundle\SecurityBundle\Authentication\Token\UsernamePasswordOrganizationToken;use Oro\Bundle\OrganizationBundle\Entity\Manager\OrganizationManager;class MyAuthenticator implements SimpleFormAuthenticatorInterface{/** @var OrganizationManager */protected $manager;public function __construct(OrganizationManager $manager){$this->manager = $manager;}public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey){// Here will be my special checks//Here i try to get username and force authenticationtry {$user = $userProvider->loadUserByUsername($token->getUsername());} catch (UsernameNotFoundException $e) {throw new AuthenticationException('This user not allowed');}// If everythin' is ok - create a tokenif ($user) {return new UsernamePasswordOrganizationToken($user,$user->getPassword(),$providerKey,$this->manager->getOrganizationById(1));} else {throw new AuthenticationException('Invalid username or password');}}public function supportsToken(TokenInterface $token, $providerKey){return $token instanceof UsernamePasswordOrganizationToken&& $token->getProviderKey() === $providerKey;}public function createToken(Request $request, $username, $password, $providerKey){//UsernamePasswordOrganizationTokenreturn new UsernamePasswordOrganizationToken($username, $password, $providerKey, $this->manager->getOrganizationById(1));}}When i try to authenticate user – i succesfully log in, but i dont see anything except black header and profiler. Profiler says me, that i’m logged as USER_NAME (yellow color), and not authenticated (red color).
Can you give me an advice – how to make t work?
And one more question – how can i retrieve user’s organization in this authenticator class?Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-docker
The forum ‘OroPlatform – How do I? Questions’ is closed to new topics and replies.