php - Laravel:如何根据用户权限返回验证消息?

标签 php laravel

我有用于检查用户权限的示例函数:is_admin()、is_university()is_student()。函数的返回值是 bool 值。我有简单的验证方法:

protected function validator(array $data)
{        
    return Validator::make($data, [

        "$this->recipient" => [
            'sometimes',
            'required',
            'email',
            'exists:users,email',
            Rule::notIn([auth()->user()->email])
        ],

        "$this->subject" => 'sometimes|required|min:10',

        "$this->message" => 'sometimes|required|min:50',

    ], $this->validator_messages());
}

现在我无法通过在此处检查用户权限来发送消息:

public function send(Request $request)
{
    $recipient = $request->recipient;

    if(User::is_student() == true AND User::is_student($recipient) == true OR User::is_student() == true AND User::is_admin($recipient) == true){
        return false; // Or here I can redirect to back.
    }

    $this->validator($request->all())->validate();
    $this->create($request->all());
    return redirect($this->redirectToAfterSendMessage);
}

在我的代码中,如果发件人是学生,则学生无法向学生和管理员发送消息。如何通过检查发件人和收件人的权限来返回验证消息?谢谢大家的回答!

最佳答案

你说过你想重定向回来。所以,而不是:

return false;

重定向用户:

return back()->with('error', 'Your error message');

关于php - Laravel:如何根据用户权限返回验证消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358324/

相关文章:

php - 数据验证后自动重定向

php - 尝试插入表单中的所有信息以在数据库中更新

php - 如何在保留权限的同时在Docker容器中共享Laravel应用代码库?

php - 有没有办法非对称加密使用 Laravel Passport 创建的访问 token ?

php - laravel 5.2 : ErrorException: preg_replace(): Parameter mismatch, 模式是一个字符串,而替换是一个数组

php - 为不同时区的用户存储特定的日期/时间值

php - 应用程序发明家 2 POST

php - C - 编写自定义处理程序

php - laravel 8 - auth()->factory() {相同的刷新()和尝试()}在JWT Auth Controller 中未定义

php - Laravel 迁移 150 错误