2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Http\Controller;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Http\Requests\Auth\User as Request;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Jobs\Auth\CreateUser;
|
|
|
|
use App\Jobs\Auth\DeleteUser;
|
|
|
|
use App\Jobs\Auth\UpdateUser;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Models\Auth\User;
|
|
|
|
use App\Models\Auth\Role;
|
2018-01-03 14:02:27 +03:00
|
|
|
use App\Traits\Uploads;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Http\Request as BaseRequest;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Users extends Controller
|
|
|
|
{
|
2018-01-03 14:02:27 +03:00
|
|
|
use Uploads;
|
|
|
|
|
2020-06-21 18:48:05 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('permission:create-auth-users')->only('create', 'store', 'duplicate', 'import');
|
|
|
|
$this->middleware('permission:read-auth-users')->only('index', 'show', 'export');
|
|
|
|
$this->middleware('permission:update-auth-users')->only('enable', 'disable');
|
|
|
|
$this->middleware('permission:delete-auth-users')->only('destroy');
|
|
|
|
|
|
|
|
$this->middleware('permission:read-auth-users|read-auth-profile')->only('edit');
|
|
|
|
$this->middleware('permission:update-auth-users|update-auth-profile')->only('update');
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-06-07 12:11:37 +03:00
|
|
|
$users = User::with('media', 'roles')->collect();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return view('auth.users.index', compact('users'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2020-01-21 11:59:52 +03:00
|
|
|
$routes = [
|
|
|
|
'dashboard' => trans_choice('general.dashboards', 1),
|
|
|
|
'items.index' => trans_choice('general.items', 2),
|
|
|
|
'invoices.index' => trans_choice('general.invoices', 2),
|
|
|
|
'revenues.index' => trans_choice('general.revenues', 2),
|
|
|
|
'customers.index' => trans_choice('general.customers', 2),
|
|
|
|
'bills.index' => trans_choice('general.bills', 2),
|
|
|
|
'payments.index' => trans_choice('general.payments', 2),
|
|
|
|
'vendors.index' => trans_choice('general.vendors', 2),
|
|
|
|
'accounts.index' => trans_choice('general.accounts', 2),
|
|
|
|
'transfers.index' => trans_choice('general.transfers', 2),
|
|
|
|
'transactions.index' => trans_choice('general.transactions', 2),
|
|
|
|
'reconciliations.index' => trans_choice('general.reconciliations', 2),
|
|
|
|
'reports.index' => trans_choice('general.reports', 2),
|
|
|
|
'settings.index' => trans_choice('general.settings', 2),
|
|
|
|
'categories.index' => trans_choice('general.categories', 2),
|
|
|
|
'currencies.index' => trans_choice('general.currencies', 2),
|
|
|
|
'taxes.index' => trans_choice('general.taxes', 2),
|
|
|
|
];
|
|
|
|
|
2018-01-03 14:02:27 +03:00
|
|
|
$roles = Role::all()->reject(function ($r) {
|
2019-11-16 10:21:14 +03:00
|
|
|
return $r->hasPermission('read-client-portal');
|
2017-12-02 14:07:12 +03:00
|
|
|
});
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-07-06 12:09:44 +03:00
|
|
|
$companies = user()->companies()->take(10)->get()->sortBy('name')->pluck('name', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-01-21 11:59:52 +03:00
|
|
|
return view('auth.users.create', compact('roles', 'companies', 'routes'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new CreateUser($request));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['redirect'] = route('users.index');
|
2018-01-03 14:02:27 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('messages.success.added', ['type' => trans_choice('general.users', 1)]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
|
|
|
$response['redirect'] = route('users.create');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = $response['message'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error();
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit(User $user)
|
|
|
|
{
|
2020-06-21 18:48:05 +03:00
|
|
|
if (user()->cannot('read-auth-users') && ($user->id != user()->id)) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2020-01-21 11:59:52 +03:00
|
|
|
$routes = [
|
|
|
|
'dashboard' => trans_choice('general.dashboards', 1),
|
|
|
|
'items.index' => trans_choice('general.items', 2),
|
|
|
|
'invoices.index' => trans_choice('general.invoices', 2),
|
|
|
|
'revenues.index' => trans_choice('general.revenues', 2),
|
|
|
|
'customers.index' => trans_choice('general.customers', 2),
|
|
|
|
'bills.index' => trans_choice('general.bills', 2),
|
|
|
|
'payments.index' => trans_choice('general.payments', 2),
|
|
|
|
'vendors.index' => trans_choice('general.vendors', 2),
|
|
|
|
'accounts.index' => trans_choice('general.accounts', 2),
|
|
|
|
'transfers.index' => trans_choice('general.transfers', 2),
|
|
|
|
'transactions.index' => trans_choice('general.transactions', 2),
|
|
|
|
'reconciliations.index' => trans_choice('general.reconciliations', 2),
|
|
|
|
'reports.index' => trans_choice('general.reports', 2),
|
|
|
|
'settings.index' => trans_choice('general.settings', 2),
|
|
|
|
'categories.index' => trans_choice('general.categories', 2),
|
|
|
|
'currencies.index' => trans_choice('general.currencies', 2),
|
|
|
|
'taxes.index' => trans_choice('general.taxes', 2),
|
|
|
|
];
|
|
|
|
|
2019-11-16 17:38:48 +03:00
|
|
|
if ($user->can('read-client-portal')) {
|
2017-12-02 14:07:12 +03:00
|
|
|
// Show only roles with customer permission
|
2018-01-03 14:02:27 +03:00
|
|
|
$roles = Role::all()->reject(function ($r) {
|
2019-11-16 10:21:14 +03:00
|
|
|
return !$r->hasPermission('read-client-portal');
|
2017-12-02 14:07:12 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Don't show roles with customer permission
|
2018-01-03 14:02:27 +03:00
|
|
|
$roles = Role::all()->reject(function ($r) {
|
2019-11-16 10:21:14 +03:00
|
|
|
return $r->hasPermission('read-client-portal');
|
2017-12-02 14:07:12 +03:00
|
|
|
});
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-07-06 12:09:44 +03:00
|
|
|
$companies = user()->companies()->take(10)->get()->sortBy('name')->pluck('name', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-01-21 11:59:52 +03:00
|
|
|
return view('auth.users.edit', compact('user', 'companies', 'roles', 'routes'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param User $user
|
|
|
|
* @param Request $request
|
2017-09-14 22:21:00 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update(User $user, Request $request)
|
|
|
|
{
|
2020-06-21 18:48:05 +03:00
|
|
|
if (user()->cannot('update-auth-users') && ($user->id != user()->id)) {
|
|
|
|
abort(403);
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new UpdateUser($user, $request));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['redirect'] = route('users.index');
|
2018-01-03 14:02:27 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('messages.success.updated', ['type' => $user->name]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
|
|
|
$response['redirect'] = route('users.edit', $user->id);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = $response['message'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error();
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-11 11:19:30 +03:00
|
|
|
/**
|
|
|
|
* Enable the specified resource.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param User $user
|
2018-06-11 11:19:30 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function enable(User $user)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new UpdateUser($user, request()->merge(['enabled' => 1])));
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['message'] = trans('messages.success.enabled', ['type' => $user->name]);
|
|
|
|
}
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2018-06-11 11:19:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the specified resource.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param User $user
|
2018-06-11 11:19:30 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function disable(User $user)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new UpdateUser($user, request()->merge(['enabled' => 0])));
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['message'] = trans('messages.success.disabled', ['type' => $user->name]);
|
|
|
|
}
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2018-06-11 11:19:30 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param User $user
|
2017-09-14 22:21:00 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy(User $user)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new DeleteUser($user));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$response['redirect'] = route('users.index');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$message = trans('messages.success.deleted', ['type' => $user->name]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
|
|
|
$message = $response['message'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error();
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark upcoming bills notifications are read and redirect to bills page.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param User $user
|
2017-09-14 22:21:00 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function readUpcomingBills(User $user)
|
|
|
|
{
|
|
|
|
// Mark bill notifications as read
|
|
|
|
foreach ($user->unreadNotifications as $notification) {
|
|
|
|
// Not a bill notification
|
2019-12-31 15:49:09 +03:00
|
|
|
if ($notification->getAttribute('type') != 'App\Notifications\Purchase\Bill') {
|
2017-09-14 22:21:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notification->markAsRead();
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return redirect()->route('bills.index');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark overdue invoices notifications are read and redirect to invoices page.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param User $user
|
2017-09-14 22:21:00 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function readOverdueInvoices(User $user)
|
|
|
|
{
|
|
|
|
// Mark invoice notifications as read
|
|
|
|
foreach ($user->unreadNotifications as $notification) {
|
|
|
|
// Not an invoice notification
|
2019-12-31 15:49:09 +03:00
|
|
|
if ($notification->getAttribute('type') != 'App\Notifications\Sale\Invoice') {
|
2017-09-14 22:21:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notification->markAsRead();
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return redirect()->route('invoices.index');
|
2017-11-21 23:37:15 +03:00
|
|
|
}
|
2017-11-23 13:38:43 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function autocomplete(BaseRequest $request)
|
2017-11-23 13:38:43 +03:00
|
|
|
{
|
|
|
|
$user = false;
|
2017-12-16 13:43:03 +03:00
|
|
|
$data = false;
|
2017-11-23 13:38:43 +03:00
|
|
|
|
|
|
|
$column = $request['column'];
|
|
|
|
$value = $request['value'];
|
|
|
|
|
|
|
|
if (!empty($column) && !empty($value)) {
|
|
|
|
switch ($column) {
|
|
|
|
case 'id':
|
2017-12-16 13:43:03 +03:00
|
|
|
$user = User::find((int) $value);
|
2017-11-23 13:38:43 +03:00
|
|
|
break;
|
|
|
|
case 'email':
|
|
|
|
$user = User::where('email', $value)->first();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$user = User::where($column, $value)->first();
|
|
|
|
}
|
2017-12-16 13:43:03 +03:00
|
|
|
|
|
|
|
$data = $user;
|
|
|
|
} elseif (!empty($column) && empty($value)) {
|
|
|
|
$data = trans('validation.required', ['attribute' => $column]);
|
2017-11-23 13:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
2017-12-16 13:43:03 +03:00
|
|
|
'errors' => ($user) ? false : true,
|
|
|
|
'success' => ($user) ? true : false,
|
|
|
|
'data' => $data
|
2017-11-23 13:38:43 +03:00
|
|
|
]);
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|