Skip to content

Commit bec0fcf

Browse files
author
Anton Komarev
authored
Release v3.0 (#12)
* Add Laravel 5.5 support * Add package autodiscovery for L5.5 * Drop ownership dependency (#9) * Renamed HasBans to Bannable. * Renamed DB table ban to bans * Add Laravel prefix in classes namespaces * Move contracts to external package Cog\Contracts\Ban * Add autoloading migrations.
1 parent 9417854 commit bec0fcf

37 files changed

+477
-318
lines changed

CHANGELOG.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
All notable changes to `laravel-ban` will be documented in this file.
44

5+
## [3.0.0] - 2017-08-27
6+
7+
### Changed
8+
9+
- `Cog\Ban\Contracts\Ban` moved to `Cog\Contracts\Ban\Ban`
10+
- `Cog\Ban\Contracts\HasBans` moved to `Cog\Contracts\Ban\HasBans`
11+
- `Cog\Ban\Contracts\BanService` moved to `Cog\Contracts\Ban\BanService`
12+
- All classes namespaces changed from `Cog\Ban\*` to `Cog\Laravel\Ban\*`
13+
- Renamed database table `ban` to `bans`
14+
- Renamed database column `owned_by_id` to `bannable_id`
15+
- Renamed database column `owned_by_type` to `bannable_type`
16+
- Renamed trait `HasBans` to `Bannable`
17+
- Renamed contract `HasBans` to `Bannable`
18+
- Renamed `Ban::whereOwnedBy($bannable)` to `Ban::whereBannable($bannable)`
19+
- Renamed Ban model relation `ownedBy` to `bannable`
20+
21+
### Removed
22+
23+
- Dependency Laravel Ownership
24+
- Removed Ban model `owner` method
25+
- Removed Ban model `getOwner` method
26+
527
## [2.1.0] - 2017-03-21
628

729
### Added
@@ -10,7 +32,7 @@ All notable changes to `laravel-ban` will be documented in this file.
1032

1133
### Changed
1234

13-
- `HasBans` is a collection of traits `HasBannedAtHelpers`, `HasBannedAtScope`, `HasBansRelation` now.
35+
- `HasBans` is a collection of traits `HasBannedAtHelpers`, `HasBannedAtScope`, `HasBansRelation` now
1436

1537
## [2.0.1] - 2017-03-19
1638

@@ -29,6 +51,7 @@ All notable changes to `laravel-ban` will be documented in this file.
2951

3052
- Initial release
3153

54+
[3.0.0]: https://github.com/cybercog/laravel-ban/compare/2.1.1...3.0.0
3255
[2.1.0]: https://github.com/cybercog/laravel-ban/compare/2.0.1...2.1.0
3356
[2.0.1]: https://github.com/cybercog/laravel-ban/compare/2.0.0...2.0.1
3457
[2.0.0]: https://github.com/cybercog/laravel-ban/compare/1.0.0...2.0.0

README.md

+37-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
![cog-laravel-ban](https://cloud.githubusercontent.com/assets/1849174/23580161/55a1c060-010d-11e7-9658-efe992bde0a7.png)
1+
# Laravel Ban
2+
3+
![cog-laravel-ban](https://user-images.githubusercontent.com/1849174/28749192-fe2d2cb4-74c7-11e7-955e-9c48e81106c2.png)
24

35
<p align="center">
46
<a href="https://travis-ci.org/cybercog/laravel-ban"><img src="https://img.shields.io/travis/cybercog/laravel-ban/master.svg?style=flat-square" alt="Build Status"></a>
@@ -18,14 +20,15 @@ Use case is not limited to User model, any Eloquent model could be banned: Organ
1820
- [Features](#features)
1921
- [Installation](#installation)
2022
- [Usage](#usage)
21-
- [Prepare bannable model](#prepare-bannable-model)
22-
- [Prepare bannable model database table](#prepare-bannable-model-database-table)
23-
- [Available methods](#available-methods)
24-
- [Scopes](#scopes)
25-
- [Events](#events)
26-
- [Middleware](#middleware)
27-
- [Scheduling](#scheduling)
23+
- [Prepare bannable model](#prepare-bannable-model)
24+
- [Prepare bannable model database table](#prepare-bannable-model-database-table)
25+
- [Available methods](#available-methods)
26+
- [Scopes](#scopes)
27+
- [Events](#events)
28+
- [Middleware](#middleware)
29+
- [Scheduling](#scheduling)
2830
- [Change log](#change-log)
31+
- [Upgrading](#upgrading)
2932
- [Contributing](#contributing)
3033
- [Testing](#testing)
3134
- [Security](#security)
@@ -63,14 +66,14 @@ And then include the service provider within `app/config/app.php`:
6366

6467
```php
6568
'providers' => [
66-
Cog\Ban\Providers\BanServiceProvider::class,
69+
Cog\Laravel\Ban\Providers\BanServiceProvider::class,
6770
],
6871
```
6972

7073
At last you need to publish and run database migrations:
7174

7275
```sh
73-
$ php artisan vendor:publish --provider="Cog\Ban\Providers\BanServiceProvider" --tag="migrations"
76+
$ php artisan vendor:publish --provider="Cog\Laravel\Ban\Providers\BanServiceProvider" --tag="migrations"
7477
$ php artisan migrate
7578
```
7679

@@ -79,18 +82,16 @@ $ php artisan migrate
7982
### Prepare bannable model
8083

8184
```php
82-
use Cog\Ban\Contracts\HasBans as HasBansContract;
83-
use Cog\Ban\Traits\HasBans;
85+
use Cog\Contracts\Ban\Bannable as BannableContract;
86+
use Cog\Laravel\Ban\Traits\Bannable;
8487
use Illuminate\Foundation\Auth\User as Authenticatable;
8588

86-
class User extends Authenticatable implements HasBansContract
89+
class User extends Authenticatable implements BannableContract
8790
{
88-
use HasBans;
91+
use Bannable;
8992
}
9093
```
9194

92-
*Note: `HasBans` contract using `CanBeOwner` contract under the hood. If you are using [`cybercog/laravel-ownership`](https://github.com/cybercog/laravel-ownership) package `CanBeOwner` contract could be omitted from bannable model.*
93-
9495
### Prepare bannable model database table
9596

9697
Bannable model must have `nullable timestamp` column named `banned_at`. This value used as flag and simplify checks if user was banned. If you are trying to make default Laravel User model to be bannable you can use example below.
@@ -133,19 +134,12 @@ class AddBannedAtColumnToUsersTable extends Migration
133134
#### Apply ban for the entity
134135

135136
```php
136-
$user->bans()->create([]);
137-
138137
$user->ban();
139138
```
140139

141-
142140
#### Apply ban for the entity with reason comment
143141

144142
```php
145-
$user->bans()->create([
146-
'comment' => 'Enjoy your ban!',
147-
]);
148-
149143
$user->ban([
150144
'comment' => 'Enjoy your ban!',
151145
]);
@@ -154,16 +148,18 @@ $user->ban([
154148
#### Apply ban for the entity which will be deleted over time
155149

156150
```php
157-
$user->bans()->create([
158-
'expired_at' => '+1 month',
159-
]);
160-
161151
$user->ban([
162152
'expired_at' => '2086-03-28 00:00:00',
163153
]);
164-
```
154+
```
165155

166-
`expired_at` attribute could be `\Carbon\Carbon` instance or any string which could be parsed by `\Carbon\Carbon::parse($string)` method.
156+
`expired_at` attribute could be `\Carbon\Carbon` instance or any string which could be parsed by `\Carbon\Carbon::parse($string)` method:
157+
158+
```php
159+
$user->ban([
160+
'expired_at' => '+1 month',
161+
]);
162+
```
167163

168164
#### Remove ban from entity
169165

@@ -188,7 +184,7 @@ $user->isNotBanned();
188184
#### Delete expired bans manually
189185

190186
```php
191-
app(\Cog\Ban\Services\BanService::class)->deleteExpiredBans();
187+
app(\Cog\Laravel\Ban\Services\BanService::class)->deleteExpiredBans();
192188
```
193189

194190
### Scopes
@@ -216,13 +212,13 @@ $users = User::onlyBanned()->get();
216212
To apply query scopes all the time you can define `shouldApplyBannedAtScope` method in bannable model. If method returns `true` all banned models will be hidden by default.
217213

218214
```php
219-
use Cog\Ban\Contracts\HasBans as HasBansContract;
220-
use Cog\Ban\Traits\HasBans;
215+
use Cog\Contracts\Ban\Bannable as BannableContract;
216+
use Cog\Laravel\Ban\Traits\Bannable;
221217
use Illuminate\Foundation\Auth\User as Authenticatable;
222218

223-
class User extends Authenticatable implements HasBansContract
219+
class User extends Authenticatable implements BannableContract
224220
{
225-
use HasBans;
221+
use Bannable;
226222

227223
/**
228224
* Determine if BannedAtScope should be applied by default.
@@ -238,9 +234,9 @@ class User extends Authenticatable implements HasBansContract
238234

239235
### Events
240236

241-
If entity is banned `\Cog\Ban\Events\ModelWasBanned` event is fired.
237+
If entity is banned `\Cog\Laravel\Ban\Events\ModelWasBanned` event is fired.
242238

243-
Is entity is unbanned `\Cog\Ban\Events\ModelWasUnbanned` event is fired.
239+
Is entity is unbanned `\Cog\Laravel\Ban\Events\ModelWasUnbanned` event is fired.
244240

245241
### Middleware
246242

@@ -250,7 +246,7 @@ To use it define new middleware in `$routeMiddleware` array of `app/Http/Kernel.
250246

251247
```php
252248
protected $routeMiddleware = [
253-
'forbid-banned-user' => \Cog\Ban\Http\Middleware\ForbidBannedUser::class,
249+
'forbid-banned-user' => \Cog\Laravel\Ban\Http\Middleware\ForbidBannedUser::class,
254250
]
255251
```
256252

@@ -284,6 +280,10 @@ Of course, the time used in the code above is just example. Adjust it to suit yo
284280

285281
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
286282

283+
## Upgrading
284+
285+
Please see [UPGRADING](UPGRADING.md) for detailed upgrade instructions.
286+
287287
## Contributing
288288

289289
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

UPGRADING.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Upgrade Guide
2+
3+
- [From v2 to v3](#from-v2-to-v3)
4+
5+
## From v2 to v3
6+
7+
Because there are many breaking changes an upgrade is not that easy. There are many edge cases this guide does not cover.
8+
We accept PRs to improve this guide.
9+
10+
Find and replace:
11+
12+
- Find all `Cog\Ban\Contracts\Ban` and replace with `Cog\Contracts\Ban\Ban`
13+
- Find all `Cog\Ban\Contracts\HasBans` and replace with `Cog\Contracts\Ban\Bannable`
14+
- Find all `Cog\Ban\Contracts\BanService` and replace with `Cog\Contracts\Ban\BanService`
15+
- Find all `Cog\Ban\Traits\HasBans` and replace with `Cog\Laravel\Ban\Traits\Bannable`
16+
- Find all `Cog\Ban` and replace with `Cog\Laravel\Ban`
17+
18+
In classes which works with bans:
19+
20+
- Find `Ban::whereOwnedBy` replace with `Ban::whereBannable`
21+
- Find calls of methods or attributes on the Ban model like `ownedBy`, `owner`, `getOwner` and replace them with `bannable`
22+
23+
These database changes should be performed:
24+
25+
- Rename `ban` table to `bans`
26+
- Rename `bans` database column `owned_by_id` to `bannable_id`
27+
- Rename `bans` database column `owned_by_type` to `bannable_type`
28+
- Update name of migration file in `migrations` table from `2017_03_04_000000_create_ban_table` to `2017_03_04_000000_create_bans_table`
29+
30+
To make all changes in MySQL you could run these 3 commands one by one:
31+
32+
```mysql
33+
ALTER TABLE `ban` RENAME TO `bans`;
34+
35+
ALTER TABLE `bans`
36+
CHANGE COLUMN `owned_by_id` `bannable_id` INT(10) UNSIGNED NOT NULL,
37+
CHANGE COLUMN `owned_by_type` `bannable_type` VARCHAR(255) NOT NULL,
38+
DROP INDEX `ban_owned_by_id_owned_by_type_index`,
39+
ADD INDEX `ban_bannable_id_bannable_type_index` (`bannable_id` ASC, `bannable_type` ASC);
40+
41+
UPDATE `migrations`
42+
SET `migration` = '2017_03_04_000000_create_bans_table'
43+
WHERE `migration` = '2017_03_04_000000_create_ban_table'
44+
LIMIT 1;
45+
```
46+
47+
Migration files:
48+
49+
- Delete `database/migrations/2017_03_04_000000_create_ban_table.php` migration file (from v3 service provider automatically loading migration files or republish it if custom changes are required to be done).

composer.json

+13-6
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
},
3737
"require": {
3838
"php": "^5.6|^7.0",
39-
"cybercog/laravel-ownership": "^3.0",
40-
"illuminate/database": "~5.1.20|~5.2.0|~5.3.0|~5.4.0",
41-
"illuminate/events": "~5.1.20|~5.2.0|~5.3.0|~5.4.0",
42-
"illuminate/support": "~5.1.20|~5.2.0|~5.3.0|~5.4.0"
39+
"illuminate/database": "~5.1.20|~5.2|~5.3|~5.4|~5.5",
40+
"illuminate/events": "~5.1.20|~5.2|~5.3|~5.4|~5.5",
41+
"illuminate/support": "~5.1.20|~5.2|~5.3|~5.4|~5.5"
4342
},
4443
"require-dev": {
4544
"friendsofphp/php-cs-fixer": "^1.11",
@@ -50,12 +49,13 @@
5049
},
5150
"autoload": {
5251
"psr-4": {
53-
"Cog\\Ban\\": "src/"
52+
"Cog\\Contracts\\Ban\\": "contracts/",
53+
"Cog\\Laravel\\Ban\\": "src/"
5454
}
5555
},
5656
"autoload-dev": {
5757
"psr-4": {
58-
"Cog\\Ban\\Tests\\": "tests/"
58+
"Cog\\Tests\\Laravel\\Ban\\": "tests/"
5959
}
6060
},
6161
"scripts": {
@@ -64,6 +64,13 @@
6464
"config": {
6565
"sort-packages": true
6666
},
67+
"extra": {
68+
"laravel": {
69+
"providers": [
70+
"Cog\\Laravel\\Ban\\Providers\\BanServiceProvider"
71+
]
72+
}
73+
},
6774
"minimum-stability": "dev",
6875
"prefer-stable" : true
6976
}

contracts/Ban.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Ban.
5+
*
6+
* (c) Anton Komarev <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Cog\Contracts\Ban;
13+
14+
use Cog\Contracts\Ban\Bannable as BannableContract;
15+
use Illuminate\Database\Eloquent\Builder;
16+
17+
/**
18+
* Interface Ban.
19+
*
20+
* @package Cog\Contracts\Ban
21+
*/
22+
interface Ban
23+
{
24+
/**
25+
* Entity responsible for ban.
26+
*
27+
* @return mixed
28+
*/
29+
public function createdBy();
30+
31+
/**
32+
* Bannable model.
33+
*
34+
* @return \Cog\Contracts\Ban\Bannable
35+
*/
36+
public function bannable();
37+
38+
/**
39+
* Scope a query to only include bans by bannable model.
40+
*
41+
* @param \Illuminate\Database\Eloquent\Builder $query
42+
* @param \Cog\Contracts\Ban\Bannable $bannable
43+
* @return \Illuminate\Database\Eloquent\Builder
44+
*/
45+
public function scopeWhereBannable(Builder $query, BannableContract $bannable);
46+
}

0 commit comments

Comments
 (0)