Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if electron app is launched with admin privileges on windows

Is there a way to check if an electron app is launched with the admin rights?

I only found electron-sudo lib to execute commands with admin privileges.

But I have multiple commands to execute and I do not want to prompt the user every time.

So how can I check if the app is started with admin privileges using electron ?

The best thing would be just to execute a command inside the software ex: .isAdminPrivilegesUsed (can be a script that is executed on Windows) that return true or false, and if false :

I will prompt the user that he has to restart the software with admin rights and close it

like image 568
Aaleks Avatar asked May 19 '16 12:05

Aaleks


People also ask

How do I distribute the electron app in Windows?

To distribute your app manually, you need to download Electron's prebuilt binaries. Next, the folder containing your app should be named app and placed in Electron's resources directory as shown in the following examples. The location of Electron's prebuilt binaries is indicated with electron/ in the examples below.

Where do electron apps install?

The installer will indeed install in %appdata% if you have set build/nsis/perMachine to false (which is default) in your package. json. If you set perMachine to true, the program will be installed in program files, and furthermore if you set oneClick to false, you allow the user to choose where to install.


1 Answers

I checked into how to do this from Node and found this answer: How to know if node-webkit app is running with Administrator/elevated privilege?.

I checked into the answer, downloaded node-windows and tried it. The solution, however, brought up the UAC dialog and always responded with "The user has administrative privileges".

I dug into the node-windows code that handles the isAdminUser command and found that it tried to run NET SESSION and, if does not have privilege, tries to run it elevated causing the UAC dialog.

I pulled out the part that does the elevate and ended up with this snippet:

var exec = require('child_process').exec; 
exec('NET SESSION', function(err,so,se) {
      console.log(se.length === 0 ? "admin" : "not admin");
    });

I tested this by running the application normally and with "Run as Administrator". The code above correctly displayed "not admin" when not run as administrator and "admin" when run as administrator.

This should work for the content of your .isAdminPrivilegesUsed method you referenced in the question.

like image 81
Shawn Rakowski Avatar answered Oct 17 '22 09:10

Shawn Rakowski