Laravel 5.2+ - 禁用非管理员用户的注册

时间:2016-05-31 23:22:18

标签: php authentication laravel-5.2 registration

我试图实现这里描述的内容 https://www.codecourse.com/index.php/forum/topics/how-can-l-protect-the-register-page-in-laravel-52-with-the-auth-middleware-after-php-artisan-make-auth/2717 但是我无法得到它。

基本上是为了防止那些不是" admin"注册新用户。

我对Laravel来说真的很新,所以可能这是我无法得到的简单事情。而且我知道网上有很多内容,但我几乎尝试了所有内容,没有什么可以工作的。

我的AuthController是:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

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 = '/';

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

    /**
     * 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, [
            'name' => 'required|max:255',
            'surname' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'role' => 'required|max:5',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $is_admin='0';
        if ($data['role']=='Admin') {$is_admin='1';}

        return User::create([
            'name' => $data['name'],
            'surname' => $data['surname'],
            'email' => $data['email'],
            'is_admin' => $is_admin,
            'password' => bcrypt($data['password']),
        ]);
    }


/**
 * Show the application registration form.
 *
 * @return \Illuminate\Http\Response
 */
public function getRegister()
{
        if (! Auth::user()->is_admin == '1')
        return abort(403);

    return $this->showRegistrationForm();
}

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function postRegister(Request $request)
{
    if (! Auth::user()->is_admin == '1')
        return abort(403);

    return $this->register($request);
}


}

和我的routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('register', 'Auth\AuthController@getRegister');
Route::post('/register', 'Auth\AuthController@postRegister');  
Route::auth();

Route::get('/home', 'HomeController@index');

但是,对于未登录的用户,注册表始终可见。

有人可以帮我告诉我,我的错误在哪里? (也许如何解决它!)

非常感谢,M。

0 个答案:

没有答案