fix(device): check privilege on Linux (#519)

* fix(device): check privilege on Linux

* device connected if also have permission

* fix rules sh path

* refactor permission detection

* fic hasPermission condition

* fix return value
This commit is contained in:
Róbert Kiss
2017-12-21 20:33:40 +01:00
committed by László Monda
parent 969c36561b
commit c11658f1fc
11 changed files with 75 additions and 74 deletions
@@ -31,7 +31,7 @@ export class AppService extends MainServiceBase {
addons: this.options.addons || false,
autoWriteConfig: this.options['auto-write-config'] || false
},
deviceConnected: this.deviceService.isConnected,
deviceConnected: this.uhkHidDeviceService.deviceConnected(),
hasPermission: this.uhkHidDeviceService.hasPermission(),
agentVersionInfo: {
version: packageJson.version,
@@ -1,9 +1,8 @@
import { ipcMain } from 'electron';
import { ConfigurationReply, IpcEvents, IpcResponse, LogService } from 'uhk-common';
import { Constants, snooze, UhkHidDevice, UhkOperations } from 'uhk-usb';
import { ConfigurationReply, DeviceConnectionState, IpcEvents, IpcResponse, LogService } from 'uhk-common';
import { snooze, UhkHidDevice, UhkOperations } from 'uhk-usb';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import { Device, devices } from 'node-hid';
import { emptyDir } from 'fs-extra';
import 'rxjs/add/observable/interval';
@@ -19,13 +18,11 @@ import { QueueManager } from './queue-manager';
/**
* IpcMain pair of the UHK Communication
* Functionality:
* - Detect device is connected or not
* - Send UserConfiguration to the UHK Device
* - Read UserConfiguration from the UHK Device
*/
export class DeviceService {
private pollTimer$: Subscription;
private connected = false;
private queueManager = new QueueManager();
constructor(private logService: LogService,
@@ -66,14 +63,6 @@ export class DeviceService {
logService.debug('[DeviceService] init success');
}
/**
* Return with true is an UHK Device is connected to the computer.
* @returns {boolean}
*/
public get isConnected(): boolean {
return this.connected;
}
/**
* Return with the actual UserConfiguration from UHK Device
* @returns {Promise<Buffer>}
@@ -102,7 +91,6 @@ export class DeviceService {
}
public close(): void {
this.connected = false;
this.stopPollTimer();
this.logService.info('[DeviceService] Device connection checker stopped.');
}
@@ -153,15 +141,16 @@ export class DeviceService {
this.pollTimer$ = Observable.interval(1000)
.startWith(0)
.map(() => {
return devices().some((dev: Device) => dev.vendorId === Constants.VENDOR_ID &&
dev.productId === Constants.PRODUCT_ID);
})
.map(() => this.device.deviceConnected())
.distinctUntilChanged()
.do((connected: boolean) => {
this.connected = connected;
this.win.webContents.send(IpcEvents.device.deviceConnectionStateChanged, connected);
this.logService.info(`[DeviceService] Device connection state changed to: ${connected}`);
const response: DeviceConnectionState = {
connected,
hasPermission: this.device.hasPermission()
};
this.win.webContents.send(IpcEvents.device.deviceConnectionStateChanged, response);
this.logService.info('[DeviceService] Device connection state changed to:', response);
})
.subscribe();
}
@@ -10,7 +10,7 @@ export class SudoService {
constructor(private logService: LogService) {
if (isDev) {
this.rootDir = path.join(path.join(process.cwd(), process.argv[1]), '..');
this.rootDir = path.join(path.join(process.cwd(), process.argv[1]), '../../../../');
} else {
this.rootDir = path.dirname(app.getAppPath());
}
@@ -40,11 +40,12 @@ export class SudoService {
name: 'Setting UHK access rules'
};
const command = `sh ${scriptPath}`;
console.log(command);
this.logService.debug('[SudoService] Set privilege command: ', command);
sudo.exec(command, options, (error: any) => {
const response = new IpcResponse();
if (error) {
this.logService.error('[SudoService] Error when set privilege: ', error);
response.success = false;
response.error = error;
} else {