// Example adapted (hacked) from Olson, Gunter, Madhusudan CCS'08, Data is random

// A copy of the `access-policy' test, this time using
// the magic set transformation
.pragma "magic-transform" "*"

.type String <: symbol

.decl view_employee(x : String, p:String, ssn:String, s:number, e:String, d:String, pos:String, bday:number)
.output view_employee()
.decl view_cwPriv(n : String, person :String, bank1Priv :number, bank2Priv : number)
.input view_cwPriv()
.decl view_bank1(n :String, Data1 :number, Data2 :number)
.input view_bank1()
.decl view_bank2(n :String, Data1 : number, Data2:number)
.input view_bank2()

.decl employee(person : String, ssn:String, salary : number, email: String, dept : String, pos : String, bday : number)
.input employee()

.decl inslogtable(user :String, person :String, day : number)
.input inslogtable()

.decl insurance(user : String)
.input insurance()

.decl bank1(d:number, d2:number)
.input bank1()
.decl bank2(d:number, d2:number)
.input bank2()

.decl inscwPriv(user : String, x:number, y:number)
.input inscwPriv()
.decl delcwPriv(user:String, x:number, y:number)
.input delcwPriv()
.decl cwPriv(person:String, bank1Priv : number, Bank2Privi : number)
.input cwPriv()


view_employee("Alice", Person, SSN, Salary, Email, Dept, Position, Bday) :-
  employee(Person, SSN, Salary, Email, Dept, Position, Bday).

view_employee(User, Person, SSN, Salary, Email, Dept, Position, Bday) :-
  User=Person, view_employee("alice", Person, SSN, Salary, Email, Dept, Position, Bday).

view_employee(User, Person, SSN, Salary, Email, Dept, Position, Bday) :-
  view_employee("Alice", User, _, _, _, Dept,"Manager", _),
  view_employee("Alice", Person, SSN, Salary, Email, Dept, Position, Bday).

view_employee(User, Person, "0", 0, Email, Dept, Position, Bday) :-
  view_employee("Alice", User, _, _, _, _, _, _),
  view_employee("Alice", Person, _, _, Email, Dept, Position, Bday).

view_employee(User, Person, "0", 0, " ", " ", " ", Bday) :-
  insurance(User),
  view_employee("Alice", Person, _, _, _, _, _, Bday),
  inslogtable(User, Person, 92311).

view_cwPriv("Bob", Person, Bank1Priv, Bank2Priv) :-
  cwPriv(Person, Bank1Priv, Bank2Priv).

view_bank1("Bob", Data1, Data2) :-
  bank1(Data1, Data2).

view_bank1(User, Data1, Data2) :-
  view_cwPriv("Bob", User, 1, _),
  delcwPriv(User, 1, _), inscwPriv(User, 1, 0),
  view_bank1("Bob", Data1, Data2).

view_bank2("Bob", Data1, Data2) :-
  bank2(Data1, Data2).

view_bank2(User, Data1, Data2) :-
  view_cwPriv("Bob", User, _, 1),
  delcwPriv(User, _, 1),
  inscwPriv(User, 0, 1),
  view_bank2("Bob", Data1, Data2).
