Skip to content

Commit 1fb8b15

Browse files
author
Rafael Grigorian
committed
Fixed #47
1 parent 9db78a2 commit 1fb8b15

File tree

13 files changed

+5913
-5045
lines changed

13 files changed

+5913
-5045
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Firewall\ZoneLockdown;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Action;
6+
7+
/**
8+
* This controller inherits from a generic controller that implements the
9+
* base functionality for interfacing with a getter model. This action
10+
* simply loads the initial value through the Cloudflare API. The rest of
11+
* this class extends on that functionality and adds more endpoints.
12+
* @version 1.2.6
13+
* @package JetRails® Cloudflare
14+
* @author Rafael Grigorian <[email protected]>
15+
* @copyright © 2018 JETRAILS, All rights reserved
16+
* @license MIT https://opensource.org/licenses/MIT
17+
*/
18+
class Create extends Action {
19+
20+
/**
21+
* This method takes in all the information that is necessary for
22+
* creating a page rule through the request parameters. It then asks the
23+
* Cloudflare API model to create said page rule.
24+
* @return void
25+
*/
26+
public function execute () {
27+
$response = $this->_api->create (
28+
$this->_request->getParam ("description"),
29+
$this->_request->getParam ("urls"),
30+
$this->_request->getParam ("configurations"),
31+
$this->_request->getParam ("paused") == "true",
32+
$this->_request->getParam ("priority") ? intval ( $this->_request->getParam ("priority") ) : null
33+
);
34+
return $this->_sendResponse ( $response );
35+
}
36+
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Firewall\ZoneLockdown;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Action;
6+
7+
/**
8+
* This controller inherits from a generic controller that implements the
9+
* base functionality for interfacing with a getter model. This action
10+
* simply loads the initial value through the Cloudflare API. The rest of
11+
* this class extends on that functionality and adds more endpoints.
12+
* @version 1.2.6
13+
* @package JetRails® Cloudflare
14+
* @author Rafael Grigorian <[email protected]>
15+
* @copyright © 2018 JETRAILS, All rights reserved
16+
* @license MIT https://opensource.org/licenses/MIT
17+
*/
18+
class Delete extends Action {
19+
20+
/**
21+
* This action takes in a page rule id through the request parameters.
22+
* It then asks the Cloudflare API model to delete the page rule with
23+
* the corresponding page rule id.
24+
* @return void
25+
*/
26+
public function execute () {
27+
$response = $this->_api->delete ( $this->_request->getParam ("id") );
28+
return $this->_sendResponse ( $response );
29+
}
30+
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Firewall\ZoneLockdown;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Action;
6+
7+
/**
8+
* This controller inherits from a generic controller that implements the
9+
* base functionality for interfacing with a getter model. This action
10+
* simply loads the initial value through the Cloudflare API. The rest of
11+
* this class extends on that functionality and adds more endpoints.
12+
* @version 1.2.6
13+
* @package JetRails® Cloudflare
14+
* @author Rafael Grigorian <[email protected]>
15+
* @copyright © 2018 JETRAILS, All rights reserved
16+
* @license MIT https://opensource.org/licenses/MIT
17+
*/
18+
class Edit extends Action {
19+
20+
/**
21+
* This action takes in all the information that is necessary for
22+
* editing a page rule though the request parameters. It then asks the
23+
* Cloudflare API model to update the values of the page rule with the
24+
* corresponding page rule id.
25+
* @return void
26+
*/
27+
public function execute () {
28+
$response = $this->_api->edit (
29+
$this->_request->getParam ("id"),
30+
$this->_request->getParam ("description"),
31+
$this->_request->getParam ("urls"),
32+
$this->_request->getParam ("configurations"),
33+
$this->_request->getParam ("paused") == "true",
34+
$this->_request->getParam ("priority")
35+
? intval ( $this->_request->getParam ("priority") )
36+
: null
37+
);
38+
return $this->_sendResponse ( $response );
39+
}
40+
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Firewall\ZoneLockdown;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Getter;
6+
7+
/**
8+
* This controller inherits from a generic controller that implements the
9+
* base functionality for interfacing with a getter model. This action
10+
* simply loads the initial value through the Cloudflare API. The rest of
11+
* this class extends on that functionality and adds more endpoints.
12+
* @version 1.2.6
13+
* @package JetRails® Cloudflare
14+
* @author Rafael Grigorian <[email protected]>
15+
* @copyright © 2018 JETRAILS, All rights reserved
16+
* @license MIT https://opensource.org/licenses/MIT
17+
*/
18+
class Index extends Getter {}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Model\Adminhtml\Api\Firewall;
4+
5+
use JetRails\Cloudflare\Model\Adminhtml\Api\Getter;
6+
use JetRails\Cloudflare\Model\Adminhtml\Api\Request;
7+
8+
/**
9+
* This model inherits from the basic Getter model. It inherits
10+
* functionality that asks the Cloudflare API for a current setting value.
11+
* It then adds on to that functionality by adding more methods that
12+
* interact with the Cloudflare API.
13+
* @version 1.2.6
14+
* @package JetRails® Cloudflare
15+
* @author Rafael Grigorian <[email protected]>
16+
* @copyright © 2018 JETRAILS, All rights reserved
17+
* @license MIT https://opensource.org/licenses/MIT
18+
*/
19+
class ZoneLockdown extends Getter {
20+
21+
/**
22+
* @var string _endpoint Appended to zone endpoint
23+
* @var boolean _usePatchToSet Use PUT HTTP method
24+
*/
25+
protected $_endpoint = "firewall/lockdowns";
26+
protected $_usePatchToSet = false;
27+
28+
/**
29+
* This method wraps the parent method because we want to get the value
30+
* that the parent returns and then append the entitlements result to
31+
* it.
32+
* @return stdClass CF response to request
33+
*/
34+
public function getValue () {
35+
$result = (array) parent::getValue ();
36+
$result ["entitlements"] = $this->getEntitlements ();
37+
return (object) $result;
38+
}
39+
40+
/**
41+
* This method simply contacts the Cloudflare API and asks for a list of
42+
* entitlements. It then only returns the entitlements that are related
43+
* to page rules.
44+
* @return stdClass CF response to request
45+
*/
46+
public function getEntitlements () {
47+
$endpoint = $this->getEndpoint ("entitlements");
48+
$this->_requestModel->setType ( Request::REQUEST_GET );
49+
$response = $this->_requestModel->resolve ( $endpoint );
50+
return current ( array_filter ( $response->result, function ( $i ) {
51+
return $i->id === "zonelockdown.max_rules";
52+
}));
53+
}
54+
55+
/**
56+
* This method takes in information about a zone lockdown and creates it
57+
* using Cloudflare's API.
58+
* @param string desc Zone lockdown description
59+
* @param array urls Array of URLs to use
60+
* @param array config Mixtrure of IPs and IP Ranges
61+
* @param boolean paused Is it paused?
62+
* @param integer priority What is the priority, default: none
63+
* @return stdClass CF response to request
64+
*/
65+
public function create ( $desc, $urls, $config, $paused, $priority = null ) {
66+
$endpoint = $this->getEndpoint ();
67+
$this->_requestModel->setType ( Request::REQUEST_POST );
68+
$this->_requestModel->setData ( array (
69+
"description" => $desc,
70+
"urls" => $urls,
71+
"configurations" => $config,
72+
"paused" => $paused,
73+
"priority" => $priority
74+
));
75+
return $this->_requestModel->resolve ( $endpoint );
76+
}
77+
78+
/**
79+
* This method takes in information about an already created zone
80+
* lockdown and it updates it accordingly.
81+
* using Cloudflare's API.
82+
* @param string id Page rule ID
83+
* @param string desc Zone lockdown description
84+
* @param array urls Array of URLs to use
85+
* @param array config Mixtrure of IPs and IP Ranges
86+
* @param boolean paused Is it paused?
87+
* @param integer priority What is the priority, default: none
88+
* @return stdClass CF response to request
89+
*/
90+
public function edit ( $id, $desc, $urls, $config, $paused, $priority = null ) {
91+
$endpoint = $this->getEndpoint ( $this->_endpoint . "/$id" );
92+
$this->_requestModel->setType ( $this->_usePatchToSet ? Request::REQUEST_PATCH : Request::REQUEST_PUT );
93+
$this->_requestModel->setData ( array (
94+
"description" => $desc,
95+
"urls" => $urls,
96+
"configurations" => $config,
97+
"paused" => $paused,
98+
"priority" => $priority
99+
));
100+
return $this->_requestModel->resolve ( $endpoint );
101+
}
102+
103+
/**
104+
* This method takes in a zone lockdown rule id and it attempts to
105+
* delete it using the Cloudflare API.
106+
* @param string id Page rule ID
107+
* @return stdClass CF response to request
108+
*/
109+
public function delete ( $id ) {
110+
$endpoint = $this->getEndpoint ( $this->_endpoint . "/$id" );
111+
$this->_requestModel->setType ( Request::REQUEST_DELETE );
112+
return $this->_requestModel->resolve ( $endpoint );
113+
}
114+
115+
}

src/app/code/JetRails/Cloudflare/etc/acl.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
<resource
129129
id="JetRails_Cloudflare::unmetered_ddos_mitigation"
130130
title="Unmetered DDoS Mitigation" />
131+
<resource
132+
id="JetRails_Cloudflare::zone_lockdown"
133+
title="Zone Lockdown" />
131134
</resource>
132135
<resource
133136
id="JetRails_Cloudflare::settings_tab"

src/app/code/JetRails/Cloudflare/view/adminhtml/templates/firewall.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
echo $block->renderSections ( "firewall/tools_tab", array (
1818
"access_rules",
1919
"user_agent_blocking",
20-
"unmetered_ddos_mitigation"
20+
"unmetered_ddos_mitigation",
21+
"zone_lockdown"
2122
));
2223

2324
?>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<section
2+
class="cloudflare firewall zone_lockdown initialize loading"
3+
data-endpoint="<?php echo $block->getApiEndpoint () ?>"
4+
data-form-key="<?php echo $block->getFormKey () ?>"
5+
data-tab-name="firewall"
6+
data-section-name="zone_lockdown" >
7+
<div class="row" >
8+
<div class="wrapper_left" >
9+
<span class="section_title" >Zone Lockdown</span>
10+
<p>Lockdown a specific URL on your zone to specific IP addresses. This is useful to protect an admin or protected area from non-specified IP addresses.</p>
11+
<p><strong>You have used <span id="rules_used" >0</span> of <span id="rules_total" >0</span> Zone Lockdown Rules</strong></p>
12+
</div>
13+
<div class="wrapper_right" >
14+
<div>
15+
<input id="action" class="trigger" type="button" value="Create Lockdown Rule" data-target="create" />
16+
</div>
17+
</div>
18+
</div>
19+
<div class="row" >
20+
<table class="rules" cellpadding="0" cellspacing="0" >
21+
<thead>
22+
<tr>
23+
<th>Rule Name/Description</th>
24+
<th></th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
<tr>
29+
<td colspan="2" >You currently have no Zone Lockdown Rules. To create some click on the button above.</td>
30+
</tr>
31+
</tbody>
32+
</table>
33+
</div>
34+
<div class="lightbox" style="background-image: url(<?php echo $block->getViewFileUrl ('JetRails_Cloudflare/images/loading.svg') ?>)" ></div>
35+
<div class="row collapsable" >
36+
<div class="wrapper_bottom" >
37+
<a data-tab="help" >Help</a>
38+
</div>
39+
<div data-tab-content="help" >
40+
<p>Zone Lockdown allows you to allow specific IP addresses and IP ranges whereby all other IPs are effectively blocked. This is best used to protect an Administrative area of a website to, e.g. an office or VPN.</p>
41+
<p>Zone Lockdown supports:<br>Specific sub-domains, allowing you to, for example, allow IP 1.2.3.4 to access domain foo.example.com and allow IP 5.6.7.8 to access domain bar.example.com, but not necessarily allow the vice versa.<br>Specific URLs, allowing you to, for example, allow IP 1.2.3.4 to access directory example.com/foo/<em> and allow IP 5.6.7.8 to access directory example.com/bar/</em>, but not necessarily allow the vice versa.<br>This is useful when you need more granularity in your IP Access Rules since, with the IP Firewall, you can only either apply the block to all sub-domains of the current domain, or all domains on your account, and you can not specify URIs.</p>
42+
<p>Advanced Options:<br>In a scenario where you have multiple Zone Lockdown rules, which overlap, you can set a priority to ensure one Zone Lockdown rule is honored over another. For example, if you protected example.com/admin/<em> but also had example.com/admin/api/</em> - and the users to API had a different set of IP allowed IP addresses, then this would keep this area protected.</p>
43+
<p>The <em>lower</em> the number, the <em>higher</em> the priority. Higher priority rules take precedence over those with lower priority.</p>
44+
</div>
45+
</div>
46+
</section>

src/app/code/JetRails/Cloudflare/view/adminhtml/web/css/bundle.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)