├── .github └── workflows │ └── CI.yml ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── terms.php ├── database └── migrations │ ├── create_terms_table.php.stub │ └── create_user_terms_table.php.stub ├── resources ├── lang │ └── en │ │ └── terms.php └── views │ └── show.blade.php ├── routes └── web.php └── src ├── Events └── AgreedToTerms.php ├── Http ├── Controllers │ └── TermsController.php └── Middleware │ └── AcceptedTerms.php.stub ├── LaravelTerms.php ├── LaravelTermsFacade.php ├── LaravelTermsServiceProvider.php ├── Models └── Term.php └── Traits └── AcceptsTerms.php /.github/workflows/CI.yml: -------------------------------------------------------------------------------- 1 | # GithHub Actions Workflow generated with Ghygen 2 | name: CI 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - develop 8 | pull_request: 9 | branches: 10 | - main 11 | - develop 12 | 13 | jobs: 14 | laravel-tests: 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | operating-system: [ubuntu-latest] 20 | php-versions: [ '8.0','7.4' ] 21 | dependency-stability: [ prefer-stable ] 22 | 23 | laravel: [ '8.*','7.*' ] 24 | include: 25 | - laravel: 8.* 26 | testbench: 6.* 27 | - laravel: 7.* 28 | testbench: 5.* 29 | 30 | name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}} 31 | 32 | steps: 33 | - name: Checkout code 34 | uses: actions/checkout@v2 35 | 36 | - name: Install PHP versions 37 | uses: shivammathur/setup-php@v2 38 | with: 39 | php-version: ${{ matrix.php-versions }} 40 | 41 | - name: Get Composer Cache Directory 2 42 | id: composer-cache 43 | run: | 44 | echo "::set-output name=dir::$(composer config cache-files-dir)" 45 | 46 | - name: Cache 47 | uses: actions/cache@v2 48 | id: actions-cache 49 | with: 50 | path: ${{ steps.composer-cache.outputs.dir }} 51 | key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} 52 | restore-keys: | 53 | ${{ runner.os }}-composer- 54 | 55 | - name: Cache PHP dependencies 56 | uses: actions/cache@v2 57 | id: vendor-cache 58 | with: 59 | path: vendor 60 | key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }} 61 | 62 | - name: Install Laravel Dependencies 63 | run: | 64 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 65 | composer update --${{ matrix.dependency-stability }} --prefer-dist --no-interaction 66 | 67 | 68 | - name: Show dir 69 | run: pwd 70 | - name: PHP Version 71 | run: php --version 72 | 73 | # Code quality 74 | - name: Execute tests (Unit and Feature tests) via PHPUnit 75 | 76 | # Set environment 77 | env: 78 | SESSION_DRIVER: array 79 | 80 | run: vendor/bin/phpunit --testdox 81 | 82 | - name: Execute Code Sniffer via phpcs 83 | run: | 84 | composer require --dev squizlabs/php_codesniffer 85 | vendor/bin/phpcs --standard=PSR12 src 86 | 87 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-terms` will be documented in this file 4 | 5 | ## 1.0.0 - 2020-08-24 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Ben Miller 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Terms 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/nowendwell/laravel-terms.svg?style=flat-square)](https://packagist.org/packages/nowendwell/laravel-terms) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/nowendwell/laravel-terms.svg?style=flat-square)](https://packagist.org/packages/nowendwell/laravel-terms) 5 | [![Build Status](https://github.com/nowendwell/laravel-terms/actions/workflows/CI.yml/badge.svg)](https://github.com/nowendwell/laravel-terms/actions/workflows/CI.yml/badge.svg) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/nowendwell/laravel-terms.svg?style=flat-square)](https://scrutinizer-ci.com/g/nowendwell/laravel-terms) 7 | ![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/nowendwell/laravel-terms) 8 | ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/nowendwell/laravel-terms) 9 | 10 | Keep users up to date with your terms and conditions changes. This package provides middleware to intercept requests and redirect to the latest terms. 11 | 12 | ## Installation 13 | 14 | You can install the package via composer: 15 | 16 | ```bash 17 | composer require nowendwell/laravel-terms 18 | php artisan vendor:publish --provider="Nowendwell\LaravelTerms\LaravelTermsServiceProvider" 19 | php artisan migrate 20 | ``` 21 | 22 | ## Usage 23 | 24 | Add the AcceptsTerms trait to your user model and you're good to go! 25 | ``` php 26 | check() && 54 | ! auth()->user()->hasAcceptedTerms() && 55 | ! in_array($request->path(), config('terms.excluded_paths')) 56 | ) { 57 | session(['url.intended' => $request->url()]); 58 | return redirect()->route('terms.show'); 59 | } 60 | 61 | return $next($request); 62 | } 63 | } 64 | ``` 65 | 66 | ### Changelog 67 | 68 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 69 | 70 | ## Contributing 71 | 72 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 73 | 74 | ### Security 75 | 76 | If you discover any security related issues, please email nowendwell@gmail.com instead of using the issue tracker. 77 | 78 | ## Credits 79 | 80 | - [Ben Miller](https://github.com/nowendwell) 81 | - [All Contributors](../../contributors) 82 | 83 | ## License 84 | 85 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 86 | 87 | ## Laravel Package Boilerplate 88 | 89 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 90 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nowendwell/laravel-terms", 3 | "description": "A tool for adding terms and conditions to your project", 4 | "keywords": [ 5 | "nowendwell", 6 | "laravel-terms" 7 | ], 8 | "homepage": "https://github.com/nowendwell/laravel-terms", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Ben Miller", 14 | "email": "nowendwell@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.4|^8.0|^8.1", 20 | "illuminate/support": "7.*|8.*|9.*|10.*|11.*", 21 | "spatie/laravel-package-tools":"^1.0" 22 | }, 23 | "require-dev": { 24 | "orchestra/testbench": "^5.0|^6.0|^7.0|^8.0", 25 | "phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Nowendwell\\LaravelTerms\\": "src" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "Nowendwell\\LaravelTerms\\Tests\\": "tests" 35 | } 36 | }, 37 | "scripts": { 38 | "test": "vendor/bin/phpunit", 39 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 40 | 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Nowendwell\\LaravelTerms\\LaravelTermsServiceProvider" 49 | ], 50 | "aliases": { 51 | "LaravelTerms": "Nowendwell\\LaravelTerms\\LaravelTermsFacade" 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /config/terms.php: -------------------------------------------------------------------------------- 1 | [ 9 | 'logout', 10 | ], 11 | 12 | /** 13 | * Paths for showing and accepting the terms. Prefixed by /terms 14 | */ 15 | 'paths' => [ 16 | // The path to show the latest terms (default: latest) 17 | 'latest_path' => '/latest', 18 | 19 | // The path to post the agreement to (default: agree) 20 | 'agree_path' => '/agree', 21 | ], 22 | 23 | /** 24 | * The path to redirect to after accepting the terms 25 | */ 26 | 'redirect' => '/', 27 | ]; 28 | -------------------------------------------------------------------------------- /database/migrations/create_terms_table.php.stub: -------------------------------------------------------------------------------- 1 | unsignedBigInteger('id', true); 19 | $table->longText('terms')->nullable(); 20 | $table->timestamps(); 21 | }); 22 | 23 | Term::create([ 24 | 'terms' => 'My first terms', 25 | ]); 26 | } 27 | 28 | /** 29 | * Reverse the migrations. 30 | * 31 | * @return void 32 | */ 33 | public function down() 34 | { 35 | Schema::dropIfExists('terms'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /database/migrations/create_user_terms_table.php.stub: -------------------------------------------------------------------------------- 1 | bigInteger('user_id')->unsigned(); 18 | $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade'); 19 | 20 | $table->bigInteger('term_id')->unsigned(); 21 | $table->foreign('term_id')->references('id')->on('terms')->onUpdate('cascade')->onDelete('cascade'); 22 | 23 | $table->timestamps(); 24 | }); 25 | } 26 | 27 | /** 28 | * Reverse the migrations. 29 | * 30 | * @return void 31 | */ 32 | public function down() 33 | { 34 | Schema::dropIfExists('user_terms'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /resources/lang/en/terms.php: -------------------------------------------------------------------------------- 1 | "I agree to the Terms and Conditions" 5 | ]; 6 | -------------------------------------------------------------------------------- /resources/views/show.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |

