mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
PR comment: extract requireRootPrivileges / requireAdminPrivileges into separate function
This commit is contained in:
parent
7218d778cf
commit
a3ab80b8b4
2 changed files with 41 additions and 23 deletions
|
|
@ -9,11 +9,29 @@ import chalk from "chalk";
|
||||||
|
|
||||||
const MACOS_PKG_IDENTIFIER = "com.aikidosecurity.safechainultimate";
|
const MACOS_PKG_IDENTIFIER = "com.aikidosecurity.safechainultimate";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if root privileges are available and displays error message if not.
|
||||||
|
* @param {string} command - The sudo command to show in the error message
|
||||||
|
* @returns {boolean} True if running as root, false otherwise.
|
||||||
|
*/
|
||||||
|
function requireRootPrivileges(command) {
|
||||||
|
if (isRunningAsRoot()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.writeError("Root privileges required.");
|
||||||
|
ui.writeInformation("Please run this command with sudo:");
|
||||||
|
ui.writeInformation(` ${command}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRunningAsRoot() {
|
||||||
|
const rootUserUid = 0;
|
||||||
|
return process.getuid?.() === rootUserUid;
|
||||||
|
}
|
||||||
|
|
||||||
export async function installOnMacOS() {
|
export async function installOnMacOS() {
|
||||||
if (!isRunningAsRoot()) {
|
if (!requireRootPrivileges("sudo safe-chain ultimate")) {
|
||||||
ui.writeError("Root privileges required.");
|
|
||||||
ui.writeInformation("Please run this command with sudo:");
|
|
||||||
ui.writeInformation(" sudo safe-chain ultimate");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,10 +74,7 @@ export async function installOnMacOS() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function uninstallOnMacOS() {
|
export async function uninstallOnMacOS() {
|
||||||
if (!isRunningAsRoot()) {
|
if (!requireRootPrivileges("sudo safe-chain ultimate uninstall")) {
|
||||||
ui.writeError("Root privileges required.");
|
|
||||||
ui.writeInformation("Please run this command with sudo:");
|
|
||||||
ui.writeInformation(" sudo safe-chain ultimate uninstall");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,11 +151,6 @@ function forgetPackage() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isRunningAsRoot() {
|
|
||||||
const rootUserUid = 0;
|
|
||||||
return process.getuid?.() === rootUserUid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} pkgPath
|
* @param {string} pkgPath
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,7 @@ const WINDOWS_SERVICE_NAME = "SafeChainUltimate";
|
||||||
const WINDOWS_APP_NAME = "SafeChain Ultimate";
|
const WINDOWS_APP_NAME = "SafeChain Ultimate";
|
||||||
|
|
||||||
export async function uninstallOnWindows() {
|
export async function uninstallOnWindows() {
|
||||||
if (!(await isRunningAsAdmin())) {
|
if (!(await requireAdminPrivileges())) {
|
||||||
ui.writeError("Administrator privileges required.");
|
|
||||||
ui.writeInformation(
|
|
||||||
"Please run this command in an elevated terminal (Run as Administrator).",
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,11 +33,7 @@ export async function uninstallOnWindows() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function installOnWindows() {
|
export async function installOnWindows() {
|
||||||
if (!(await isRunningAsAdmin())) {
|
if (!(await requireAdminPrivileges())) {
|
||||||
ui.writeError("Administrator privileges required.");
|
|
||||||
ui.writeInformation(
|
|
||||||
"Please run this command in an elevated terminal (Run as Administrator).",
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,6 +68,22 @@ export async function installOnWindows() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if admin privileges are available and displays error message if not.
|
||||||
|
* @returns {Promise<boolean>} True if running as admin, false otherwise.
|
||||||
|
*/
|
||||||
|
async function requireAdminPrivileges() {
|
||||||
|
if (await isRunningAsAdmin()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.writeError("Administrator privileges required.");
|
||||||
|
ui.writeInformation(
|
||||||
|
"Please run this command in an elevated terminal (Run as Administrator).",
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async function isRunningAsAdmin() {
|
async function isRunningAsAdmin() {
|
||||||
// Uses Windows Security API to check if current process has admin privileges.
|
// Uses Windows Security API to check if current process has admin privileges.
|
||||||
// Returns "True" or "False" as a string.
|
// Returns "True" or "False" as a string.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue