Skip to content

Commit

Permalink
Merge pull request #16 from venveo/develop
Browse files Browse the repository at this point in the history
2.1.0
  • Loading branch information
Mosnar authored Oct 8, 2019
2 parents 4ab1624 + ec454c1 commit f63d0c4
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 75 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.1.0 - 2019-10-08
### Added
- Added project config support
- Added events `EVENT_BEFORE_APP_DELETED` and `EVENT_AFTER_APP_DELETED`

### Changed
- Minimum Craft version require is now 3.1.34.3
- Events now extend `ModelEvent`
- Optimized event triggers

### Fixed
- Fixed deleting apps

## 2.0.4 - 2019-10-03
### Added
- Added renderConnector() to app model
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OAuth 2.0 Client plugin for Craft CMS 3.1
OAuth 2.0 Client plugin for Craft CMS 3
===

This plugin provides developers with an easy centralized approach to managing and storing OAuth 2.0
Expand All @@ -7,6 +7,13 @@ clients and tokens.
It exposes an easy to use API and frontend for authorizing tokens for internal business logic. What it does not do is
act as an authentication provider for users to login to the CMS.

## Features
- Simple API for integrating League OAuth Providers
- Lots of events for developers
- CLI for refreshing tokens
- Project config support
- 1-line Twig helper for generating authentication UI in your module

## Example Use Cases
- Building a custom CRM integration
- Reading from and writing to Google Sheets
Expand All @@ -19,7 +26,7 @@ act as an authentication provider for users to login to the CMS.

## Requirements

This plugin should work on Craft CMS 3.1.0 or later, and likely earlier versions of Craft.
This plugin should work on Craft CMS 3.1.34.3 or later

## Installation

Expand All @@ -35,6 +42,8 @@ To install the plugin, follow these instructions.

3. In the Control Panel, go to Settings → Plugins and click the “Install” button for OAuth 2.0 Client.

4. Configure on Craft settings page

---

## Providers
Expand Down Expand Up @@ -130,6 +139,10 @@ Generally, you'll only find yourself using the `Apps` and `Credentials` services
- `venveo\oauthclient\events\AppEvent`
- `Apps:EVENT_AFTER_APP_SAVED`
- `venveo\oauthclient\events\AppEvent`
- `Apps:EVENT_BEFORE_APP_DELETED`
- `venveo\oauthclient\events\AppEvent`
- `Apps:EVENT_AFTER_APP_DELETED`
- `venveo\oauthclient\events\AppEvent`

