Laravel注册。仅注册拥有来自特定域的电子邮件的用户

时间:2015-07-03 10:06:45

标签: php laravel laravel-5

我有一个laravel应用程序,我想限制仅注册到拥有公司电子邮件的特定目标组的用户。我在Registrar.php

中尝试了一些东西
public function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'lastname' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',

        'password' => 'required|confirmed|min:6',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
public function create(array $data)
{
    $result = null;
    $confirmation_code = str_random(30);

    $data = array_add($data, 'conf',$confirmation_code);
    if(!$data['email'].ends_with(('email'), 'specificdomain.com')){
        Flash::message('Welcome ' .$data["name"]. '. Thanks for registering. We have sent you a validation link in your e-mail address!');
        Mail::send('emails.verify', $data, function($message) use ($data) {
            $message->to($data['email'], $data['name'])
                ->subject('Verify your email address');
        });
        $result = User::create([
            'name' => $data['name'],
            'lastname' => $data['lastname'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'confirmation_code' => $confirmation_code
        ]);

    }else{
       $result = Flash::warning('Hi ' .$data["name"]. '. We appreciate your interest on using our System. However at the moment we offer this service only to this company!');
        //break;
    }
    return $result;

}

这会引发以下异常

Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, Laracasts/Flash/Flash given.

我无法在else语句中破解,因为我得到以下内容:

Cannot break/continue 1 level

显然我必须return Users::create([....])但是为了这样做,我必须将此块保留在if语句之外。如果我这样做,我无法检查电子邮件域是否是必需的。所以我问,我怎样才能将其整合到public function validator(array $data){.....}块中?

感谢所有帮助。

2 个答案:

答案 0 :(得分:4)

您可以在验证程序规则中扩展电子邮件验证,例如:

'email' => 'required|email|max:255|unique:users|regex:/(.*)your\.domain\.com$/i',

(如果需要在正则表达式中输入管道,则将其作为数组传递)

然后,您可以向验证程序添加一组消息,如:

$messages = array(
 'email.regex' => 'We appreciate your interest on using our System. However at the moment we offer this service only to this company!',

);

您将Validator称为第三个参数:

// Where $rules is the array you pass on now
$validator = Validator::make($data, $rules, $messages);

在laravel documentation中,您可以准备好有关回复的所有内容。

您无法返回Flash。您可以使用Flash(在您的情况下)将消息放入会话中,该消息将在请求后删除。我不完全确定你如何调用create函数以及返回的预期结果应该是什么,但我会与此一致。由于您现在可以使用验证消息解决它,因此您只需要发送成功消息或错误。

答案 1 :(得分:2)

我最近使用Laravel v5.3面对了这个问题,并通过extending Validator门面解决了这个问题。规则包括:

'email' => 'required|email|allowed_domain|max:255|unique:users'

扩展名已放入app/Providers/AuthServiceProvider.php引导方法中:

        Validator::extend('allowed_domain', function($attribute, $value, $parameters, $validator) {
            return in_array(explode('@', $value)[1], $this->allowedDomains);
        }, 'Domain not valid for registration.');

$this->allowedDomains是允许的域的数组。