-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 20e75a1
Showing
33 changed files
with
1,934 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 @@ | ||
.DS_Store |
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,6 @@ | ||
# Membership Plugin Changelog | ||
|
||
## 1.0.0 - 2020-02-26 | ||
|
||
### Added | ||
- Initial release! 🎉 |
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,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 oof. Studio | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,109 @@ | ||
# Membership Plugin for Craft Commerce | ||
|
||
> Give your users special access based on their [Commerce Subscriptions](https://docs.craftcms.com/commerce/v3/subscriptions.html). | ||
The Membership plugin works by listening to key [Subscription Events](https://docs.craftcms.com/commerce/v3/events.html#subscription-events) in Craft Commerce, and moving the Subscriber in (or out) of groups based on rules or “Grants” configured in the Control Panel. | ||
|
||
Much of it could be implemented in a module specific to your application—this plugin is primarily intended for those who need a simple system for granting access based on active Subscriptions. | ||
|
||
The plugin covers creation, cancellation, expiry of Subscriptions, and switching of Plans. At the moment, _it doesn't support special access or restrictions based on Trial periods_. | ||
|
||
## Details | ||
|
||
Each _Grant_ is a kind of policy or rule for which Plans map to which User Groups. By default, the plugin doesn't do anything—with no Grants configured, it won't make any changes to your Users' permissions. | ||
|
||
Because Membership operates on User Groups (not Permissions, directly), it's good to start by designing a sensible group-based permissions structure—for example, if your organization had _Bronze_, _Silver_, and _Gold_ support tiers, you might create three User Groups, and assign the relevant permissions to each. | ||
|
||
You can create multiple Grants per Plan—for example, if you wanted to structure your permissions in an additive way, you could grant _Gold_ supporters access to all three groups. In this way, you can be sure that benefits granted to lower support tiers always bubble up to higher ones. | ||
|
||
## Usage | ||
|
||
All the configuration happens via the Control Panel. Go to the Settings section, and click on the _Membership_ tile to manage _Grants_. | ||
|
||
In your template, you can use the normal Craft User methods to check whether someone has a particular access level: | ||
|
||
```twig | ||
{% if currentUser.isInGroup('membersBronze') %} | ||
<p>Thank you for your support! You have access to our entire lesson catalog.</p> | ||
{% endif %} | ||
``` | ||
|
||
In addition to checking groups, you can also directly check permissions: For example, if you didn't really care which Subscription(s) they have, but just need to determine whether or not they have some capability, you can use the `.can()` method: | ||
|
||
```twig | ||
{% set section = craft.app.sections.getSectionByHandle('classifieds') %} | ||
{% if currentUser.can("createEntries:#{section.uid}") %} | ||
You’re ready to <a href="{{ url('account/classifieds/new') }}">create a listing</a>! | ||
{% endif %} | ||
``` | ||
|
||
## Auditing | ||
|
||
We have a lightweight logging system set up so that admin have some visibility into what the plugin is doing. The `{{%membership_logs}}` table keeps track of any actions (successes and failures) taken by the plugin, and the relevant logs are output on the Subscription's edit page (as of [Commerce 3.0.11](https://github.com/craftcms/commerce/blob/develop/CHANGELOG.md#3011---2020-02-25)). | ||
|
||
## Extensibility | ||
|
||
Craft and Yii provide a rich system of Events to help developers alter the behavior of built-in and “pluggable” functionality. | ||
|
||
We emit two events: one just before a permission is about to be granted, and one when a permission is about to be revoke. Keep in mind that these are _in addition to_ Craft's own permissions events! | ||
|
||
### `Permissions::EVENT_BEFORE_GRANT_PERMISSION` | ||
|
||
Raised just before a membership to a User Group is granted. This is not emitted when a permission is not granted due to a User already being in a given Group. | ||
|
||
```php | ||
use yii\base\Event; | ||
|
||
use oofbar\membership\services\Permissions; | ||
use oofbar\membership\events\GrantPermission as GrantPermissionEvent; | ||
|
||
Event::on( | ||
Permissions::class, | ||
Permissions::EVENT_BEFORE_GRANT_PERMISSION, | ||
function (GrantPermissionsEvent $e) { | ||
// Optionally: prevent the grant from occurring, based on some criteria! | ||
$e->isValid = false; | ||
}); | ||
``` | ||
|
||
### `Permissions::EVENT_BEFORE_REVOKE_PERMISSION` | ||
|
||
Raised just before membership to a User Group is revoked. This is not emitted if the User was not in the Group a Grant is attempting to revoke. | ||
|
||
```php | ||
use yii\base\Event; | ||
|
||
use oofbar\membership\services\Permissions; | ||
use oofbar\membership\events\RevokePermission as RevokePermissionEvent; | ||
|
||
Event::on( | ||
Permissions::class, | ||
Permissions::EVENT_BEFORE_REVOKE_PERMISSION, | ||
function (RevokePermissionsEvent $e) { | ||
// Optionally: prevent the revokation from occurring, based on some criteria! | ||
$e->isValid = false; | ||
}); | ||
``` | ||
|
||
## Requirements | ||
|
||
This plugin requires Craft CMS 3.4 and Commerce 3.0. It may work with earlier versions, but it hasn't been tested! | ||
|
||
> In order for Subscriptions to work at all, **you must have Stripe Webhooks configured**! | ||
## Installation | ||
|
||
To install the plugin, follow these instructions (or just search “Membership” in the [Craft Plugin Store](#)): | ||
|
||
1. Open your terminal and go to your Craft project: | ||
|
||
cd /path/to/project | ||
|
||
2. Then tell Composer to load the plugin: | ||
|
||
composer require oof-bar/membership | ||
|
||
3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Membership. | ||
|
||
:deciduous_tree: |
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,43 @@ | ||
{ | ||
"name": "oofbar/membership", | ||
"description": "Give your users special access based on their Commerce Subscriptions.", | ||
"type": "craft-plugin", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"craft", | ||
"cms", | ||
"craftcms", | ||
"craft-plugin", | ||
"commerce", | ||
"membership", | ||
"subscriptions" | ||
], | ||
"support": { | ||
"docs": "https://github.com/oof-bar/membership/blob/master/README.md", | ||
"issues": "https://github.com/oof-bar/membership/issues" | ||
}, | ||
"license": "proprietary", | ||
"authors": [ | ||
{ | ||
"name": "oof. Studio", | ||
"homepage": "https://oof.studio/" | ||
} | ||
], | ||
"require": { | ||
"craftcms/cms": "^3.4.0", | ||
"craftcms/commerce": "^3.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"oofbar\\membership\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"name": "Membership", | ||
"handle": "membership", | ||
"hasCpSettings": true, | ||
"hasCpSection": false, | ||
"changelogUrl": "https://raw.githubusercontent.com/oof-bar/membership/master/CHANGELOG.md", | ||
"class": "oofbar\\membership\\Membership" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,164 @@ | ||
<?php | ||
/** | ||
* Membership plugin for Craft CMS 3.x | ||
* | ||
* Give your users special access based on their Commerce Subscriptions. | ||
* | ||
* @link https://oof.studio/ | ||
* @copyright Copyright (c) 2020 oof. Studio | ||
*/ | ||
|
||
namespace oofbar\membership; | ||
|
||
use Craft; | ||
use craft\base\Plugin; | ||
use craft\events\RegisterUrlRulesEvent; | ||
use craft\events\RegisterUserPermissionsEvent; | ||
use craft\helpers\UrlHelper; | ||
use craft\services\UserPermissions; | ||
use craft\web\UrlManager; | ||
use craft\web\twig\variables\CraftVariable; | ||
|
||
use craft\commerce\Plugin as Commerce; | ||
use craft\commerce\events\SubscriptionEvent; | ||
use craft\commerce\events\SubscriptionSwitchPlansEvent; | ||
use craft\commerce\services\Subscriptions; | ||
|
||
use yii\base\Event; | ||
|
||
use oofbar\membership\services\Grants; | ||
use oofbar\membership\services\Logs; | ||
use oofbar\membership\services\Permissions; | ||
use oofbar\membership\web\twig\CraftVariableBehavior; | ||
|
||
/** | ||
* Membership Plugin | ||
* | ||
* This plugin is really simple, and I hope if you're reading through the source | ||
* that it might encourage you to implement some | ||
* | ||
* @author oof. Studio | ||
* @package Membership | ||
* @since 1.0.0 | ||
*/ | ||
class Membership extends Plugin | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $version = '1.0.0'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $schemaVersion = '1.0.0'; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
public $hasCpSettings = true; | ||
|
||
/** | ||
* Set our $plugin static property to this class so that it can be accessed via | ||
* Membership::$plugin | ||
* | ||
* Called after the plugin class is instantiated; do any one-time initialization | ||
* here such as hooks and events. | ||
* | ||
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; | ||
* you do not need to load it in your init() method. | ||
* | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
$this->setComponents([ | ||
'grants' => Grants::class, | ||
'logs' => Logs::class, | ||
'permissions' => Permissions::class, | ||
]); | ||
|
||
// Attach plugin to the Craft template variable: | ||
Event::on( | ||
CraftVariable::class, | ||
CraftVariable::EVENT_INIT, | ||
function (Event $event) { | ||
/** @var CraftVariable $variable */ | ||
$variable = $event->sender; | ||
|
||
$variable->attachBehavior('membership', CraftVariableBehavior::class); | ||
}); | ||
|
||
// Add CP Routes | ||
Event::on( | ||
UrlManager::class, | ||
UrlManager::EVENT_REGISTER_CP_URL_RULES, | ||
function (RegisterUrlRulesEvent $event) { | ||
$event->rules['membership'] = ['template' => 'membership/index']; | ||
$event->rules['membership/grants'] = 'membership/grants/index'; | ||
$event->rules['membership/grants/new'] = 'membership/grants/edit'; | ||
$event->rules['membership/grants/<grantId:\d+>'] = 'membership/grants/edit'; | ||
}); | ||
|
||
// Register Permissions | ||
Event::on( | ||
UserPermissions::class, | ||
UserPermissions::EVENT_REGISTER_PERMISSIONS, | ||
function (RegisterUserPermissionsEvent $event) { | ||
$event->permissions[Craft::t('membership', 'Membership')] = [ | ||
'membership-manageGrants' => ['label' => Craft::t('membership', 'Manage Grants')], | ||
'membership-viewLogs' => ['label' => Craft::t('membership', 'View Logs')], | ||
]; | ||
}); | ||
|
||
// Listen for key Subscription events: | ||
Event::on( | ||
Subscriptions::class, | ||
Subscriptions::EVENT_AFTER_CREATE_SUBSCRIPTION, | ||
function (SubscriptionEvent $event) { | ||
Membership::getInstance()->permissions->grantPermissionsForSubscription($event->subscription); | ||
}); | ||
|
||
Event::on( | ||
Subscriptions::class, | ||
Subscriptions::EVENT_AFTER_EXPIRE_SUBSCRIPTION, | ||
function (SubscriptionEvent $event) { | ||
Membership::getInstance()->permissions->revokePermissionsForSubscription($event->subscription); | ||
}); | ||
|
||
Event::on( | ||
Subscriptions::class, | ||
Subscriptions::EVENT_AFTER_SWITCH_SUBSCRIPTION_PLAN, | ||
function (SubscriptionSwitchPlansEvent $event) { | ||
// Revoke old permissions: | ||
Membership::getInstance()->permissions->revokePermissionsForSubscription($event->subscription, $event->oldPlan); | ||
|
||
// Grant new permissions: | ||
Membership::getInstance()->permissions->grantPermissionsForSubscription($event->subscription, $event->newPlan); | ||
}); | ||
|
||
// Display log messages with the relevant subscriptions: | ||
$view = Craft::$app->getView(); | ||
|
||
$view->hook('cp.commerce.subscriptions.edit.content', function (array &$context) use ($view) { | ||
return $view->renderTemplate('membership/_hooks/cp.commerce.subscriptions.edit', $context); | ||
}); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSettingsResponse() | ||
{ | ||
return Craft::$app->controller->redirect(UrlHelper::cpUrl('membership/grants')); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function settingsHtml() | ||
{ | ||
return Craft::$app->getView()->renderTemplate('membership/index'); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
/** | ||
* Membership plugin for Craft CMS 3.x | ||
* | ||
* Give your users special access based on their Commerce Subscriptions. | ||
* | ||
* @link https://oof.studio/ | ||
* @copyright Copyright (c) 2020 oof. Studio | ||
*/ | ||
|
||
namespace oofbar\membership\controllers; | ||
|
||
use Craft; | ||
|
||
/** | ||
* Base Admin Controller | ||
* | ||
* Enforces Admin access, and passes on the BaseController methods. | ||
* | ||
* @author oof. Studio | ||
* @package Membership | ||
* @since 1.0.0 | ||
*/ | ||
class BaseAdminController extends BaseController | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function beforeAction($action) | ||
{ | ||
$this->requireAdmin(); | ||
|
||
return parent::beforeAction($action); | ||
} | ||
} |
Oops, something went wrong.