#### `venveo\oauthclient\services\Tokens`

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "venveo/craft-oauthclient",
"description": "Simple OAuth 2.0 client",
"type": "craft-plugin",
"version": "2.0.4",
"version": "2.1.0",
"keywords": [
"craft",
"cms",
Expand All @@ -18,11 +18,11 @@
"authors": [
{
"name": "Venveo",
"homepage": "https://venveo.com"
"homepage": "https://www.venveo.com"
}
],
"require": {
"craftcms/cms": "^3.1.0",
"craftcms/cms": "^3.1.34.3",
"league/oauth2-client": "^2.2.1",
"league/oauth2-google": "^2.2",
"league/oauth2-facebook": "^2.0",
Expand Down
42 changes: 42 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

use Craft;
use craft\base\Plugin as BasePlugin;
use craft\events\RebuildConfigEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\log\FileTarget;
use craft\services\ProjectConfig;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use venveo\oauthclient\services\Apps as AppsService;
Expand Down Expand Up @@ -40,6 +42,7 @@ class Plugin extends BasePlugin
{
// Static Properties
// =========================================================================
public static $PROJECT_CONFIG_KEY = 'oauthClient';

/**
* @var Plugin
Expand Down Expand Up @@ -74,6 +77,7 @@ public function init()
$this->_setComponents();
$this->_registerCpRoutes();
$this->_registerVariables();
$this->_registerProjectConfig();
}

// Protected Methods
Expand Down Expand Up @@ -126,12 +130,50 @@ private function _registerCpRoutes()
'oauthclient/apps' => 'oauthclient/apps/index',
'oauthclient/apps/new' => 'oauthclient/apps/edit',
'oauthclient/apps/<handle:{handle}>' => 'oauthclient/apps/edit',
'oauthclient/apps/delete' => 'oauthclient/apps/delete',
'oauthclient/authorize/refresh/<id:\d+>' => 'oauthclient/authorize/refresh',
'oauthclient/authorize/<handle:{handle}>' => 'oauthclient/authorize/authorize-app',
]);
});
}

/**
* Register project config handlers
*/
private function _registerProjectConfig(): void
{
Craft::$app->projectConfig
->onAdd(self::$PROJECT_CONFIG_KEY . '.apps.{uid}', [$this->apps, 'handleUpdatedApp'])
->onUpdate(self::$PROJECT_CONFIG_KEY . '.apps.{uid}', [$this->apps, 'handleUpdatedApp'])
->onRemove(self::$PROJECT_CONFIG_KEY . '.apps.{uid}', [$this->apps, 'handleRemovedApp']);

Event::on(ProjectConfig::class, ProjectConfig::EVENT_REBUILD, function(RebuildConfigEvent $e) {
$this->_handleProjectConfigRebuild($e);
});
}

/**
* Handle project config rebuilding
* @param RebuildConfigEvent $e
*/
private function _handleProjectConfigRebuild(RebuildConfigEvent $e): void
{
$appData = [];
$apps = $this->apps->getAllApps();
foreach($apps as $app) {
$appData[$app->uid] = [
'name' => $app->name,
'handle' => $app->handle,
'provider' => $app->provider,
'clientId' => $app->clientId,
'clientSecret' => $app->clientSecret,
'scopes' => $app->scopes
];
}

$e->config[self::$PROJECT_CONFIG_KEY]['apps'] = $appData;
}

/**
* Set our Twig variable
*/
Expand Down
50 changes: 42 additions & 8 deletions src/controllers/AppsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use craft\web\Response;
use venveo\oauthclient\models\App as AppModel;
use venveo\oauthclient\Plugin;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;

/**
* @author Venveo
Expand Down Expand Up @@ -43,6 +45,9 @@ public function actionIndex()
*/
public function actionEdit($handle = null, $app = null)
{
if(!Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {
throw new ForbiddenHttpException('Administrative changes are disallowed in this environment');
}
$this->requireAdmin();
$variables = [
'handle' => $handle,
Expand Down Expand Up @@ -82,34 +87,64 @@ public function actionEdit($handle = null, $app = null)
return $this->renderTemplate('oauthclient/apps/_edit', $variables);
}

/**
* Attempt to delete an app by its ID
* @return \yii\web\Response
* @throws \yii\db\Exception
* @throws \yii\web\BadRequestHttpException
* @throws \yii\web\ForbiddenHttpException
*/
public function actionDelete() {
if(!Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {
throw new ForbiddenHttpException('Administrative changes are disallowed in this environment');
}
$this->requireAdmin();
$this->requirePostRequest();

$request = Craft::$app->getRequest();
$id = $request->getRequiredBodyParam('id');
$app = Plugin::$plugin->apps->getAppById($id);

if (!$app) {
throw new NotFoundHttpException('App does not exist');
}

Plugin::$plugin->apps->deleteApp($app);

return $this->asJson(['success' => true]);
}

/**
* @return Response|null
* @throws HttpException
* @throws \Exception
*/
public function actionSave()
{
if(!Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {
throw new ForbiddenHttpException('Administrative changes are disallowed in this environment');
}
$this->requireAdmin();
$this->requirePostRequest();

$request = Craft::$app->getRequest();
$gatewayService = Plugin::getInstance()->apps;
$appService = Plugin::getInstance()->apps;

$scopes = $request->getBodyParam('scopes');
$scopes = implode(',', ArrayHelper::getColumn($scopes, 'scope'));

$config = [
'id' => $request->getBodyParam('id'),
'provider' => $request->getBodyParam('provider'),
'name' => $request->getBodyParam('name'),
'handle' => $request->getBodyParam('handle'),
'clientId' => $request->getBodyParam('clientId'),
'clientSecret' => $request->getBodyParam('clientSecret'),
'provider' => $request->getRequiredBodyParam('provider'),
'name' => $request->getRequiredBodyParam('name'),
'handle' => $request->getRequiredBodyParam('handle'),
'clientId' => $request->getRequiredBodyParam('clientId'),
'clientSecret' => $request->getRequiredBodyParam('clientSecret'),
'scopes' => $scopes
];

/** @var AppModel $app */
$app = $gatewayService->createApp($config);
$app = $appService->createApp($config);

$session = Craft::$app->session;

Expand All @@ -120,7 +155,6 @@ public function actionSave()
Craft::$app->getUrlManager()->setRouteParams([
'app' => $app
]);

return null;
}

Expand Down
11 changes: 3 additions & 8 deletions src/events/AppEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@

namespace venveo\oauthclient\events;

use craft\events\ModelEvent;
use venveo\oauthclient\models\App;
use yii\base\Event;

class AppEvent extends Event
class AppEvent extends ModelEvent
{
public function __construct(App $app)
{
parent::__construct();
$this->app = $app;
}

/** @var App */
public $app;

}
15 changes: 4 additions & 11 deletions src/events/TokenEvent.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
<?php
/**
* OAuth 2.0 Client plugin for Craft CMS 3
* @link https://www.venveo.com
* @copyright Copyright (c) 2018-2019 Venveo
* @link https://www.venveo.com
* @copyright Copyright (c) 2018-2019 Venveo
*/

namespace venveo\oauthclient\events;

use venveo\oauthclient\models\App;
use craft\events\ModelEvent;
use venveo\oauthclient\models\Token;
use yii\base\Event;

class TokenEvent extends Event
class TokenEvent extends ModelEvent
{
public function __construct(Token $token)
{
parent::__construct();
$this->token = $token;
}

/** @var Token */
public $token;
}
4 changes: 3 additions & 1 deletion src/models/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
* @property array $allTokens
* @property string $redirectUrl
* @property string $cpEditUrl
* @property string uid
*/
class App extends Model
{
public $uid;
public $id;
public $dateCreated;
public $dateUpdated;
Expand Down Expand Up @@ -198,7 +200,7 @@ public function renderConnector() {
public function rules()
{
return [
[['userId', 'handle', 'name', 'clientId', 'clientSecret', 'provider'], 'required'],
[['handle', 'name', 'clientId', 'clientSecret', 'provider'], 'required'],
[
['handle'],
UniqueValidator::class,
Expand Down
Loading

0 comments on commit f63d0c4

Please sign in to comment.