forked from ezsystems/DemoBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-22324: Implemented premium subscriber content
A custom location view provider displays a teasing template if the content is Premium (section id 7 (Premium)), and if the user doesn't have the subscriber role (id: 6) section_id and role_id are configured in default_settings.yml.
- Loading branch information
Bertrand Dunogier
committed
Nov 19, 2014
1 parent
0a59a6e
commit 9c5b83b
Showing
6 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* This file is part of the eZ Publish Kernel package | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributd with this source code. | ||
*/ | ||
namespace EzSystems\DemoBundle\PremiumContent; | ||
|
||
use eZ\Publish\API\Repository\Repository; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use eZ\Publish\API\Repository\Values\User\Role; | ||
use eZ\Publish\Core\MVC\Symfony\View\ContentView; | ||
use eZ\Publish\Core\MVC\Symfony\View\Provider\Location as LocationViewProvider; | ||
|
||
/** | ||
* Returns the premium_content view if it applies to a location and if the user isn't a Premium subscriber. | ||
*/ | ||
class PremiumLocationViewProvider implements LocationViewProvider | ||
{ | ||
/** | ||
* ID of the section used to mark content as Premium | ||
* @var int | ||
*/ | ||
private $premiumSectionId; | ||
|
||
/** @var PremiumSubscriptionChecker */ | ||
private $subscriptionChecker; | ||
|
||
/** | ||
* @var Repository | ||
*/ | ||
private $repository; | ||
|
||
public function __construct( Repository $repository, PremiumSubscriptionChecker $subscriptionChecker, $premiumSectionId ) | ||
{ | ||
$this->repository = $repository; | ||
$this->premiumSectionId = $premiumSectionId; | ||
$this->subscriptionChecker = $subscriptionChecker; | ||
} | ||
|
||
public function getView( Location $location, $viewType ) | ||
{ | ||
if ( $viewType !== 'full' ) | ||
{ | ||
return null; | ||
} | ||
|
||
if ( $location->getContentInfo()->sectionId !== $this->premiumSectionId ) | ||
{ | ||
return null; | ||
} | ||
|
||
if ( $this->subscriptionChecker->userIsSubscriber( $this->repository->getCurrentUser() ) ) | ||
{ | ||
return null; | ||
} | ||
|
||
return new ContentView( "eZDemoBundle:$viewType:premium_content.html.twig" ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* This file is part of the eZ Publish Kernel package | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributd with this source code. | ||
*/ | ||
namespace EzSystems\DemoBundle\PremiumContent; | ||
|
||
use eZ\Publish\API\Repository\Values\User\User; | ||
use eZ\Publish\API\Repository\Repository; | ||
use eZ\Publish\API\Repository\Values\User\Role; | ||
|
||
class PremiumSubscriptionChecker | ||
{ | ||
/** | ||
* ID of the premium subscriber role | ||
*/ | ||
private $roleId = 6; | ||
|
||
/** | ||
* @var Repository|\eZ\Publish\Core\Repository\Repository | ||
*/ | ||
private $repository; | ||
|
||
public function __construct( Repository $repository, $subscriberRoleId ) | ||
{ | ||
$this->repository = $repository; | ||
$this->roleId = $subscriberRoleId; | ||
} | ||
|
||
public function userIsSubscriber( User $user ) | ||
{ | ||
$roleService = $this->repository->getRoleService(); | ||
return $this->repository->sudo( | ||
function ( Repository $repository ) use ( $user, $roleService ) | ||
{ | ||
foreach ( $repository->getUserService()->loadUserGroupsOfUser( $user ) as $group ) | ||
{ | ||
foreach ( $roleService->getRoleAssignmentsForUserGroup( $group ) as $role ) | ||
{ | ||
if ( $this->isSubscriberRole( $role->role ) ) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
); | ||
} | ||
|
||
public function isSubscriberRole( Role $role ) | ||
{ | ||
return $role->id === $this->roleId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{% extends "eZDemoBundle::pagelayout.html.twig" %} | ||
|
||
{% block content %} | ||
<section class="content-view-full"> | ||
<article class="class-article row"> | ||
<div class="span8"> | ||
<div class="attribute-header"> | ||
<h1>{{ ez_render_field( content, 'title' ) }}</h1> | ||
</div> | ||
|
||
{% if not ez_is_field_empty( content, 'subscriber_teaser' ) %} | ||
<div class="attribute-short"> | ||
{{ ez_render_field( content, 'subscriber_teaser' ) }} | ||
</div> | ||
{% endif %} | ||
|
||
This article is only available to Premium subscribers. | ||
</div> | ||
</article> | ||
</section> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Premium content | ||
|
||
This feature displays content marked as Premium using a special teaser and informations about the status. It is | ||
an example of what could be used to offer a paywall. | ||
|
||
## Implementation | ||
Premium content is identified belonging to section Premium (ID: 7). Subscribers have the Subscriber role (ID: 6). | ||
|
||
A standard policy check can't be used here, as it would prevent the content from being returned in searches and lists. | ||
Instead, Anonymous user can read this content, but a custom ViewProvider is used to return a special premium_teaser | ||
template when a premium content is viewed by a non-subscriber. |