Please agree to our updated Terms of Service.

5 |
6 | {!! $terms->terms !!} 7 |
8 |
9 | @csrf 10 | 11 |
12 | 13 | 14 | @error('terms') 15 | 16 | @enderror 17 |
18 | 19 |
20 | 21 |
22 |
23 |
24 |
25 |
26 | -------------------------------------------------------------------------------- /routes/web.php: -------------------------------------------------------------------------------- 1 | middleware('web')->prefix('terms')->name('terms.')->group(function () { 4 | Route::get(config('terms.paths.latest_path'), 'TermsController@show')->name('show'); 5 | Route::post(config('terms.paths.agree_path'), 'TermsController@store')->name('store'); 6 | }); 7 | -------------------------------------------------------------------------------- /src/Events/AgreedToTerms.php: -------------------------------------------------------------------------------- 1 | user = $user; 20 | $this->term = $term; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Http/Controllers/TermsController.php: -------------------------------------------------------------------------------- 1 | Term::latest('id')->first(), 17 | ]); 18 | } 19 | 20 | public function store(Request $request) 21 | { 22 | $request->validate([ 23 | 'terms' => 'required', 24 | ]); 25 | 26 | $term = Term::latest('id')->first(); 27 | $request->user()->terms()->attach($term->id); 28 | 29 | event(new AgreedToTerms($request->user(), $term)); 30 | 31 | if (session()->has('url.intended')) { 32 | $url = session('url.intended'); 33 | session()->forget('url.intended'); 34 | 35 | return redirect()->to($url); 36 | } 37 | 38 | return redirect()->to(config('terms.redirect', '/')); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Http/Middleware/AcceptedTerms.php.stub: -------------------------------------------------------------------------------- 1 | check() && 15 | ! auth()->user()->hasAcceptedTerms() && 16 | ! in_array($request->path(), config('terms.excluded_paths')) 17 | ) { 18 | session(['url.intended' => $request->url()]); 19 | return redirect()->route('terms.show'); 20 | } 21 | */ 22 | 23 | return $next($request); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/LaravelTerms.php: -------------------------------------------------------------------------------- 1 | name('terms') 23 | ->hasConfigFile() 24 | ->hasMigrations([ 25 | 'create_terms_table', 26 | 'create_user_terms_table', 27 | ]) 28 | ->hasRoute('web') 29 | ->hasTranslations() 30 | ->hasViews(); 31 | 32 | $this->publishMiddleware(); 33 | $this->updateConfig(); 34 | } 35 | 36 | /** 37 | * Run after package is booted 38 | * 39 | * @return void 40 | */ 41 | public function packageBooted() 42 | { 43 | tap($this->app->make(Router::class), function ($router) { 44 | return $router->pushMiddlewareToGroup('web', \App\Http\Middleware\AcceptedTerms::class); 45 | }); 46 | } 47 | 48 | /** 49 | * Run after package is registered 50 | * 51 | * @return void 52 | */ 53 | public function packageRegistered() 54 | { 55 | $this->app->singleton('laravel-terms', function () { 56 | return new LaravelTerms(); 57 | }); 58 | } 59 | 60 | public function publishMiddleware() 61 | { 62 | $this->publishes([ 63 | $this->package->basePath('/../src/Http/Middleware/AcceptedTerms.php.stub') => 64 | app_path("Http/Middleware/AcceptedTerms.php"), 65 | ], "{$this->package->shortName()}-middleware"); 66 | } 67 | 68 | public function updateConfig() 69 | { 70 | // Add terms paths to the excluded_paths key 71 | $existing_paths = Arr::wrap(config('terms.excluded_paths', [])); 72 | $paths = Arr::wrap(config('terms.paths', [])); 73 | $new_paths = []; 74 | 75 | foreach ($paths as $path) { 76 | $new_paths[] = 'terms/' . ltrim($path, '/'); 77 | } 78 | 79 | $paths = array_merge($existing_paths, $new_paths); 80 | 81 | config(['terms.excluded_paths' => $paths]); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Models/Term.php: -------------------------------------------------------------------------------- 1 | belongsToMany(Term::class, 'user_terms')->withTimestamps(); 12 | } 13 | 14 | public function hasAcceptedTerms() 15 | { 16 | return $this->terms->contains(Term::latest()->first()->id); 17 | } 18 | } 19 | --------------------------------------------------------------------------------