Browse Source

Added a script to remove a user from one or more group(s)

theo 2 years ago
parent
commit
954d66db77
1 changed files with 43 additions and 0 deletions
  1. 43 0
      removefromgroups

+ 43 - 0
removefromgroups

@@ -0,0 +1,43 @@
+#!/bin/bash
+. .env
+. funcs
+
+uid=$1
+
+if user_exists $uid; then
+    true
+else
+    echo "User $uid does not exist." 
+    exit 1
+fi
+
+group_ofnames="dn: cn=%GROUP%,$GROUPSDN
+changeType: modify
+delete: member
+member: uid=%UID%,$PEOPLEDN"
+
+group_posix="dn: cn=%GROUP%,$GROUPSDN
+changeType: modify
+delete: memberUid
+memberUid: %UID%"
+
+# remove uid ($1) from args so that only the list
+# of groups remains in $@
+shift
+
+# remove user from each of the groups given
+for g in $@; do
+    if group_exists $g ; then
+	if group_is_posix $g; then
+		user_is_posix $uid || echo "User $uid is not a posixAccount, skipping posixGroup $g..." && continue
+		group="$group_posix"
+	else
+		group="$group_ofnames"
+	fi
+	echo "$group" | sed \
+	    -e "s/%GROUP%/$g/" \
+	    -e "s/%UID%/$uid/" | ldapmodify $LDAPOPTS
+    else
+        echo "Group $g does not exist, skipping..."
+    fi
+done