Commit c30e8b96 by Nguyen Thanh Son

Merge branch 'feature-7-nus03-add-new-user' into 'master'

7 NUS03 Add new user

See merge request !29
parents 645caf5c 4baea8b1
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\StoreUserRequest;
use App\Models\User;
use Illuminate\Http\Request;
......@@ -52,4 +53,35 @@ class UserController extends Controller
return view('user.show', ['user' => $user]);
}
/**
* Show create page
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
return view('user.create');
}
/**
* Create user by input and validation
*
* @param Request $request
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function store(StoreUserRequest $request)
{
$inputs = $request->all();
$user = User::storeUser($inputs);
if ($user) {
flash(__('ユーザーを作成しました。'))->success();
} else {
flash(__('ユーザーの作成に失敗しました。'))->error();
}
return redirect()->route('users.index');
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|between:8,50|alpha_num',
'password_confirmation' => 'required|same:password',
'status' => 'required',
'role' => 'required'
];
}
public function messages()
{
return [
'required' => __('必須項目が入力されていません。'),
'email' => __('メールアドレスを正しい形式で入力してください。'),
'unique' => __('入力されたメールアドレスは既に使用されています。'),
'between' => __('8~50桁の半角英数字で入力してください。'),
'alpha_num' => __('8~50桁の半角英数字(それぞれ1文字以上含む)で入力してください。'),
'password_confirmation.same' => __('入力された2つのパスワードが一致していません。')
];
}
}
......@@ -2,8 +2,8 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
......@@ -15,12 +15,16 @@ class User extends Authenticatable
const ADMIN = 1;
const ADMIN_EVENT = 2;
const ADMIN_COUPON = 3;
const DISABLE = 0;
const ENABLE = 1;
public static $enable = '有効';
public static $disable = '無効';
public static $status = [
self::DISABLE => '無効',
self::ENABLE => '有効'
];
public static $role = [
self::ADMIN => '管理者',
self::ADMIN_EVENT => 'イベント管理',
self::ADMIN => '管理者',
self::ADMIN_EVENT => 'イベント管理',
self::ADMIN_COUPON => 'クーポン管理'
];
......@@ -30,7 +34,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'password', 'role', 'deleted_at'
];
/**
......@@ -80,4 +84,39 @@ class User extends Authenticatable
return $user->delete();
}
/**
* Store user to database
*
* @param $inputs
* @return mixed
*/
public static function storeUser($inputs)
{
if ($inputs['status'] == self::ENABLE) {
$deleted = null;
} else {
$deleted = Carbon::now();
}
return User::create([
'name' => $inputs['name'],
'email' => $inputs['email'],
'password' => $inputs['password'],
'role' => $inputs['role'],
'deleted_at' => $deleted
]);
}
/**
* Encrypt password befor insert to database
*
* @param $value
*/
public function setPasswordAttribute($value)
{
if (!empty($value)) {
$this->attributes['password'] = bcrypt($value);
}
}
}
......@@ -20,7 +20,7 @@ $factory->define(App\Models\User::class, function (Faker $faker) {
$user = [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'password' => $password ?: $password = 'secret',
'role' => $faker->randomElement([User::ADMIN, User::ADMIN_EVENT, User::ADMIN_COUPON]),
'remember_token' => str_random(10),
];
......
@extends('layouts.master')
@section('pagetitle', __('ADMINユーザ管理'))
@section('pageheader', __('ADMINユーザ管理'))
@section('content')
<div class="row">
<div class="col-md-12">
<div class="btn-link panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">{{ __('▼ 新規登録') }}</h3>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<form class="form-horizontal" role="form" action="{{ route('users.create') }}" method="post">
{{csrf_field()}}
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<div class="col-sm-2">
<label for="inputName" class="control-label">{{ __('ユーザ名') }}</label>
</div>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputName" placeholder="{{ __('例: クレアンス太郎') }}" name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<p class="text-danger">{{ $errors->first('name') }}</p>
@endif
</div>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
<div class="col-sm-2">
<label for="inputEmail3" class="control-label">{{ __('Eメールアドレス') }}</label>
</div>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="{{ __('例: creans.taro@creansmaerd.co.jp') }}" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<p class="text-danger">{{ $errors->first('email') }}</p>
@endif
</div>
</div>
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
<div class="col-sm-2">
<label for="inputPassword" class="control-label">{{ __('パスワード') }}</label>
</div>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword" placeholder="********" name="password">
@if ($errors->has('password'))
<p class="text-danger">{{ $errors->first('password') }}</p>
@endif
</div>
</div>
<div class="form-group {{ $errors->has('password_confirmation') ? 'has-error' : '' }}">
<div class="col-sm-2">
<label for="inputPasswordConfirmation" class="control-label">{{ __('パスワード確認') }}</label>
</div>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPasswordConfirmation" placeholder="********" name="password_confirmation">
@if ($errors->has('password_confirmation'))
<p class="text-danger">{{ $errors->first('password_confirmation') }}</p>
@endif
</div>
</div>
<div class="form-group {{ $errors->has('status') ? 'has-error' : '' }}">
<div class="col-sm-2">
<label for="inputStatus" class="control-label">{{ __('ステータス') }}</label>
</div>
<div class="col-sm-10">
@foreach (\App\Models\User::$status as $key => $value)
<label class="radio-inline"><input type="radio" name="status" value="{{ $key }}">{{ __($value) }}</label>
@endforeach
@if ($errors->has('status'))
<p class="text-danger">{{ $errors->first('status') }}</p>
@endif
</div>
</div>
<div class="form-group {{ $errors->has('role') ? 'has-error' : '' }}">
<div class="col-sm-2">
<label for="inputStatus" class="control-label">{{ __('ユーザ種類') }}</label>
</div>
<div class="col-sm-10">
@foreach (\App\Models\User::$role as $key => $value)
<label class="radio-inline"><input type="radio" name="role" value="{{ $key }}">{{ __($value) }}</label>
@endforeach
@if ($errors->has('role'))
<p class="text-danger">{{ $errors->first('role') }}</p>
@endif
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">{{ __('保存') }}</button>
</div>
</div>
</form>
</div>
</div>
@endsection
\ No newline at end of file
......@@ -35,7 +35,7 @@
<div class="row">
<div class="col-md-4 text-left">
<p class="pagination">
<a class="btn btn-default">{{ __('新規登録') }}</a>
<a href="{{ route('users.create') }}" class="btn btn-default">{{ __('新規登録') }}</a>
</p>
</div>
@include('elements.pagination')
......@@ -62,7 +62,7 @@
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ isset($user->deleted_at) ? __(\App\Models\User::$disable) : __(\App\Models\User::$enable) }}</td>
<td>{{ isset($user->deleted_at) ? __(\App\Models\User::$status[\App\Models\User::DISABLE]) : __(\App\Models\User::$status[\App\Models\User::ENABLE]) }}</td>
<td>{{ __(\App\Models\User::$role[$user->role]) }}</td>
<td>
<p class="text-right">
......@@ -81,7 +81,7 @@
<div class="row">
<div class="col-md-4 text-left">
<p class="pagination">
<a class="btn btn-default">{{ __('新規登録') }}</a>
<a href="{{ route('users.create') }}" class="btn btn-default">{{ __('新規登録') }}</a>
</p>
</div>
@include('elements.pagination')
......
......@@ -35,7 +35,7 @@
<label for="inputStatus" class="control-label">{{ __('ステータス') }}</label>
</div>
<div class="col-sm-10">
{{ $user->deleted_at ? __(\App\Models\User::$disable) : __(\App\Models\User::$enable) }}
{{ $user->deleted_at ? __(\App\Models\User::$status[\App\Models\User::DISABLE]) : __(\App\Models\User::$status[\App\Models\User::ENABLE]) }}
</div>
</div>
<div class="form-group">
......
......@@ -24,3 +24,7 @@ Route::get('users/serach', 'UserController@index')->name('users.search');
Route::delete('users/delete/{id}', 'UserController@destroy')->name('users.destroy');
Route::get('users/show/{id}', 'UserController@show')->name('users.show');
Route::get('users/create', 'UserController@create')->name('users.create');
Route::post('users/create', 'UserController@store');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment