├── .phpunit.result.cache ├── LICENSE ├── README.md ├── composer.json └── src ├── Contracts └── Ownable.php └── OwnsModels.php /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | C:37:"PHPUnit\Runner\DefaultTestResultCache":406:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:4:{s:49:"Ownable\Tests\OwnsModelsTraitTest::testOwnsMethod";d:0.115;s:54:"Ownable\Tests\OwnsModelsTraitTest::testdoesntOwnMethod";d:0.01;s:102:"Ownable\Tests\OwnsModelsTraitTest::testItShouldUseTheInterfaceImplementationIfClassImplementsInterface";d:0.012;s:93:"Ownable\Tests\OwnsModelsTraitTest::testItShouldOverrideTheContractAndUserTheRegularValidation";d:0.009;}}} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Santiago García 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 | # Check model ownership for Laravel 2 | 3 | ![tests](https://github.com/santigarcor/laravel-ownable/workflows/tests/badge.svg?branch=master) 4 | [![Latest Stable Version](https://poser.pugx.org/santigarcor/laravel-ownable/v)](//packagist.org/packages/santigarcor/laravel-ownable) 5 | [![Total Downloads](https://poser.pugx.org/santigarcor/laravel-ownable/downloads)](//packagist.org/packages/santigarcor/laravel-ownable) 6 | [![License](https://poser.pugx.org/santigarcor/laravel-ownable/license)](//packagist.org/packages/santigarcor/laravel-ownable) 7 | 8 | This trait allows you to check if a model owns some other model in your Laravel application. 9 | 10 | ## Installation 11 | 12 | Simply run: 13 | ```bash 14 | composer require santigarcor/laravel-ownable 15 | ``` 16 | 17 | Then you have to use the `OwnsModels` trait in the models you want to check if they own other models. 18 | 19 | ```php 20 | owns($video)){} 55 | 56 | // Check If the user doesn't owns the video 57 | if ($user->doesntOwn($video)){} 58 | 59 | // Check If the user owns the video but the foreign key is the_user_id 60 | if ($user->owns($video, 'the_user_id')){} 61 | ``` 62 | 63 | If for some reason the ownership check needs a more complex logic, you can implement the `Ownable` interface inside your ownable objects and then you can define your custom logic in the `isOwnedBy` method. When this method is called, the object that called the `owns` method is passed as an attribute to the `isOwnedBy` method. 64 | 65 | ```php 66 | someRelationship->user_id == $owner->id; 84 | } 85 | 86 | return false; 87 | } 88 | } 89 | 90 | $user = User::first(); 91 | $video = Video::first(); 92 | 93 | // Then you can simply call the owns method on the user and it will work. 94 | 95 | // Check If the user owns the video 96 | if ($user->owns($video)){} 97 | 98 | // Check If the user owns the video but don't use the ownable logic, instead the regular one with the foreign key. 99 | if ($user->owns($video, null, false)){} 100 | ``` 101 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "santigarcor/laravel-ownable", 3 | "description": "Simple trait to manage the ownershipt of models/objects", 4 | "keywords": ["laravel", "ownership", "ownable", "owns"], 5 | "require": { 6 | "illuminate/database": "^6.0|^7.0|^8.0|^9.0|^10.0", 7 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0", 8 | "laravel/legacy-factories": "^1.3" 9 | }, 10 | "require-dev": { 11 | "orchestra/testbench": "^4.0|^5.0|^6.0", 12 | "phpunit/phpunit": "^7.5.15|^8.4|^9.0" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Ownable\\": "src/" 17 | } 18 | }, 19 | "autoload-dev": { 20 | "psr-4": { 21 | "Ownable\\Tests\\": "tests/" 22 | } 23 | }, 24 | "config": { 25 | "sort-packages": true 26 | }, 27 | "prefer-stable": true, 28 | "license": "MIT", 29 | "minimum-stability": "dev" 30 | } 31 | -------------------------------------------------------------------------------- /src/Contracts/Ownable.php: -------------------------------------------------------------------------------- 1 | isOwnedBy($this); 21 | } 22 | 23 | $foreignKeyName = $foreignKey ?: $this->getForeignKey(); 24 | 25 | return $model->{$foreignKeyName} == $this->getKey(); 26 | } 27 | 28 | /** 29 | * Check if the model doesn't owns the given model/object. 30 | * 31 | * @param mixed $model 32 | * @param string $foreignKey 33 | * @param bool $overrideContract If true the Ownable contract is implemented it won't be used. 34 | * @return bool 35 | */ 36 | public function doesntOwn($model, $foreignKey = null, $overrideContract = false) 37 | { 38 | return !$this->owns($model, $foreignKey); 39 | } 40 | } 41 | --------------------------------------------------------------------------------