Skip to content

Commit

Permalink
Symfony 3.4, php 7.2 upgrade (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj authored Feb 21, 2019
1 parent 61d7e0f commit aa08fe9
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 83 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Os2Display/CoreBundle CHANGELOG

## 2.0.0

* Symfony 3.4 upgrade.

## 1.2.0

* Changed MiddlewareCommunications to be event based, and moved improvements from CampaignBundle into CoreBundle.
Expand Down
31 changes: 4 additions & 27 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Os2Display\CoreBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Util\Codes;
use JMS\Serializer\SerializationContext;
use Symfony\Component\HttpFoundation\Response;
use JMS\Serializer\Context;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -29,38 +29,15 @@ protected function getData(Request $request, $key = NULL) {
return $key ? $request->request->get($key) : $request->request->all();
}

/**
* Create a JSON response with serialized data.
*
* @param $data
* @param int $status
* @param array $headers
* @param array $serializationGroups
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
protected function json($data, $status = 200, array $headers = [], array $serializationGroups = ['api']) {
$response = new JsonResponse(NULL, $status, $headers);

$serializer = $this->get('serializer');
$context = SerializationContext::create()->enableMaxDepthChecks();
if ($serializationGroups) {
$context->setGroups($serializationGroups);
}
$content = $serializer->serialize($data, 'json', $context);
$response->setContent($content);

return $response;
}

/**
* @param $data
* @param array $headers
* @param array $serializationGroups
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function createCreatedResponse($data, array $headers = [], array $serializationGroups = ['api']) {
$view = $this->view($data, Codes::HTTP_CREATED);
$context = $view->getSerializationContext();
$view = $this->view($data, Response::HTTP_CREATED);
$context = $view->getContext();
$context->setGroups($serializationGroups);

return $this->handleView($view);
Expand Down
2 changes: 1 addition & 1 deletion Controller/ChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function channelSaveAction(Request $request)
$channel->setCreatedAt(time());

// Set creator.
$userEntity = $this->get('security.context')->getToken()->getUser();
$userEntity = $this->get('security.token_storage')->getToken()->getUser();
$channel->setUser($userEntity->getId());
}

Expand Down
12 changes: 6 additions & 6 deletions Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Os2Display\CoreBundle\Controller;

use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Util\Codes;
use Symfony\Component\HttpFoundation\Response;
use Os2Display\CoreBundle\Entity\Group;
use Os2Display\CoreBundle\Exception\DuplicateEntityException;
use Os2Display\CoreBundle\Exception\HttpDataException;
Expand Down Expand Up @@ -62,14 +62,14 @@ public function newAction(Request $request)
$group = $this->get('os2display.group_manager')->createGroup($data);
} catch (ValidationException $e) {
throw new HttpDataException(
Codes::HTTP_BAD_REQUEST,
Response::HTTP_BAD_REQUEST,
$data,
'Invalid data',
$e
);
} catch (DuplicateEntityException $e) {
throw new HttpDataException(
Codes::HTTP_CONFLICT,
Response::HTTP_CONFLICT,
$data,
'Duplicate group',
$e
Expand Down Expand Up @@ -145,14 +145,14 @@ public function editAction(Request $request, Group $group)
->updateGroup($group, $data);
} catch (ValidationException $e) {
throw new HttpDataException(
Codes::HTTP_BAD_REQUEST,
Response::HTTP_BAD_REQUEST,
$data,
'Invalid data',
$e
);
} catch (DuplicateEntityException $e) {
throw new HttpDataException(
Codes::HTTP_CONFLICT,
Response::HTTP_CONFLICT,
$data,
'Duplicate group',
$e
Expand Down Expand Up @@ -190,7 +190,7 @@ public function deleteAction(Request $request, Group $group)
$em->remove($group);
$em->flush();

return $this->view(null, Codes::HTTP_NO_CONTENT);
return $this->view(null, Response::HTTP_NO_CONTENT);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ public function mediaUploadAction(Request $request) {
$media->setContext('default');

// Set creator.
$userEntity = $this->get('security.context')->getToken()->getUser();
$userEntity = $this->get('security.token_storage')->getToken()->getUser();
$media->setUser($userEntity->getId());

$groups = json_decode($request->request->get('groups'));
$groups = new ArrayCollection($groups ?: []);
$media->setGroups($groups);

$media_sizes = getimagesize($file->getPathname());
$media->setWidth($media_sizes[0]);
$media->setHeight($media_sizes[1]);

$mediaManager = $this->get('sonata.media.manager.media');
$mediaManager->save($media);

Expand Down
2 changes: 1 addition & 1 deletion Controller/ScreenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function screenSaveAction(Request $request)
$screen->setCreatedAt(time());

// Set creator.
$userEntity = $this->get('security.context')->getToken()->getUser();
$userEntity = $this->get('security.token_storage')->getToken()->getUser();
$screen->setUser($userEntity->getId());
}

Expand Down
2 changes: 1 addition & 1 deletion Controller/SlideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function slideSaveAction(Request $request)
$slide->setCreatedAt(time());

// Set creator.
$userEntity = $this->get('security.context')->getToken()->getUser();
$userEntity = $this->get('security.token_storage')->getToken()->getUser();
$slide->setUser($userEntity->getId());
}

Expand Down
34 changes: 19 additions & 15 deletions Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\Util\Codes;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Os2Display\CoreBundle\Entity\Group;
use Os2Display\CoreBundle\Entity\User;
use Os2Display\CoreBundle\Entity\UserGroup;
use Os2Display\CoreBundle\Exception\DuplicateEntityException;
use Os2Display\CoreBundle\Exception\HttpDataException;
use Os2Display\CoreBundle\Exception\ValidationException;
use Os2Display\CoreBundle\Security\Roles;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
Expand All @@ -26,6 +26,17 @@
* @Rest\View(serializerGroups={"api"})
*/
class UserController extends ApiController {
/**
* @Rest\Get("/current")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getCurrentUser() {
$user = $this->getUser();
$user->buildRoleGroups();

return $this->setApiData($user);
}

/**
* Lists all user entities.
*
Expand All @@ -34,7 +45,6 @@ class UserController extends ApiController {
* name="filter",
* description="Filter to apply",
* requirements="string",
* array=true,
* nullable=true
* )
* @ApiDoc(
Expand Down Expand Up @@ -92,10 +102,10 @@ public function newAction(Request $request) {
$user = $this->get('os2display.user_manager')->createUser($data);
}
catch (ValidationException $e) {
throw new HttpDataException(Codes::HTTP_BAD_REQUEST, $data, 'Invalid data', $e);
throw new HttpDataException(Response::HTTP_BAD_REQUEST, $data, 'Invalid data', $e);
}
catch (DuplicateEntityException $e) {
throw new HttpDataException(Codes::HTTP_CONFLICT, $data, 'Duplicate user', $e);
throw new HttpDataException(Response::HTTP_CONFLICT, $data, 'Duplicate user', $e);
}

// Send response.
Expand Down Expand Up @@ -164,10 +174,10 @@ public function editAction(Request $request, User $aUser) {
$aUser = $this->get('os2display.user_manager')->updateUser($aUser, $data);
}
catch (ValidationException $e) {
throw new HttpDataException(Codes::HTTP_BAD_REQUEST, $data, 'Invalid data', $e);
throw new HttpDataException(Response::HTTP_BAD_REQUEST, $data, 'Invalid data', $e);
}
catch (DuplicateEntityException $e) {
throw new HttpDataException(Codes::HTTP_CONFLICT, $data, 'Duplicate user', $e);
throw new HttpDataException(Response::HTTP_CONFLICT, $data, 'Duplicate user', $e);
}

$aUser->buildRoleGroups();
Expand All @@ -191,7 +201,7 @@ public function deleteAction(Request $request, User $aUser) {
$em->remove($aUser);
$em->flush();

return $this->view(NULL, Codes::HTTP_NO_CONTENT);
return $this->view(NULL, Response::HTTP_NO_CONTENT);
}

/**
Expand Down Expand Up @@ -232,12 +242,6 @@ public function getUserGroupRolesAction(User $user, Group $group) {
/**
* @Rest\Post("/{user}/group/{group}", name="api_user_group_create")
*
* @Rest\RequestParam(
* name="roles",
* description="Roles to give user in group.",
* requirements="string[]",
* nullable=true
* )
* @ApiDoc(
* section="Users and groups",
* description="Add user to group"
Expand Down Expand Up @@ -318,7 +322,7 @@ public function deleteUserGroupRolesAction(User $user, Group $group, ParamFetche
}
$em->flush();

return $this->view(NULL, Codes::HTTP_NO_CONTENT);
return $this->view(NULL, Response::HTTP_NO_CONTENT);
}

private function fetchUserGroupRoles(User $user, Group $group) {
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Os2DisplayBaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->getParameter('external_bootstrap') : [];
if (array_key_exists('bootstrap', $angular) && is_array($angular['bootstrap'])) {
foreach ($angular['bootstrap'] as $key => $module) {
if (!in_array($key, $bootstrap)) {
if (!array_key_exists($key, $bootstrap)) {
$bootstrap[$key] = $module;
} else {
$bootstrap[$key] = array_merge_recursive(
Expand Down
2 changes: 1 addition & 1 deletion Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
use FOS\UserBundle\Model\User as BaseUser;
use Os2Display\CoreBundle\Traits\ApiData;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
Expand Down
15 changes: 14 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ services:
os2display.entity_service:
class: Os2Display\CoreBundle\Services\EntityService
arguments: ['@validator']
public: true

os2display.group_manager:
class: Os2Display\CoreBundle\Services\GroupManager
arguments: ['@doctrine.orm.entity_manager', '@os2display.entity_service', '@service_container']
public: true

os2display.grouping_listener:
class: Os2Display\CoreBundle\EventListener\GroupingListener
Expand All @@ -27,14 +29,17 @@ services:
os2display.user_manager:
class: Os2Display\CoreBundle\Services\UserManager
arguments: ['@fos_user.user_manager', '@os2display.user_mailer_service', '@os2display.entity_service', '@fos_user.util.token_generator', '@os2display.security_manager']
public: true

os2display.security_manager:
class: Os2Display\CoreBundle\Services\SecurityManager
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.access.decision_manager', '@security.role_hierarchy', '@event_dispatcher']
public: true

os2display.api_data:
class: Os2Display\CoreBundle\Services\ApiDataService
arguments: ['@service_container']
arguments: ['@service_container', '@security.role_hierarchy']
public: true

os2display.user_mailer_service:
class: Os2Display\CoreBundle\Services\UserMailerService
Expand All @@ -44,6 +49,7 @@ services:
os2display.entity_manager:
class: Os2Display\CoreBundle\Services\EntityManagerService
arguments: ['@doctrine.orm.entity_manager', '@security.token_storage', '@security.authorization_checker']
public: true

os2display.edit_voter:
class: Os2Display\CoreBundle\Security\EditVoter
Expand All @@ -60,6 +66,7 @@ services:

os2display.api_authentication_service:
class: Os2Display\CoreBundle\Security\ApiAuthenticationEntryPoint
public: true

os2display.serialization_listener:
class: Os2Display\CoreBundle\EventListener\SerializationListener
Expand All @@ -71,6 +78,7 @@ services:
class: Os2Display\CoreBundle\Command\PushContentCommand
tags:
- { name: console.command }
public: true

os2display.core.cleanup_service:
class: Os2Display\CoreBundle\Services\CleanupService
Expand All @@ -80,6 +88,7 @@ services:
os2display.template_service:
class: Os2Display\CoreBundle\Services\TemplateService
arguments: ['@service_container']
public: true

os2display.feed_service:
class: Os2Display\CoreBundle\Services\FeedService
Expand All @@ -90,6 +99,7 @@ services:
os2display.authentication_service:
class: Os2Display\CoreBundle\Services\AuthenticationService
arguments: ['@service_container']
public: true

os2display.core.subscriber.roles:
class: Os2Display\CoreBundle\EventListener\RolesSubscriber
Expand All @@ -104,16 +114,19 @@ services:
- { name: kernel.event_listener, event: os2display.core.sharing_service.remove_channel_from_index, method: onRemoveChannelFromIndex }
- { name: kernel.event_listener, event: os2display.core.sharing_service.update_channel, method: onUpdateChannel }
- { name: kernel.event_listener, event: ik.cron, method: onCron }
public: true

os2display.utility_service:
class: Os2Display\CoreBundle\Services\UtilityService
arguments: ['@os2display.authentication_service']
public: true

os2display.middleware.communication:
class: Os2Display\CoreBundle\Services\MiddlewareCommunication
arguments: ['@service_container', '@os2display.utility_service', '@event_dispatcher']
tags:
- { name: kernel.event_listener, event: ik.cron, method: onCron, priority: 1 }
public: true

sonata.media.provider.zencoder:
class: Os2Display\CoreBundle\Provider\ZencoderProvider
Expand Down
7 changes: 5 additions & 2 deletions Services/ApiDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
use Os2Display\CoreBundle\Security\Roles;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\RoleHierarchy;

class ApiDataService {
protected $container;
protected $roleHierarchy;

public function __construct(ContainerInterface $container) {
public function __construct(ContainerInterface $container, RoleHierarchy $roleHierarchy) {
$this->container = $container;
$this->roleHierarchy = $roleHierarchy;
}

/**
Expand Down Expand Up @@ -108,7 +111,7 @@ protected function setApiDataUser(User $user, $inCollection = FALSE) {
$userRoles = array_map(function ($role) {
return new Role($role);
}, $user->getRoles(FALSE));
$roles = $this->container->get('security.role_hierarchy')->getReachableRoles($userRoles);
$roles = $this->roleHierarchy->getReachableRoles($userRoles);
$roles = array_unique(array_map(function (Role $role) { return $role->getRole(); }, $roles));

$apiData = [
Expand Down
Loading

0 comments on commit aa08fe9

Please sign in to comment.