Skip to content

Commit

Permalink
Add makeAdministrator action entry and functionality (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean28518 committed Mar 7, 2024
1 parent 34ee696 commit 0d62b5d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/content/basic_entries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,18 @@ List<ActionEntry> getBasicEntries(BuildContext context) {
.contains(SOFTWARE_MANAGERS.SNAP);
},
),
ActionEntry(
name: AppLocalizations.of(context)!.makeAdministrator,
description: AppLocalizations.of(context)!.makeAdministratorDescription,
action: "make_administrator",
iconWidget: Icon(
Icons.admin_panel_settings,
size: 48,
color: MintY.currentColor,
),
disableEntryIf: () {
return Linux.hasCurrentUserAdministratorRights();
},
)
];
}
2 changes: 2 additions & 0 deletions lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@
"setupSnap": "Snap einrichten",
"setupSnapDescription": "Installiere Snap und den Snap-Store, um viele weitere Anwendungen zu installieren.\nDanach ist ein Neustart des Rechners empfohlen.",
"vivaldiDescription": "Proprietärer Browser mit Fokus auf Privatsphäre und vielen Anpassungsmöglichkeiten.",
"makeAdministrator": "Aktuellen Benutzer zum Administrator machen",
"makeAdministratorDescription": "Füge den aktuellen Benutzer zur Gruppe 'sudo' hinzu, um Root-Rechte zu erhalten. Dafür ist das Passwort des Root-Benutzers nötig.\nDanach ist ein Neustart des Rechners empfohlen.",
"@helloWorld": {
"placeholders": {},
"description": "",
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@
"setupSnap": "Set up Snap",
"setupSnapDescription": "Install Snap and the Snap Store to install many more applications.\nAfterwards a restart of the computer is recommended.",
"vivaldiDescription": "Proprietary browser with focus on privacy and many customization options.",
"makeAdministrator": "Make the current user an administrator",
"makeAdministratorDescription": "Add the current user to the 'sudo' group to obtain root rights. The password of the root user is required for this.\nAfterwards a restart of the computer is recommended.",
"@helloWorld": {
"placeholders": {},
"description": "The conventional newborn programmer greeting",
Expand Down
1 change: 1 addition & 0 deletions lib/models/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Environment {
String kernelVersion = "";
String cpuModel = "";
String gpuModel = "";
List<String> groups = [];

/// POSIX User ID.
var currentUserId = 1000;
Expand Down
4 changes: 4 additions & 0 deletions lib/services/action_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,9 @@ class ActionHandler {
if (actionEntry.action.startsWith("setup_snap")) {
Linux.setupSnapAndSnapStore(context);
}

if (actionEntry.action.startsWith("make_administrator")) {
Linux.makeCurrentUserToAdministrator(context);
}
}
}
26 changes: 26 additions & 0 deletions lib/services/linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ class Linux {
newEnvironment.currentUserId = await getUserIdOfCurrentUser();
newEnvironment.hostname = await getHostname();
newEnvironment.username = await getUsername();
newEnvironment.groups = await getGroupsOfCurrentUser();
newEnvironment.osPrettyName = await Linux.getOsPrettyName();
newEnvironment.kernelVersion = await getKernelVersion();
newEnvironment.cpuModel = await getCpuModel();
Expand All @@ -926,6 +927,11 @@ class Linux {
return newEnvironment;
}

static Future<List<String>> getGroupsOfCurrentUser() async {
String groups = await runCommand("groups");
return groups.split(" ");
}

static String getExecutablePathOfSoftwareManager(SOFTWARE_MANAGERS input) {
switch (input) {
case SOFTWARE_MANAGERS.FLATPAK:
Expand Down Expand Up @@ -2387,4 +2393,24 @@ class Linux {
route: MainSearchLoader()),
));
}

/// Checks if the current user is in the group sudo
static bool hasCurrentUserAdministratorRights() {
return currentenvironment.groups.contains("sudo");
}

/// Adds the current user to sudo group
static void makeCurrentUserToAdministrator(context) {
commandQueue.add(LinuxCommand(
userId: 0,
command: "/usr/sbin/usermod -aG sudo ${currentenvironment.username}",
));

Navigator.of(context).push(MaterialPageRoute(
builder: (context) => RunCommandQueue(
title: AppLocalizations.of(context)!.makeAdministrator,
message: AppLocalizations.of(context)!.makeAdministratorDescription,
route: MainSearchLoader()),
));
}
}

0 comments on commit 0d62b5d

Please sign in to comment.