group 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env bash
  2. __rvm_add_user_to_group()
  3. {
  4. case "${_system_type}" in
  5. "OpenBSD")
  6. __rvm_try_sudo usermod -G "$1" "$2"
  7. ;;
  8. "FreeBSD"|"DragonFly")
  9. __rvm_try_sudo pw usermod "$2" -G "$1"
  10. ;;
  11. "Linux")
  12. if [[ -f "/etc/SuSE-release" ]] ; then
  13. __rvm_try_sudo groupmod -A "$2" "$1"
  14. else
  15. __rvm_try_sudo /usr/sbin/usermod -a -G "$1" "$2"
  16. fi
  17. ;;
  18. "Darwin")
  19. __rvm_try_sudo dscl . -append "/Groups/$1" GroupMembership "$2"
  20. ;;
  21. "SunOS")
  22. groups="$(id -G "$2") \"$1\""
  23. __rvm_try_sudo usermod -G "${1// /, }" "$2"
  24. ;;
  25. esac
  26. }
  27. __rvm_show_user_groups()
  28. {
  29. groups "$1" | __rvm_sed -e 's/.*:[[:space:]]*//' | \command \tr ' ' $'\n'
  30. }
  31. __rvm_is_user_in_group()
  32. {
  33. __rvm_show_user_groups "$2" | __rvm_grep "^$1$" >/dev/null
  34. }
  35. __rvm_list_all_users()
  36. {
  37. case "${_system_type}" in
  38. "Darwin")
  39. dscl . -search /Users PrimaryGroupID 20 | __rvm_grep PrimaryGroupID | cut -f 1
  40. ;;
  41. *)
  42. __rvm_grep -xF -f <(\command \cat /etc/passwd | cut -d: -f1) <(__rvm_find /home -mindepth 1 -maxdepth 1 -type d | cut -d '/' -f 3)
  43. ;;
  44. esac
  45. }
  46. __rvm_group_exists()
  47. {
  48. case "${_system_type}" in
  49. (Darwin)
  50. dscl . -read "/Groups/$1" 1>/dev/null 2>&1 || return $?
  51. ;;
  52. (*)
  53. __rvm_grep "^$1:" /etc/group >/dev/null 2>&1
  54. ;;
  55. esac
  56. }
  57. __rvm_create_group()
  58. {
  59. \typeset -a __group_params
  60. __group_params=()
  61. if [[ -n "${2:-}" ]]
  62. then __group_params+=( -g "$2" )
  63. fi
  64. case "${_system_type}" in
  65. (Linux|SunOS)
  66. __rvm_try_sudo groupadd "${__group_params[@]}" "$1"
  67. ;;
  68. (BSD)
  69. case "${_system_name}" in
  70. (OpenBSD)
  71. __rvm_try_sudo groupadd "${__group_params[@]}" "$1"
  72. ;;
  73. (FreeBSD|DragonFly)
  74. __rvm_try_sudo pw groupadd "${__group_params[@]}" "$1" -q
  75. ;;
  76. esac
  77. ;;
  78. (Darwin)
  79. \typeset _new_group_id
  80. if
  81. [[ -n "${2:-}" ]]
  82. then
  83. _new_group_id="$2"
  84. else
  85. _new_group_id="501" #only gids > 500 show up in user preferences
  86. #Find an open gid
  87. while true
  88. do
  89. name=$(dscl . search /groups PrimaryGroupID ${_new_group_id} | cut -f1 -s)
  90. if [[ -z "$name" ]]
  91. then break
  92. fi
  93. _new_group_id=$(( _new_group_id + 1 ))
  94. done
  95. fi
  96. # Create the group, isn't OSX "fun"?! :)
  97. # Thanks for the assist frogor of ##osx-server on freenode! Appreciate the assist!
  98. __rvm_try_sudo dscl . -create "/Groups/$1" gid "${_new_group_id}"
  99. ;;
  100. esac
  101. }