如何在电子应用程序中获取持久权限?
我正在创建一个电子应用程序,我需要在其中扫描和更新需要 root 权限的文件.我知道我可以以这种方式使用 sudo.exec() 运行此类命令:
I am creating an electron app where I need to scan and update files which need root permission. I know I can run such commands using sudo.exec() in that way:
sudo.exec ("rm /private/var/log/fsck_hfs.log", options, (e, stdout, stderr) => {});
甚至我可以在一个脚本中放入多个命令并使用单个 sudo.exec() 执行它们.就我而言,命令需要在不同的时间点执行,我无法使用单个脚本执行它们.用户一次又一次地授予权限很烦人.
And even I can put multiple commands in a script and execute them with single sudo.exec(). In my case, commands needs to be executed at different point of time and I cannot execute them with a single script. It is annoying for user to grant permissions again and again.
我尝试了另一种方式(运行一段代码,可以执行所有需要 root 权限的内容).为此,我在这里发布了另一个问题.但似乎不可能.
I tried to do it another way (to run a piece of code where everything that require root permission can be executed). For that, I posted another question here. But it seems that it is not possible.
现在我想要一种在使用安装应用程序时获得许可的方法(就像大多数应用程序在用户安装它们之后所做的那样)并且能够在整个应用程序中使用 sudo.exec() 或其他一些方法来执行命令(最好还有需要root权限的代码fs.readdir等).
Now I want a way to get permission once when use install app (as most of the apps does after user install them) and be able to use throughout the app sudo.exec() or some other method to execute commands (preferably also the code fs.readdir, etc) that require root permission.
推荐答案
您需要以管理员权限运行应用程序.如果您使用 electron builder 构建应用程序,请使用 requestedExecutionLevel 值作为requireAdministrator"
You need to run the applicaton with administrator privilege. If you are using electron builder to build the app, use the requestedExecutionLevel value as "requireAdministrator"
https://www.electron.build/configuration/win
相关文章