-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextend.php
118 lines (91 loc) · 4.73 KB
/
extend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
* This file is part of fof/ban-ips.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\BanIPs;
use Flarum\Api\Controller;
use Flarum\Api\Serializer;
use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Extend;
use Flarum\Gdpr\Extend\UserData;
use Flarum\Post\Post;
use Flarum\User\User;
use FoF\BanIPs\Api\Controllers\CheckIPsController;
use FoF\BanIPs\Api\Serializers\BannedIPSerializer;
use FoF\BanIPs\Events\IPWasBanned;
use FoF\BanIPs\Listeners\BanUserAttributes;
use FoF\BanIPs\Middleware\RegisterMiddleware;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/resources/less/admin.less'),
new Extend\Locales(__DIR__.'/resources/locale'),
(new Extend\Routes('api'))
->post('/users/{id}/ban', 'fof.ban-ips.users.ban', Api\Controllers\BanUserController::class)
->post('/users/{id}/unban', 'fof.ban-ips.users.unban', Api\Controllers\UnbanUserController::class)
->get('/users/{id}/banned-ips', 'fof.ban-ips.users.banned-ips', Api\Controllers\ListUserBannedIPsController::class)
->get('/fof/ban-ips/check-users', 'fof.ban-ips.check', Api\Controllers\CheckIPsController::class)
->get('/fof/ban-ips/check-users/{user}', 'fof.ban-ips.check.others', Api\Controllers\CheckIPsController::class)
->get('/fof/ban-ips', 'fof.ban-ips.index', Api\Controllers\ListBannedIPsController::class)
->post('/fof/ban-ips', 'fof.ban-ips.store', Api\Controllers\CreateBannedIPController::class)
->patch('/fof/ban-ips/{id}', 'fof.ban-ips.update', Api\Controllers\UpdateBannedIPController::class)
->delete('/fof/ban-ips/{id}', 'fof.ban-ips.delete', Api\Controllers\DeleteBannedIPController::class),
(new Extend\Middleware('forum'))
->add(RegisterMiddleware::class),
(new Extend\Model(User::class))
->hasMany('banned_ips', BannedIP::class),
(new Extend\Model(Post::class))
->hasOne('banned_ip', BannedIP::class, 'address', 'ip_address'),
(new Extend\Event())
->listen(IPWasBanned::class, Listeners\RemoveAccessToBannedUsers::class),
(new Extend\Policy())
->modelPolicy(User::class, Access\UserPolicy::class),
(new Extend\ApiSerializer(Serializer\PostSerializer::class))
->attributes(function (AbstractSerializer $serializer, Post $post, array $attributes): array {
$attributes['canBanIP'] = $serializer->getActor()->can('banIP', $post->user);
return $attributes;
})
->hasOne('banned_ip', BannedIPSerializer::class),
(new Extend\ApiSerializer(Serializer\UserSerializer::class))
->attributes(BanUserAttributes::class)
->hasMany('banned_ips', BannedIPSerializer::class),
(new Extend\ApiController(Controller\ShowUserController::class))
->prepareDataForSerialization(Listeners\BannedIPData::class)
->addInclude(['banned_ips', 'banned_ips.user']),
(new Extend\ApiController(Controller\CreateUserController::class))
->prepareDataForSerialization(Listeners\BannedIPData::class)
->addInclude(['banned_ips', 'banned_ips.user']),
(new Extend\ApiController(Controller\UpdateUserController::class))
->prepareDataForSerialization(Listeners\BannedIPData::class)
->addInclude(['banned_ips', 'banned_ips.user']),
(new Extend\ApiController(Controller\ListUsersController::class))
->prepareDataForSerialization(Listeners\BannedIPsData::class)
->addInclude(['banned_ips', 'banned_ips.user']),
(new Extend\ApiController(CheckIPsController::class))
->prepareDataForSerialization(Listeners\BannedIPsData::class)
->addInclude(['banned_ip', 'banned_ip.user']),
(new Extend\ApiController(Controller\ListPostsController::class))
->addInclude(['banned_ip', 'banned_ip.user']),
(new Extend\ApiController(Controller\ShowPostController::class))
->addInclude(['banned_ip', 'banned_ip.user']),
(new Extend\ApiController(Controller\CreatePostController::class))
->addInclude(['banned_ip', 'banned_ip.user']),
(new Extend\ApiController(Controller\UpdatePostController::class))
->addInclude(['banned_ip', 'banned_ip.user']),
(new Extend\SimpleFlarumSearch(Search\BannedIPSearcher::class))
->setFullTextGambit(Search\NxGambit::class),
(new Extend\Filter(Search\BannedIPFilterer::class))
->addFilter(Search\NxGambit::class),
(new Extend\Conditional())
->whenExtensionEnabled('flarum-gdpr', fn () => [
(new UserData())
->addType(Data\BannedIPData::class),
]),
];