Symfony, security.yml "Unknown Entity namespace alias" - php

I would like to implement Symfony security into my project. Problem is I have to use two databases (one for admins, second for users). I have configured config.yml :
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: system
mappings:
FinanceCmsBundle: ~
admin:
connection: admin
mappings:
FinanceCzBundle: ~
and set my security.yml :
security:
encoders:
Finance\CmsBundle\Entity\AuthUser:
algorithm: md5
encode_as_base64: false
iterations: 0
Finance\CzBundle\Entity\SystemAuthMailCentrum:
algorithm: md5
encode_as_base64: false
iterations: 0
providers:
administrators:
entity:
class: FinanceCmsBundle:AuthUser
property: username
ussers:
entity:
class: FinanceCzBundle:SystemAuthMailCentrum
property: username
Before I started I tested it on a copy of a table on a database under FinanceCmsBundle:SystemAuthMailCentrum and it worked correctly.
Now aplication ends with "Doctrine\ORM\ORMException
Unknown Entity namespace alias 'FinanceCzBundle'"
I have tried to switch them (FinanceCzBundle with FinanceCmsBundle) in config.yml and it looks like the aplication knows only the default Bundle set in config.yml. Because after this switch it does not find FinanceCmsBundle.
So I have tried to use full routes like:
providers:
administrators:
entity:
class: Finance\CmsBundle\Entity\AuthUser
property: username
ussers:
entity:
class: Finance\CzBundle\Entity\SystemAuthMailCentrum
property: username
After that I get different errors: "Doctrine\Common\Persistence\Mapping\MappingException
The class 'Finance\CzBundle\Entity\SystemAuthMailCentrum' was not found in the chain configured namespaces Finance\CmsBundle\Entity"
Thanks a lot for any answer.
P.S : It would be easier to use one database, but the code is old and I would have to fix it on many places.

Try to set the entity manager to providers, I think it should help:
providers:
administrators:
entity:
class: FinanceCmsBundle:AuthUser
property: username
ussers:
entity:
class: FinanceCzBundle:SystemAuthMailCentrum
property: username
manager_name: admin

Related

stof_doctrine_extensions can't load in symfony

I'm trying to do a "soft-delete" for a symfony project. So i want to use StofDoctrineExtensionsBundle and i did :
stof_doctrine_extensions:
orm:
default:
...
softdeleteable: true
doctrine:
...
orm:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
In my config.yml.
But now i have the error message:
Unrecognized option "softdeleteable" under
"doctrine.orm.entity_managers.filters"
in the error message you post seems you put the key filters under entity_managers key (as message says: under "doctrine.orm.entity_managers.filters"). fix moving the filters section under the orm key, as example:
doctrine:
orm:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
Hope this help

Extended FOSUserBundle Not Creating Base Entities

Our project has been migrated from Symfony 2.1 to 2.3 and now to 2.7. FOSUserBundle created it's own properties on 2.1 but does not on the later editions.
php app/console doctrine:schema:update actually results in commands to drop the existing username, usernameCanonical, etc., fields.
Additionally, when trying to log in using our log in form, we get:
"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationServiceException(code: 0): Unrecognized field: usernameCanonical at .../vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94
It appears the system has no knowledge of the FOSUserBundle entities that accompany our extended base user.
I have reduced our extended user class to the default as a test and still get the same issues. Here is the current code and configuration:
config.yml
orm:
#default_entity_manager: ~
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
result_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
metadata_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
query_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
mappings:
FOSUserBundle: ~
fos_user:
db_driver: orm
firewall_name: main
user_class: ODR\OpenRepository\UserBundle\Entity\User
change_password:
form:
type: odr_user_change_password
security.yml
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
# Symfony >= 2.8
# csrf_token_generator: security.csrf.token_manager
# Symfony < 2.8
csrf_provider: form.csrf_provider
remember_me:
key: asdlkjf2ji802fiuaskdjokjhafsdjsfdjhjh
lifetime: 1209600
path: /
domain: ~
User Class [ src/ODR/OpenRepository/UserBundle/Entity/User.php ]
namespace ODR\OpenRepository\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Running:
php app/console doctrine:mapping:info
does not show the FOS User or Group entities as being mapped. It does show our user class but the extension does not appear to pull in the base properties.
Thank you for your assistance.
As is usually the case when having upgrade problems, the issue turned out to be an incorrect dependency.
Updating the composer.json to include all the correct dependencies as shown in the reference composer.json:
https://github.com/symfony/symfony-standard/blob/2.7/composer.json
solved the issue. Our doctrine version was not correct and was incorrectly identifying the FOSUserBundle parent class as "transient" so that it was not compiled into the metadata array.
After updating the composer.json file and running composer update all schemas generated correctly and login functionality was restored.
Did you add the fos routing to routing config ?

