#!/bin/sh 

#####################################################################
# scenario #2
#     Explores relationship between project and member
#
#  Drd, a geniPI, created(owner of) projects 'proj1' and 'proj2'
#  Drd grant Frank, a geniUser, member permission on 'proj1'
#  Drd grant Dan, a geniUser, member_ permission on 'proj1'
#  Drd grant Jim, a geniUser, delegate_member_ permission on 'proj1'
#  Jim grant Nancy, a geniUser, member_ permission on 'proj1'
#  PA grant Lisa, a geniUser, member permission on 'proj1'
#  PA grant Dan, a geniUser, member_ permission on 'proj1'
#  PA grant Dan, a geniUser, member_ permission on 'proj2'
#  PA grant Cary, a geniUser, delegate_member_ permission on 'proj1'
#  PA grant Alice, a geniUser, member_ permission on 'proj2'
#  PA grant Alice, a geniUser, member_ permission on 'proj1'
#
unset ABAC_CN
keyloc=`pwd`
sidx=s2

makeWho() {
    WHO=$1
    rm -rf ${WHO}_*.pem
    creddy --generate --cn ${WHO}
}
makeWho GR
makeWho IDP
makeWho EXO
makeWho Drd
makeWho Frank
makeWho Dan
makeWho Lisa
makeWho Cary
makeWho Jim
makeWho Nancy
makeWho Alice

g=`creddy --keyid --cert $keyloc/G_ID.pem`
gr=`creddy --keyid --cert $keyloc/GR_ID.pem`
pa=`creddy --keyid --cert $keyloc/PA_ID.pem`
sa=`creddy --keyid --cert $keyloc/SA_ID.pem`
idp=`creddy --keyid --cert $keyloc/IDP_ID.pem`
exo=`creddy --keyid --cert $keyloc/EXO_ID.pem`
drd=`creddy --keyid --cert $keyloc/Drd_ID.pem`
frank=`creddy --keyid --cert $keyloc/Frank_ID.pem`
dan=`creddy --keyid --cert $keyloc/Dan_ID.pem`
lisa=`creddy --keyid --cert $keyloc/Lisa_ID.pem`
cary=`creddy --keyid --cert $keyloc/Cary_ID.pem`
jim=`creddy --keyid --cert $keyloc/Jim_ID.pem`
nancy=`creddy --keyid --cert $keyloc/Nancy_ID.pem`
alice=`creddy --keyid --cert $keyloc/Alice_ID.pem`

pname=${sidx}_yap_clauses
namespace=access1
sfile=sfile_${sidx}

idp_gUser()
{
eval "namename=[keyid:\$$1]"
attr="[keyid:$idp].role:geniUser <- $namename"
name=${sidx}_IDP_gUser__$1_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
}

## drd requesting a proj1 from pa
## makeProject $drd proj1 Drd
makeProject() {
   what="'$2'"
# Drd.project <- 'proj1'
   attr="[keyid:$1].oset:project <- [string:$what]"
   name=${sidx}_$3_project__$2_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# PA.standard <- 'proj1'
   attr="[keyid:$pa].oset:standard <- [string:$what]"
   name=${sidx}_PA_standard__$2_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# PA.owner('proj1') <- Drd
   attr="[keyid:$pa].role:owner([string:$what]) <- [keyid:$1]"
   name=${sidx}_PA_owner_$2__$3_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# PA.root('proj1') <-Drd
   attr="[keyid:$pa].role:root([string:$what]) <- [keyid:$1]"
   name=${sidx}_PA_root_$2__$3_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
}

###############################################################################
echo "s/$g/G/g "  > $sfile 
echo "s/$gr/GR/g "  >> $sfile 
echo "s/$pa/PA/g "  >> $sfile
echo "s/$sa/SA/g "  >> $sfile
echo "s/$idp/IDP/g "  >> $sfile
echo "s/$exo/EXO/g "  >> $sfile
echo "s/$drd/Drd/g "  >> $sfile
echo "s/$frank/Frank/g "  >> $sfile
echo "s/$dan/Dan/g "  >> $sfile
echo "s/$lisa/Lisa/g "  >> $sfile
echo "s/$cary/Cary/g "  >> $sfile
echo "s/$jim/Jim/g "  >> $sfile
echo "s/$nancy/Nancy/g "  >> $sfile
echo "s/$alice/Alice/g "  >> $sfile

