Commit c6c1e4c7 authored by cygnet's avatar cygnet

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

parents 0a196fa2 793c4ef1
......@@ -4,8 +4,12 @@ namespace App\Exceptions;
use Exception;
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;
class Handler extends ExceptionHandler
{
......@@ -102,6 +106,32 @@ class Handler extends ExceptionHandler
return redirect()->back()->withInput()->withFlashDanger($exception->getMessage());
}
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 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());
}
}
return parent::render($request, $exception);
}
......@@ -122,4 +152,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);
}
}
......@@ -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;
......
......@@ -31,7 +31,6 @@ class BlogTagsController extends APIController
*/
public function index(Request $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
return BlogTagsResource::collection(
......@@ -109,9 +108,8 @@ class BlogTagsController extends APIController
public function validatingRequest(Request $request, $id = 0)
{
$validation = Validator::make($request->all(), [
'name' => 'required|max:191|unique:blog_tags,name,'. $id,
'name' => 'required|max:191|unique:blog_tags,name,'.$id,
]);
return $validation;
......
......@@ -29,7 +29,6 @@ class BlogsController extends APIController
*/
public function index(Request $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
return BlogsResource::collection(
......@@ -89,9 +88,9 @@ class BlogsController extends APIController
return new BlogsResource($blog);
}
public function validatingRequest(Request $request, $type="insert")
public function validatingRequest(Request $request, $type = 'insert')
{
$featured_image = ($type=="insert")?"required":"";
$featured_image = ($type == 'insert') ? 'required' : '';
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
......@@ -103,6 +102,7 @@ class BlogsController extends APIController
return $validation;
}
public function messages()
{
return [
......
......@@ -2,6 +2,8 @@
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;
......@@ -25,7 +27,9 @@ class UsersController extends APIController
/**
* Return the users.
*
* @return \Illuminate\Http\Response
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
......@@ -41,89 +45,120 @@ 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)
);
event(new UserCreated($user));
return new UserResource(User::orderBy('created_at', 'desc')->first());
}
/**
* Update the specified resource in storage.
* Update User.
*
* @param Request $request
* @param User $user
*
* @return \Illuminate\Http\JsonResponse
*/
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());
}
$this->repository->update($user, $request);
$updatedUser = $this->repository->update($user, $request);
$user = User::findOrfail($user->id);
event(new UserUpdated($user));
return new UserResource($user);
return new UserResource($updatedUser);
}
/**
* 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'),
]);
}
$this->repository->create($request);
return new UserResource(User::orderBy('created_at', 'desc')->first());
/**
* 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)
);
}
/**
* 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 +170,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,
];
/**
......
......@@ -15,11 +15,10 @@ class BlogTagsResource extends Resource
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'status' => ($this->isActive()) ? "Active" : "InActive",
'status' => ($this->isActive()) ? 'Active' : 'InActive',
'created_at' => optional($this->created_at)->toDateString(),
'created_by' => (isset($this->creator)) ? optional($this->creator)->first_name : $this->user_name,
];
......
......@@ -15,7 +15,6 @@ class BlogsResource extends Resource
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
......
......@@ -24,7 +24,6 @@ class UserResource extends Resource
'role' => optional($this->roles()->first())->name,
'registered_at' => $this->created_at->toIso8601String(),
'last_updated_at' => $this->updated_at->toIso8601String(),
];
}
}
......@@ -86,6 +86,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(),
];
}
}
......@@ -144,7 +144,7 @@ class UserRepository extends BaseRepository
if ($user->update($data)) {
$user->status = isset($data['status']) ? 1 : 0;
$user->confirmed = isset($data['confirmed']) ? 1 : 0;
$user->save();
$updatedUser = tap($user)->save();
$this->checkUserRolesCount($roles);
$this->flushRoles($roles, $user);
......@@ -152,7 +152,7 @@ class UserRepository extends BaseRepository
$this->flushPermissions($permissions, $user);
event(new UserUpdated($user));
return true;
return $updatedUser;
}
throw new GeneralException(trans('exceptions.backend.access.users.update_error'));
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "bc9d94beb0a4b88c5eaee8a9a2238bea",
"content-hash": "cda8a083b7461c2abcd24181fa94e165",
"packages": [
{
"name": "arcanedev/log-viewer",
......@@ -1414,16 +1414,16 @@
},
{
"name": "laravel/framework",
"version": "v5.6.9",
"version": "v5.6.11",
"source": {
"type": "git",
"url": "https://github.com/laravel/framework.git",
"reference": "98fdbb098cf52a74441fe949be121c18e3dbbe6a"
"reference": "195ba6a67bdad2a23105c7ab410cd43e0f20bb73"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/framework/zipball/98fdbb098cf52a74441fe949be121c18e3dbbe6a",
"reference": "98fdbb098cf52a74441fe949be121c18e3dbbe6a",
"url": "https://api.github.com/repos/laravel/framework/zipball/195ba6a67bdad2a23105c7ab410cd43e0f20bb73",
"reference": "195ba6a67bdad2a23105c7ab410cd43e0f20bb73",
"shasum": ""
},
"require": {
......@@ -1434,7 +1434,7 @@
"ext-openssl": "*",
"league/flysystem": "~1.0",
"monolog/monolog": "~1.12",
"nesbot/carbon": "^1.22.1",
"nesbot/carbon": "^1.24.1",
"php": "^7.1.3",
"psr/container": "~1.0",
"psr/simple-cache": "^1.0",
......@@ -1548,7 +1548,7 @@
"framework",
"laravel"
],
"time": "2018-03-07T14:04:49+00:00"
"time": "2018-03-09T16:53:27+00:00"
},
{
"name": "laravel/socialite",
......@@ -1614,16 +1614,16 @@
},
{
"name": "laravel/tinker",
"version": "v1.0.4",
"version": "v1.0.5",
"source": {
"type": "git",
"url": "https://github.com/laravel/tinker.git",
"reference": "eb8d3cfb41b7f74fb0ef4724e459d44f4cbb35b8"
"reference": "94f6daf2131508cebd11cd6f8632ba586d7ecc41"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/tinker/zipball/eb8d3cfb41b7f74fb0ef4724e459d44f4cbb35b8",
"reference": "eb8d3cfb41b7f74fb0ef4724e459d44f4cbb35b8",
"url": "https://api.github.com/repos/laravel/tinker/zipball/94f6daf2131508cebd11cd6f8632ba586d7ecc41",
"reference": "94f6daf2131508cebd11cd6f8632ba586d7ecc41",
"shasum": ""
},
"require": {
......@@ -1673,7 +1673,7 @@
"laravel",
"psysh"
],
"time": "2018-03-06T13:48:07+00:00"
"time": "2018-03-06T17:34:36+00:00"
},
{
"name": "laravelcollective/html",
......@@ -2091,20 +2091,20 @@
},
{
"name": "nesbot/carbon",
"version": "1.23.0",
"version": "1.24.2",
"source": {
"type": "git",
"url": "https://github.com/briannesbitt/Carbon.git",
"reference": "4a874a39b2b00d7e0146cd46fab6f47c41ce9e65"
"reference": "bba6c6e410c6b4317e37a9474aeaa753808c3875"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/4a874a39b2b00d7e0146cd46fab6f47c41ce9e65",
"reference": "4a874a39b2b00d7e0146cd46fab6f47c41ce9e65",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/bba6c6e410c6b4317e37a9474aeaa753808c3875",
"reference": "bba6c6e410c6b4317e37a9474aeaa753808c3875",
"shasum": ""
},
"require": {
"php": ">=5.3.0",
"php": ">=5.3.9",
"symfony/translation": "~2.6 || ~3.0 || ~4.0"
},
"require-dev": {
......@@ -2140,7 +2140,7 @@
"datetime",
"time"
],
"time": "2018-02-28T09:22:05+00:00"
"time": "2018-03-10T10:10:14+00:00"
},
{
"name": "nikic/php-parser",
......@@ -2587,6 +2587,65 @@
],
"time": "2018-01-20T00:28:24+00:00"
},
{
"name": "spatie/laravel-cors",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-cors.git",
"reference": "7e5fa8db4b57e3f8026dd7df9ebdf4ca9086e4e9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-cors/zipball/7e5fa8db4b57e3f8026dd7df9ebdf4ca9086e4e9",
"reference": "7e5fa8db4b57e3f8026dd7df9ebdf4ca9086e4e9",
"shasum": ""
},
"require": {
"illuminate/support": "5.5.*|5.6.*",
"php": "^7.0"
},
"require-dev": {
"orchestra/testbench": "3.5.*|3.6.*",
"phpunit/phpunit": "^6.5.4|^7.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\Cors\\CorsServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Spatie\\Cors\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Send CORS headers in a Laravel or Lumen application",
"homepage": "https://github.com/spatie/laravel-cors",
"keywords": [
"ajax",
"api",
"cors",
"laravel-cors",
"request",
"spatie"
],
"time": "2018-03-09T20:14:04+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.0.2",
......@@ -3609,12 +3668,12 @@
"source": {
"type": "git",
"url": "https://github.com/tymondesigns/jwt-auth.git",
"reference": "592e5e10c4e2d40bbf6db7d0e64367847118606a"
"reference": "2b79229235d83523a05069ccb9c97cd5ec0b8123"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/592e5e10c4e2d40bbf6db7d0e64367847118606a",
"reference": "592e5e10c4e2d40bbf6db7d0e64367847118606a",
"url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/2b79229235d83523a05069ccb9c97cd5ec0b8123",
"reference": "2b79229235d83523a05069ccb9c97cd5ec0b8123",
"shasum": ""
},
"require": {
......@@ -3676,7 +3735,7 @@
"jwt",
"laravel"
],
"time": "2018-03-05T22:18:39+00:00"
"time": "2018-03-10T22:14:03+00:00"
},
{
"name": "unisharp/laravel-filemanager",
......
<?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',
'Authorization',
],
'forbidden_response' => [
'message' => 'Forbidden (cors).',
'status' => 403,
],
/*
* Preflight request will respond with value for the max age header.
*/
'max_age' => 60 * 60 * 24,
],
];
......@@ -28,12 +28,13 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio
// 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');
// Roles
Route::resource('roles', 'RolesController');
......
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