Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdiggle committed Mar 26, 2018
0 parents commit 39cf99e
Show file tree
Hide file tree
Showing 20 changed files with 845 additions and 0 deletions.
1 change: 1 addition & 0 deletions .craftplugin
@@ -0,0 +1 @@
{"pluginName":"Gatekeeper","pluginDescription":"Protect your craftcms website with a simple password form.","pluginVersion":"0.1.0","pluginAuthorName":"Tom Diggle","pluginVendorName":"tomdiggle","pluginAuthorUrl":"http://tomdiggle.com","pluginAuthorGithub":"tomdiggle","codeComments":"yes","pluginComponents":["controllers","settings"],"consolecommandName":"","controllerName":"Gatekeeper","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
32 changes: 32 additions & 0 deletions .gitignore
@@ -0,0 +1,32 @@
# CRAFT ENVIRONMENT
.env.php
.env.sh
.env

# COMPOSER
/vendor

# BUILD FILES
/bower_components/*
/node_modules/*
/build/*
/yarn-error.log

# MISC FILES
.cache
.DS_Store
.idea
.project
.settings
*.esproj
*.sublime-workspace
*.sublime-project
*.tmproj
*.tmproject
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
config.codekit3
prepros-6.config
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,9 @@
# Gatekeeper Changelog

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/).

## 0.1.0 - 2018-03-20
### Added
- Initial release
9 changes: 9 additions & 0 deletions LICENSE.md
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2018 Tom Diggle

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.
43 changes: 43 additions & 0 deletions README.md
@@ -0,0 +1,43 @@
# Gatekeeper plugin for Craft CMS 3.x

Protect your Craft CMS website from access with a universal password.

![Screenshot](resources/img/plugin-logo.png)

## Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later.

## Installation

To install the plugin, follow these instructions.

1. Open your terminal and go to your Craft project:

cd /path/to/project

2. Then tell Composer to load the plugin:

composer require tomdiggle/gatekeeper

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

## Gatekeeper Overview

-Insert text here-

## Configuring Gatekeeper

-Insert text here-

## Using Gatekeeper

-Insert text here-

## Gatekeeper Roadmap

Some things to do, and ideas for potential features:

* Release it

Brought to you by [Tom Diggle](http://tomdiggle.com)
40 changes: 40 additions & 0 deletions composer.json
@@ -0,0 +1,40 @@
{
"name": "tomdiggle/gatekeeper",
"description": "Protect your Craft CMS website from access with a universal password.",
"type": "craft-plugin",
"version": "0.1.0",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"gatekeeper"
],
"support": {
"docs": "https://github.com/tomdiggle/gatekeeper/blob/master/README.md",
"issues": "https://github.com/tomdiggle/gatekeeper/issues"
},
"license": "MIT",
"authors": [
{
"name": "Tom Diggle",
"homepage": "http://tomdiggle.com"
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1"
},
"autoload": {
"psr-4": {
"tomdiggle\\gatekeeper\\": "src/"
}
},
"extra": {
"name": "Gatekeeper",
"handle": "gatekeeper",
"hasCpSettings": true,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/tomdiggle/gatekeeper/master/CHANGELOG.md",
"class": "tomdiggle\\gatekeeper\\Gatekeeper"
}
}
Binary file added resources/img/plugin-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
189 changes: 189 additions & 0 deletions src/Gatekeeper.php
@@ -0,0 +1,189 @@
<?php
/**
* Gatekeeper plugin for Craft CMS 3.x
*
* Protect your Craft CMS website from access with a universal password.
*
* @link http://tomdiggle.com
* @copyright Copyright (c) 2018 Tom Diggle
*/

namespace tomdiggle\gatekeeper;

use tomdiggle\gatekeeper\models\Settings;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\Application;
use craft\web\UrlManager;
use craft\web\Session;
use craft\web\View;
use craft\events\RegisterUrlRulesEvent;

use yii\base\Event;

/**
* TODO:
* When loging in redirect user to URL they wanted to go to eg. /about
* Improve styling of login screen
* Display errors when entering wrong pwd
*/

/**
* @author Tom Diggle
* @package Gatekeeper
* @since 0.1.0
*
* @property Settings $settings
* @method Settings getSettings()
*/
class Gatekeeper extends Plugin
{
// Static Properties
// =========================================================================

/**
* @var Gatekeeper
*/
public static $plugin;

/**
* @var Settings
*/
public static $settings;

// Public Properties
// =========================================================================

/**
* @var string
*/
public $schemaVersion = '0.1.0';

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function init()
{
parent::init();

self::$plugin = $this;
self::$settings = Gatekeeper::$plugin->getSettings();

$this->registerEventListeners();

Craft::info(
Craft::t(
'gatekeeper',
'{name} plugin loaded',
['name' => $this->name]
),
__METHOD__
);
}

/**
* @return bool
*/
public function isAuthenticated(): bool
{
return Craft::$app->getRequest()->getCookies()->get('gatekeeper') !== null;
}

// Protected Methods
// =========================================================================

/**
*
*/
protected function registerEventListeners()
{
// Register our site routes
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
$event->rules['gatekeeper'] = 'gatekeeper/gatekeeper';
$event->rules['gatekeeper/login'] = 'gatekeeper/gatekeeper/login';
}
);

// Handler: EVENT_AFTER_LOAD_PLUGINS
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_LOAD_PLUGINS,
function () {
// Only respond to non-console site requests
$request = Craft::$app->getRequest();
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
$this->handleSiteRequests();
}
}
);
}

