Commit a0f1a12f authored by Vipul Basapati's avatar Vipul Basapati

Merge branch 'master' of https://github.com/viralsolani/laravel-adminpanel into develop

parents f7336639 23cc26ba
......@@ -93,7 +93,7 @@ You can now access the server at http://localhost:8000
npm install
npm run development
php artisan key:generate
php artisan jwt:generate
php artisan jwt:secret
php artisan vendor:publish --tag=lfm_public
## Logging In
......@@ -122,7 +122,7 @@ Password: `1234`
If you come across any issues please report them [here](https://github.com/viralsolani/laravel-adminpanel/issues).
## Contributing
Feel free to create any pull requests for the project. For propsing any new changes or features you want to add to the project, you can send us an email at viral.solani@gmail.com or basapativipulkumar@gmail.com.
Feel free to create any pull requests for the project. For proposing any new changes or features you want to add to the project, you can send us an email at viral.solani@gmail.com or basapativipulkumar@gmail.com.
## License
......
......@@ -3,9 +3,15 @@
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class Handler extends ExceptionHandler
{
......@@ -47,6 +53,53 @@ class Handler extends ExceptionHandler
*/
public function render($request, Exception $exception)
{
//dd($exception);
if (strpos($request->url(), '/api/') !== false) {
\Log::debug('API Request Exception - '.$request->url().' - '.$exception->getMessage().(!empty($request->all()) ? ' - '.json_encode($request->except(['password'])) : ''));
if ($exception instanceof AuthorizationException) {
return $this->setStatusCode(403)->respondWithError($exception->getMessage());
}
if ($exception instanceof MethodNotAllowedHttpException) {
return $this->setStatusCode(403)->respondWithError('Please check HTTP Request Method. - MethodNotAllowedHttpException');
}
if ($exception instanceof NotFoundHttpException) {
return $this->setStatusCode(403)->respondWithError('Please check your URL to make sure request is formatted properly. - NotFoundHttpException');
}
if ($exception instanceof GeneralException) {
return $this->setStatusCode(403)->respondWithError($exception->getMessage());
}
if ($exception instanceof ModelNotFoundException) {
return $this->setStatusCode(403)->respondWithError('Item could not be found. Please check identifier.');
}
if ($exception instanceof ValidationException) {
\Log::debug('API Validation Exception - '.json_encode($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 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
......@@ -81,4 +134,58 @@ class Handler extends ExceptionHandler
return redirect()->guest(route('frontend.auth.login'));
}
/**
* get the status code.
*
* @return statuscode
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* set the status code.
*
* @param [type] $statusCode [description]
*
* @return statuscode
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* respond with error.
*
* @param $message
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithError($message)
{
return $this->respond([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode(),
],
]);
}
/**
* Respond.
*
* @param array $data
* @param array $headers
*
* @return \Illuminate\Http\JsonResponse
*/
public function respond($data, $headers = [])
{
return response()->json($data, $this->getStatusCode(), $headers);
}
}
......@@ -178,7 +178,7 @@ 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
......@@ -188,6 +188,6 @@ class APIController extends Controller
public function throwValidation($message)
{
return $this->setStatusCode(422)
->respondWithError($message);
->respondWithError($message);
}
}
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api\V1;
use App\Models\Access\User\User;
use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
......
......@@ -23,9 +23,11 @@ class BlogCategoriesController extends APIController
}
/**
* Return the users.
* Return the blog-categories.
*
* @return \Illuminate\Http\Response
* @param Request $request
*
*@return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
......@@ -39,9 +41,9 @@ class BlogCategoriesController extends APIController
/**
* Return the specified resource.
*
* @param User $user
* @param BlogCategory $blog_category
*
* @return \Illuminate\Http\Response
*@return \Illuminate\Http\JsonResponse
*/
public function show(BlogCategory $blog_category)
{
......@@ -53,11 +55,11 @@ class BlogCategoriesController extends APIController
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*@return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateBlogCategory($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
......@@ -68,14 +70,14 @@ class BlogCategoriesController extends APIController
}
/**
* @param BlogCategory $blog_category
* @param UpdateBlogCategoryRequest $request
* @param BlogCategory $blog_category
* @param Request $request
*
* @return mixed
*/
public function update(Request $request, BlogCategory $blog_category)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateBlogCategory($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -88,25 +90,34 @@ class BlogCategoriesController extends APIController
return new BlogCategoriesResource($blog_category);
}
public function validatingRequest(Request $request)
/**
* @param BlogCategory $blog_category
* @param Request $request
*
* @return mixed
*/
public function destroy(BlogCategory $blog_category, Request $request)
{
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
]);
$this->repository->delete($blog_category);
return $validation;
return $this->respond([
'message' => trans('alerts.backend.blogcategories.deleted'),
]);
}
/**
* @param BlogCategory $blog_category
* @param DeleteBlogCategoryRequest $request
* validateUser BlogCategory Requests.
*
* @return mixed
* @param Request $request
*
* @return Validator object
*/
public function destroy(BlogCategory $blog_category, Request $request)
public function validateBlogCategory(Request $request)
{
$this->repository->delete($blog_category);
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
]);
return ['message'=>'success'];
return $validation;
}
}
......@@ -23,9 +23,9 @@ class BlogTagsController extends APIController
}
/**
* Return the users.
* Return the BlogTags.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
......@@ -39,9 +39,9 @@ class BlogTagsController extends APIController
/**
* Return the specified resource.
*
* @param User $user
* @param BlogTag $blog_tag
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(BlogTag $blog_tag)
{
......@@ -53,7 +53,7 @@ class BlogTagsController extends APIController
*
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
......@@ -68,17 +68,19 @@ class BlogTagsController extends APIController
}
/**
* @param BlogTag $blog_tag
* @param UpdateBlogTagRequest $request
* Update BlogTag.
*
* @param BlogTag $blog_tag
* @param Request $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, BlogTag $blog_tag)
{
$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());
......@@ -88,20 +90,13 @@ class BlogTagsController extends APIController
return new BlogTagsResource($blog_tag);
}
public function validatingRequest(Request $request, $id = 0)
{
$validation = Validator::make($request->all(), [
'name' => 'required|max:191|unique:blog_tags,name,'.$id,
]);
return $validation;
}
/**
* Delete BlogTag.
*
* @param BlogTag $blog_tag
* @param DeleteBlogTagRequest $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(BlogTag $blog_tag, Request $request)
{
......@@ -109,4 +104,20 @@ class BlogTagsController extends APIController
return ['message'=>'success'];
}
/**
* validate BlogTag.
*
* @param $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function validatingRequest(Request $request, $id = 0)
{
$validation = Validator::make($request->all(), [
'name' => 'required|max:191|unique:blog_tags,name,'.$id,
]);
return $validation;
}
}
......@@ -23,9 +23,9 @@ class BlogsController extends APIController
}
/**
* Return the users.
* Return the blogs.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
......@@ -39,9 +39,9 @@ class BlogsController extends APIController
/**
* Return the specified resource.
*
* @param User $user
* @param Blog blog
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(Blog $blog)
{
......@@ -53,11 +53,12 @@ class BlogsController extends APIController
*
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateBlog($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
......@@ -68,14 +69,16 @@ class BlogsController extends APIController
}
/**
* @param Blog $blog
* @param UpdateBlogRequest $request
* Update blog.
*
* @param Blog $blog
* @param Request $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, Blog $blog)
{
$validation = $this->validatingRequest($request, 'update');
$validation = $this->validateBlog($request, 'update');
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -88,9 +91,33 @@ class BlogsController extends APIController
return new BlogsResource($blog);
}
public function validatingRequest(Request $request, $type = 'insert')
/**
* Delete Blog.
*
* @param Blog $blog
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Blog $blog, Request $request)
{
$featured_image = ($type == 'insert') ? 'required' : '';
$this->repository->delete($blog);
return $this->respond([
'message' => trans('alerts.backend.blogs.deleted'),
]);
}
/**
* validate Blog.
*
* @param $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function validateBlog(Request $request, $action = 'insert')
{
$featured_image = ($action == 'insert') ? 'required' : '';
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
......@@ -103,6 +130,11 @@ class BlogsController extends APIController
return $validation;
}
/**
* validate message for validate blog.
*
* @return \Illuminate\Http\JsonResponse
*/
public function messages()
{
return [
......@@ -110,17 +142,4 @@ class BlogsController extends APIController
'name.max' => 'Blog Title may not be greater than 191 characters.',
];
}
/**
* @param Blog $blog
* @param DeleteBlogRequest $request
*
* @return mixed
*/
public function destroy(Blog $blog, Request $request)
{
$this->repository->delete($blog);
return ['message'=>'success'];
}
}
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Resources\UserResource;
use App\Repositories\Backend\Access\User\UserRepository;
use Illuminate\Http\Request;
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)
);
}
}
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Resources\UserResource;
use App\Repositories\Backend\Access\User\UserRepository;
use Illuminate\Http\Request;
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)
);
}
}
......@@ -23,9 +23,9 @@ class FaqsController extends APIController
}
/**
* Return the users.
* Return the faqs.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
......@@ -39,9 +39,9 @@ class FaqsController extends APIController
/**
* Return the specified resource.
*
* @param User $user
* @param Faq $faq
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(Faq $faq)
{
......@@ -53,11 +53,11 @@ class FaqsController extends APIController
*
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateFaq($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
......@@ -68,14 +68,16 @@ class FaqsController extends APIController
}
/**
* @param Faq $faq
* @param UpdateFaqRequest $request
* Update Faq.
*
* @return mixed
* @param Faq $faq
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, Faq $faq)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateFaq($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -88,26 +90,37 @@ class FaqsController extends APIController
return new FaqsResource($faq);
}
public function validatingRequest(Request $request)
/**
* Delete Faq.
*
* @param Faq $faq
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Faq $faq, Request $request)
{
$validation = Validator::make($request->all(), [
'question' => 'required|max:191',
'answer' => 'required',
]);
$this->repository->delete($faq);
return $validation;
return $this->respond([
'message' => trans('alerts.backend.faqs.deleted'),
]);
}
/**
* @param Faq $faq
* @param DeleteFaqRequest $request
* validate Faq.
*
* @return mixed
* @param $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Faq $faq, Request $request)
public function validateFaq(Request $request)
{
$this->repository->delete($faq);
$validation = Validator::make($request->all(), [
'question' => 'required|max:191',
'answer' => 'required',
]);
return ['message'=>'success'];
return $validation;
}
}
......@@ -23,9 +23,9 @@ class PagesController extends APIController
}
/**
* Return the users.
* Return the pages.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
......@@ -39,9 +39,9 @@ class PagesController extends APIController
/**
* Return the specified resource.
*
* @param User $user
* @param Pages $page
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(Page $page)
{
......@@ -49,15 +49,15 @@ class PagesController extends APIController
}
/**
* Creates the Resourse for Page.
* Creates the Resource for Page.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
$validation = $this->validatePages($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
......@@ -68,14 +68,16 @@ class PagesController extends APIController
}
/**
* @param Page $page
* @param UpdatePageRequest $request
* Update Page.
*
* @param Page $page
* @param Request $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, Page $page)
{
$validation = $this->validatingRequest($request);
$validation = $this->validatePages($request, $page->id);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -88,26 +90,38 @@ class PagesController extends APIController
return new PagesResource($page);
}
public function validatingRequest(Request $request)
{
$validation = Validator::make($request->all(), [
'title' => 'required|max:191',
'description' => 'required',
]);
return $validation;
}
/**
* Delete Page.
*
* @param Page $page
* @param DeletePageRequest $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Page $page, Request $request)
{
$this->repository->delete($page);
return ['message'=>'success'];
return $this->respond([
'message' => trans('alerts.backend.pages.deleted'),
]);
}
/**
* validateUser Pages Requests.
*
* @param Request $request
* @param int $id
*
* @return \Illuminate\Http\JsonResponse
*/
public function validatePages(Request $request, $id = 0)
{
$validation = Validator::make($request->all(), [
'title' => 'required|max:191|unique:pages,title,'.$id,
'description' => 'required',
]);
return $validation;
}
}
......@@ -23,25 +23,27 @@ class PermissionController extends APIController
}
/**
* Return the users.
* Return the permissions.
*
* @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 PermissionResource::collection(
$this->repository->getPaginated($limit)
$this->repository->getForDataTable()->paginate($limit)
);
}
/**
* Return the specified resource.
*
* @param User $user
* @param Permission $permission
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(Permission $permission)
{
......@@ -49,15 +51,15 @@ class PermissionController extends APIController
}
/**
* Creates the Resource for Role.
* Creates the Resource for Permission.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
$validation = $this->validatePermission($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
......@@ -69,14 +71,14 @@ class PermissionController extends APIController
}
/**
* @param Role $role
* @param UpdateRoleRequest $request
* @param Permission $permission
* @param Request $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, Permission $permission)
{
$validation = $this->validatingRequest($request);
$validation = $this->validatePermission($request, $permission->id);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -89,26 +91,38 @@ class PermissionController extends APIController
return new PermissionResource($permission);
}
public function validatingRequest(Request $request)
{
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
'display_name' => 'required|max:191',
]);
return $validation;
}
/**
* Delete permission.
*
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Permission $permission, Request $request)
{
$this->repository->delete($permission);
return ['message'=>'success'];
return $this->respond([
'message' => trans('alerts.backend.permissions.deleted'),
]);
}
/**
* validateUser Permission Requests.
*
* @param Request $request
* @param int $id
*
* @return Validator object
*/
public function validatePermission(Request $request, $id = 0)
{
$validation = Validator::make($request->all(), [
'name' => 'required|max:191|unique:permissions,name,'.$id,
'display_name' => 'required|max:191',
]);
return $validation;
}
}
......@@ -23,25 +23,27 @@ class RolesController extends APIController
}
/**
* Return the users.
* Return the roles.
*
* @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->getPaginated($limit)
$this->repository->getForDataTable()->paginate($limit)
);
}
/**
* Return the specified resource.
*
* @param User $user
* @param Role $role
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(Role $role)
{
......@@ -49,15 +51,15 @@ class RolesController extends APIController
}
/**
* Creates the Resourse for Role.
* Creates the Resource for Role.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateRole($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
......@@ -68,14 +70,16 @@ class RolesController extends APIController
}
/**
* @param Role $role
* @param UpdateRoleRequest $request
* Update Role.
*
* @param Request $request
* @param Role $role
*
* @return mixed
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, Role $role)
{
$validation = $this->validatingRequest($request);
$validation = $this->validateRole($request, $role->id);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -88,7 +92,32 @@ class RolesController extends APIController
return new RoleResource($role);
}
public function validatingRequest(Request $request)
/**
* Delete Role.
*
* @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 Role Requests.
*
* @param Request $request
* @param int $id
*
* @return Validator object
*/
public function validateRole(Request $request, $id = 0)
{
$permissions = '';
......@@ -97,23 +126,10 @@ class RolesController extends APIController
}
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
'name' => 'required|max:191|unique:roles,name,'.$id,
'permissions' => $permissions,
]);
return $validation;
}
/**
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
*/
public function destroy(Role $role, Request $request)
{
$this->repository->delete($role);
return ['message'=>'success'];
}
}
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api\V1;
use App\Http\Requests\Backend\Access\User\ManageUserRequest;
use App\Http\Resources\UserResource;
use App\Models\Access\User\User;
use App\Repositories\Backend\Access\User\UserRepository;
......@@ -25,9 +26,11 @@ class UsersController extends APIController
/**
* Return the users.
*
* @return \Illuminate\Http\Response
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
public function index(ManageUserRequest $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
......@@ -41,56 +44,44 @@ class UsersController extends APIController
*
* @param User $user
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function show(User $user)
{
$data = new UserResource($user);
$history['history'] = history()->renderEntity('User', $user->id);
$maindata = $data->toArray($user);
$maindata = array_merge($maindata, $history);
return $maindata;
return new UserResource($user);
}
/**
* Return the specified resource.
* Create User.
*
* @param Request
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function deactivatedUserList(Request $request)
public function store(Request $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
$validation = $this->validateUser($request);
return UserResource::collection(
$this->repository->getForDataTable(0, false)->paginate($limit)
);
}
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
/**
* Return the specified resource.
*
* @param User $user
*
* @return \Illuminate\Http\Response
*/
public function deleteUserList(Request $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
$this->repository->create($request);
return UserResource::collection(
$this->repository->getForDataTable(0, true)->paginate($limit)
);
return new UserResource(User::orderBy('created_at', 'desc')->first());
}
/**
* Update the specified resource in storage.
* Update User.
*
* @param Request $request
* @param User $user
*
* @return Validator object
*/
public function update(Request $request, User $user)
{
$validation = $this->validatingRequest($request, 'edit', $user->id);
$validation = $this->validateUser($request, 'edit', $user->id);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
......@@ -104,26 +95,61 @@ class UsersController extends APIController
}
/**
* Store the specified resource in storage.
* Delete User.
*
* @param User $user
* @param Request $request
*
* @return mixed
*/
public function store(Request $request)
public function destroy(User $user, Request $request)
{
$validation = $this->validatingRequest($request);
$this->repository->delete($user);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
return $this->respond([
'message' => trans('alerts.backend.users.deleted'),
]);
}
/**
* Delete All User.
*
* @param Request $request
*
* @return mixed
*/
public function deleteAll(Request $request)
{
$ids = $request->get('ids');
if (isset($ids) && !empty($ids)) {
$result = $this->repository->deleteAll($ids);
}
$this->repository->create($request);
return new UserResource(User::orderBy('created_at', 'desc')->first());
if ($result) {
return $this->respond([
'message' => trans('alerts.backend.users.deleted'),
]);
}
return $this->respond([
'message' => trans('exceptions.backend.access.users.not_found'),
]);
}
/**
* Validation function to validate user input.
* validateUser User.
*
* @param $request
* @param $action
* @param $id
*
* @return \Illuminate\Http\JsonResponse
*/
public function validatingRequest(Request $request, $string = '', $id = 0)
public function validateUser(Request $request, $action = '', $id = 0)
{
$password = ($string == 'edit') ? '' : 'required|min:6|confirmed';
$password = ($action == 'edit') ? '' : 'required|min:6|confirmed';
$validation = Validator::make($request->all(), [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
......@@ -135,19 +161,4 @@ class UsersController extends APIController
return $validation;
}
/**
* Api to delete the resource.
*
* @param Role $role
* @param DeleteRoleRequest $request
*
* @return mixed
*/
public function destroy(User $user, Request $request)
{
$this->repository->delete($user);
return ['message' => 'success'];
}
}
......@@ -23,6 +23,7 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Spatie\Cors\Cors::class,
];
/**
......
<?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.',
];
}
}
......@@ -2,6 +2,7 @@
namespace App\Http\Requests;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Http\FormRequest;
/**
......@@ -25,4 +26,9 @@ abstract class Request extends FormRequest
return redirect()->back()->withErrors($this->error);
}
protected function failedAuthorization()
{
throw new AuthorizationException('This action is unauthorized.');
}
}
......@@ -16,10 +16,13 @@ class PermissionResource extends Resource
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'display_name' => $this->display_name,
'sort' => $this->sort,
'id' => $this->id,
'name' => $this->name,
'display_name' => $this->display_name,
'sort' => $this->sort,
'registered_at' => $this->created_at->toIso8601String(),
'last_updated_at' => $this->updated_at->toIso8601String(),
];
}
}
......@@ -16,11 +16,14 @@ class RoleResource extends Resource
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'permission' => ($this->all) ? 'All' : optional($this->permissions)->pluck('display_name'),
'noofuses' => $this->users->count(),
'sort' => $this->sort,
'id' => $this->id,
'name' => $this->name,
'permission' => ($this->all) ? 'All' : optional($this->permissions)->pluck('display_name'),
'noofuses' => $this->users->count(),
'sort' => $this->sort,
'status' => $this->status,
'registered_at' => $this->created_at->toIso8601String(),
'last_updated_at' => $this->updated_at->toIso8601String(),
];
}
}
......@@ -20,11 +20,13 @@ class UserResource extends Resource
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
'picture' => $this->getPicture(),
'confirmed' => $this->confirmed,
'role' => optional($this->roles()->first())->name,
'permissions' => $this->permissions()->get(),
'status' => $this->status,
'registered_at' => $this->created_at->toIso8601String(),
'last_updated_at' => $this->updated_at->toIso8601String(),
];
}
}
......@@ -40,7 +40,6 @@ class User extends Authenticatable implements JWTSubject
'first_name',
'last_name',
'email',
'password',
'status',
'confirmation_code',
'confirmed',
......@@ -86,6 +85,15 @@ class User extends Authenticatable implements JWTSubject
*/
public function getJWTCustomClaims()
{
return [];
return [
'id' => $this->id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
'picture' => $this->getPicture(),
'confirmed' => $this->confirmed,
'registered_at' => $this->created_at->toIso8601String(),
'last_updated_at' => $this->updated_at->toIso8601String(),
];
}
}
......@@ -31,6 +31,8 @@ class PermissionRepository extends BaseRepository
config('access.permissions_table').'.name',
config('access.permissions_table').'.display_name',
config('access.permissions_table').'.sort',
config('access.permissions_table').'.created_at',
config('access.permissions_table').'.updated_at',
]);
}
......
......@@ -49,6 +49,9 @@ class RoleRepository extends BaseRepository
config('access.roles_table').'.name',
config('access.roles_table').'.all',
config('access.roles_table').'.sort',
config('access.roles_table').'.status',
config('access.roles_table').'.created_at',
config('access.roles_table').'.updated_at',
DB::raw("GROUP_CONCAT( DISTINCT permissions.display_name SEPARATOR '<br/>') as permission_name"),
DB::raw('(SELECT COUNT(role_user.id) FROM role_user LEFT JOIN users ON role_user.user_id = users.id WHERE role_user.role_id = roles.id AND users.deleted_at IS NULL) AS userCount'),
])
......
......@@ -142,8 +142,9 @@ class UserRepository extends BaseRepository
DB::transaction(function () use ($user, $data, $roles, $permissions) {
if ($user->update($data)) {
$user->status = isset($data['status']) ? 1 : 0;
$user->confirmed = isset($data['confirmed']) ? 1 : 0;
$user->status = isset($data['status']) && $data['status'] == '1' ? 1 : 0;
$user->confirmed = isset($data['confirmed']) && $data['confirmed'] == '1' ? 1 : 0;
$user->save();
$this->checkUserRolesCount($roles);
......@@ -189,6 +190,8 @@ class UserRepository extends BaseRepository
}
/**
* Delete User.
*
* @param Model $user
*
* @throws GeneralException
......@@ -210,6 +213,34 @@ class UserRepository extends BaseRepository
throw new GeneralException(trans('exceptions.backend.access.users.delete_error'));
}
/**
* Delete All Users.
*
* @param Model $user
*
* @throws GeneralException
*
* @return bool
*/
public function deleteAll($ids)
{
if (in_array(access()->id(), $ids)) {
throw new GeneralException(trans('exceptions.backend.access.users.cant_delete_self'));
}
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;
}
return false;
}
/**
* @param $user
*
......
This diff is collapsed.
......@@ -70,6 +70,11 @@ return [
*/
'history_types_table' => 'history_types',
/*
* History table used to store History
*/
'history_table' => 'history',
/*
* Notifications table used to store user notification
*/
......@@ -99,7 +104,7 @@ return [
/*
* Whether or not the user has to confirm their email when signing up
*/
'confirm_email' => true,
'confirm_email' => false,
/*
* Whether or not the users email can be changed on the edit profile screen
......
<?php
return [
/*
* A cors profile determines which origins, methods, headers are allowed for
* a given requests. The `DefaultProfile` reads its configuration from this
* config file.
*
* You can easily create your own cors profile.
* More info: https://github.com/spatie/laravel-cors/#creating-your-own-cors-profile
*/
'cors_profile' => Spatie\Cors\CorsProfile\DefaultProfile::class,
/*
* This configuration is used by `DefaultProfile`.
*/
'default_profile' => [
'allow_origins' => [
'*',
],
'allow_methods' => [
'POST',
'GET',
'OPTIONS',
'PUT',
'PATCH',
'DELETE',
],
'allow_headers' => [
'Content-Type',
'X-Auth-Token',
'Origin',
'x-xsrf-token',
'x_csrftoken',
'Authorization',
],
'forbidden_response' => [
'message' => 'Forbidden (cors).',
'status' => 403,
],
/*
* Preflight request will respond with value for the max age header.
*/
'max_age' => 60 * 60 * 24,
],
];
......@@ -22,6 +22,7 @@ class HistoryTypeTableSeeder extends Seeder
{
$this->disableForeignKeys();
$this->truncate(config('access.history_types_table'));
$this->truncate(config('access.history_table'));
$types = [
[
......
......@@ -117,11 +117,9 @@ var Backend = {}; // common variable used in all the files of the backend
associated_container: document.getElementById("#available-permissions"),
},
init(page) {
this.setSelectors();
this.setRolepermission(page);
this.addHandlers();
},
setSelectors: function () {
this.selectors.associated = document.querySelector("select[name='associated_permissions']");
......@@ -697,17 +695,17 @@ var Backend = {}; // common variable used in all the files of the backend
*/
Faq:
{
selectors:
{
},
selectors:
{
},
init: function () {
// this.addHandlers();
Backend.tinyMCE.init();
},
init: function () {
// this.addHandlers();
Backend.tinyMCE.init();
},
addHandlers: function () {
}
addHandlers: function () {
}
},
/**
......@@ -735,7 +733,6 @@ var Backend = {}; // common variable used in all the files of the backend
if (this.selectors.cities != null) {
this.selectors.cities.select2();
}
}
},
......@@ -842,6 +839,10 @@ var Backend = {}; // common variable used in all the files of the backend
},
/**
* Settings
*
*/
Settings:
{
selectors: {
......@@ -898,9 +899,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);
}
});
};
}
}
};
......
......@@ -37,6 +37,7 @@ return [
'users' => [
'cant_deactivate_self' => 'You can not do that to 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_restore' => 'This user is not deleted so it can not be restored.',
......
<?php
<?php
/*
......@@ -27,32 +27,33 @@ 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');
});
// Users
Route::group(['prefix' => 'users'], function () {
Route::get('deactiveUsers', 'UsersController@deactivatedUserList');
Route::get('deleteUsers', 'UsersController@deleteUserList');
});
Route::resource('users', 'UsersController');
Route::resource('users', 'UsersController', ['except' => ['create', 'edit']]);
Route::post('users/delete-all', 'UsersController@deleteAll');
//@todo need to change the route name and related changes
Route::get('deactivated-users', 'DeactivatedUsersController@index');
Route::get('deleted-users', 'DeletedUsersController@index');
// Roles
Route::resource('roles', 'RolesController');
Route::resource('roles', 'RolesController', ['except' => ['create', 'edit']]);
Route::post('roles/delete-all', 'RolesController@deleteAll');
// Permission
Route::resource('permission', 'PermissionController');
Route::resource('permissions', 'PermissionController', ['except' => ['create', 'edit']]);
// Page
Route::resource('pages', 'PagesController');
Route::resource('pages', 'PagesController', ['except' => ['create', 'edit']]);
// Faqs
Route::resource('faqs', 'FaqsController');
Route::resource('faqs', 'FaqsController', ['except' => ['create', 'edit']]);
// Blog Categories
Route::resource('blog_categories', 'BlogCategoriesController');
Route::resource('blog_categories', 'BlogCategoriesController', ['except' => ['create', 'edit']]);
// Blog Tags
Route::resource('blog_tags', 'BlogTagsController');
Route::resource('blog_tags', 'BlogTagsController', ['except' => ['create', 'edit']]);
// Blogs
Route::resource('blogs', 'BlogsController');
Route::resource('blogs', 'BlogsController', ['except' => ['create', 'edit']]);
});
});
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