├── .editorconfig ├── .styleci.yml ├── CHANGELOG.md ├── CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE.md ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── composer.json ├── migrations └── create_approvals_table.php.stub ├── phpunit.xml └── src ├── Approvable.php ├── ApprovableServiceProvider.php └── Approval.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `laravel-approvable` will be documented in this file. 4 | 5 | Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 6 | 7 | ## Version 1.2.0 - 2019-08-14 8 | Modified: 9 | - Updated dependencies to require php@^7.2, laravel/framework@^5.8|^6.0 10 | 11 | ## Version 1.1.0 - 2018-08-27 12 | Added: 13 | - `withApproval()` method. 14 | - `withoutApproval()` is now chainable. 15 | 16 | Deprecated: 17 | - `withoutApproval($withoutApproval = true)` the `$withoutApproval` flag is now deprecated, use the `withApproval()` method if you want to re-enable the approval process. 18 | 19 | ## Version 1.0.0 - 2018-05-18 20 | Initial release. -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at `victorlap@outlook.com`. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/victorlap/laravel-approvable). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **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](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | ``` bash 28 | $ composer test 29 | ``` 30 | 31 | 32 | **Happy coding**! 33 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Detailed description 4 | 5 | Provide a detailed description of the change or addition you are proposing. 6 | 7 | Make it clear if the issue is a bug, an enhancement or just a question. 8 | 9 | ## Context 10 | 11 | Why is this change important to you? How would you use it? 12 | 13 | How can it benefit other users? 14 | 15 | ## Possible implementation 16 | 17 | Not obligatory, but suggest an idea for implementing addition or change. 18 | 19 | ## Your environment 20 | 21 | Include as many relevant details about the environment you experienced the bug in and how to reproduce it. 22 | 23 | * Version used (e.g. PHP 5.6, HHVM 3): 24 | * Operating system and version (e.g. Ubuntu 16.04, Windows 7): 25 | * Link to your project: 26 | * ... 27 | * ... 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Victor Lap 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Description 4 | 5 | Describe your changes in detail. 6 | 7 | ## Motivation and context 8 | 9 | Why is this change required? What problem does it solve? 10 | 11 | If it fixes an open issue, please link to the issue here (if you write `fixes #num` 12 | or `closes #num`, the issue will be automatically closed when the pull is accepted.) 13 | 14 | ## How has this been tested? 15 | 16 | Please describe in detail how you tested your changes. 17 | 18 | Include details of your testing environment, and the tests you ran to 19 | see how your change affects other areas of the code, etc. 20 | 21 | ## Screenshots (if appropriate) 22 | 23 | ## Types of changes 24 | 25 | What types of changes does your code introduce? Put an `x` in all the boxes that apply: 26 | - [ ] Bug fix (non-breaking change which fixes an issue) 27 | - [ ] New feature (non-breaking change which adds functionality) 28 | - [ ] Breaking change (fix or feature that would cause existing functionality to change) 29 | 30 | ## Checklist: 31 | 32 | Go over all the following points, and put an `x` in all the boxes that apply. 33 | 34 | Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our [continuous integration](http://www.phptherightway.com/#continuous-integration) server to make sure your [tests and code style pass](https://help.github.com/articles/about-required-status-checks/). 35 | 36 | - [ ] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document. 37 | - [ ] My pull request addresses exactly one patch/feature. 38 | - [ ] I have created a branch for this patch/feature. 39 | - [ ] Each individual commit in the pull request is meaningful. 40 | - [ ] I have added tests to cover my changes. 41 | - [ ] If my change requires a change to the documentation, I have updated it accordingly. 42 | 43 | If you're unsure about any of these, don't hesitate to ask. We're here to help! 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Approvable 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![Coverage Status][ico-scrutinizer]][link-scrutinizer] 7 | [![Quality Score][ico-code-quality]][link-code-quality] 8 | [![StyleCI](https://styleci.io/repos/80375034/shield?branch=master)](https://styleci.io/repos/80375034) 9 | [![Total Downloads][ico-downloads]][link-downloads] 10 | 11 | Easily add an approval process to any laravel model. 12 | 13 | ## Description 14 | 15 | Laravel Approvable is a package which helps when you have certain models in your application that should be editable by users, but the fields that they edit need to be approved first. 16 | 17 | ## Installation 18 | 19 | Via Composer 20 | 21 | ``` bash 22 | $ composer require victorlap/laravel-approvable 23 | ``` 24 | 25 | You can publish the migration with: 26 | ```bash 27 | php artisan vendor:publish --provider="Victorlap\Approvable\ApprovableServiceProvider" --tag="migrations" 28 | php artisan migrate 29 | ``` 30 | 31 | ## Setup 32 | Assume you have a `Post` model. Each visitor on your site can edit any post, but before you want to publish the change to your website, you want to approve it first. By adding the `\Victorlap\Approvable\Approvable` trait to your `Post` model, when a visitor makes a change, a change request gets stored in the database. These changes can then later be applied, or denied by administrators. The `currentUserCanApprove` method can be used to determine who is authorized to make a change. 33 | 34 | ```php 35 | use Illuminate\Database\Eloquent\Model; 36 | use Victorlap\Approvable\Approvable; 37 | 38 | // Minimal 39 | class Post extends Model 40 | { 41 | use Approvable; 42 | } 43 | 44 | // Extended 45 | class Post extends Model 46 | { 47 | use Approvable; 48 | 49 | protected $approveOf = array(); 50 | 51 | protected $dontApproveOf = array(); 52 | 53 | protected function currentUserCanApprove() 54 | { 55 | return Auth::check(); 56 | } 57 | 58 | protected function getSystemUserId() 59 | { 60 | return Auth::id(); 61 | } 62 | } 63 | ``` 64 | 65 | ## Usage 66 | Making a change to a model by a user who can approve does not change. 67 | ```php 68 | $post->title = "Very Good Post"; 69 | $post->save(); // This still works! 70 | ``` 71 | 72 | Making a change by an unauthorized user works the same. 73 | ```php 74 | $post->title = "Very Good Post"; 75 | $post->save(); // Post remains with the old title in the database, however a change request is now also present. 76 | ``` 77 | 78 | You can retrieve a list of attributes that have pending changes by using 79 | ```php 80 | $post->getPendingApprovalAttributes(); 81 | ``` 82 | 83 | Or check if a certain attribute has pending changes 84 | ```php 85 | $post->isPendingApproval('title'); 86 | ``` 87 | 88 | Scopes have been defined to quickly see approvals in different states. For example if you wnat to show administrators a list with changes that can be accepted you can use the `open` scope. Other scopes are `accepted`, `rejected` and `ofClass`. 89 | ```php 90 | Approval::open()->get(); 91 | Approval::accepted()->get(); 92 | Approval::rejected()->get(); 93 | Approval::ofClass(Post::class)->get(); 94 | ``` 95 | 96 | You can combine the scopes of course, or use them in combination with regular query builder methods 97 | ```php 98 | Approval::open()->ofClass(Post::class)->get(); 99 | ``` 100 | 101 | Accepting and rejecting of approvals can be done using the `accept` and `reject` methods on the Approval. 102 | ```php 103 | $approvals = Post::find(1)->approvals()->open()->get(); 104 | $approvals->each->accept(); // or 105 | $approvals->each->reject(); 106 | ``` 107 | 108 | If you dont want a model to pass approval, you can use the `withoutApproval()` method. 109 | ```php 110 | // Now this post model is not checked for changes. 111 | $post->withoutApproval() 112 | ->fill([ 113 | 'title' => 'A new title', 114 | ]) 115 | ->save(); 116 | ``` 117 | 118 | To re-enable the approval for this model instance, you can use the `withApproval()` method. 119 | 120 | ## Limitations 121 | Currently Approvable does not handle creation of models, PR's are welcome for this. 122 | 123 | ## Change log 124 | 125 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 126 | 127 | ## Testing 128 | 129 | ``` bash 130 | $ composer test 131 | ``` 132 | 133 | ## Contributing 134 | 135 | Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. 136 | 137 | ## Security 138 | 139 | If you discover any security related issues, please email victorlap@outlook.com instead of using the issue tracker. 140 | 141 | ## Credits 142 | 143 | - [Victor Lap][link-author] 144 | - [All Contributors][link-contributors] 145 | 146 | ## License 147 | 148 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 149 | 150 | [ico-version]: https://img.shields.io/packagist/v/victorlap/laravel-approvable.svg?style=flat-square 151 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 152 | [ico-travis]: https://img.shields.io/travis/victorlap/laravel-approvable/master.svg?style=flat-square 153 | [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/victorlap/laravel-approvable.svg?style=flat-square 154 | [ico-code-quality]: https://img.shields.io/scrutinizer/g/victorlap/laravel-approvable.svg?style=flat-square 155 | [ico-downloads]: https://img.shields.io/packagist/dt/victorlap/laravel-approvable.svg?style=flat-square 156 | 157 | [link-packagist]: https://packagist.org/packages/victorlap/laravel-approvable 158 | [link-travis]: https://travis-ci.org/victorlap/laravel-approvable 159 | [link-scrutinizer]: https://scrutinizer-ci.com/g/victorlap/laravel-approvable/code-structure 160 | [link-code-quality]: https://scrutinizer-ci.com/g/victorlap/laravel-approvable 161 | [link-downloads]: https://packagist.org/packages/victorlap/laravel-approvable 162 | [link-author]: https://github.com/victorlap 163 | [link-contributors]: ../../contributors 164 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "victorlap/laravel-approvable", 3 | "type": "library", 4 | "description": "Easily add an approval process to any laravel model", 5 | "keywords": [ 6 | "victorlap", 7 | "laravel-approvable", 8 | "approvable" 9 | ], 10 | "homepage": "https://github.com/victorlap/laravel-approvable", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Victor Lap", 15 | "email": "victorlap@outlook.com", 16 | "homepage": "https://github.com/victorlap", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^7.2|^8.0", 22 | "illuminate/database": "^5.8|^6.0|^7.0|^8.0", 23 | "illuminate/support": "^5.8|^6.0|^7.0|^8.0" 24 | }, 25 | "require-dev": { 26 | "mockery/mockery": "^1.1", 27 | "orchestra/testbench": "^3.8|^4.0|^5.0|^6.0", 28 | "phpunit/phpunit": "^7.5|^8.0|^9.0" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Victorlap\\Approvable\\": "src" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Victorlap\\Approvable\\Tests\\": "tests" 38 | } 39 | }, 40 | "scripts": { 41 | "test": "vendor/bin/phpunit", 42 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 43 | }, 44 | "config": { 45 | "sort-packages": true 46 | }, 47 | "extra": { 48 | "laravel": { 49 | "providers": [ 50 | "Victorlap\\Approvable\\ApprovableServiceProvider" 51 | ] 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /migrations/create_approvals_table.php.stub: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('approvable_type'); 19 | $table->integer('approvable_id'); 20 | $table->integer('user_id')->nullable(); 21 | $table->string('key'); 22 | $table->text('value')->nullable(); 23 | $table->boolean('approved')->nullable()->default(null); 24 | $table->timestamps(); 25 | $table->index(array('approvable_id', 'approvable_type')); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('approvals'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/Approvable.php: -------------------------------------------------------------------------------- 1 | preSave(); 31 | }); 32 | } 33 | 34 | public function approvals(): MorphMany 35 | { 36 | return $this->morphMany(Approval::class, 'approvable'); 37 | } 38 | 39 | /** 40 | * Check if this model has pending changes, 41 | * If an attribute is provided, check if the attribute has pending changes. 42 | * 43 | * @param null $attribute 44 | * @return bool 45 | */ 46 | public function isPendingApproval($attribute = null): bool 47 | { 48 | return $this->approvals() 49 | ->when($attribute !== null, function ($query) use ($attribute) { 50 | $query->where('key', $attribute); 51 | }) 52 | ->where('approved', null) 53 | ->exists(); 54 | } 55 | 56 | /** 57 | * List all the attributes, that currently have pending changes. 58 | * 59 | * @return \Illuminate\Support\Collection 60 | */ 61 | public function getPendingApprovalAttributes(): Collection 62 | { 63 | return $this->approvals() 64 | ->where('approved', null) 65 | ->groupBy('key') 66 | ->pluck('key'); 67 | } 68 | 69 | /** 70 | * Disable the approval process for this model instance. 71 | * 72 | * @param bool $withoutApproval Deprecated, see withoApproval() 73 | * Will be removed in 2.0.0 74 | * 75 | * @return self 76 | */ 77 | public function withoutApproval(bool $withoutApproval = true): self 78 | { 79 | $this->withoutApproval = $withoutApproval; 80 | 81 | return $this; 82 | } 83 | 84 | /** 85 | * Enable the approval process for this model instance 86 | * 87 | * @return self 88 | */ 89 | public function withApproval(): self 90 | { 91 | $this->withoutApproval = false; 92 | 93 | return $this; 94 | } 95 | 96 | /** 97 | * Invoked before a model is saved. Return false to abort the operation. 98 | * 99 | * @return bool 100 | */ 101 | protected function preSave(): bool 102 | { 103 | if ($this->withoutApproval) { 104 | return true; 105 | } 106 | 107 | if ($this->currentUserCanApprove()) { 108 | // If the user is able to approve edits, do nothing. 109 | return true; 110 | } 111 | 112 | if (!$this->exists) { 113 | // There is currently no way (implemented) to enable this for new models. 114 | return true; 115 | } 116 | 117 | $changes_to_record = $this->changedApprovableFields(); 118 | 119 | $approvals = array(); 120 | foreach ($changes_to_record as $key => $change) { 121 | $approvals[] = array( 122 | 'approvable_type' => $this->getMorphClass(), 123 | 'approvable_id' => $this->getKey(), 124 | 'key' => $key, 125 | 'value' => $change, 126 | 'user_id' => $this->getSystemUserId(), 127 | 'created_at' => new \DateTime(), 128 | 'updated_at' => new \DateTime(), 129 | ); 130 | } 131 | 132 | if (count($approvals) > 0) { 133 | $approval = new Approval(); 134 | DB::table($approval->getTable())->insert($approvals); 135 | } 136 | 137 | return true; 138 | } 139 | 140 | /** 141 | * Get all of the changes that have been made, that are also supposed 142 | * to be approved. 143 | * 144 | * @return array fields with new data, that should be recorded 145 | */ 146 | private function changedApprovableFields(): array 147 | { 148 | $dirty = $this->getDirty(); 149 | $changes_to_record = array(); 150 | 151 | foreach ($dirty as $key => $value) { 152 | if ($this->isApprovable($key)) { 153 | if (!isset($this->original[$key]) || $this->original[$key] != $this->attributes[$key]) { 154 | $changes_to_record[$key] = $value; 155 | 156 | // Reset changes that we want to approve 157 | if (!isset($this->original[$key])) { 158 | unset($this->attributes[$key]); 159 | } else { 160 | $this->attributes[$key] = $this->original[$key]; 161 | } 162 | } 163 | } 164 | } 165 | 166 | return $changes_to_record; 167 | } 168 | 169 | /** 170 | * Return whether an attribute of this model should be approvable. 171 | * 172 | * @param string $key 173 | * @return bool 174 | */ 175 | private function isApprovable(string $key): bool 176 | { 177 | if (isset($this->approveOf) && in_array($key, $this->approveOf)) { 178 | return true; 179 | } 180 | if (isset($this->dontApproveOf) && in_array($key, $this->dontApproveOf)) { 181 | return false; 182 | } 183 | 184 | return empty($this->approveOf); 185 | } 186 | 187 | /** 188 | * Get the user id that should be stored as the requester for the approval. 189 | * 190 | * @return int|null 191 | */ 192 | protected function getSystemUserId(): ?int 193 | { 194 | return Auth::id() ?? null; 195 | } 196 | 197 | /** 198 | * Check if the approval process needs to happen for the currently logged in user. 199 | * 200 | * @return bool 201 | */ 202 | protected function currentUserCanApprove(): bool 203 | { 204 | return Auth::check() && Auth::user()->can('approve', $this) ?? false; 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /src/ApprovableServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__ . '/../migrations/create_approvals_table.php.stub' 18 | => database_path("/migrations/{$timestamp}_create_approvals_table.php"), 19 | ], 'migrations'); 20 | } 21 | } 22 | 23 | /** 24 | * Register the service provider. 25 | */ 26 | public function register() 27 | { 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Approval.php: -------------------------------------------------------------------------------- 1 | 'bool']; 14 | 15 | public function approvable(): MorphTo 16 | { 17 | return $this->morphTo(); 18 | } 19 | 20 | public function getFieldName(): string 21 | { 22 | return $this->key; 23 | } 24 | 25 | public function accept(): void 26 | { 27 | $approvable = $this->approvable; 28 | $approvable->withoutApproval(); 29 | $approvable->{$this->getFieldName()} = $this->value; 30 | $approvable->save(); 31 | $approvable->withApproval(); 32 | 33 | $this->approved = true; 34 | $this->save(); 35 | } 36 | 37 | public function reject(): void 38 | { 39 | $this->approved = false; 40 | $this->save(); 41 | } 42 | 43 | public function scopeOpen($query): Builder 44 | { 45 | return $query->where('approved', null); 46 | } 47 | 48 | public function scopeRejected($query): Builder 49 | { 50 | return $query->where('approved', false); 51 | } 52 | 53 | public function scopeAccepted($query): Builder 54 | { 55 | return $query->where('approved', true); 56 | } 57 | 58 | public function scopeOfClass($query, $class): Builder 59 | { 60 | return $query->where('approvable_type', $class); 61 | } 62 | } 63 | --------------------------------------------------------------------------------