<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;
class UsersController extends Controller
{
public function index()
{
$users = Admin::latest()->take(30)->get();
return view('admin.users.all', compact('users'));
}
public function add()
{
return view('admin.users.add');
}
public function store(Request $request)
{
$validateArray = [
'name' => 'required|string',
'username' => 'required|string',
'email' => 'required|string',
'password' => 'required|confirmed',
"rights" => 'required|array',
"rights.*" => "required|string",
];
$validator = Validator::make($request->all(), $validateArray, ['rights.*' => 'At least one permission is required']);
if ($validator->fails()) {
return error_api_processor('Some fields seems not correct!', 200, validation_error_processor($validator));
}
try {
DB::beginTransaction();
$user = Admin::create([
'name' => $request->name,
'username' => $request->username,
'email' => $request->email,
'password' => Hash::make($request['password']),
'rights' => json_encode($request->rights),
]);
DB::commit();
return success_api_processor($user);
} catch (Exception $e) {
DB::rollBack();
return error_api_processor('Something went wrong! Please try again later.', 200, []);
}
}
public function listApi(Request $request)
{
if ($request->ajax()) {
$data = Admin::all();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function ($row) {
return '
<div class="">
<a href="' . route('admin.users.edit', $row->id) . '" class="align-items-center btn btn-primary">
<span class="icon icofont-pencil-alt-2"></span>
Edit
</a>
<input class="delete_url" value="' . route("admin.users.delete", $row->id) . '" hidden>
<a href="#" class="align-items-center btn btn-danger delete_btn">
<span class="icon icofont-pencil-alt-2"></span>
Delete
</a>
</div>';
})
->rawColumns(['action'])
->make(true);
} else {
return 'lost';
}
}
public function edit($id)
{
$thisUser = Admin::find($id);
if ($thisUser)
return view('admin.users.edit', compact('thisUser'));
else
return redirect()->route('admin.users.all')->with('status', 'No user found');
}
public function delete($id)
{
$users = Admin::find($id);
if ($users) {
$users->delete();
return success_api_processor([], 'User deleted successfully.');
}
return error_api_processor('Something went wrong! Please try again later.', 200, []);
}
public function update(Request $request, $id): JsonResponse
{
$validateArray = [
'name' => 'required|string',
'username' => 'required|string',
'email' => 'required|string',
'password' => 'confirmed',
"rights" => 'required|array',
"rights.*" => "required|string",
];
//validate
$validator = Validator::make($request->all(), $validateArray, []);
if ($validator->fails()) {
return error_api_processor('Some fields seems not correct!', 200, validation_error_processor($validator));
}
try {
DB::beginTransaction();
$user = Admin::find($id);
if ($user) {
$user->update([
'name' => $request->name,
'username' => $request->username,
'email' => $request->email,
'password' => Hash::make($request['password']),
'rights' => json_encode($request->rights),
]);
}
DB::commit();
return success_api_processor([], 'User Updated successfully');
} catch (Exception $e) {
DB::rollBack();
return error_api_processor('Something went wrong! Please try again later.', 200, [$e]);
}
}
public function changepass()
{
return view('admin.auth.change_password');
}
public function changePassword(Request $request): JsonResponse
{
$validateArray = [
'current_password' => 'required',
'new_password' => 'required|string|min:8|confirmed',
];
$validator = Validator::make($request->all(), $validateArray, []);
if ($validator->fails()) {
return error_api_processor('Some fields seems not correct!', 200, validation_error_processor($validator));
}
if (!(Hash::check($request->get('current_password'), Auth::user()->password))) {
// The passwords matches
return error_api_processor('New password cannot be same as current password!', 200, validation_error_processor($validator));
}
if (strcmp($request->get('current_password'), $request->get('new_password')) == 0) {
// Current password and new password same
return error_api_processor('New Password cannot be the same as the current password', 200, validation_error_processor($validator));
}
//Change Password
$user = Auth::user();
$user->password = bcrypt($request->new_password);
$user->save();
return success_api_processor([], 'Password changed successfully.');
}
}