From fa65a9f4a4ce623eb53085c7c0a9a1f8b4dc26aa Mon Sep 17 00:00:00 2001 From: kebler Date: Tue, 7 Feb 2023 09:26:05 -0800 Subject: [PATCH] add basic user functions to get id name and to do basic checks is_uer is_user_group is_user_sudo --- modules/utility/user.lib | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/utility/user.lib b/modules/utility/user.lib index 7013e81..50b9ac3 100755 --- a/modules/utility/user.lib +++ b/modules/utility/user.lib @@ -1,8 +1,27 @@ #!/bin/bash -is_user () { +user_id () { local id;local user - echo -n $(id -u $1 2> /dev/null) + echo -n $(id -u ${1:-$USER} 2> /dev/null) +} + +user_name () { + echo -n $(id -un -- ${1:-$USER} 2> /dev/null) +} + +is_user () { + user_id $1 +} + +#group name plus optional username or id +is_user_group () { + # echo id -nG ${2:-$USER} + # id -nG ${2:-$USER} | grep -w "sudo" + if id -nG ${2:-$USER} | grep -qw "$1"; then echo ${2:-$USER} in $1; fi +} + +is_user_sudo () { + is_user_group sudo $1 } user_group_add() {