Skip to content

Commit f8e4058

Browse files
jcohlmeyerJRC9 Devs
authored andcommitted
🐣
0 parents  commit f8e4058

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Commands/FixCommand.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Statamic\Addons\Permissions\Commands;
4+
5+
use Statamic\Extend\Command;
6+
use Statamic\API\Path;
7+
8+
class FixCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'permissions:fix';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Fix file permissions!';
23+
24+
/**
25+
* Create a new command instance.
26+
*/
27+
public function __construct()
28+
{
29+
parent::__construct();
30+
}
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return mixed
36+
*/
37+
public function handle()
38+
{
39+
// Get the Configuration or Defaults
40+
$site_root = escapeshellarg(Path::makeFull('/'));
41+
$shell_path = getenv('PERMISSIONS_SHELL_PATH') ? getenv('PERMISSIONS_SHELL_PATH') : '/usr/bin';
42+
$user = getenv('PERMISSIONS_USER') ? getenv('PERMISSIONS_USER') : 'www-data';
43+
$group = getenv('PERMISSIONS_GROUP') ? getenv('PERMISSIONS_GROUP') : 'www-data';
44+
$folder_permissions = getenv('PERMISSIONS_FOLDERS') ? getenv('PERMISSIONS_FOLDERS') : '775';
45+
$file_permissions = getenv('PERMISSIONS_FILES') ? getenv('PERMISSIONS_FILES') : '664';
46+
$lock_statamic = getenv('PERMISSIONS_LOCK_STATAMIC') ? getenv('PERMISSIONS_LOCK_STATAMIC') : false;
47+
$locked_user = getenv('PERMISSIONS_LOCKED_USER') ? getenv('PERMISSIONS_LOCKED_USER') : 'root';
48+
$locked_group = getenv('PERMISSIONS_LOCKED_GROUP') ? getenv('PERMISSIONS_LOCKED_GROUP') : 'root';
49+
50+
// Set Up Enviroment for the Shell
51+
putenv('SHELL=' . $shell_path);
52+
53+
// Fix File Ownership
54+
$this->info('Fixing ownership, this may take a while ...');
55+
shell_exec('chown -R ' . $user . ':' . $group .' ' . $site_root);
56+
57+
// Fix Directory Permissions
58+
$this->info('Fixing directory permissions, this may take a while ...');
59+
shell_exec('find ' . $site_root . ' -type d -exec chmod ' . $folder_permissions . ' {} \;');
60+
61+
// Fix File Permissions
62+
$this->info('Fixing file permissions, this may take a while ...');
63+
shell_exec('find ' . $site_root . ' -type f -exec chmod ' . $file_permissions . ' {} \;');
64+
65+
// Lock Statamic
66+
if ($lock_statamic) {
67+
$this->info('Securing git folder, this may take a while ...');
68+
shell_exec('chown -R ' . $locked_user . ':' . $locked_group .' ' . $site_root . '.git');
69+
shell_exec('chmod -R 770 ' . $site_root . '.git');
70+
71+
$this->info('Securing Statamic directory, this may take a while ...');
72+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'statamic');
73+
shell_exec('find ' . $site_root . 'statamic -type d -exec chmod 755 {} \;');
74+
shell_exec('find ' . $site_root . 'statamic -type f -exec chmod 640 {} \;');
75+
shell_exec('find ' . $site_root . 'statamic/resources/dist -type f -exec chmod 644 {} \;');
76+
77+
$this->info('Securing addons directory, this may take a while ...');
78+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/addons');
79+
shell_exec('find ' . $site_root . 'site/addons -type d -exec chmod 755 {} \;');
80+
shell_exec('find ' . $site_root . 'site/addons -type f -exec chmod 640 {} \;');
81+
shell_exec('find ' . $site_root . 'site/addons/*/resources/assets -type f -exec chmod 644 {} \;');
82+
83+
$this->info('Securing content directory, this may take a while ...');
84+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/content');
85+
shell_exec('find ' . $site_root . 'site/content -type d -exec chmod 770 {} \;');
86+
shell_exec('find ' . $site_root . 'site/content -type f -exec chmod 660 {} \;');
87+
88+
$this->info('Securing database directory, this may take a while ...');
89+
shell_exec('chown -R ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'site/database');
90+
shell_exec('find ' . $site_root . 'site/database -type d -exec chmod 770 {} \;');
91+
shell_exec('find ' . $site_root . 'site/database -type f -exec chmod 660 {} \;');
92+
93+
$this->info('Securing settings directory, this may take a while ...');
94+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/settings');
95+
shell_exec('find ' . $site_root . 'site/settings -type d -exec chmod 770 {} \;');
96+
shell_exec('find ' . $site_root . 'site/settings -type f -exec chmod 660 {} \;');
97+
98+
$this->info('Securing storage directory, this may take a while ...');
99+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/storage');
100+
shell_exec('find ' . $site_root . 'site/storage -type d -exec chmod 770 {} \;');
101+
shell_exec('find ' . $site_root . 'site/storage -type f -exec chmod 660 {} \;');
102+
103+
$this->info('Securing tests directory, this may take a while ...');
104+
shell_exec('chown -R ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'site/tests');
105+
shell_exec('find ' . $site_root . 'site/tests -type d -exec chmod 770 {} \;');
106+
shell_exec('find ' . $site_root . 'site/tests -type f -exec chmod 660 {} \;');
107+
108+
$this->info('Securing themes directory, this may take a while ...');
109+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/themes');
110+
shell_exec('find ' . $site_root . 'site/themes -type d -exec chmod 755 {} \;');
111+
shell_exec('find ' . $site_root . 'site/themes -type f -exec chmod 640 {} \;');
112+
shell_exec('find ' . $site_root . 'site/themes/*/assets -type f -exec chmod 644 {} \;');
113+
shell_exec('find ' . $site_root . 'site/themes/*/dist -type f -exec chmod 644 {} \;');
114+
shell_exec('find ' . $site_root . 'site/themes/*/css -type f -exec chmod 644 {} \;');
115+
shell_exec('find ' . $site_root . 'site/themes/*/js -type f -exec chmod 644 {} \;');
116+
shell_exec('find ' . $site_root . 'site/themes/*/vendor -type f -exec chmod 644 {} \;');
117+
118+
$this->info('Securing users directory, this may take a while ...');
119+
shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/users');
120+
shell_exec('find ' . $site_root . 'site/users -type d -exec chmod 770 {} \;');
121+
shell_exec('find ' . $site_root . 'site/users -type f -exec chmod 660 {} \;');
122+
123+
$this->info('Securing local directory, this may take a while ...');
124+
shell_exec('find ' . $site_root . 'local -type d -exec chmod 770 {} \;');
125+
shell_exec('find ' . $site_root . 'local -type f -exec chmod 660 {} \;');
126+
127+
$this->info('Securing base files, this may take a while ...');
128+
shell_exec('chown ' . $locked_user . ':' . $group .' ' . $site_root . '.env');
129+
shell_exec('chmod 640 ' . $site_root . '.env');
130+
shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . '.gitignore');
131+
shell_exec('chmod 660 ' . $site_root . '.gitignore');
132+
shell_exec('chown ' . $locked_user . ':' . $group .' ' . $site_root . '.htaccess');
133+
shell_exec('chmod 754 ' . $site_root . '.htaccess');
134+
shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'favicon.ico');
135+
shell_exec('chown ' . $locked_user . ':' . $group .' ' . $site_root . 'index.php');
136+
shell_exec('chmod 754 ' . $site_root . 'index.php');
137+
shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'phpunit.xml');
138+
shell_exec('chmod 660 ' . $site_root . 'phpunit.xml');
139+
shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'please');
140+
shell_exec('chmod 660 ' . $site_root . 'please');
141+
shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'robots.txt');
142+
}
143+
144+
// Display an All Clear Message :)
145+
$this->info('All done! If the script reported any "Operation not permitted" errors, try running it again with sudo.');
146+
}
147+
}

