diff --git a/libs/user/* b/libs/user/* index 38bff6b..d6cd185 100644 --- a/libs/user/* +++ b/libs/user/* @@ -1,6 +1,7 @@ # shellcheck shell=bash #include user/get_gid +#include user/get_groups #include user/get_home #include user/get_shell #include user/get_uid diff --git a/libs/user/get_groups b/libs/user/get_groups new file mode 100644 index 0000000..785e23e --- /dev/null +++ b/libs/user/get_groups @@ -0,0 +1,27 @@ +# shellcheck shell=bash + +# user::get_groups +# Retrieves the group memberships of a specified user. +# +# Description: +# This function fetches the group memberships of the user specified by the username argument. +# If no username is provided, it defaults to the current user. This is useful for scripts +# that need to manage permissions or access control based on user groups. +# +# Usage: +# user::get_groups +# +# Parameters: +# username - The username whose group memberships are to be retrieved. Optional, defaults to the +# current user. +# +# Outputs: +# The group memberships of the specified or default user. +# +# Example: +# user::get_groups "exampleUser" # Outputs the groups of 'exampleUser' +# user::get_groups # Outputs the groups of the current user +function user::get_groups() { + local username=${1:-${user:-$(whoami)}} + id --groups --name "$username" +}