SonataUserBundle override model - symfony

How to delete unwanted fileds in the entity/model: facebook, twitter, bio, website from the Model/User.php ?
I try to ovveride the Model but it doesn't work.
I succed to override the entity, i added some new property, and it work, but want to delete unwanted stuff.
Thx, bye

You can create own user entity which will not extend SonataUserBundle User.php class.
If you used FOSUSerBundle:
class User extends FOS\UserBundle\Entity\User
or you can extend by default Symfony 2 Security User.php class.
Next you can configure SonataUserBundle to use your user entity:
sonata_user:
class:
user: MyBundle\Entity\User
Full configuration is here:
https://github.com/sonata-project/SonataUserBundle/blob/master/Resources/doc/reference/advanced_configuration.rst
If you used SonataUserBundle default controller to manage users you need to create own UserAdmin Class and configure sonata:
sonata_user:
admin: # Admin Classes
user:
class: MyBundle\Admin\UserAdmin

in fact, i got it.
I must to extend the model, not the entity, and it works !!!
I can remove unwanted stuff

Related

EasyAdmin Using your Own Templates Problem

I am trying to override "new" template for EasyAdmin but system ignores template code.
Here is the relevant part of easy_admin.yaml
entities:
# List the entity class name you want to manage
Places:
class: App\Entity\Places
templates:
list: 'asdfasdf'
As you see, value of list attribute is invalid but system ignores it and works without error and i can not override the template.
Do you have a suggestion?
So, as I mentioned in the comment - for some reason easyadmin doesn't give any errors if specified template doesn't exists. So, you just need to place your new template in templates folder, in example, templates/admin/listPlaces.html.twig and then specify correct path in easyadmin's config file, in example:
entities:
Places:
class: App\Entity\Places
templates:
list: 'admin/listPlaces.html.twig'
If you use EasyAdmin 3.x, for that you can overwrite a specific template specifying it in your entity's Controller or you can create your own folder structures like symfony does.
In this example I am overwriting only the edit template for my 'studient' entity, if you want to change all the edits of your project you must do so by creating the folder structure as symfony does.
public function configureCrud(): Crud
{
return Crud::new()
->overrideTemplate('crud/edit', 'studient/edit.html.twig')
;
}
You can even combine both methods. Suppose that in addition to modifying only the template 'edit' of studient you want to modify the way in which easyadmin displays the flash messages for that you only have to create this structure in your templates folder: "templates\bundles\EasyAdminBundles\flash_messages.html.twig"
I leave the link with the documentation for EasyAdmin 3.x
overriding-templates

Custom block service not found by Sonata

I am having a weird situation which has halted my progress for the second day now and I am almost going bald from pulling my hair on this. I have a custom block on the sonata admin dashboard which is not being found when I try to load the page.
I have gone over the configuration a couple of times and maybe I am missing something which an extra pair of eyes may be able to spot which is why I am posting this question here.
I have built my block as below and saved it under src\AppBundle\Block\NumbersBlockService.php
namespace AppBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use DoctrineExtensions\Query\Mysql;
class NumbersBlockService extends BaseBlockService
{
......
}
Then defined my service in the service.yml file as below:
sonata.block.service.topnumbers:
class: AppBundle\Block\NumbersBlockService
arguments:
- sonata.block.service.topnumbers
- "#templating"
- "#doctrine.orm.entity_manager"
- "#security.token_storage"
tags:
- { name: sonata.block }
My config.yml file includes the block like this
sonata_block:
default_contexts: [cms]
blocks:
sonata.user.block.menu: # used to display the menu in profile pages
sonata.user.block.account: # used to display menu option (login option)
sonata.block.service.text: # used to if you plan to use Sonata user routes
sonata.block.service.topnumbers:
and finally, I position the block on top with the line below
sonata_admin:
dashboard:
blocks:
- { position: top, type: sonata.block.service.topnumbers, class: col-md-12}
I have checked the tutorial on creating a custom block here https://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html and everything seems to check out but I still get the following error below:
An exception has been thrown during the rendering of a template ("The block type "sonata.block.service.topnumbers" does not exist") in SonataAdminBundle:Core:dashboard.html.twig at line 60.
Someone please help put me out of my misery. Thanks in advance

Sonata Admin overrides layout template per one Admin

I try to override layout template in Sonata Admin. I did all steps founded in official documentation, but my changes don`t work.
I did:
Copied from vendor appropriate template (standard_layout.html.twig) to app/Resources/SonataAdminBundle/views/. I will check if I override here template my changes applied to all of Admins in project (I want have this change only in one Admin)
In next step I created new file in my Bundle (Name/InfoBundle/Resources/views/JobOffer) and add there my custom template: findCandidate.html.twig. Below is content of this file:
https://gist.github.com/anonymous/5f4780a1ae8d7329cd91
Added to bundle service:
name_info.admin.offers:
class: Name\InfoBundle\Admin\JobOfferAdmin
tags:
- {name: sonata.admin, manager_type: orm, group: Info, label: Job offers}
arguments: [~, Name\Info\Entity\JobOffer, NameInfoBundle:JobOffer]
calls:
- [ setTemplate, [findCandidate, NameInfoBundle:JobOffer:findCandidate.html.twig]]
After that my changes are not applied. So probably I made mistake in services or maybe I have to call this template also in controller? I am not sure where I make mistake. Could anyone help me?
Probably you mistyped a template placeholder in setTemplate function.
Try to set it like:
calls:
- [setTemplate, [layout, NameInfoBundle:JobOffer:findCandidate.html.twig]]
It will change a standard_layout only for the selected admin.

how to best secure controllers actions and display different content symfony2

I'm building a web application using symfony2. I have different types of users with different roles; ROLE_STUDENT and ROLE_TEACHER, those two user can access a course's details; if the user is a teacher, a button edit is shown and if it's the student then a button subscribe will be shown, and actually this is not secure because it just hides the path to the controllers action, if the student types in the address bar /course/2/edit the edit action would be executed so I had to secure the action using #security annotation:
This is what I have done so far:
/**
* #Security("has_role('ROLE_TEACHER')")
*/ public function editAction()
{}
and in twig :
{% if is_granted('ROLE_TEACHER') %}
edit
{% elseif is_granted('ROLE_STUDENT')%}
subscribe
.
The problem is that I have a lot of accessible content to both users and I think there is a better solution to this instead of copy/past the same code all over. I'm new to Symfony 2, please bear with me.
There are multiple ways to achieve this but what you are doing is not wrong.
One way to achieve this is to set ROLE for the ROUTES so that ROLE_STUDENT roles can only access URLs that will be something like this website.com/students and ROLE_TEACHER can only access website.com/teachers
access_control:
- { path: ^/student/, roles: ROLE_STUDENT }
- { path: ^/teamleader/, roles: ROLE_TEACHER }
You can then set the edit route only for teachers like website.com/teachers/course/2/edit this way no edit route is going to be available for ROLE_STUDENT and they will get 404 error or access denied error if they try to access teacher route. You can do the same for the subscribe feature.
Like I said there are more ways to achieve this and this is one of them.

overriding template of multi user in RollerworksMultiUserBundle Symfony2

I use RollerworksMultiUserBundle over FOSUserBundle to handle multi user but i cant override template of each bundle seperatly
I have 2 bundle
AdminBundle
DesignerBundle
I override AdminBundle but DesignerBundle template was overrided !
I try to override DesignerBundle but this error accured
Bundle "RollerworksMultiUserBundle" is directly extended by two bundles "
I found the solution in there:
rollerworkers

Resources