--- /dev/null
+#!/bin/bash
+
+USER_BARCODE="$1"
+PSQL=/usr/bin/psql
+PSQL_USER=mydbuser
+DB_HOST=mydbhost
+
+
+
+read -d '' SQL <<EOL
+select perm.code as "Permission",
+ perm.description as "Description",
+ grp.name as "Permission Level",
+ case
+ when map.depth = 0 then 'Consortium'
+ when map.depth = 1 then 'System'
+ when map.depth = 2 then 'Branch'
+ end as "Depth",
+ case
+ when map.grantable = true then 'Grantable'
+ when map.grantable = false then 'Not Grantable'
+ end as "Grantability"
+from permission.grp_tree grp
+ join permission.grp_perm_map map on (map.grp = grp.id)
+ join permission.perm_list perm on (map.perm = perm.id)
+ where grp.id in (
+ select id
+ from permission.grp_ancestors(
+ (select profile
+ from actor.usr
+ where card in (
+ select id
+ from actor.card
+ where barcode = '$USER_BARCODE'
+ )
+ )
+ )
+ )
+UNION ALL
+select perm2.code as "Permission",
+ perm2.description as "Description",
+ 'Per-User Assignment' as "Permission Level",
+ case
+ when map2.depth = 0 then 'Consortium'
+ when map2.depth = 1 then 'System'
+ when map2.depth = 2 then 'Branch'
+ end as "Depth",
+ case
+ when map2.grantable = true then 'Grantable'
+ when map2.grantable = false then 'Not Grantable'
+ end as "Grantability"
+from permission.usr_perm_map map2
+ join permission.perm_list perm2 on (map2.perm = perm2.id)
+where map2.usr in (
+ select usr
+ from actor.card
+ where barcode = '$USER_BARCODE')
+UNION ALL
+select perm3.code as "Permission",
+ perm3.description as "Description",
+ 'Secondary: ' || grp3.name as "Permission Level",
+ case
+ when grp_perms.depth = 0 then 'Consortium'
+ when grp_perms.depth = 1 then 'System'
+ when grp_perms.depth = 2 then 'Branch'
+ end as "Depth",
+ case
+ when grp_perms.grantable = true then 'Grantable'
+ when grp_perms.grantable = false then 'Not Grantable'
+ end as "Grantability"
+from permission.usr_grp_map map3
+ join permission.grp_perm_map grp_perms on (map3.grp = grp_perms.grp)
+ join permission.grp_tree grp3 on (map3.grp = grp3.id)
+ join permission.perm_list perm3 on (grp_perms.perm = perm3.id)
+where map3.usr in (
+ select usr
+ from actor.card
+ where barcode = '$USER_BARCODE')
+
+order by 1, 2;
+EOL
+
+$PSQL -U $PSQL_USER -h $DB_HOST -1 -c "$SQL"
+