Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-adminpanel
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
laravel-adminpanel
Commits
78a91df2
Unverified
Commit
78a91df2
authored
Mar 13, 2018
by
Viral Solani
Committed by
GitHub
Mar 13, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #159 from indranshastri/develop
Change done for api validation and exception handling
parents
cc0e7808
a99a8018
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
253 additions
and
71 deletions
+253
-71
Handler.php
app/Exceptions/Handler.php
+41
-0
APIController.php
app/Http/Controllers/Api/V1/APIController.php
+16
-3
BlogTagsController.php
app/Http/Controllers/Api/V1/BlogTagsController.php
+19
-1
DeactivatedUsersController.php
app/Http/Controllers/Api/V1/DeactivatedUsersController.php
+41
-0
DeletedUsersController.php
app/Http/Controllers/Api/V1/DeletedUsersController.php
+42
-0
RolesController.php
app/Http/Controllers/Api/V1/RolesController.php
+41
-26
UsersController.php
app/Http/Controllers/Api/V1/UsersController.php
+1
-33
StoreApiBlogTagsRequest.php
...ttp/Requests/Backend/BlogTags/StoreApiBlogTagsRequest.php
+47
-0
api.php
routes/api.php
+5
-8
No files found.
app/Exceptions/Handler.php
View file @
78a91df2
...
...
@@ -55,6 +55,35 @@ class Handler extends ExceptionHandler
* 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'
);
}
...
...
@@ -63,6 +92,17 @@ class Handler extends ExceptionHandler
* 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
());
}
...
...
@@ -105,6 +145,7 @@ class Handler extends ExceptionHandler
*/
protected
function
unauthenticated
(
$request
,
AuthenticationException
$exception
)
{
if
(
$request
->
expectsJson
())
{
return
response
()
->
json
([
'error'
=>
'Unauthenticated.'
],
401
);
}
...
...
app/Http/Controllers/Api/V1/APIController.php
View file @
78a91df2
...
...
@@ -178,6 +178,20 @@ class APIController extends Controller
return
$this
->
setStatusCode
(
204
)
->
respond
(
null
);
}
/**Note this function is same as the below function but instead of responding with error below function returns error json
* Throw Validation.
*
* @param string $message
*
* @return mix
*/
// public function throwValidation($message)
// {
// return $this->setStatusCode(422)
// ->respondWithError($message);
// }
/**
* Throw Validation.
*
...
...
@@ -185,9 +199,8 @@ class APIController extends Controller
*
* @return mix
*/
public
function
throwValidation
(
$
message
)
public
function
throwValidation
(
$
validation
)
{
return
$this
->
setStatusCode
(
422
)
->
respondWithError
(
$message
);
return
[
"error"
=>
$validation
->
errors
()];
}
}
app/Http/Controllers/Api/V1/BlogTagsController.php
View file @
78a91df2
...
...
@@ -6,7 +6,9 @@ use App\Http\Resources\BlogTagsResource;
use
App\Models\BlogTags\BlogTag
;
use
App\Repositories\Backend\BlogTags\BlogTagsRepository
;
use
Illuminate\Http\Request
;
use
App\Http\Requests\Backend\BlogTags\StoreApiBlogTagsRequest
;
use
Validator
;
use
Exception
;
class
BlogTagsController
extends
APIController
{
...
...
@@ -67,6 +69,22 @@ class BlogTagsController extends APIController
return
new
BlogTagsResource
(
BlogTag
::
orderBy
(
'created_at'
,
'desc'
)
->
first
());
}
/** NOTE This function is same as about but uses StoreApiBlogTagsRequest for validation of the api
* Creates the Resource for BlogTag.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*
*
*/
// public function store(StoreApiBlogTagsRequest $request)
// {
// $this->repository->create($request->all());
// return new BlogTagsResource(BlogTag::orderBy('created_at', 'desc')->first());
// }
/**
* @param BlogTag $blog_tag
* @param UpdateBlogTagRequest $request
...
...
@@ -78,7 +96,7 @@ class BlogTagsController extends APIController
$validation
=
$this
->
validatingRequest
(
$request
,
$blog_tag
->
id
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
()
);
return
$this
->
throwValidation
(
$validation
);
}
$this
->
repository
->
update
(
$blog_tag
,
$request
->
all
());
...
...
app/Http/Controllers/Api/V1/DeactivatedUsersController.php
0 → 100644
View file @
78a91df2
<?php
namespace
App\Http\Controllers\Api\V1
;
use
App\Events\Backend\Access\User\UserCreated
;
use
App\Events\Backend\Access\User\UserUpdated
;
use
App\Http\Resources\UserResource
;
use
App\Models\Access\User\User
;
use
App\Repositories\Backend\Access\User\UserRepository
;
use
Illuminate\Http\Request
;
use
Validator
;
class
DeactivatedUsersController
extends
APIController
{
protected
$repository
;
/**
* __construct.
*
* @param $repository
*/
public
function
__construct
(
UserRepository
$repository
)
{
$this
->
repository
=
$repository
;
}
/**
* Return the deactivated users.
*
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
index
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
UserResource
::
collection
(
$this
->
repository
->
getForDataTable
(
0
,
false
)
->
paginate
(
$limit
)
);
}
}
app/Http/Controllers/Api/V1/DeletedUsersController.php
0 → 100644
View file @
78a91df2
<?php
namespace
App\Http\Controllers\Api\V1
;
use
App\Events\Backend\Access\User\UserCreated
;
use
App\Events\Backend\Access\User\UserUpdated
;
use
App\Http\Resources\UserResource
;
use
App\Models\Access\User\User
;
use
App\Repositories\Backend\Access\User\UserRepository
;
use
Illuminate\Http\Request
;
use
Validator
;
class
DeletedUsersController
extends
APIController
{
protected
$repository
;
/**
* __construct.
*
* @param $repository
*/
public
function
__construct
(
UserRepository
$repository
)
{
$this
->
repository
=
$repository
;
}
/**
* Return the deleted users.
*
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
index
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
UserResource
::
collection
(
$this
->
repository
->
getForDataTable
(
0
,
true
)
->
paginate
(
$limit
)
);
}
}
app/Http/Controllers/Api/V1/RolesController.php
View file @
78a91df2
...
...
@@ -22,26 +22,29 @@ class RolesController extends APIController
$this
->
repository
=
$repository
;
}
/**
* Return the
user
s.
* Return the
role
s.
*
* @return \Illuminate\Http\Response
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
index
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
RoleResource
::
collection
(
$this
->
repository
->
get
Paginated
(
$limit
)
$this
->
repository
->
get
ForDataTable
()
->
paginate
(
$limit
)
);
}
/**
* Return the specified resource.
*
* @param
User $user
* @param
Role $role
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\
Json
Response
*/
public
function
show
(
Role
$role
)
{
...
...
@@ -49,7 +52,7 @@ class RolesController extends APIController
}
/**
* Creates the Resour
s
e for Role.
* Creates the Resour
c
e for Role.
*
* @param Request $request
*
...
...
@@ -57,7 +60,7 @@ class RolesController extends APIController
*/
public
function
store
(
Request
$request
)
{
$validation
=
$this
->
validat
ingRequest
(
$request
);
$validation
=
$this
->
validat
eRole
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
}
...
...
@@ -68,14 +71,16 @@ class RolesController extends APIController
}
/**
* Update User.
*
* @param Request $request
* @param Role $role
* @param UpdateRoleRequest $request
*
* @return
mixed
* @return
\Illuminate\Http\JsonResponse
*/
public
function
update
(
Request
$request
,
Role
$role
)
{
$validation
=
$this
->
validat
ingRequest
(
$request
);
$validation
=
$this
->
validat
eRole
(
$request
);
if
(
$validation
->
fails
())
{
return
$this
->
throwValidation
(
$validation
->
messages
()
->
first
());
...
...
@@ -88,7 +93,30 @@ class RolesController extends APIController
return
new
RoleResource
(
$role
);
}
public
function
validatingRequest
(
Request
$request
)
/**
* @param Role $role
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
destroy
(
Role
$role
,
Request
$request
)
{
$this
->
repository
->
delete
(
$role
);
return
$this
->
respond
([
'message'
=>
trans
(
'alerts.backend.roles.deleted'
),
]);
}
/**
* validateUser User.
*
* @param $request
*
* @return Validator object
*/
public
function
validateRole
(
Request
$request
)
{
$permissions
=
''
;
...
...
@@ -103,17 +131,4 @@ class RolesController extends APIController
return
$validation
;
}
/**
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
*/
public
function
destroy
(
Role
$role
,
Request
$request
)
{
$this
->
repository
->
delete
(
$role
);
return
[
'message'
=>
'success'
];
}
}
app/Http/Controllers/Api/V1/UsersController.php
View file @
78a91df2
...
...
@@ -76,7 +76,7 @@ class UsersController extends APIController
* @param Request $request
* @param User $user
*
* @return
\Illuminate\Http\JsonResponse
* @return
Validator object
*/
public
function
update
(
Request
$request
,
User
$user
)
{
...
...
@@ -108,38 +108,6 @@ class UsersController extends APIController
]);
}
/**
* Return the deactivate users.
*
* @param Request
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
deactivatedUserList
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
UserResource
::
collection
(
$this
->
repository
->
getForDataTable
(
0
,
false
)
->
paginate
(
$limit
)
);
}
/**
* Return the deleted users.
*
* @param User $user
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
deleteUserList
(
Request
$request
)
{
$limit
=
$request
->
get
(
'paginate'
)
?
$request
->
get
(
'paginate'
)
:
25
;
return
UserResource
::
collection
(
$this
->
repository
->
getForDataTable
(
0
,
true
)
->
paginate
(
$limit
)
);
}
/**
* validateUser User.
*
...
...
app/Http/Requests/Backend/BlogTags/StoreApiBlogTagsRequest.php
0 → 100644
View file @
78a91df2
<?php
namespace
App\Http\Requests\Backend\BlogTags
;
use
App\Http\Requests\Request
;
/**
* Class StoreBlogTagsRequest.
*/
class
StoreApiBlogTagsRequest
extends
Request
{
/**
* 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|max:191'
,
];
}
/**
* Get the custom validation messages.
*
* @return array
*/
public
function
messages
()
{
return
[
'name.required'
=>
'Blog Tag name is a required field.111111'
,
'name.max'
=>
'Blog Tag may not be greater than 191 characters.'
,
];
}
}
routes/api.php
View file @
78a91df2
...
...
@@ -27,17 +27,14 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio
Route
::
post
(
'password/email'
,
'ForgotPasswordController@sendResetLinkEmail'
);
// Route::post('password/reset', 'ResetPasswordController@reset')->name('password.reset');
});
Route
::
resource
(
'users'
,
'UsersController'
,
[
'except'
=>
[
'edit'
,
'udpate'
]]);
// Users
Route
::
group
([
'prefix'
=>
'users'
],
function
()
{
Route
::
get
(
'deactiveUsers'
,
'UsersController@deactivatedUserList
'
);
Route
::
get
(
'deleteUsers'
,
'UsersController@deleteUserList
'
);
});
Route
::
resource
(
'users'
,
'UsersController'
,
[
'except'
=>
[
'create'
,
'edit'
]]);
Route
::
get
(
'deactivatedUsers'
,
'DeactivatedUsersController@index
'
);
Route
::
get
(
'deletedUsers'
,
'DeletedUsersController@index
'
);
// Roles
Route
::
resource
(
'roles'
,
'RolesController'
);
Route
::
resource
(
'roles'
,
'RolesController'
,
[
'except'
=>
[
'create'
,
'edit'
]]
);
// Permission
Route
::
resource
(
'permission'
,
'PermissionController'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment