Aaleks
Aaleks

Reputation: 4343

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

Upvotes: 12

Views: 23612

Answers (5)

Kim T
Kim T

Reputation: 6426

A popular Electron app has a solution for this problem https://github.com/microsoft/vscode

In the package.json file they have two useful dependencies:

They check to see if permissions are elevated using native-is-elevated, and if not, prompt for an admin password using sudo-prompt.

You can read the source code for the process here: https://github.com/microsoft/vscode/blob/8845f89c1e4183b54126cd629cd45c8f0f7549f2/src/vs/platform/native/electron-main/nativeHostMainService.ts#L491

I have created an example Electron app using this approach here: https://github.com/kmturley/electron-runas-admin

Upvotes: 2

DBolton
DBolton

Reputation: 548

You can now specify that an app should run with elevated privileges using the electron build tools:

electron-builder

Add the following to your package.json:

  "build": {
    "win": {
      "requestedExecutionLevel": "highestAvailable"
    }
  },

highestAvailable or requireAdministrator available. For full details, see: https://www.electron.build/configuration/win.html#WindowsConfiguration-requestedExecutionLevel

electron-packager

When you call electron-packager add the following command-line parameter:

--win32metadata.requested-execution-level=highestAvailable

highestAvailable or requireAdministrator available. For full details, see https://electron.github.io/electron-packager/master/interfaces/electronpackager.win32metadataoptions.html#requested_execution_level

Note

These options make the program request elevated privileges rather than check whether the program is running with administrator privileges.

Upvotes: 12

If you are using electron-packager, just add --win32metadata.requested-execution-level=requireAdministrator. Eg:

electron-packager app --asar=true --platform=win32 --arch=ia32 --win32metadata.requested-execution-level=requireAdministrator --overwrite

Upvotes: 6

Robin Andersson
Robin Andersson

Reputation: 5380

Not a direct answer to your question. Another option to solve this problem is to force the application to be executed as administrator.

This can be done by updating the manifest file for the application, one guide on how to do this with Electron is here: http://layer0.authentise.com/electron-and-uac-on-windows.html

Upvotes: 1

Shawn Rakowski
Shawn Rakowski

Reputation: 5714

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.

Upvotes: 13

Related Questions