php


Decorating a service when user has x role


I have been looking for an answer to this question, but I can not seem to find it anywhere.
I have currently defined a decorator service that decorates the translator service. I however want to decorate the translator service only when the user has a certain role.
services.yml
services:
app.my_translator_decorator:
class: MyBundle\MyTranslatorDecorator
decorates: translator
arguments: ['#app.my_translator_decorator.inner']
public: false
MyTranslatorDecorator.php
class MyTranslatorDecorator {
/**
* #var TranslatorInterface
*/
private $translator;
/**
* #param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
// more code...
}
The container is "compiled" before the runtime. You can't decorate a service depending of the context, it will always be decorated.
However, in your decorator, you can add a guard clause to not execute your custom code if not necessary.
Service definition:
services:
app.my_translator_decorator:
class: AppBundle\MyTranslatorDecorator
decorates: translator
arguments: ['#app.my_translator_decorator.inner', '#security.authorization_checker']
public: false
Decorator:
<?php
namespace AppBundle;
use Symfony\Component\Security\Core\Authorization\ AuthorizationCheckerInterface;
use Symfony\Component\Translation\TranslatorInterface;
class MyTranslatorDecorator implements TranslatorInterface
{
private $translator;
private $authorizationChecker;
public function __construct(TranslatorInterface $translator, AuthorizationCheckerInterface $authorizationChecker)
{
$this->translator = $translator;
$this->authorizationChecker = $authorizationChecker;
}
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
if (!$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
return $this->translator->trans($id, $parameters, $domain, $locale);
}
// return custom translation here
}
// implement other methods
}

Related Links

Use Custom Post Type post ID in permalink rather than post title
Array stops working when i update the value of a key to 0
Associative json array with curl
how to validate numbers and special characters only with regex in php
Can I modify a variable within a string with another variable in PHP?
Replacing php realpth when outputing to users
Image not found code for my googlebooks thumbnail
jquery is not updating my php value when user selects dropdown option
How to automatically pull the version number?
Mixing Laravel and Wordpress User Info - Security Issues?
Merging 2 subqueries having different number of rows. Getting double the data
PHP - how i count numerical character and string character to check username length value?
HTML/PHP set input field to an exact number.
cannot access nusoap webservice on my hosted website to localhost, but work on localhost to localhost
CSV: Change One Field Column
How to set values of all element in array with loop conditional?

Categories

HOME
generics
verification
ontouchlistener
vagrant
hid
nsis
azure-graph-api
xbap
jpa-2.0
vertica
exe
scheduled-tasks
cmake-gui
amazon-mws
opam
libgit2
neo4j.rb
device
candlestick-chart
pyopencl
data-collection
strophe.js
background-color
clipboard
network-analysis
materialize
locks
autodesk-model-derivative
actframework
bean-validation
orchardcms-1.8
forms-authentication
strapi
mars-simulator
assemblies
praat
grunt-contrib-watch
haar-classifier
contactless-smartcard
viewmodel
return-type
non-linear-regression
head.js
ilrepack
phppgadmin
comm
dynamics-crm-4
oid
qtextedit
mathjs
dmarc
svnserve
android-syncadapter
highlighting
wikimedia-commons
phpquery
variable-length-array
google-maps-api-2
opencyc
fanotify
cfwheels
fiware-bosun
kognitio-wx2
createfile
phonegap-facebook-plugin
famo.us
datagridcomboboxcolumn
quantify
oracle-adf-mobile
treeline
mfmessagecomposeview
sql-function
jms-serializer
nosql-aggregation
spring-security-acl
frontbase
acitree
iirf
microformats
background-thread
gprof
spark-view-engine
clearinterval
chronometer
coordinate
trialware
platform-independent
prototypal-inheritance
wcf-callbacks
mmc3
litespeed
server-variables
digiflow
rational-unified-process
powergui
processors
ntruencrypt
activex-exe
synthesizer

Resources

Encrypt Message