├── .github └── workflows │ └── build.yml ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── spec └── ReadOnlyTraitSpec.php └── src ├── ReadOnlyException.php └── ReadOnlyTrait.php /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: PHP Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | build_php_version_82: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v2 14 | 15 | - name: Set up PHP 8.2 16 | uses: shivammathur/setup-php@v2 17 | with: 18 | php-version: 8.2 19 | 20 | - name: Install dependencies 21 | run: | 22 | sudo composer self-update 23 | composer install -n --prefer-dist --no-plugins 24 | 25 | - name: Run tests 26 | run: ./vendor/bin/kahlan -reporter=verbose 27 | 28 | build_php_version_83: 29 | runs-on: ubuntu-latest 30 | steps: 31 | - name: Checkout code 32 | uses: actions/checkout@v2 33 | 34 | - name: Set up PHP 8.3 35 | uses: shivammathur/setup-php@v2 36 | with: 37 | php-version: 8.3 38 | 39 | - name: Install dependencies 40 | run: | 41 | sudo composer self-update 42 | composer install -n --prefer-dist --no-plugins 43 | 44 | - name: Run tests 45 | run: ./vendor/bin/kahlan -reporter=verbose 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | /.idea 6 | .php_cs.cache 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Michael Chrisco 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Total Downloads](https://img.shields.io/packagist/dt/michaelachrisco/readonly.svg)](https://packagist.org/packages/michaelachrisco/readonly) 2 | ![build workflow](https://github.com/michaelachrisco/ReadOnlyTraitLaravel/actions/workflows/build.yml/badge.svg) 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/michaelachrisco/readonly.svg)](https://packagist.org/packages/michaelachrisco/readonly) 4 | 5 | 6 | # Laravel 5+ Read Only Models 7 | The read only trait removes the ability to save, delete or modify Laravel models. 8 | Ideally, this would be used in addition to DB permissions to ensure users and developers cannot write to a Legacy system. 9 | 10 | ## Install 11 | 12 | ``` 13 | composer require michaelachrisco/readonly 14 | ``` 15 | 16 | ## To use: 17 | 18 | 19 | ```php 20 | set_user_name('bob'); 29 | 30 | $result = $legacyUser->save(); 31 | //User is not saved. 32 | //ReadOnlyException is thrown. 33 | ?> 34 | ``` 35 | 36 | ## Methods that will throw ReadOnlyExceptions: 37 | 38 | * create 39 | * forceCreate 40 | * save 41 | * update 42 | * firstOrCreate 43 | * firstOrNew 44 | * delete 45 | * destroy 46 | * restore 47 | * forceDelete 48 | * performDeleteOnModel 49 | * push 50 | * finishSave 51 | * performUpdate 52 | * touch 53 | * insert 54 | * truncate 55 | * Add in a PR for any other methods you can find! 56 | 57 | ### 58 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "michaelachrisco/readonly", 3 | "description": "Readonly Models for Laravel 5+", 4 | "authors": [ 5 | { 6 | "name": "Michael Chrisco", 7 | "email": "michaelachrisco@gmail.com" 8 | } 9 | ], 10 | "keywords": ["laravel", "eloquent", "read-only"], 11 | "type": "library", 12 | "license": "MIT", 13 | "autoload": { 14 | "psr-4": {"MichaelAChrisco\\ReadOnly\\": "src/"} 15 | }, 16 | "require": { 17 | "php": ">=7.0.0" 18 | }, 19 | "require-dev": { 20 | "kahlan/kahlan": "^5.2.5", 21 | "illuminate/database": "=11.1.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spec/ReadOnlyTraitSpec.php: -------------------------------------------------------------------------------- 1 | create([]); 20 | })->toThrow(new ReadOnlyException('create', 'User')); 21 | }); 22 | }); 23 | describe("::forceCreate()", function () { 24 | it("is expected to throw ReadOnlyException", function () { 25 | $closure = function () { 26 | $user = new User; 27 | $user->forceCreate([]); 28 | }; 29 | expect($closure)->toThrow(new ReadOnlyException('forceCreate', 'User')); 30 | }); 31 | }); 32 | describe("::save()", function () { 33 | it("is expected to throw ReadOnlyException", function () { 34 | expect( 35 | function () { 36 | $user = new User; 37 | $user->save([]); 38 | })->toThrow(new ReadOnlyException('save', 'User')); 39 | }); 40 | }); 41 | describe("::update()", function () { 42 | it("is expected to throw ReadOnlyException", function () { 43 | expect( 44 | function () { 45 | $user = new User; 46 | $user->update([]); 47 | })->toThrow(new ReadOnlyException('update', 'User')); 48 | }); 49 | }); 50 | describe("::firstOrCreate()", function () { 51 | it("is expected to throw ReadOnlyException", function () { 52 | expect( 53 | function () { 54 | $user = new User; 55 | $user->firstOrCreate([]); 56 | })->toThrow(new ReadOnlyException('firstOrCreate', 'User')); 57 | }); 58 | }); 59 | describe("::firstOrNew()", function () { 60 | it("is expected to throw ReadOnlyException", function () { 61 | expect( 62 | function () { 63 | $user = new User; 64 | $user->firstOrNew([]); 65 | })->toThrow(new ReadOnlyException('firstOrNew', 'User')); 66 | }); 67 | }); 68 | describe("::delete()", function () { 69 | it("is expected to throw ReadOnlyException", function () { 70 | expect( 71 | function () { 72 | $user = new User; 73 | $user->delete(); 74 | })->toThrow(new ReadOnlyException('delete', 'User')); 75 | }); 76 | }); 77 | describe("::destroy()", function () { 78 | it("is expected to throw ReadOnlyException", function () { 79 | expect( 80 | function () { 81 | $user = new User; 82 | $user->destroy(1); 83 | })->toThrow(new ReadOnlyException('destroy', 'User')); 84 | }); 85 | }); 86 | describe("::restore()", function () { 87 | it("is expected to throw ReadOnlyException", function () { 88 | expect( 89 | function () { 90 | $user = new User; 91 | $user->restore(); 92 | })->toThrow(new ReadOnlyException('restore', 'User')); 93 | }); 94 | }); 95 | describe("::forceDelete()", function () { 96 | it("is expected to throw ReadOnlyException", function () { 97 | expect( 98 | function () { 99 | $user = new User; 100 | $user->forceDelete(); 101 | })->toThrow(new ReadOnlyException('forceDelete', 'User')); 102 | }); 103 | }); 104 | describe("::performDeleteOnModel()", function () { 105 | it("is expected to throw ReadOnlyException", function () { 106 | expect( 107 | function () { 108 | $user = new User; 109 | $user->performDeleteOnModel(); 110 | })->toThrow(new ReadOnlyException('performDeleteOnModel', 'User')); 111 | }); 112 | }); 113 | describe("::push()", function () { 114 | it("is expected to throw ReadOnlyException", function () { 115 | expect( 116 | function () { 117 | $user = new User; 118 | $user->push(); 119 | })->toThrow(new ReadOnlyException('push', 'User')); 120 | }); 121 | }); 122 | describe("::finishSave()", function () { 123 | it("is expected to throw ReadOnlyException", function () { 124 | expect( 125 | function () { 126 | $user = new User; 127 | $user->finishSave([]); 128 | })->toThrow(new ReadOnlyException('finishSave', 'User')); 129 | }); 130 | }); 131 | describe("::performUpdate()", function () { 132 | it("is expected to throw ReadOnlyException", function () { 133 | $user = new User; 134 | //TODO: Mock up 135 | // $user = new User; 136 | // $user->performUpdate(new Builder, []); 137 | unset($user); 138 | }); 139 | }); 140 | describe("::touch()", function () { 141 | it("is expected to throw ReadOnlyException", function () { 142 | expect( 143 | function () { 144 | $user = new User; 145 | $user->touch(); 146 | })->toThrow(new ReadOnlyException('touch', 'User')); 147 | }); 148 | }); 149 | describe("::truncate()", function () { 150 | it("is expected to throw ReadOnlyException", function () { 151 | expect( 152 | function () { 153 | $user = new User; 154 | $user->truncate(); 155 | })->toThrow(new ReadOnlyException('truncate', 'User')); 156 | }); 157 | }); 158 | describe("::insert()", function () { 159 | it("is expected to throw ReadOnlyException", function () { 160 | expect( 161 | function () { 162 | $user = new User; 163 | $user->insert(); 164 | })->toThrow(new ReadOnlyException('insert', 'User')); 165 | }); 166 | }); 167 | }); 168 | }); 169 | 170 | class MockModel 171 | { 172 | public static function create(array $attributes = []) 173 | { 174 | return true; 175 | } 176 | } 177 | 178 | class UserReadOnlyNotActive extends MockModel 179 | { 180 | use ReadOnlyTrait; 181 | 182 | protected static function isActive(): bool 183 | { 184 | return false; 185 | } 186 | } 187 | 188 | describe("UserReadOnlyNotActive", function () { 189 | context("When UserReadOnlyNotActive calls unsupported method and isActive is true", function () { 190 | describe("::create()", function () { 191 | it("expects `create()` to be toBeTruthy", function () { 192 | $user = new UserReadOnlyNotActive; 193 | expect($user->create([]))->toBeTruthy(); 194 | }); 195 | }); 196 | }); 197 | }); 198 | -------------------------------------------------------------------------------- /src/ReadOnlyException.php: -------------------------------------------------------------------------------- 1 |