php


Jms Serializer dynamic exposure


I am using Symfony 3, Doctrine 2, FOS Rest bundle and JMS Serializer.
I am using the exclude all policy across my app and then selectively exposing fields.
On the User Entity, I want to be able to expose additional fields only for the current user.
For example on the normal api/user/{id} endpoint, I want to expose the normal data, but then for api/user/current I want to expose slightly more data.
Eg.
/**
* ...
* #Serializer\ExclusionPolicy("all")
*/
class Users implements UserInterface
{
/**
* #var string
* #Serializer\Expose
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="secretfield", type="string", length=255, nullable=true)
*/
private $secretfield;
I have tried the #groups decorator, but that only works to further whittle down the fields and would require me changing loads and being careful to set the "default" group context everywhere.
I have seen the Dynamic Exclusion Strategy, mentioned in the docs, but I can't figure out if it is what I need or not and which variables are available to build an expression out of.
Any ideas what the best way to do this would be?
My main worry was the "send everything by default" behaviour that would come from setting the exclusion policy to none.
But I looked into the groups a bit more and found that I could set a default group in the config.
fos_rest:
serializer:
groups: ["default"]
I tested this on my User controller and without specifying any groups on the entity or in the Serializer context, it returned nothing.
So I modified my entity to use #Serializer\Groups({"default"}) instead of #Serializer\Expose. Which brought me back to the same returned data as when I started.
I then added the current_user group to the secret fields of my entity and added the group to the serializer context to get those additional fields for that particular view.
In the entity:
/**
* ...
* #Serializer\ExclusionPolicy("none")
*/
class Users implements UserInterface
{
/**
* #var string
* #Serializer\Groups({"default"})
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #var string
* #Serializer\Groups({"current_user"})
*
* #ORM\Column(name="secretfield", type="string", length=255, nullable=true)
*/
private $secretfield;
And in the controller:
/**
* #Rest\Get("user/current")
*
* #return \FOS\RestBundle\View\View
*/
public function getCurrentAction()
{
$me = $this->getUser();
$view = new View($me, Response::HTTP_OK);
/** #var Context $context */
$context = $view->getContext();
$context->setGroups(['current_user', 'default']);
$view->setContext($context);
return $view;
}

Related Links

Codeigniter this->db->group_by date from timestamp format
Symfony 3: Resize image on upload
Body is json but request->get() return null
All Params Not Being Passed
Can't submit an input for later use. php
array undefined offset with php
Outputting array in sections PHP
PHP - Getting an Hex Character code #x2f in strings instead of its symbol “/” (Slash)
How to separate json array and put it on a html table?
An invalid form control with name=' is not focusable in jQuery for Rating
Two values in one switch case
How to change limit of selected data in MYSQL PHP.?
How to get specific date from days, number of week and year
fb login with codeigniter
PHP Unit not working on Ubuntu 12.04 LTS with PHP5.6
How to create a table in html with using a foreach loop in PHP

Categories

HOME
powerbi
adfs3.0
writefile
tcsh
ant
slider
apk
nsis
tfs2015
implicit-conversion
openfoam
onvif
spring-security-oauth2
yeoman-generator-angular
xamarin-zebble
alljoyn
business-intelligence
createjs
mpu6050
outlook-restapi
django-rq
scheduled-tasks
pfobject
spring-ldap
mime-types
react-dnd
environment
prompt
unrar
uri
clipboard
marquee
iwebbrowser2
amazon-cloudtrail
matlab-app-designer
tightvnc
riotjs
mongoengine
restful-architecture
runtimeexception
cordys-opentext
forms-authentication
ms-dos
cheat-engine
assert
rdp
sbjson
best-buy-api
angular-datatables
return-type
libreoffice-writer
beeline
password-hash
solr-query-syntax
lvalue
d3v4
xcopy
gravatar
ioexception
fanotify
auto-generate
jnlp
hmisc
roxygen2
azure-xplat-cli
subfolder
camus
signed
kognitio-wx2
comctl32
vs-unit-testing-framework
visual-studio-6
visual-c++-2012
stripe.net
redpitaya
tabletools
iosched
meteorite
vlab
revmob
lru
acceptance-testing
microformats
blobstorage
flv
cmath
jsplitpane
getproperty
mkannotation
uploading
django-apps
servercontrol
getopts
botnet
page-curl
downgrade
active-record-query
oracle-pro-c
projectpier
synthesizer

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile