Skip to content

Commit 1ee5cc3

Browse files
committed
Update
- Added: restrict users from deleting persisten products
1 parent 8f1c0f5 commit 1ee5cc3

File tree

8 files changed

+117
-3
lines changed

8 files changed

+117
-3
lines changed

app/Http/Controllers/Dashboard/UsersController.php

+17
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,21 @@ public function deleteToken( $tokenId )
208208
{
209209
return $this->usersService->deleteToken( $tokenId );
210210
}
211+
212+
public function checkPermission( Request $request )
213+
{
214+
$result = $this->usersService->checkPermission( $request->input( 'permission' ) );
215+
216+
if ( $result ) {
217+
return response()->json( [
218+
'status' => 'success',
219+
'message' => __( 'The permission is granted' )
220+
] );
221+
} else {
222+
return response()->json( [
223+
'status' => 'error',
224+
'message' => __( 'The permission is denied' )
225+
], 403 );
226+
}
227+
}
211228
}

app/Services/UsersService.php

+6
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,10 @@ public function deleteToken( $tokenId, ?User $user = null )
364364
'message' => __( 'The token has been successfully deleted.' ),
365365
];
366366
}
367+
368+
public function checkPermission( $permission, ?User $user = null ): bool
369+
{
370+
ns()->restrict( $permission );
371+
return true;
372+
}
367373
}

config/nexopos.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* This is the core version of NexoPOS. This is used to displays on the
1010
* dashboard and to ensure a compatibility with the modules.
1111
*/
12-
'version' => '5.1.0',
12+
'version' => '5.1.1',
1313

1414
/**
1515
* --------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use App\Models\Permission;
4+
use App\Models\Role;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
if ( ! Permission::namespace( 'nexopos.pos.delete-order-product' ) instanceof Permission ) {
17+
$pos = Permission::firstOrNew( [ 'namespace' => 'nexopos.pos.delete-order-product' ] );
18+
$pos->name = __( 'POS: Delete Order Product' );
19+
$pos->namespace = 'nexopos.pos.delete-order-product';
20+
$pos->description = __( 'Let the user delete order products on POS.' );
21+
$pos->save();
22+
23+
/**
24+
* @var Role
25+
*/
26+
$admin = Role::firstOrNew( [ 'namespace' => Role::ADMIN ] );
27+
28+
/**
29+
* @var Role
30+
*/
31+
$storeAdmin = Role::firstOrNew( [ 'namespace' => Role::STOREADMIN ] );
32+
33+
$admin->addPermissions( $pos );
34+
$storeAdmin->addPermissions( $pos );
35+
}
36+
}
37+
38+
/**
39+
* Reverse the migrations.
40+
*/
41+
public function down(): void
42+
{
43+
$permission = Permission::namespace( 'nexopos.pos.delete-order-product' );
44+
$permission->removeFromRoles();
45+
$permission->delete();
46+
}
47+
};

database/permissions/admin-role.php

-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@
5151
$admin->addPermissions( Permission::includes( '.units' )->get()->map( fn( $permission ) => $permission->namespace ) );
5252
$admin->addPermissions( Permission::includes( '.manage-payments-types' )->get()->map( fn( $permission ) => $permission->namespace ) );
5353
$admin->addPermissions( Permission::includes( '.pos' )->get()->map( fn( $permission ) => $permission->namespace ) );
54-
$admin->addPermissions( Permission::includes( '.pos' )->get()->map( fn( $permission ) => $permission->namespace ) );
5554
$admin->addPermissions( Permission::includes( '-widget' )->get()->map( fn( $permission ) => $permission->namespace ) );

database/permissions/pos.php

+8
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@
3333
$pos->description = __( 'Let the user add discount on cart.' );
3434
$pos->save();
3535
}
36+
37+
if ( ! Permission::namespace( 'nexopos.pos.delete-order-product' ) instanceof Permission ) {
38+
$pos = Permission::firstOrNew( [ 'namespace' => 'nexopos.pos.delete-order-product' ] );
39+
$pos->name = __( 'POS: Delete Order Products' );
40+
$pos->namespace = 'nexopos.pos.delete-order-product';
41+
$pos->description = __( 'Let the user delete order products on POS.' );
42+
$pos->save();
43+
}

resources/ts/pos-init.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { nsCurrency } from "./filters/currency";
2424
import Print from "./libraries/print";
2525
import Tax from "./libraries/tax";
2626
import * as math from "mathjs"
27+
import nsPosLoadingPopupVue from "./popups/ns-pos-loading-popup.vue";
2728

2829

2930
/**
@@ -1575,8 +1576,43 @@ export class POS {
15751576
this._types.next(types);
15761577
}
15771578

1578-
removeProductUsingIndex(index) {
1579+
async removeProductUsingIndex(index) {
15791580
const products = this._products.getValue();
1581+
const product = products[index];
1582+
1583+
/**
1584+
* if the product is persistent,
1585+
* we should check on the database if the user is allowed
1586+
* to delete those products.
1587+
*/
1588+
if ( product.id ) {
1589+
try {
1590+
await new Promise((resolve, reject) => {
1591+
const popup = Popup.show( nsPosLoadingPopupVue );
1592+
nsHttpClient.post(`/api/users/check-permission/`, {
1593+
permission: 'nexopos.pos.delete-order-product'
1594+
}).subscribe({
1595+
next: (response: any) => {
1596+
popup.close();
1597+
resolve( response );
1598+
},
1599+
error: error => {
1600+
popup.close();
1601+
reject( error );
1602+
}
1603+
})
1604+
});
1605+
1606+
this.resumeRemovingProductUsingIndex( index, products );
1607+
} catch( exception ) {
1608+
nsNotice.error( __( 'Forbidden Action' ), __( 'You are not allowed to remove this product.' ) );
1609+
}
1610+
} else {
1611+
this.resumeRemovingProductUsingIndex( index, products );
1612+
}
1613+
}
1614+
1615+
private resumeRemovingProductUsingIndex( index, products ) {
15801616
products.splice(index, 1);
15811617
this.products.next(products);
15821618
nsHooks.doAction( 'ns-after-cart-changed' );

routes/api/users.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
Route::post( '/users/create-token', [ UsersController::class, 'createToken' ] );
1313
Route::get( '/users/tokens', [ UsersController::class, 'getTokens' ] );
1414
Route::delete( '/users/tokens/{id}', [ UsersController::class, 'deleteToken' ] );
15+
Route::post( '/users/check-permission', [ UsersController::class, 'checkPermission' ] );

0 commit comments

Comments
 (0)