Page MenuHomePhorge

UserEmailDomain.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

UserEmailDomain.php

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
class UserEmailDomain implements Rule
{
private $message;
private $domains;
/**
* Class constructor.
*
* @param array|null $domains Allowed domains
*/
public function __construct($domains = null)
{
$this->domains = $domains;
}
/**
* Determine if the validation rule passes.
*
* Validation of local part of an email address that's
* going to be user's login.
*
* @param string $attribute Attribute name
* @param mixed $domain Domain part of email address
*
* @return bool
*/
public function passes($attribute, $domain): bool
{
// don't allow @localhost and other no-fqdn
if (empty($domain) || strpos($domain, '.') === false || stripos($domain, 'www.') === 0) {
$this->message = \trans('validation.domaininvalid');
return false;
}
$domain = Str::lower($domain);
// Use email validator to validate the domain part
$v = Validator::make(['email' => 'user@' . $domain], ['email' => 'required|email']);
if ($v->fails()) {
$this->message = \trans('validation.domaininvalid');
return false;
}
// Check if specified domain is allowed for signup
if (is_array($this->domains) && !in_array($domain, $this->domains)) {
$this->message = \trans('validation.domaininvalid');
return false;
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message(): ?string
{
return $this->message;
}
}

File Metadata

Mime Type
text/x-php
Expires
Wed, Jul 8, 6:28 PM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1051727
Default Alt Text
UserEmailDomain.php (1 KB)

Event Timeline