package SeamSpacePermissions;

import java.security.Principal;

import org.jboss.seam.security.PermissionCheck;
import org.jboss.seam.security.Role;

import org.jboss.seam.example.seamspace.Member;
import org.jboss.seam.example.seamspace.MemberBlog;
import org.jboss.seam.example.seamspace.MemberFriend;
import org.jboss.seam.example.seamspace.MemberImage;

rule ViewImage
  no-loop
  activation-group "permissions"
when
  check: PermissionCheck(name == "memberImage", action == "view", granted == false)
  image: MemberImage()
  eval( image.getMember().getPicture() == image )
then
  check.grant();
end

rule RestrictCommentPage
  no-loop
  activation-group "permissions"
when
  check: PermissionCheck(name == "/comment.xhtml", granted == false)
  Role(name == "user")
then
  check.grant();
end

rule CreateBlogComment
  no-loop
  activation-group "permissions"
when
  check: PermissionCheck(name == "blog", action == "createComment", granted == false)
  Role(name == "user")
then
  check.grant();
end

rule CreateBlog
  no-loop
  activation-group "permissions"
when
  check: PermissionCheck(name == "blog", action == "create", granted == false)
  member: Member()
  not Member(mbrId : memberId -> (!mbrId.equals(member.getMemberId())))
then
  check.grant();
end

rule InsertMemberBlog
  no-loop
  activation-group "permissions"  
when
  check: PermissionCheck(name == "memberBlog", action == "insert", granted == false)
  MemberBlog(member : member)
  not Member(mbrId : memberId -> (!mbrId.equals(member.getMemberId())))
then
  check.grant();
end

rule CreateFriendComment
  no-loop
  activation-group "permissions"
when
  check: PermissionCheck(name == "friendComment", action == "create", granted == false)
  MemberFriend(authorized == true, f : friend)
  not Member(mbrId : memberId -> (!mbrId.equals(f.getMemberId())))
then
  check.grant();
end

rule CreateFriendRequest
  no-loop
  activation-group "permissions"
when
  check: PermissionCheck(name == "friendRequest", action == "create", granted == false)
  member: Member()
  not MemberFriend(f : friend -> (f.getMemberId().equals(member.getMemberId())))
then
  check.grant();
end
