1

每个人。我目前在修改 Laravel 5.2 中的 Auth 注册系统时遇到问题。我想要做的是为普通用户和管理员提供两个独立的日志系统。我在数据库中为名为 admin 的用户添加了一个新字段,并将默认值设置为 0 或 false。使用 Laravel 的 auth 系统使用的注册页面,我添加了一个隐藏的输入,并将值设置为 1。这个页面显然是用于登录管理员,以便他们可以进入管理区域。

当我提交表单时,管理字段被简单地设置为默认值,这不是我想要的站点的这一部分。一旦用户单击此页面上的提交,我希望他的信息以管理员值 1 发送到数据库。有没有办法做到这一点?

到目前为止,这是我为自定义身份验证注册系统所做的工作。

我删除了 name 字段并添加了 firstname 和 lastname 字段,以便用户可以在单独的文本输入中输入这两个值。我更新了 User 模型以将这些字段包含在 $fillable 数组中,并更新了 AuthControllers 验证方法,以便可以验证这些字段。

这是每个的代码

这是 User.php 模型文件

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'firstname', 'lastname', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token', 'admin',
];
}

这里是 AuthContoller 文件

class AuthController extends Controller
{

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']);
}

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

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'firstname' => $data['firstname'],
        'lastname' => $data['lastname'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'admin' => $data['admin'],
    ]);
}
}

如您所见,我还在 User.php 模型文件中添加了 admin 字段,希望它会自动填充。

如果有人能告诉我如何做到这一点,那就太好了。但是正如你所看到的,这对于一个实际的网站来说并不是一个真正的实用解决方案,但它更多的是关于学习 Laravel 的来龙去脉并学习如何定制它。当我开始写这个问题时,在我看来,我可以轻松地为任何用户使用注册,然后允许上级管理员将 admin 字段的值设置为 true,以便用户可以冒险进入应用程序的管理部分。当然,像这样的系统,没有必要有两个单独的登录屏幕,但就像我说的,我只是在学习。

我还计划添加角色,类似于不同级别的管理员的文字印刷机,这将控制诸如创建、编辑和发布诸如帖子之类的内容之类的事情。但现在,我只是按原样开发这个系统。

感谢您对此主题的任何帮助和评论。

4

2 回答 2

0

首先,正如您所提到的,这根本不是一个实际示例,它实际上是建立在主要的安全漏洞之上的,所以我在这里的回答是纯粹展示 Laravel 的功能,而不深入研究给定示例的有效性。

其次,不需要在表单中添加隐藏字段,对于管理员状态,您可以在保存用户模型之前设置该值:

$user = User($request->all());
$user->admin = 1;
$user->save()

通常使某些东西自动填充。您需要在模型中指定

class User extends Model
{
    protected $fillable = ['first_name', 'lastname', 'email', 'password', 'admin']);
}
于 2016-07-31T16:21:27.387 回答
0

如果您要实现基于角色的系统,则无需在用户模型中添加“管理员”。

这是我所做的简短版本

Permission model
- ID
- name
- display_name
- description

Role model
- ID
- name
- display_name
- description
- power
(Let's say both roles Manager and Admin can delete users,
the manager has 50 power and the admin has 90 power.  
The manager is not able to delete the admin, but the admin can delete the manager.)

User model
- ID
- fname
- lname
- username
- email
- password

permission_role table
- permission_id
- role_id
$table->primary(['permission_id', 'role_id'])

role_user table
- role_id
- user_id
$table->primary(['role_id', 'user_id'])

通过此设置,您的用户可以拥有多个角色,并且所有角色都可以拥有 1 个权限。
我也为管理员和其他类型的用户创建了一个单独的登录页面。

为此,我使用不同的路线

Route::get('/[secure_string]/auth', 'AuthController@getNormalLogin')->name('UserLogin');
Route::post('/[secure_string]/auth', 'AuthController@postNormalLogin');
Route::get('/[secure_string]/auth/strong', 'AuthController@getAdminLogin')->name('AdminLogin');
Route::post('/[secure_string]/auth/strong', 'AuthController@postAdminLogin');
(The secure string is placed in a config file,  
anyone can guess /dashboard will get you to the CMS)

这两种方法都将返回相同的视图,但在表单上具有不同的操作属性。

public function getNormalLogin() {
    return view('login_form')->with(['action' => route('UserLogin')]);
}

public function getAdminLogin() {
    return view('login_form')->with(['action' => route('AdminLogin')]);
}

我的表格看起来像

<form method='post' action='{{ $action }}'>
    // input fields and submit button
</form>

我的发帖方法

public function validateForm($request) {
    // validate input
}

public function fetchUser($username) {
    return User::where('username', $username)->first();

public function postNormalLogin(Request $request) {
    $this->validateForm($request);
    $user = $this->fetchUser($request->get('username');
    // see if the user has the admin role assigned (recommend writing a $user->hasRole($role) method)
    if ($user->hasRole('admin')) {
        // if so reject the login request
    } else {
        // continue with login
    }
}

public function postAdminLogin(Request $request) {
     $this->validateForm($request);
    $user = $this->fetchUser($request->get('username');
    // see if the user has the admin role assigned (recommend writing a $user->hasRole($role) method)
    if ($user->hasRole('admin')) {
        // continue with login
    } else {
        // if so reject the login request
    }
}
于 2016-08-01T16:07:27.363 回答