/**
*
*/
protected function handleSiteRequests()
{
// Handler: View::EVENT_BEFORE_RENDER_TEMPLATE
Event::on(
View::class,
View::EVENT_BEFORE_RENDER_TEMPLATE,
function () {
Craft::debug(
'View::EVENT_BEFORE_RENDER_TEMPLATE',
__METHOD__
);

if ($this->isGuest() && !$this->isAuthenticated() && !$this->isGatekeeperRequest()) {
Craft::$app->getResponse()->redirect('gatekeeper');
}
}
);
}

/**
* @return bool
*/
protected function isGuest(): bool
{
return Craft::$app->getUser()->getIsGuest();
}

/**
* @return bool
*/
protected function isGatekeeperRequest(): bool
{
$url = Craft::$app->getRequest()->getUrl();
return stripos($url, 'gatekeeper');
}

/**
* @inheritdoc
*/
protected function createSettingsModel()
{
return new Settings();
}

/**
* @inheritdoc
*/
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'gatekeeper/settings',
[
'settings' => $this->getSettings()
]
);
}
}
65 changes: 65 additions & 0 deletions src/assetbundles/gatekeeper/GatekeeperAsset.php
@@ -0,0 +1,65 @@
<?php
/**
* Gatekeeper plugin for Craft CMS 3.x
*
* Protect your Craft CMS website from access with a universal password.
*
* @link http://tomdiggle.com
* @copyright Copyright (c) 2018 Tom Diggle
*/

namespace tomdiggle\gatekeeper\assetbundles\Gatekeeper;

use Craft;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

/**
* GatekeeperAsset AssetBundle
*
* AssetBundle represents a collection of asset files, such as CSS, JS, images.
*
* Each asset bundle has a unique name that globally identifies it among all asset bundles used in an application.
* The name is the [fully qualified class name](http://php.net/manual/en/language.namespaces.rules.php)
* of the class representing it.
*
* An asset bundle can depend on other asset bundles. When registering an asset bundle
* with a view, all its dependent asset bundles will be automatically registered.
*
* http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
*
* @author Tom Diggle
* @package Gatekeeper
* @since 0.1.0
*/
class GatekeeperAsset extends AssetBundle
{
// Public Methods
// =========================================================================

/**
* Initializes the bundle.
*/
public function init()
{
// define the path that your publishable resources live
$this->sourcePath = "@tomdiggle/gatekeeper/assetbundles/gatekeeper/dist";

// define the dependencies
$this->depends = [
CpAsset::class,
];

// define the relative path to CSS/JS files that should be registered with the page
// when this asset bundle is registered
$this->js = [
'js/Gatekeeper.js',
];

$this->css = [
'css/Gatekeeper.css',
];

parent::init();
}
}

0 comments on commit 39cf99e

Please sign in to comment.