Davical deluser

From Davical
Revision as of 18:29, 21 August 2009 by Cs (talk) (New page: <pre> #!/bin/sh # # written by Christian Schwamborn # bugs and suggestions to: # christian.schwamborn[you-know-what-comes-here]nswit.de # # published under GNU General Public License v.3 #...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#!/bin/sh
#
# written by Christian Schwamborn
# bugs and suggestions to:
# christian.schwamborn[you-know-what-comes-here]nswit.de
#
# published under GNU General Public License v.3
# parts of this script origins from http://wiki.davical.org
# version 1.0 (2009-08-21)
#
# You are using this scrip at your own risk, the author is not responsible
# for data damage or losses in any way.
#
# What this is:
# This simple scrip is intended to delete users from the davical database
# (look at http://rscds.sourceforge.net/) until davical contains that feature
# itself.
#
# How to use:
# Call the script with just the username you want to delete, will fo course
# delete the user, his calendars and all his relationships. Events modified or
# creates by the user in other than his own calendars, will be changed as if
# they had been created/modified by their owner.
#
# Example:
# >davical_deluser john
#
# The output will look similar like this:
# >changing entries of user "john" ...
# >UPDATE 0
# >delete user "john", his calendars and relationships ...
# >BEGIN
# >DELETE 0
# >DELETE 0
# >DELETE 1
# >DELETE 0
# >DELETE 1
# >COMMIT
# 
# If you define an other user, the script will use that user, to change the
# mod/create attibute to.
#
# Example:
# >davical_deluser john moduser=bob
#
# If you define "moduser=_DELETE_", the script will delete all entries, the
# user created or modified (not verry usefull, I know - but I thought maybe
# someone has the urge to whipe someone else out of existense ;-).
#
# Example:
# >davical_deluser john moduser=_DELETE_
#
# have fun...
###############################################################################

sql_user=postgres
sql_bin=/usr/bin/psql
sql_database=davical

[ $# -eq 0 ] && echo -e "Syntax:\n  davical_deluser <username> [moduser=<othername>|_DELETE_]" && exit 1

for arg in "$@"; do
	if expr 'index' "${arg}" '=' '>' '1' >/dev/null; then
		eval "${arg}"
	elif ! expr 'index' "${arg}" '=' >/dev/null; then
		username="${arg}"
	fi
done

for user2check in "${username}" "${moduser}"; do
	if [ -n "${user2check}" ] && [ "${user2check}" != "_DELETE_" ]; then
		su ${sql_user} -c "${sql_bin} -d ${sql_database} << _EOT_

SELECT username FROM usr WHERE username='${user2check}';
_EOT_" | grep -q ${user2check}

		[ $? -ne 0 ] && echo "username \"${user2check}\" not found in database \"${sql_database}\"" && exit 1
	fi
done

[ -n "${moduser}" ] && [ "${moduser}" = "${username}" ] && \
	echo "\"moduser\" should not be the same username, as the user to be deleted!" && exit 1

if [ "${moduser}" = "_DELETE_" ]; then
	echo "deleting entries of user \"${username}\" ..."
	su ${sql_user} -c "${sql_bin} -d ${sql_database} << _EOT_

DELETE FROM caldav_data WHERE logged_user = (SELECT user_no FROM usr WHERE username='${username}');
_EOT_"

else
	if [ -z "${moduser}" ]; then
		moduser=user_no
	else
		moduser="(SELECT user_no FROM usr WHERE username='${moduser}')"
	fi
	echo "changing entries of user \"${username}\" ..."
	su ${sql_user} -c "${sql_bin} -d ${sql_database} << _EOT_

UPDATE caldav_data SET logged_user = user_no WHERE logged_user = (SELECT user_no FROM usr WHERE username='${username}')
_EOT_"

fi

echo "delete user \"${username}\", his calendars and relationships ..."
su ${sql_user} -c "${sql_bin} -d ${sql_database} << _EOT_
BEGIN;
DELETE FROM relationship WHERE to_user = (SELECT user_no FROM usr WHERE username='${username}');
DELETE FROM relationship WHERE from_user = (SELECT user_no FROM usr WHERE username='${username}');
DELETE FROM collection WHERE user_no = (SELECT user_no FROM usr WHERE username='${username}');
DELETE FROM property WHERE dav_name LIKE '/${username}/%';
DELETE FROM usr WHERE username = '${username}';
COMMIT;
_EOT_
"