Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WebBundle] "GET" actions should not be able to modify any resource #1471

Merged
merged 1 commit into from
May 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@
</button>
</form>
{% endmacro %}

{% macro patch(url, message, icon) %}
<form action="{{ url }}" method="post" class="delete-action-form" novalidate>
<input type="hidden" name="_method" value="PATCH">
<button class="btn btn-success" type="submit">
<i class="glyphicon glyphicon-{{ icon|default('transfer') }}"></i> <span>{{ message }}</span>
</button>
</form>
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Addressing\Model\AddressInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -109,47 +111,45 @@ public function deleteAction($id)
}

/**
* Set an address as default billing address for the current user.
* Set an address as default billing/shipping address for the current user.
*
* @param int $id
* @param string $type
*
* @return RedirectResponse
*
* @throws NotFoundHttpException
*/
public function setAsBillingAction($id)
public function setAddressAsAction($id, $type)
{
$address = $this->findUserAddressOr404($id);

$user = $this->getUser();
$user->setBillingAddress($address);

$manager = $this->getUserManager();
$manager->persist($user);
$manager->flush();
if ('billing' === $type) {
$user->setBillingAddress($address);

$this->addFlash('success', 'sylius.account.address.set_as_billing');

return $this->redirectToIndex();
}

/**
* Set an address as shipping billing address for the current user.
*
* @return RedirectResponse
*/
public function setAsShippingAction($id)
{
$address = $this->findUserAddressOr404($id);
$this->addFlash('success', 'sylius.account.address.set_as_billing');
} elseif ('shipping' === $type) {
$user->setShippingAddress($address);

$user = $this->getUser();
$user->setShippingAddress($address);
$this->addFlash('success', 'sylius.account.address.set_as_shipping');
} else {
throw new NotFoundHttpException();
}

$manager = $this->getUserManager();
$manager->persist($user);
$manager->flush();

$this->addFlash('success', 'sylius.account.address.set_as_shipping');

return $this->redirectToIndex();
}

/**
* @param AddressInterface $address
*
* @return FormInterface
*/
private function getAddressForm(AddressInterface $address)
{
return $this->get('form.factory')->create('sylius_address', $address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sylius_backend_product_variant_delete:

sylius_backend_product_variant_generate:
pattern: /generate
methods: [GET]
methods: [PATCH]
defaults:
_controller: sylius.controller.product_variant:generateAction
_sylius:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ sylius_account_address_delete:

sylius_account_address_set_default_billing:
pattern: /{id}/default/billing
methods: [GET]
methods: [PATCH]
defaults:
_controller: sylius.controller.frontend.account.address:setAsBillingAction
_controller: sylius.controller.frontend.account.address:setAddressAsAction
type: billing
requirements:
id: \d+

sylius_account_address_set_default_shipping:
pattern: /{id}/default/shipping
methods: [GET]
methods: [PATCH]
defaults:
_controller: sylius.controller.frontend.account.address:setAsShippingAction
_controller: sylius.controller.frontend.account.address:setAddressAsAction
type: shipping
requirements:
id: \d+
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
<div class="well well-sm">
{{ buttons.create(path('sylius_backend_product_variant_create', {'productId': product.id}), 'sylius.variant.create'|trans) }}
{% if product.hasOptions %}
{{ buttons.manage(path('sylius_backend_product_variant_generate', {'productId': product.id}), 'sylius.product.generate_variants'|trans) }}
{{ buttons.patch(path('sylius_backend_product_variant_generate', {'productId': product.id}), 'sylius.product.generate_variants'|trans) }}
{% endif %}
<div class="pull-right">
<strong>{{ 'sylius.product.variant_selection_method'|trans }}</strong>: {{ product.variantSelectionMethodLabel }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@
{% endif %}

{% if address != app.user.billingAddress %}
{{ buttons.btn(
{{ buttons.patch(
path('sylius_account_address_set_default_billing', {'id': address.id}),
'sylius.account.address.action.billing', '', 'star-empty') }}
<br>
'sylius.account.address.action.billing', 'star-empty') }}
<br>
{% endif %}
{% if address != app.user.shippingAddress %}
{{ buttons.btn(
{{ buttons.patch(
path('sylius_account_address_set_default_shipping', {'id': address.id}),
'sylius.account.address.action.shipping', '', 'star') }}
'sylius.account.address.action.shipping', 'star') }}
{% endif %}
</td>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@
</form>
{% endif %}
{% endmacro %}

{% macro patch(url, message, icon) %}
<form action="{{ url }}" method="post" novalidate>
<input type="hidden" name="_method" value="PATCH">
<button class="btn btn-default" type="submit">
<i class="glyphicon glyphicon-{{ icon|default('transfer') }}"></i> <span>{{ message|trans }}</span>
</button>
</form>
{% endmacro %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line.