Authentication with the new LDAP component in Symfony 2.8

I wanted to try the new LDAP component in Symfony 2.8 and started to play with it few days ago. However I don't really get it and have problems to authenticate the users. I have followed this article:
http://symfony.com/blog/new-in-symfony-2-8-ldap-component
Here are my configuration files:
# app/config/services.yml
services:
app.ldap:
class: Symfony\Component\Ldap\LdapClient
arguments: ["ldaps://ldap.uni-rostock.de"]
and:
# app/config/security.yml
security:
providers:
# in_memory:
# memory: ~
app_users:
ldap:
service: app.ldap
base_dn: ou=people,o=uni-rostock,c=de
search_dn: uid=tester,ou=people,o=uni-rostock,c=de
search_password: testpass
#filter: (sAMAccountName={username})
filter: (uid={username})
default_roles: ROLE_USER
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
admin:
provider: app_users
stateless: true
pattern: ^/admin
http_basic_ldap:
service: app.ldap
dn_string: "{username}"
Here is a guide (only in German, but the essential part is the code) to connecting to the server:
http://www.itmz.uni-rostock.de/en/software/windows/universitaetsweite-dienste/ldap-authentifizierungsserver/
When I run the server like this: php app/console server:run -vvv and open http://localhost:8000/admin I'm prompted to enter the credentials. Unfortunately I'm not passing through even though I tried many times and I'm very sure I didn't make a typo. In the console there is only this relevant line:
[Fri Mar 11 08:39:32 2016] 127.0.0.1:36632 [401]: /admin
I'm unauthorized (401) and prompted again to enter the credentials.
Am I maybe missing something? I have tried many different combinations, put the values in quotes, tried to add:
access_control:
- { path: ^/admin, roles: ROLE_USER }
to the security.yml, but it didn't help.
There is also another question with somewhat similar problematic:
LDAP Authentication with Symfony 2.8
but I couldn't really move further.
Does someone maybe have an idea what else could I try?
Just for reference, adding this in as the answer:
The issue would be dn_string: "{username}". Unless you're typing a full DN when prompted for a username/password, this will not work. For example, if all your users are in a common OU/container you could make it something like: dn_string: uid={username},ou=people,o=uni-rostock,c=de.
Glad this fixed it!

Symfony 2.8 http_digest throws DaoAuthenticationProvider error

Hopefully I'm missing something here.
The following security.yml file works without issue in Symfony 2.3. However, upgrading to Symfony 2.8 throws this error:
Argument 2 passed to Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider::__construct() must be an instance of Symfony\Component\Security\Core\User\UserCheckerInterface
I've tested this on a fresh install of Symfony 2.8.2 and 2.3, having only changed the security.yml file.
security.yml
security:
providers:
my_in_memory_provider:
memory:
users:
foo:
password: foo
roles: ROLE_USER
bar:
password: bar
roles: [ROLE_USER, ROLE_ADMIN]
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api
http_digest:
key: %secret%
anonymous: false
I've checked the upgrade path from 2.3 to 2.8 and nothing has jumped out at me as obviously being the problem. However searching online for the issue hasn't brought up much so I'm guessing I've missed something?
Eventually found out this was a bug in Symfony 2.8.
Fixed here https://github.com/symfony/symfony/pull/17559

Symfony2 production environment: Mapping exception due to custom User class not found in chain configuration

I'm developing a bit complex Symfony2.1 based app, with connection to two different databases (with two entity managers, one connected to Oracle and the other one to PostgreSQL) and a custom User class (with a custom password encoder).
I've managed to have it running correctly in dev environment, but whenever I try to run it in production environment I'm getting the next error:
Doctrine\Common\Persistence\Mapping\MappingException: The class 'MyFirm\UserManagerBundle\Entity\User' was not found in the chain configured namespaces (uncaught exception)
My security and encoders in app/config/security.yml:
security:
encoders:
MyFirm\UserManagerBundle\Entity\User:
id: myfirm_usermanager.password_encoder
providers:
user_db:
entity:
class: MyFirm\UserManagerBundle\Entity\User
property: username
The doctrine.orm section in app/config/config.yml:
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: user_config_db
entity_managers:
event_db:
connection: event_db
mappings:
MyFirmEventManagerBundle: ~
user_config_db:
connection: user_config_db
mappings:
MyFirmUserManagerBundle: ~
And finally, this is the final Exception log:
Exception trace:
() at C:\SF2_project\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:38
Doctrine\Common\Persistence\Mapping\MappingException::classNotFoundInNamespaces() at C:\SF2_project\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain.php:114
Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass() at C:\SF2_project\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php:112
Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at C:\SF2_project\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:302
Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at C:\SF2_project\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:205
Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at C:\SF2_project\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:268
Doctrine\ORM\EntityManager->getClassMetadata() at C:\SF2_project\app\cache\prod\jms_diextra\doctrine\EntityManager_5075a0dd6e27a.php:345
EntityManager5075a0dd6e27a_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager->getClassMetadata() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Security\User\EntityUserProvider.php:39
Symfony\Bridge\Doctrine\Security\User\EntityUserProvider->__construct() at C:\SF2_project\app\cache\prod\appProdProjectContainer.php:1132
appProdProjectContainer->getSecurity_User_Provider_Concrete_UserDbService() at C:\SF2_project\app\bootstrap.php.cache:211
Symfony\Component\DependencyInjection\Container->get() at C:\SF2_project\app\cache\prod\appProdProjectContainer.php:1121
appProdProjectContainer->getSecurity_User_Provider_Concrete_ChainProviderService() at C:\SF2_project\app\bootstrap.php.cache:211
Symfony\Component\DependencyInjection\Container->get() at C:\SF2_project\app\cache\prod\appProdProjectContainer.php:1105
appProdProjectContainer->getSecurity_Authentication_ManagerService() at C:\SF2_project\app\bootstrap.php.cache:211
Symfony\Component\DependencyInjection\Container->get() at C:\SF2_project\app\cache\prod\appProdProjectContainer.php:552
appProdProjectContainer->getSecurity_ContextService() at C:\SF2_project\app\bootstrap.php.cache:211
Symfony\Component\DependencyInjection\Container->get() at C:\SF2_project\app\cache\prod\appProdProjectContainer.php:1002
appProdProjectContainer->getTwigService() at C:\SF2_project\app\bootstrap.php.cache:211
Symfony\Component\DependencyInjection\Container->get() at C:\SF2_project\app\cache\prod\appProdProjectContainer.php:30
appProdProjectContainer->getAssetic_AssetManagerService() at C:\SF2_project\app\bootstrap.php.cache:211
Symfony\Component\DependencyInjection\Container->get() at C:\SF2_project\vendor\symfony\assetic-bundle\Symfony\Bundle\AsseticBundle\CacheWarmer\AssetManagerCacheWarmer.php:33
Symfony\Bundle\AsseticBundle\CacheWarmer\AssetManagerCacheWarmer->warmUp() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate.php:47
Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand.php:64
Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand->execute() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Component\Console\Command\Command.php:238
Symfony\Component\Console\Command\Command->run() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Component\Console\Application.php:193
Symfony\Component\Console\Application->doRun() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php:78
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at C:\SF2_project\vendor\symfony\symfony\src\Symfony\Component\Console\Application.php:106
Symfony\Component\Console\Application->run() at C:\SF2_project\app\console:22
Has somebody any clue? Thanks a lot.
The problem seems it is a bug in Symfony2: the default entity manager must be named "default" if you want it to work in Production environment as the default entity manager.
After changing the doctrine.orm section in app/config/config.yml to:
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
event_db:
connection: event_db
mappings:
MyFirmEventManagerBundle: ~
default:
connection: user_config_db
mappings:
MyFirmUserManagerBundle: ~
everything works.
OK. Finally, after too much time, I found the solution for this: you have to add the entity manager name to the security definition with the manager_name option:
security:
encoders:
MyFirm\UserManagerBundle\Entity\User:
id: myfirm_usermanager.password_encoder
providers:
user_db:
entity:
class: MyFirm\UserManagerBundle\Entity\User
property: username
manager_name: user_config_db

Categories