-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_database_grant.sh
executable file
·122 lines (103 loc) · 3.33 KB
/
custom_database_grant.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
pgm="${0##*/}" # Program basename
progdir="${0%/*}" # Program directory
: ${REALPATH_CMD=$( which realpath )}
: ${SQLITE3_CMD=$( which sqlite3 )}
: ${RM_CMD=$( which rm )}
: ${MKDIR_CMD=$( which mkdir )}
: ${FORM_PATH="/opt/forms"}
: ${distdir="/usr/local/cbsd"}
MY_PATH="$( ${REALPATH_CMD} ${progdir} )"
HELPER="postgresql"
# MAIN
if [ -z "${workdir}" ]; then
[ -z "${cbsd_workdir}" ] && . /etc/rc.conf
[ -z "${cbsd_workdir}" ] && exit 0
workdir="${cbsd_workdir}"
fi
set -e
. ${distdir}/cbsd.conf
. ${subrdir}/tools.subr
. ${subr}
. ${subrdir}/forms.subr
set +e
FORM_PATH="${workdir}/formfile"
[ ! -d "${FORM_PATH}" ] && err 1 "No such ${FORM_PATH}"
###
groupname="database_grant"
err() {
exitval=$1
shift
echo "$*"
exit $exitval
}
add()
{
local _custom_id=
_custom_id=$( get_custom_id "database_grant_name" )
if [ -r "${formfile}" ]; then
${SQLITE3_CMD} ${formfile} <<EOF
BEGIN TRANSACTION;
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'database_grant_name${_custom_id}','uniq grant name, e.g: mydb','grant${_custom_id}','grant${_custom_id}','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'database_grant_privilege${_custom_id}', 'privilege','ALL', 'ALL','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'database_grant_db${_custom_id}', 'grant DB','dbname1', 'dbname1','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'database_grant_role${_custom_id}', 'grant ROLE','pguser1', 'pguser1','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
COMMIT;
EOF
else
/bin/cat <<EOF
BEGIN TRANSACTION;
INSERT INTO forms ( mytable,group_id,order_id,param,desc,def,cur,new,mandatory,attr,xattr,type,link,groupname ) VALUES ( 'forms', ${index},${order_id},'roles_name${_custom_id}','roles part ${_custom_id}','','','',1, 'maxlen=60', 'dynamic', 'inputbox', '', '${groupname}' );
COMMIT;
EOF
fi
}
del()
{
if [ -r "${formfile}" ]; then
${SQLITE3_CMD} ${formfile} <<EOF
BEGIN TRANSACTION;
DELETE FROM forms WHERE group_id = '${index}' AND groupname = '${groupname}';
COMMIT;
EOF
else
/bin/cat <<EOF
BEGIN TRANSACTION;
DELETE FROM forms WHERE group_id = '${index}' AND groupname = '${groupname}';
COMMIT;
EOF
fi
}
usage()
{
echo "$0 -a add/remove -i index"
}
### MAIN
while getopts "a:i:f:o:" opt; do
case "$opt" in
a) action="${OPTARG}" ;;
i) index="${OPTARG}" ;;
f) formfile="${OPTARG}" ;;
o) order_id="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${action}" ] && usage
[ -z "${index}" ] && err 1 "${pgm}: empty index"
[ -z "${order_id}" ] && err 1 "${pgm}: empty order_id"
if [ ${index} -eq 1 ]; then
err 1 "${pgm} error: index=0"
fi
#echo "Index: $index, Action: $action, Groupname: $groupname"
case "${action}" in
add|create)
add
;;
del*|remove)
del
;;
*)
echo "Unknown action: must be 'add' or 'del'"
;;
esac
exit 0