Skip to content

Commit a247a3b

Browse files
committed
Add a blacklist for urls
1 parent 8295c66 commit a247a3b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

config/shorturl.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
| Number of items per page.
66
*/
77
'items_per_page' => 10,
8+
9+
/*
10+
| A list of non allowed keywords in url.
11+
*/
12+
'blacklist' => [],
813
];

src/Http/Requests/UrlRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Gallib\ShortUrl\Http\Requests;
44

5+
use Gallib\ShortUrl\Rules\Blacklist;
56
use Illuminate\Foundation\Http\FormRequest;
67

78
class UrlRequest extends FormRequest
@@ -30,7 +31,7 @@ public function rules()
3031
}
3132

3233
return [
33-
'url' => 'required|url',
34+
'url' => ['required', 'url', new Blacklist()],
3435
'code' => 'max:255'.$uniqueCode,
3536
];
3637
}

src/Rules/Blacklist.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Gallib\ShortUrl\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class Blacklist implements Rule
8+
{
9+
/**
10+
* @var array
11+
*/
12+
13+
protected $blacklist = [];
14+
/**
15+
* Create a new rule instance.
16+
*
17+
* @return void
18+
*/
19+
public function __construct()
20+
{
21+
$this->blacklist = config('shorturl.blacklist');
22+
}
23+
24+
/**
25+
* Determine if the validation rule passes.
26+
*
27+
* @param string $attribute
28+
* @param mixed $value
29+
* @return bool
30+
*/
31+
public function passes($attribute, $value)
32+
{
33+
return !str_contains($value, $this->blacklist);
34+
}
35+
36+
/**
37+
* Get the validation error message.
38+
*
39+
* @return string
40+
*/
41+
public function message()
42+
{
43+
return 'This :attribute contains blacklisted keyword.';
44+
}
45+
}

0 commit comments

Comments
 (0)