Laravel中的默认密码
我想配置一个默认密码,因为在我正在处理的项目中,管理员将为用户创建帐户 当然,如果a在我的模型上放置了一个默认值,它将不会散列密码。我还能怎么做呢Laravel中的默认密码,laravel,Laravel,我想配置一个默认密码,因为在我正在处理的项目中,管理员将为用户创建帐户 当然,如果a在我的模型上放置了一个默认值,它将不会散列密码。我还能怎么做呢 public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name');
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('POBox');
$table->string('password');
$table->rememberToken();
$table->unsignedBigInteger('address_id')->index();
$table->unsignedInteger('role_id');
$table->timestamps();
$table->foreign('address_id')->references('id')->on('addresses');
});
}
可以在模型中使用默认值 User.php
public class User extends Model
{
protected $attributes = [
'password' => Hash::make(Str::random(40))
];
}
Str::random将为您提供一个随机的40个字符的字符串。你可以把这个换成你喜欢的任何东西。
Hash::make()将对字符串进行散列,以便Laravel可以在用户尝试登录时检查散列。让管理员只创建用户,然后通过电子邮件向新用户发送密码重置 表格:
<form method="POST" action="{{ route('admin.user.store') }}" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
@csrf
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="first_name" class="no-pad-left col-form-label white-txt muli w4">First Name</label>
<input id="first_name" type="text" class="form-control{{ $errors->has('first_name') ? ' is-invalid' : '' }}" name="first_name" placeholder="Enter First Name" value="{{ old('first_name') }}" required autofocus>
@if ($errors->has('first_name'))
<span class="invalid-feedback appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
<div class="col-md-6">
<label for="last_name" class="no-pad-left col-form-label white-txt muli w4">Last Name</label>
<input id="last_name" type="text" class="form-control{{ $errors->has('last_name') ? ' is-invalid' : '' }}" name="last_name" placeholder="Enter Last Name" value="{{ old('last_name') }}" required autofocus>
@if ($errors->has('last_name'))
<span class="invalid-feedback appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white">
<strong>{{ $errors->first('last_name') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<label for="email" class="no-pad-left col-form-label white-txt muli w4">E-Mail Address</label>
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" placeholder="Enter Email Address" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="invalid-feedback appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="flex">
<button type="submit" class="bg-yellow-500 hover:bg-yellow-700 text-navy-500 md:w-1/4 sm:w-full w-full font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline shadow-lg" type="button">
Create User
</button>
</div>
</form>
你应该能够理解电子邮件部分。然后在发送给新用户的电子邮件中,包括一个指向重置密码路由的链接
我就是这样做的。我不确定我是否100%理解。我看到你有一个
role\u id
,那么为什么不在Laravel中使用内置的Auth,并将你的管理部分设置为只允许role\u id
为1的用户访问该部分呢。为其他所有人设置默认值0。我有多个用户,总共4个,因此角色id从0变为3。我只想让我的管理员注册其他用户,所以我需要一个默认密码创建一个角色表,并给你的管理员角色id为5。然后使用路由组保护您的管理员页面<代码>///ADMIN受保护的路由::组(['middleware'=>['auth','roles','roles'=>'ADMIN'],函数(){Route::get('/ADMIN','AdminController@index')->name('admin');})代码>我已经做过了。为了让我的系统有意义,管理员不应该为用户创建密码。我希望他只能为他们创建帐户,并设置一个用户可以更改的默认密码。你现在明白我的意思了吗?这就是我的控制器的样子吗?@Jimmyjbk这是表单和控制器。这将使你达到85%的成功率。这是你通过电子邮件发送给新用户的标准Laravel密码重置链接。我们已经为他们创建了一个帐户,因此他们只需输入电子邮件即可重置密码www.example.com/password/reset
如我所见,您使用了NewUserPassReset。它是在拉威尔预先实施的吗?是的,伙计。它是在执行php artisan make:auth并执行第一次迁移时创建的。不知道你怎么会错过这个。
public function adminUserStore(Request $request){
$validatedData = $request->validate([
'first_name'=> 'required|string|max:255',
'last_name'=> 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
]);
$quickpass = substr( str_shuffle( str_repeat( 'abcdefghijklmnopqrstuvwxyz0123456789', 10 ) ), 0, 10 );
$adminName = Auth::user()->first_name.' '.Auth::user()->last_name;
$newuser = User::create([
'first_name'=> $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email,
'password' => Hash::make($quickpass),
'role_id' => '0',
'added_by' => $adminName,
]);
Mail::to($newuser->email)
->send(new NewUserPassReset(
$request->input('first_name'),
$request->input('last_name'),
$request->input('email')
));
return back()->with('success','The user has been created and a password reset email has been sent to them.');
}