diff --git a/app/Exports/CurrencyExport.php b/app/Exports/CurrencyExport.php
index 3492a67..fa79a92 100644
--- a/app/Exports/CurrencyExport.php
+++ b/app/Exports/CurrencyExport.php
@@ -6,7 +6,7 @@
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
- use Modules\Lpj\Models\Currency;
+ use Modules\Basicdata\Models\Currency;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class CurrencyExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php
index 2924a82..eb42ac3 100644
--- a/app/Http/Controllers/CurrencyController.php
+++ b/app/Http/Controllers/CurrencyController.php
@@ -12,15 +12,35 @@
class CurrencyController extends Controller
{
- public $user;
+ /**
+ * Get the authenticated user.
+ *
+ * @return \Illuminate\Contracts\Auth\Authenticatable|null
+ */
+ protected function getUser()
+ {
+ return \Illuminate\Support\Facades\Auth::guard('web')->user();
+ }
public function index()
{
+ // Check if the authenticated user has the required permission to view currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.read')) {
+ abort(403, 'Sorry! You are not allowed to view currencies.');
+ }
+
return view('basicdata::currency.index');
}
public function store(CurrencyRequest $request)
{
+ // Check if the authenticated user has the required permission to create currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.create')) {
+ abort(403, 'Sorry! You are not allowed to create currencies.');
+ }
+
$validate = $request->validated();
if ($validate) {
@@ -40,17 +60,35 @@
public function create()
{
+ // Check if the authenticated user has the required permission to create currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.create')) {
+ abort(403, 'Sorry! You are not allowed to create currencies.');
+ }
+
return view('basicdata::currency.create');
}
public function edit($id)
{
+ // Check if the authenticated user has the required permission to update currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.update')) {
+ abort(403, 'Sorry! You are not allowed to update currencies.');
+ }
+
$currency = Currency::find($id);
return view('basicdata::currency.create', compact('currency'));
}
public function update(CurrencyRequest $request, $id)
{
+ // Check if the authenticated user has the required permission to update currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.update')) {
+ abort(403, 'Sorry! You are not allowed to update currencies.');
+ }
+
$validate = $request->validated();
if ($validate) {
@@ -71,28 +109,42 @@
public function destroy($id)
{
+ // Check if the authenticated user has the required permission to delete currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.delete')) {
+ return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete currencies.'], 403);
+ }
+
try {
// Delete from database
$currency = Currency::find($id);
$currency->delete();
- echo json_encode(['success' => true, 'message' => 'Currency deleted successfully']);
+ return response()->json(['success' => true, 'message' => 'Currency deleted successfully']);
} catch (Exception $e) {
- echo json_encode(['success' => false, 'message' => 'Failed to delete currency']);
+ return response()->json(['success' => false, 'message' => 'Failed to delete currency']);
}
}
public function deleteMultiple(Request $request)
{
+ // Check if the authenticated user has the required permission to delete currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.delete')) {
+ return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete currencies.'], 403);
+ }
+
$ids = $request->input('ids');
Currency::whereIn('id', $ids)->delete();
- return response()->json(['message' => 'Currencies deleted successfully']);
+ return response()->json(['success' => true, 'message' => 'Currencies deleted successfully']);
}
public function dataForDatatables(Request $request)
{
- if (is_null($this->user) || !$this->user->can('currency.view')) {
- //abort(403, 'Sorry! You are not allowed to view users.');
+ // Check if the authenticated user has the required permission to view currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.read')) {
+ return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view currencies.'], 403);
}
// Retrieve data from the database
@@ -153,6 +205,12 @@
public function export()
{
+ // Check if the authenticated user has the required permission to export currencies
+ $user = $this->getUser();
+ if (is_null($user) || !$user->can('basic-data.export')) {
+ abort(403, 'Sorry! You are not allowed to export currencies.');
+ }
+
return Excel::download(new CurrencyExport, 'currency.xlsx');
}
}
diff --git a/app/Http/Requests/CurrencyRequest.php b/app/Http/Requests/CurrencyRequest.php
index 247796a..2868727 100644
--- a/app/Http/Requests/CurrencyRequest.php
+++ b/app/Http/Requests/CurrencyRequest.php
@@ -23,7 +23,8 @@
];
if ($this->method() == 'PUT') {
- $rules['code'] = 'required|string|max:3|unique:currencies,code,' . $this->id;
+ $id = $this->id ? (int)$this->id : null;
+ $rules['code'] = 'required|string|max:3|unique:currencies,code,' . $id;
} else {
$rules['code'] = 'required|string|max:3|unique:currencies,code';
}
@@ -37,6 +38,14 @@
public function authorize()
: bool
{
+ $user = auth()->guard('web')->user();
+
+ if ($this->method() == 'PUT') {
+ return $user && $user->can('basic-data.update');
+ } elseif ($this->method() == 'POST') {
+ return $user && $user->can('basic-data.create');
+ }
+
return true;
}
}
diff --git a/resources/views/currency/create.blade.php b/resources/views/currency/create.blade.php
index 5b1c3e8..a9f973a 100644
--- a/resources/views/currency/create.blade.php
+++ b/resources/views/currency/create.blade.php
@@ -6,75 +6,83 @@
@section('content')
- @if(isset($currency->id))
-
+ @can('basic-data.export')
Export to Excel
+ @endcan
+ @can('basic-data.create')
Tambah Mata Uang
+ @endcan
+ @can('basic-data.delete')
+ @endcan