Davical deluser


 * 1) !/bin/sh
 * 2) written by Christian Schwamborn
 * 3) bugs and suggestions to:
 * 4) christian.schwamborn[you-know-what-comes-here]nswit.de
 * 5) published under GNU General Public License v.3
 * 6) parts of this script origins from http://wiki.davical.org
 * 7) version 1.0 (2009-08-21)
 * 8) You are using this scrip at your own risk, the author is not responsible
 * 9) for data damage or losses in any way.
 * 10) What this is:
 * 11) This simple scrip is intended to delete users from the davical database
 * 12) (look at http://rscds.sourceforge.net/) until davical contains that feature
 * 13) itself.
 * 14) How to use:
 * 15) Call the script with just the username you want to delete, will fo course
 * 16) delete the user, his calendars and all his relationships. Events modified or
 * 17) creates by the user in other than his own calendars, will be changed as if
 * 18) they had been created/modified by their owner.
 * 19) Example:
 * 20) >davical_deluser john
 * 21) The output will look similar like this:
 * 22) >changing entries of user "john" ...
 * 23) >UPDATE 0
 * 24) >delete user "john", his calendars and relationships ...
 * 25) >BEGIN
 * 26) >DELETE 0
 * 27) >DELETE 0
 * 28) >DELETE 1
 * 29) >DELETE 0
 * 30) >DELETE 1
 * 31) >COMMIT
 * 32) If you define an other user, the script will use that user, to change the
 * 33) mod/create attibute to.
 * 34) Example:
 * 35) >davical_deluser john moduser=bob
 * 36) If you define "moduser=_DELETE_", the script will delete all entries, the
 * 37) user created or modified (not verry usefull, I know - but I thought maybe
 * 38) someone has the urge to whipe someone else out of existense ;-).
 * 39) Example:
 * 40) >davical_deluser john moduser=_DELETE_
 * 41) have fun...
 * 1) Example:
 * 2) >davical_deluser john moduser=bob
 * 3) If you define "moduser=_DELETE_", the script will delete all entries, the
 * 4) user created or modified (not verry usefull, I know - but I thought maybe
 * 5) someone has the urge to whipe someone else out of existense ;-).
 * 6) Example:
 * 7) >davical_deluser john moduser=_DELETE_
 * 8) have fun...
 * 1) Example:
 * 2) >davical_deluser john moduser=_DELETE_
 * 3) have fun...
 * 1) have fun...

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

[ $# -eq 0 ] && echo -e "Syntax:\n davical_deluser [moduser= |_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_ "