#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then 
	LANG001="Force_webauth_users_offline "	#ǿweb֤û
	LANG002="Success"	#ɹ
fi


show_user()
{
	local utype=$1

	${FLOWEYE} pppoesvr listclnt type=${utype} poolid=${CGI_ippool} | awk \
	-v utype=${utype} \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"utype\":\"%s\",", utype;
		printf "\"sid\":\"%s\",", $(col++);
		printf "\"name\":\"%s\",", $(col++);
		printf "\"poolid\":\"%s\",", $(col++);
		printf "\"poolname\":\"%s\",", $(col++);
		printf "\"svrname\":\"%s\",", $(col++);
		printf "\"pctime\":\"%s\",", $(col++);
		printf "\"clntmac\":\"%s\",", $(col++);
		printf "\"svrmac\":\"%s\",", $(col++);
		printf "\"ipstr\":\"%s\",", $(col++);
		printf "\"ipnum\":\"%s\",", $(col++);
		printf "\"ip6str\":\"%s\",", $(col++);
		printf "\"mtu\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"state\":\"%s\",", $(col++);
		printf "\"birth\":\"%s\",", $(col++);
		printf "\"expired\":\"%s\",", $(col++);
		printf "\"auth\":\"%s\",", $(col++);
		printf "\"ratein\":\"%s\",", $(col++);
		printf "\"rateout\":\"%s\",", $(col++);
		printf "\"clntpxy\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_webauth_user()
{
	[ "${CGI_sort}" = "" ] && CGI_sort="-k14"

	printf "["
	printf "`show_user webacct`"
	printf ",`show_user webrad`"
	printf ",`show_user cmccportal`"
	printf ",`show_user adlogin`"
	printf ",`show_user weixin`"
	printf ",`show_user dingding`"
	printf ",`show_user feishu`"
	printf ",`show_user oauth`"
	printf "]"
}


kick_webauth_user()
{
	for ip in ${CGI_ips}
	do
		${FLOWEYE} ipobj remove ip=${ip} uid=webacct
		${FLOWEYE} ipobj remove ip=${ip} uid=webrad
		${FLOWEYE} ipobj remove ip=${ip} uid=cmccportal
		${FLOWEYE} ipobj remove ip=${ip} uid=adlogin
		${FLOWEYE} ipobj remove ip=${ip} uid=weixin
		${FLOWEYE} ipobj remove ip=${ip} uid=dingding
		${FLOWEYE} ipobj remove ip=${ip} uid=feishu
		${FLOWEYE} ipobj remove ip=${ip} uid=oauth
	done

	WEB_LOGGER "${LANG001:=ǿweb֤û}" "ip=${CGI_ip} uid=${CGI_tname} account=${CGI_account}"
	retjson 0 "${LANG002:=ɹ}"
}


case "${CGI_action}" in
    "load_webauth_user")
        retjson 0 "OK" "`load_webauth_user`"
        ;;

    "kick_webauth_user")
        action_check
        kick_webauth_user
        ;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
