我想在我的网络应用程序中提供自己更改密码的功能,但我发现标题上的错误已解决,所以有人可以帮我解决这个问题吗?
URL:
http://nuatthai.loc/auth/password
路由:
Route::post('auth/password', ['as' => 'auth.postPassword', 'uses' => 'PasswordController@postPassword']);
Route::get('auth/password', ['as' => 'auth.prePassword', 'uses' => 'PasswordController@prePassword']);
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Mail;
use App\Role;
use App\User;
use App\Http\Controllers\Controller;
class PasswordController extends Controller
{
use ResourceController;
public function __construct()
{
$this->middleware('role:' . Role::ADMIN + Role::OWNER + Role::MANAGER + Role::ACCOUNTANT);
}
protected function validator(array $data)
{
return Validator::make($data, [
'password' => 'required|confirmed|min:8',
]);
}
private function updateData(Request $request)
{
$user = User::find($this::getMyId());
$user->password = $request->input('password');
$user->remember_token = str_random(60);
$user->save();
return $user;
}
public function prePassword()
{
return view('auth.change');
}
public function postPassword($request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
\Session::flash('flash_error', $validator->messages());
$this->throwValidationException(
$request, $validator
);
}
$this::updateData($request);
}
}
视图:
{{-- resources/views/auth/change.blade.php --}}
@extends('common.layout')
@section('header') @parent @endsection
@section('navbar') @include('common.navbar') @endsection
@section('sidebar') @include('common.sidebar') @endsection
@section('content')
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::open(array('action' => ['PasswordController@postPassword'], 'class' => 'form-horizontal')) !!}
<div class="form-group">
<label class="col-md-4 control-label input-lg">Password</label>
<div class="col-md-6">
<input type="password" class="form-control input-lg" name="password">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label input-lg">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control input-lg" name="password_confirmation">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary btn-lg" style="margin-right: 15px;">
Reset Password
</button>
</div>
</div>
</form>
</div><!-- .panel-body -->
</div><!-- .panel --> @endsection
{{-- resources/views/auth/change.blade.php --}}
中间件:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Role;
use App\UserRole;
class RoleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $mode)
{
if(!\Auth::check()) {
// session expired.
return redirect()->guest('auth/login');
}
else {
$admin = $mode & Role::ADMIN;
$owner = $mode & Role::OWNER;
$manager = $mode & Role::MANAGER;
$accountant = $mode & Role::ACCOUNTANT;
$type = \Auth::user()->role->type;
if(($admin) && ($type == Role::ADMIN)) {
return $next($request);
}
if(($owner) && ($type == Role::OWNER)) {
return $next($request);
}
if(($manager) && ($type == Role::MANAGER)) {
return $next($request);
}
if(($accountant) && ($type == Role::ACCOUNTANT)) {
return $next($request);
}
return response('Access Denied.', 403);
}
}
}
作用:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\ResourceController;
class Role extends Model
{
use ResourceController;
const TABLE = 'common_roles';
const ROLE_ID = 'role_id';
const ROLE_TYPE = 'type';
const TABLE_ROLE_ID = self::TABLE . '.' . self::ROLE_ID;
const TABLE_ROLE_TYPE = self::TABLE . '.' . self::ROLE_TYPE;
protected $table = self::TABLE;
protected $primaryKey = self::ROLE_ID;
const ADMIN = 0b1000; // 8
const OWNER = 0b100; // 4
const MANAGER = 0b10; // 2
const ACCOUNTANT = 0b1; // 1
public function users()
{
return $this->hasMany('App\User');
}
}
我非常感谢您提前提出任何建议和建议。