laravel 5.2注册后不要登录用户

时间:2016-07-05 01:15:28

标签: php laravel

我正在构建一个管理员可以添加用户的系统。这就是说我的Auth控制器看起来像这样,效果很好,但使用标准注册,用户在创建后登录。显然,如果管理员添加用户(另一个管理员或只是一般用户),管理员希望保持登录状态而不是登录刚创建的用户。我怎样才能以不必编辑任何库的方式执行此操作?如果我更新Laravel会覆盖这些更改。

AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Role;
use Mail;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image as Image;


class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/add';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'first-name' => 'required|max:255',
            'last-name' => 'required|max:255',
            'phone' => 'required|max:255',
            'form' => 'max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create( array $data ) 
    {
        //Create the user
        $user = User::create([
            'first_name' => $data['first-name'],
            'last_name' => $data['last-name'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        //Is it a User? Then give them that role
        if ($data['user-role'] == 'user')
        {
            $role = Role::where('name', '=', 'user')->firstOrFail();
            $user = User::find($user->id);
            $user->roles()->attach($role->id);
        }

        //Is it an Admin? Then give them that role
        if ($data['user-role'] == 'admin')
        {
            $role = Role::where('name', '=', 'owner')->firstOrFail();
            $user = User::find($user->id);
            $user->roles()->attach($role->id);
        }

        Mail::send('auth.emails.registered', ['user' => $user], function ($m) use ($user) 
        {
            $m->to($user->email, $user->first_name)->subject('You Have Been Added');
        });

        return $user;
    }

}

1 个答案:

答案 0 :(得分:1)

注册时使用的 RegisterUsers 特征会自动使用register()方法记录用户,但您可以安全地在 AuthController 中覆盖它,如下所示:< / p>

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    $this->create($request->all());

    return redirect($this->redirectPath());
}