首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 8登录时注册用户

Laravel 8登录时注册用户
EN

Stack Overflow用户
提问于 2021-01-21 20:53:00
回答 1查看 1.1K关注 0票数 1

我有两张有用户的桌子,一张给公共用户,一张给工人。工人只能由其他工人创建,但是当我进入员工注册网址时,它会检测到我正在登录并将我重定向到家,我如何改变这种行为?

我已经尝试过在RedirectIfAuthenticated.php文件中注释掉handle函数的一部分,但是这会使用户在登录时能够访问登录链接。

我在RedirectIfAuthenticated.php中注释的函数:

代码语言:javascript
复制
public function handle(Request $request, Closure $next, ...$guards)
{
    $guards = empty($guards) ? [null] : $guards;

    // foreach ($guards as $guard) {
    //     if (Auth::guard($guard)->check()) {
    //         return redirect(RouteServiceProvider::HOME);
    //     }
    // }

    return $next($request);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-22 10:41:06

users表中,定义一个字段,允许您在用户的types之间进行不同的操作。

代码语言:javascript
复制
$table->string('type')->default('public');

我定义了一个data type string字段,并给该字段一个默认值public。还有其他方法可以做到这一点,比如使data type成为一个integer和使用一个查找表,但是现在让我们保持简单,老实说,这并不重要。

然后,type就可以在您的user对象上访问;$user->type

要为当前通过身份验证的type获取user,您可以使用auth()->user()->typeAuth::user()->type (不管是哪个,相同的结果)。

基于type的值,您将允许或拒绝对某些东西的访问。在您的示例中,您希望拒绝不属于type worker的用户创建type worker的用户。

不要重用register表单来创建新用户,创建一个新的支架(路由、控制器、视图等)。完成这项任务。下面是一个基本的例子,说明这是什么样子的。

routes/web.php

代码语言:javascript
复制
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
    Route::resource('users', \App\Http\Controllers\UserController::class);
});

resources/views/users/create.blade.php

代码语言:javascript
复制
@if ($errors->any())
    @foreach ($errors->all() as $error)
        {{ $error }}
    @endforeach
@endif

<form action="{{ route('admin.users.store') }}" method="post">
    @csrf

    {{--
        If the authenticated user is of type public
        you can choose to either hide the option for choosing a user type
    --}}

    @if (auth()->user()->type != 'public')
    <select name="type" id="type">
        <option value="public">Public</option>
        <option value="worker">Worker</option>
    </select>
    @endif

    {{--
        Or you can simply disable it
    --}}

    <select name="type" id="type" @if (auth()->user()->type == 'public') disabled @endif>
        <option value="public">Public</option>
        <option value="worker">Worker</option>
    </select>

</form>

app/Http/Controllers/UserController.php

代码语言:javascript
复制
class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth']);
    }

    public function create()
    {
        return view('users.create');
    }

    public function store(Request $request)
    {
        // perform some validation on the data coming in
        // the value of the type field should be either public or worker
        // other values should cause an error

        $validator = Validator::make($request->all(), [
            'type' => ['in:public,worker']
        ]);

        // an additional check to prevent non workers creating worker users
        // this could be extracted to a custom validation rule

        if (auth()->user()->type != 'worker' && $request->type == 'worker') {
            return redirect(route('admin.users.create')
                ->withErrors([
                    'unauthorised' => 
                        'You do not have authorisation to create users of type Worker'
                )]
                ->withInput();
        }

        // Don't forget to add 'type' to the $fillable array on your user model

        User::create($validator->validated());

        return redirect(route('users.admin.create')->with(['success' => 'User created!')];
    }
}

以上是一个非常基本的实现,为您指出正确的方向。您可以将检查user是否为worker的功能提取到middleware中,创建gatescustom validation rules以使事物更可重用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65835538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档