Symfony 2.8: How to prevent editing user's service fields

203 views Asked by At

I'm newby in Symfony, so excuse my ignorance.

In my project i use Sonata User Bundle, with User entity extended by several fields. Some of them can be edited by user himself, while others are serviceable and they can only be edited by the admin. The question is: How to ensure security when editing information by the user?
I see 2 ways:
1. Use custom validation constraints that will check user's role.
2. Use one-to-one related entity with its own Admin.
Perhaps there are some standard ways to accomplish this task?

Thanks in advance!

UPD:
The problem is that there is one entity (User) that can be edited from different places: admin area and public area. Naturally, the forms for admin and public areas are different, but there is a possibility that an attacker can forge a form and edit fields that are accessible only to the administrator. Therefore, I want to protect them at the level of entity. Is it possible?

1

There are 1 answers

2
T. Abdelmalek On

There are many cases of security. You can use @Security with has_role('ROLE_ADMIN') annotation for your action or in twig is_granted to display or not a field or use custom voter to check if user can edit or delete un objet look at this. Describe your need clearly to help you.