Skip to content

Commit a7d7279

Browse files
committed
#990 - Security check if permissions are granted
1 parent d1a6ef1 commit a7d7279

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

src/app/beans/beans.page.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Subscription } from 'rxjs';
2929
import { BeanSortFilterHelperService } from '../../services/beanSortFilterHelper/bean-sort-filter-helper.service';
3030
import { NfcService } from '../../services/nfcService/nfc-service.service';
3131

32+
import { UIImage } from '../../services/uiImage';
3233
@Component({
3334
selector: 'beans',
3435
templateUrl: './beans.page.html',
@@ -110,6 +111,7 @@ export class BeansPage implements OnDestroy {
110111
private readonly modalController: ModalController,
111112
private readonly beanSortFilterHelper: BeanSortFilterHelperService,
112113
private readonly nfcService: NfcService,
114+
private readonly uiImage: UIImage,
113115
) {}
114116

115117
public ionViewWillEnter(): void {
@@ -349,8 +351,11 @@ export class BeansPage implements OnDestroy {
349351
public async scanBean() {
350352
if (this.platform.is('capacitor')) {
351353
try {
352-
const scannedCode = await this.qrScannerService.scan();
353-
await this.intenthandler.handleQRCodeLink(scannedCode);
354+
const hasPermission = await this.uiImage.checkCameraPermission();
355+
if (hasPermission) {
356+
const scannedCode = await this.qrScannerService.scan();
357+
await this.intenthandler.handleQRCodeLink(scannedCode);
358+
}
354359
} catch (error) {
355360
// Just log and do nothing else, it's likely the user just cancelled
356361
this.uiLog.warn('Bean QR code scan error:', error);

src/assets/i18n/en.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,5 +1537,6 @@
15371537
"STATISTICS_BEANS_BY_ROASTER": "Beans by Roaster",
15381538
"STATISTICS_BEANS_AVG_RATING_BY_COUNTRY": "Average Bean Rating by Country",
15391539
"PAGE_STATISTICS_AVG_RATING": "Average Rating",
1540-
"PAGE_SETTINGS_LANGUAGE_GREEK": "Greek"
1541-
}
1540+
"PAGE_SETTINGS_LANGUAGE_GREEK": "Greek",
1541+
"NO_CAMERA_PERMISSION": "No camera permission given, please enable inside the OS-System"
1542+
}

src/components/async-image/async-image.component.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33
@if (!isLoading && !errorOccured && (img | async)) {
44
<img [src]="img | async"/>
55
}
6-
@if (isLoading && showLoadingImage) {
7-
<img [src]="preloadImg" alt="Image is loading" class="preload-img"/>
8-
}

src/components/async-image/async-image.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ import { SafeResourceUrl } from '@angular/platform-browser';
1010
})
1111
export class AsyncImageComponent implements OnChanges {
1212
@Input() public filePath: string;
13-
@Input() public showLoadingImage = false;
1413

1514
public errorOccured = false;
1615
public isLoading = false;
1716
public img: Promise<SafeResourceUrl | undefined> = Promise.resolve(undefined);
18-
public preloadImg = 'assets/img/loading.gif';
1917
constructor(private uiFileHelper: UIFileHelper) {}
2018

2119
public ngOnChanges(): void {

src/services/uiImage.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CameraDirection,
66
CameraResultType,
77
CameraSource,
8+
CameraPluginPermissions,
89
} from '@capacitor/camera';
910
import { AndroidPermissions } from '@awesome-cordova-plugins/android-permissions/ngx';
1011

@@ -26,6 +27,10 @@ import { Water } from '../classes/water/water';
2627
import { Mill } from '../classes/mill/mill';
2728
import { Preparation } from '../classes/preparation/preparation';
2829
import { FilePicker } from '@capawesome/capacitor-file-picker';
30+
import {
31+
CameraPermissionType,
32+
PermissionStatus,
33+
} from '@capacitor/camera/dist/esm/definitions';
2934

3035
@Injectable({
3136
providedIn: 'root',
@@ -185,4 +190,29 @@ export class UIImage {
185190
await modal.present();
186191
await modal.onWillDismiss();
187192
}
193+
194+
public async checkCameraPermission() {
195+
try {
196+
const permissionGiven: PermissionStatus = await Camera.checkPermissions();
197+
if (permissionGiven.camera == 'denied') {
198+
const requestPermission: PermissionStatus =
199+
await Camera.requestPermissions({ permissions: ['camera'] });
200+
if (requestPermission.camera == 'denied') {
201+
await this.uiAlert.showMessage(
202+
'NO_CAMERA_PERMISSION',
203+
null,
204+
null,
205+
true,
206+
);
207+
return false;
208+
} else {
209+
return true;
210+
}
211+
} else {
212+
return true;
213+
}
214+
} catch (ex) {
215+
return false;
216+
}
217+
}
188218
}

0 commit comments

Comments
 (0)