# G.geniRoot <- GR 
attr="[keyid:$g].role:geniRoot <- [keyid:$gr]"
name=${sidx}_G_geniRoot__gr_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.sliceAuthorith <- SA
attr="[keyid:$gr].role:sliceAuthority <- [keyid:$sa]"
name=${sidx}_GR_sliceAuthority__sa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.identityProvider <- IDP
attr="[keyid:$gr].role:identityProvider <- [keyid:$idp]"
name=${sidx}_GR_identityProvider__idp_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.projectAuthority <- PA
attr="[keyid:$gr].role:projectAuthority <- [keyid:$pa]"
name=${sidx}_GR_projAuthority__pa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.aggregate <- EXO
attr="[keyid:$gr].role:aggregate <- [keyid:$exo]"
name=${sidx}_GR_aggregate__exo_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# IDP.geniUser <- Drd
idp_gUser drd
# IDP.geniUser <- Frank
idp_gUser frank
# IDP.geniUser <- Dan
idp_gUser dan
# IDP.geniUser <- Lisa
idp_gUser lisa
# IDP.geniUser <- Cary
idp_gUser cary
# IDP.geniUser <- Jim
idp_gUser jim
# IDP.geniUser <- Nancy
idp_gUser nancy
# IDP.geniUser <- Alice
idp_gUser alice 

# IDP.geniPI <- Drd
attr="[keyid:$idp].role:geniPI <- [keyid:$drd]"
name=${sidx}_IDP_geniPI__drd_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## drd requesting a proj1 from pa
makeProject $drd proj1 Drd
## drd requesting a proj2 from pa
makeProject $drd proj2 Drd

## Drd gives member permission on proj1 to Frank
attr="[keyid:$drd].role:member([string:'proj1']) <- [keyid:$frank]"
name=${sidx}_Drd_member_proj1__frank_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives member_ permission on proj1 to Dan
attr="[keyid:$drd].role:member_([string:'proj1']) <- [keyid:$dan]"
name=${sidx}_Drd_memberDel_proj1__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives a delegate_member_ permission on proj1 to Jim
attr="[keyid:$drd].role:delegate_member_([string:'proj1']) <- [keyid:$jim]"
name=${sidx}_Drd_delmemberDel_proj1__jim_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Jim gives a member_ permission on proj1 to Nancy
attr="[keyid:$jim].role:member_([string:'proj1']) <- [keyid:$nancy]"
name=${sidx}_Jim_memberDel_proj1__nancy_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives member permission on proj1 to Lisa 
attr="[keyid:$pa].role:member([string:'proj1']) <- [keyid:$lisa]"
name=${sidx}_PA_member_proj1__lisa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives member_ permission on proj1 to Dan
attr="[keyid:$pa].role:member_([string:'proj2']) <- [keyid:$dan]"
name=${sidx}_PA_memberDel_proj2__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives a delegate_member_ permission on proj1 to Cary
attr="[keyid:$pa].role:delegate_member_([string:'proj1']) <- [keyid:$cary]"
name=${sidx}_PA_delmemberDel_proj1__cary_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives member_ permission on proj1 to Dan
attr="[keyid:$pa].role:member_([string:'proj1']) <- [keyid:$dan]"
name=${sidx}_PA_memberDel_proj1__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives member_ permission on proj2 to Alice
attr="[keyid:$pa].role:member_([string:'proj2']) <- [keyid:$alice]"
name=${sidx}_PA_memberDel_proj2__alice_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives member_ permission on proj1 to Alice
attr="[keyid:$pa].role:member_([string:'proj1']) <- [keyid:$alice]"
name=${sidx}_PA_memberDel_proj1__alice_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace



