Skip to content

Commit 4cb24d3

Browse files
Ivan LiIvan Li
Ivan Li
authored and
Ivan Li
committed
user status fixed, user management enhanced
1 parent 86334f4 commit 4cb24d3

File tree

5 files changed

+263
-49
lines changed

5 files changed

+263
-49
lines changed

app/Http/Controllers/Auth/AuthController.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use App\User;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Facades\Input;
910
use Illuminate\Support\Facades\Mail;
1011
use Illuminate\Support\Facades\Session;
12+
use Symfony\Component\HttpFoundation\JsonResponse;
1113
use Validator;
1214
use App\Http\Controllers\Controller;
1315
use Illuminate\Foundation\Auth\ThrottlesLogins;
@@ -63,6 +65,55 @@ protected function validator(array $data)
6365
]);
6466
}
6567

68+
/**
69+
* override login process by adding status validation
70+
*
71+
* @param Request $request
72+
* @return AuthController|\Illuminate\Http\RedirectResponse
73+
*/
74+
public function login(Request $request)
75+
{
76+
$this->validateLogin($request);
77+
// If the class is using the ThrottlesLogins trait, we can automatically throttle
78+
// the login attempts for this application. We'll key this by the username and
79+
// the IP address of the client making these requests into this application.
80+
$throttles = $this->isUsingThrottlesLoginsTrait();
81+
82+
if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
83+
$this->fireLockoutEvent($request);
84+
return $this->sendLockoutResponse($request);
85+
}
86+
$credentials = $this->getCredentials($request);
87+
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
88+
if (Auth::check() && (Auth::user()->status == "locked" || Auth::user()->status == "deleted")) {
89+
$this->fireLockoutEvent($request);
90+
Auth::guard($this->getGuard())->logout();
91+
$output = new \stdClass();
92+
$output->status = false;
93+
$output->responseJSON = array(
94+
"Your account is locked out. Please contact Ivan for more information."
95+
);
96+
if ($request->ajax()) {
97+
if ($request->wantsJson()) {
98+
return new JsonResponse($output);
99+
} else {
100+
return $output;
101+
}
102+
} else {
103+
return $this->sendLockoutResponse($request);
104+
}
105+
}
106+
return $this->handleUserWasAuthenticated($request, $throttles);
107+
}
108+
109+
if ($throttles && !$lockedOut) {
110+
$this->incrementLoginAttempts($request);
111+
}
112+
113+
return $this->sendFailedLoginResponse($request);
114+
}
115+
116+
66117
/**
67118
* Create a new user instance after a valid registration.
68119
*
@@ -167,11 +218,15 @@ protected function authenticated(Request $request, $user)
167218
*/
168219
protected function sendFailedLoginResponse(Request $request)
169220
{
170-
if ($request->json()) {
221+
if ($request->ajax()) {
171222
$output = new \stdClass();
172223
$output->status = false;
173224
$output->responseJSON = array($this->loginUsername() => $this->getFailedLoginMessage());
174-
return json_encode($output);
225+
if ($request->wantsJson()) {
226+
return new JsonResponse($output);
227+
} else {
228+
return $output;
229+
}
175230
} else {
176231
return redirect()->back()
177232
->withInput($request->only($this->loginUsername(), 'remember'))

app/Http/Controllers/UserController.php

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Role;
6+
use App\User;
57
use Illuminate\Database\Eloquent\ModelNotFoundException;
6-
use Illuminate\Foundation\Auth\User;
78
use Illuminate\Http\JsonResponse;
89
use Illuminate\Http\Request;
910

1011
use App\Http\Requests;
12+
use Illuminate\Support\Facades\Validator;
1113

1214
class UserController extends Controller
1315
{
@@ -20,14 +22,17 @@ public function __construct()
2022
public function index(Request $request)
2123
{
2224
if ($request->ajax()) {
23-
$users = User::all();
24-
if ($request->json()) {
25+
$users = User::with("roles")->get();
26+
if ($request->wantsJson()) {
2527
return new JsonResponse($users);
2628
} else {
2729
return $users;
2830
}
2931
} else {
30-
return view('admin.user.index');
32+
$roles = Role::all();
33+
return view('admin.user.index')->with(array(
34+
"roles" => $roles
35+
));
3136
}
3237
}
3338

@@ -52,8 +57,10 @@ public function edit($user_id)
5257
{
5358
try {
5459
$user = User::findOrFail($user_id);
60+
$roles = Role::all();
5561
return view('admin.user.edit')->with(array(
5662
"user" => $user,
63+
"roles" => $roles
5764
));
5865
} catch (ModelNotFoundException $e) {
5966
abort(404, "Page not found");
@@ -63,7 +70,66 @@ public function edit($user_id)
6370

6471
public function update(Request $request, $user_id)
6572
{
66-
73+
$validator = Validator::make($request->all(), [
74+
"name" => "required|max:255",
75+
"email" => "exists:users|max:255",
76+
"status" => "in:inactive,active,locked,deleted",
77+
]);
78+
if ($validator->fails()) {
79+
$output = new \stdClass();
80+
$output->status = true;
81+
$output->errors = $validator->errors();
82+
if ($request->ajax()) {
83+
if ($request->wantsJson()) {
84+
return new JsonResponse($output);
85+
} else {
86+
return $output;
87+
}
88+
} else {
89+
return back()->withErrors($validator->errors())->withInput();
90+
}
91+
}
92+
try {
93+
$user = User::findOrFail($user_id);
94+
$user->name = $request->get("name");
95+
$user->status = $request->get("status");
96+
$user->save();
97+
$user->detachRoles();
98+
$roles = $request->get("roles");
99+
if (is_array($roles)) {
100+
foreach ($roles as $roleID) {
101+
$role = Role::findOrFail($roleID);
102+
$user->attachRole($role);
103+
}
104+
}
105+
$output = new \stdClass();
106+
$output->status = true;
107+
$output->data = array(
108+
"user" => $user
109+
);
110+
if ($request->ajax()) {
111+
if ($request->wantsJson()) {
112+
return new JsonResponse($output);
113+
} else {
114+
return $output;
115+
}
116+
} else {
117+
return redirect()->route("admin.user", [$user]);
118+
}
119+
} catch (ModelNotFoundException $e) {
120+
$output = new \stdClass();
121+
$output->status = true;
122+
$output->errors = "User not found";
123+
if ($request->ajax()) {
124+
if ($request->wantsJson()) {
125+
return new JsonResponse($output);
126+
} else {
127+
return $output;
128+
}
129+
} else {
130+
return back()->withErrors(array("User not found"))->withInput();
131+
}
132+
}
67133
}
68134

69135
public function destroy(Request $request, $user_id)
@@ -72,20 +138,21 @@ public function destroy(Request $request, $user_id)
72138
$user = User::findOrFail($user_id);
73139
$user->status = "deleted";
74140
$user->save();
75-
$output = array(
76-
"status" => true,
77-
"data" => array(
78-
"user" => $user
79-
)
141+
$output = new \stdClass();
142+
$output->status = true;
143+
$output->data = array(
144+
"user" => $user
80145
);
81-
if ($request->json()) {
82-
return response()->json($output);
146+
} catch (ModelNotFoundException $e) {
147+
$output = new \stdClass();
148+
$output->status = false;
149+
$output->errors = array("User not found");
150+
} finally {
151+
if ($request->ajax()) {
152+
return new JsonResponse($output);
83153
} else {
84154
return $output;
85155
}
86-
} catch (ModelNotFoundException $e) {
87-
abort(404, "Page not found");
88-
return false;
89156
}
90157
}
91158

@@ -100,10 +167,14 @@ public function revive(Request $request, $user_id)
100167
"user" => $user
101168
)
102169
);
103-
if ($request->json()) {
104-
return response()->json($output);
170+
if ($request->ajax()) {
171+
if ($request->wantsJson()) {
172+
return new JsonResponse($output);
173+
} else {
174+
return $output;
175+
}
105176
} else {
106-
return $output;
177+
return redirect()->route("admin.user");
107178
}
108179
} catch (ModelNotFoundException $e) {
109180
abort(404, "Page not found");

public/assets/internal/css/app.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,12 @@
7777

7878
select.control-inline {
7979
padding-right: 30px;
80+
}
81+
82+
table th.shrink, table td.shrink {
83+
width: 1%;
84+
}
85+
86+
.form-group{
87+
margin-bottom: 10px;
8088
}

resources/views/admin/user/edit.blade.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
</div>
3535
{!! Form::model($user, array('route' => array('admin.user.update', $user->id), 'method' => 'patch', 'files' => true, 'onsubmit' => 'return false;', "id" => "frm-update-user")) !!}
3636
@include('admin.user.forms.user')
37+
<div class="form-group">
38+
<label for="roles">Roles</label>
39+
<select name="roles[]" id="roles" class="form-control input-sm" multiple="multiple">
40+
@foreach($roles as $role)
41+
<option value="{{$role->id}}" {{$user->hasRole($role->name) ? "selected" : ""}}>{{$role->display_name}}</option>
42+
@endforeach
43+
</select>
44+
</div>
3745
{!! Form::submit('Save', ["class"=>"btn btn-default btn-sm", "href"=>"#", "onclick" => "updateUserOnClick()"]) !!}
3846
{!! Form::close() !!}
3947
</div>
@@ -52,9 +60,9 @@ function updateUserOnClick() {
5260
"data": $("#frm-update-user").serialize(),
5361
"dataType": "json",
5462
"success": function (response) {
55-
if(response.status == true){
56-
57-
}else{
63+
if (response.status == true) {
64+
window.location.href = "{{url('admin/user')}}";
65+
} else {
5866
if (response.responseJSON) {
5967
try {
6068
var $error = response.responseJSON;
@@ -94,8 +102,7 @@ function updateUserOnClick() {
94102
})
95103
}
96104
97-
function showEditUserErrMsg(callback)
98-
{
105+
function showEditUserErrMsg(callback) {
99106
$(".error-msgs").slideDown(function () {
100107
if ($.isFunction(callback)) {
101108
callback();

0 commit comments

Comments
 (0)