license.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 JRC9 Design Studio
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

meta.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Permissions
2+
version: 0.0.1
3+
description: Fix & Lock down your file & folder permissions
4+
url: https://github.com/jrc9designstudio/statamic-permissions
5+
developer: JRC9 Design Studio
6+
developer_url: https://github.com/jrc9designstudio

readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Permissions for Statamic 2
2+
*Requirement:* Statamic v2.x
3+
*Version:* 0.0.1 Alpha
4+
5+
## Caution
6+
This project has not been widley tested, and only works for sites have not been moved above the web root. Only use this if you have a good understanding of file permissions as this could break your entire website, if the permissions are set wrong.
7+
8+
### What is this?
9+
A CLI tool to help correct & Secure file and folder permissions for Statamic
10+
11+
### Installation
12+
- Rename the folder `Permissions` and copy it to your `site/addons` folder
13+
14+
### Usage
15+
- Change the seetings in your enviroment file (samples found in sample.env)
16+
- Use `sudo php please permissions:fix` from the command line.
17+
18+
### Notes
19+
- `PERMISSIONS_LOCKED_USER` and `PERMISSIONS_LOCKED_GROUP` should propaly be set to the user that you use to edit / deploy code.
20+
- Using the `PERMISSIONS_LOCK_STATAMIC` option will break the web updater, but some of us want to only be able to update Statamic via the CLI.

sample.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PERMISSIONS_SHELL_PATH=/usr/bin
2+
PERMISSIONS_USER=www-data
3+
PERMISSIONS_GROUP=www-data
4+
PERMISSIONS_FOLDERS=775
5+
PERMISSIONS_FILES=664
6+
PERMISSIONS_LOCK_STATAMIC=false
7+
PERMISSIONS_LOCKED_USER=root
8+
PERMISSIONS_LOCKED_GROUP=root

0 commit comments

Comments
 (0)