Commit 23b20325 authored by Viral Solani's avatar Viral Solani

delete all api for user

refactor exception handeling
parent 191033ca
...@@ -51,61 +51,6 @@ class Handler extends ExceptionHandler ...@@ -51,61 +51,6 @@ class Handler extends ExceptionHandler
*/ */
public function render($request, Exception $exception) public function render($request, Exception $exception)
{ {
/*
* Redirect if token mismatch error
* Usually because user stayed on the same screen too long and their session expired
*/
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException) {
switch (get_class($exception->getPrevious())) {
case \App\Exceptions\Handler::class:
return response()->json([
'status' => 'error',
'error' => 'Token has not been provided',
'data' => json_decode('{}'),
], $exception->getStatusCode());
case \Tymon\JWTAuth\Exceptions\TokenExpiredException::class:
return response()->json([
'status' => 'error',
'error' => 'Token has expired',
'data' => json_decode('{}'),
], $exception->getStatusCode());
case \Tymon\JWTAuth\Exceptions\TokenInvalidException::class:
case \Tymon\JWTAuth\Exceptions\TokenBlacklistedException::class:
return response()->json([
'status' => 'error',
'error' => 'Token is invalid',
'data' => json_decode('{}'),
], $exception->getStatusCode());
default:
break;
}
}
/*
* Redirect if token mismatch error
* Usually because user stayed on the same screen too long and their session expired
*/
if ($exception instanceof TokenMismatchException) {
return redirect()->route('frontend.auth.login');
}
/*
* All instances of GeneralException redirect back with a flash message to show a bootstrap alert-error
*/
if ($exception instanceof GeneralException) {
//Note:Below code is required when we use an extra class as api request then we need to pass accept:application/json in the header also
//if the header has accept application/json then $request->wantsJson() returns true
// if ($request->ajax() || $request->wantsJson()){
// $json = [
// 'success' => false,
// 'error' => [
// 'message' => $exception->getMessage(),
// ],
// ];
// return response()->json($json, 400);
// }
return redirect()->back()->withInput()->withFlashDanger($exception->getMessage());
}
if (strpos($request->url(), '/api/') !== false) { if (strpos($request->url(), '/api/') !== false) {
\Log::debug('API Request Exception - '.$request->url().' - '.$exception->getMessage().(!empty($request->all()) ? ' - '.json_encode($request->except(['password'])) : '')); \Log::debug('API Request Exception - '.$request->url().' - '.$exception->getMessage().(!empty($request->all()) ? ' - '.json_encode($request->except(['password'])) : ''));
...@@ -131,6 +76,36 @@ class Handler extends ExceptionHandler ...@@ -131,6 +76,36 @@ class Handler extends ExceptionHandler
return $this->setStatusCode(422)->respondWithError($exception->validator->messages()); return $this->setStatusCode(422)->respondWithError($exception->validator->messages());
} }
} }
/*
* Redirect if token mismatch error
* Usually because user stayed on the same screen too long and their session expired
*/
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException) {
switch (get_class($exception->getPrevious())) {
case \App\Exceptions\Handler::class:
return $this->setStatusCode($exception->getStatusCode())->respondWithError('Token has not been provided.');
case \Tymon\JWTAuth\Exceptions\TokenExpiredException::class:
return $this->setStatusCode($exception->getStatusCode())->respondWithError('Token has expired.');
case \Tymon\JWTAuth\Exceptions\TokenInvalidException::class:
case \Tymon\JWTAuth\Exceptions\TokenBlacklistedException::class:
return $this->setStatusCode($exception->getStatusCode())->respondWithError('Token is invalid.');
}
}
/*
* Redirect if token mismatch error
* Usually because user stayed on the same screen too long and their session expired
*/
if ($exception instanceof TokenMismatchException) {
return redirect()->route('frontend.auth.login');
}
/*
* All instances of GeneralException redirect back with a flash message to show a bootstrap alert-error
*/
if ($exception instanceof GeneralException) {
return redirect()->back()->withInput()->withFlashDanger($exception->getMessage());
}
return parent::render($request, $exception); return parent::render($request, $exception);
} }
......
...@@ -117,12 +117,24 @@ class UsersController extends APIController ...@@ -117,12 +117,24 @@ class UsersController extends APIController
* *
* @return mixed * @return mixed
*/ */
public function delteAll(Request $request) public function deleteAll(Request $request)
{ {
$ids = $request->get('ids'); $ids = $request->get('ids');
if (isset($ids) && !empty($ids)) { if (isset($ids) && !empty($ids)) {
$this->repository->deleteAll($ids); $result = $this->repository->deleteAll($ids);
}
if($result)
{
return $this->respond([
'message' => trans('alerts.backend.users.deleted'),
]);
} }
return $this->respond([
'message' => trans('exceptions.backend.access.users.not_found'),
]);
} }
/** /**
......
...@@ -40,7 +40,6 @@ class User extends Authenticatable implements JWTSubject ...@@ -40,7 +40,6 @@ class User extends Authenticatable implements JWTSubject
'first_name', 'first_name',
'last_name', 'last_name',
'email', 'email',
'password',
'status', 'status',
'confirmation_code', 'confirmation_code',
'confirmed', 'confirmed',
......
...@@ -189,7 +189,7 @@ class UserRepository extends BaseRepository ...@@ -189,7 +189,7 @@ class UserRepository extends BaseRepository
} }
/** /**
* Delete User. * Delete User
* *
* @param Model $user * @param Model $user
* *
...@@ -213,9 +213,9 @@ class UserRepository extends BaseRepository ...@@ -213,9 +213,9 @@ class UserRepository extends BaseRepository
} }
/** /**
* Delete All User. * Delete All Users
* *
* @param $ids * @param Model $user
* *
* @throws GeneralException * @throws GeneralException
* *
...@@ -227,9 +227,18 @@ class UserRepository extends BaseRepository ...@@ -227,9 +227,18 @@ class UserRepository extends BaseRepository
throw new GeneralException(trans('exceptions.backend.access.users.cant_delete_self')); throw new GeneralException(trans('exceptions.backend.access.users.cant_delete_self'));
} }
$result = DB::table('users')->whereIn('id', explode(',', $ids))->delete(); if (in_array(1, $ids)) {
throw new GeneralException(trans('exceptions.backend.access.users.cant_delete_admin'));
}
$result = DB::table('users')->whereIn('id', $ids)->delete();
if($result)
{
return true;
}
dd($result); return false;
} }
/** /**
......
...@@ -110,11 +110,9 @@ var Backend = {}; // common variable used in all the files of the backend ...@@ -110,11 +110,9 @@ var Backend = {}; // common variable used in all the files of the backend
associated_container: document.getElementById("#available-permissions"), associated_container: document.getElementById("#available-permissions"),
}, },
init(page) { init(page) {
this.setSelectors(); this.setSelectors();
this.setRolepermission(page); this.setRolepermission(page);
this.addHandlers(); this.addHandlers();
}, },
setSelectors: function () { setSelectors: function () {
this.selectors.associated = document.querySelector("select[name='associated_permissions']"); this.selectors.associated = document.querySelector("select[name='associated_permissions']");
...@@ -477,17 +475,17 @@ var Backend = {}; // common variable used in all the files of the backend ...@@ -477,17 +475,17 @@ var Backend = {}; // common variable used in all the files of the backend
*/ */
Faq: Faq:
{ {
selectors: selectors:
{ {
}, },
init: function () { init: function () {
// this.addHandlers(); // this.addHandlers();
Backend.tinyMCE.init(); Backend.tinyMCE.init();
}, },
addHandlers: function () { addHandlers: function () {
} }
}, },
/** /**
...@@ -515,7 +513,6 @@ var Backend = {}; // common variable used in all the files of the backend ...@@ -515,7 +513,6 @@ var Backend = {}; // common variable used in all the files of the backend
if (this.selectors.cities != null) { if (this.selectors.cities != null) {
this.selectors.cities.select2(); this.selectors.cities.select2();
} }
} }
}, },
...@@ -622,6 +619,10 @@ var Backend = {}; // common variable used in all the files of the backend ...@@ -622,6 +619,10 @@ var Backend = {}; // common variable used in all the files of the backend
}, },
/**
* Settings
*
*/
Settings: Settings:
{ {
selectors: { selectors: {
...@@ -678,9 +679,7 @@ var Backend = {}; // common variable used in all the files of the backend ...@@ -678,9 +679,7 @@ var Backend = {}; // common variable used in all the files of the backend
Backend.Utils.ajaxrequest(route, "POST", { data: value, _token: Backend.Utils.csrf }, Backend.Utils.csrf, callback); Backend.Utils.ajaxrequest(route, "POST", { data: value, _token: Backend.Utils.csrf }, Backend.Utils.csrf, callback);
} }
}); });
}; };
} }
} }
}; };
......
...@@ -37,6 +37,7 @@ return [ ...@@ -37,6 +37,7 @@ return [
'users' => [ 'users' => [
'cant_deactivate_self' => 'You can not do that to yourself.', 'cant_deactivate_self' => 'You can not do that to yourself.',
'cant_delete_self' => 'You can not delete yourself.', 'cant_delete_self' => 'You can not delete yourself.',
'cant_delete_admin' => 'You can not delete Admin.',
'cant_delete_own_session' => 'You can not delete your own session.', 'cant_delete_own_session' => 'You can not delete your own session.',
'cant_delete_own_session' => 'You can not delete your own session.', 'cant_delete_own_session' => 'You can not delete your own session.',
'cant_restore' => 'This user is not deleted so it can not be restored.', 'cant_restore' => 'This user is not deleted so it can not be restored.',
......
<?php <?php
/* /*
...@@ -29,7 +29,7 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio ...@@ -29,7 +29,7 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio
}); });
// Users // Users
Route::resource('users', 'UsersController', ['except' => ['create', 'edit']]); Route::resource('users', 'UsersController', ['except' => ['create', 'edit']]);
Route::post('users/delete-all', 'UsersController@delteAll'); Route::post('users/delete-all', 'UsersController@deleteAll');
//@todo need to change the route name and related changes //@todo need to change the route name and related changes
/*Route::get('deactivatedUsers', 'DeactivatedUsersController@index'); /*Route::get('deactivatedUsers', 'DeactivatedUsersController@index');
Route::get('deletedUsers', 'DeletedUsersController@index');*/ Route::get('deletedUsers', 'DeletedUsersController@index');*/
......
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