| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.acls.domain; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.AccessDeniedException; |
| 19 |
|
import org.acegisecurity.Authentication; |
| 20 |
|
import org.acegisecurity.GrantedAuthority; |
| 21 |
|
|
| 22 |
|
import org.acegisecurity.acls.Acl; |
| 23 |
|
import org.acegisecurity.acls.Permission; |
| 24 |
|
import org.acegisecurity.acls.sid.PrincipalSid; |
| 25 |
|
import org.acegisecurity.acls.sid.Sid; |
| 26 |
|
import org.acegisecurity.acls.sid.SidRetrievalStrategy; |
| 27 |
|
import org.acegisecurity.acls.sid.SidRetrievalStrategyImpl; |
| 28 |
|
|
| 29 |
|
import org.acegisecurity.context.SecurityContextHolder; |
| 30 |
|
|
| 31 |
|
import org.springframework.util.Assert; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
public class AclAuthorizationStrategyImpl implements AclAuthorizationStrategy { |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
private GrantedAuthority gaGeneralChanges; |
| 47 |
|
private GrantedAuthority gaModifyAuditing; |
| 48 |
|
private GrantedAuthority gaTakeOwnership; |
| 49 |
1 |
private SidRetrievalStrategy sidRetrievalStrategy = new SidRetrievalStrategyImpl(); |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
1 |
public AclAuthorizationStrategyImpl(GrantedAuthority[] auths) { |
| 63 |
1 |
Assert.notEmpty(auths, "GrantedAuthority[] with three elements required"); |
| 64 |
1 |
Assert.isTrue(auths.length == 3, "GrantedAuthority[] with three elements required"); |
| 65 |
1 |
this.gaTakeOwnership = auths[0]; |
| 66 |
1 |
this.gaModifyAuditing = auths[1]; |
| 67 |
1 |
this.gaGeneralChanges = auths[2]; |
| 68 |
1 |
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
public void securityCheck(Acl acl, int changeType) { |
| 73 |
10 |
if ((SecurityContextHolder.getContext() == null) |
| 74 |
|
|| (SecurityContextHolder.getContext().getAuthentication() == null) |
| 75 |
|
|| !SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) { |
| 76 |
0 |
throw new AccessDeniedException("Authenticated principal required to operate with ACLs"); |
| 77 |
|
} |
| 78 |
|
|
| 79 |
10 |
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 80 |
|
|
| 81 |
|
|
| 82 |
10 |
Sid currentUser = new PrincipalSid(authentication); |
| 83 |
|
|
| 84 |
10 |
if (currentUser.equals(acl.getOwner()) |
| 85 |
|
&& ((changeType == CHANGE_GENERAL) || (changeType == CHANGE_OWNERSHIP))) { |
| 86 |
10 |
return; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
0 |
GrantedAuthority requiredAuthority = null; |
| 91 |
|
|
| 92 |
0 |
if (changeType == CHANGE_AUDITING) { |
| 93 |
0 |
requiredAuthority = this.gaModifyAuditing; |
| 94 |
0 |
} else if (changeType == CHANGE_GENERAL) { |
| 95 |
0 |
requiredAuthority = this.gaGeneralChanges; |
| 96 |
0 |
} else if (changeType == CHANGE_OWNERSHIP) { |
| 97 |
0 |
requiredAuthority = this.gaTakeOwnership; |
| 98 |
|
} else { |
| 99 |
0 |
throw new IllegalArgumentException("Unknown change type"); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
|
| 103 |
0 |
GrantedAuthority[] auths = authentication.getAuthorities(); |
| 104 |
|
|
| 105 |
0 |
for (int i = 0; i < auths.length; i++) { |
| 106 |
0 |
if (requiredAuthority.equals(auths[i])) { |
| 107 |
0 |
return; |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
0 |
Sid[] sids = sidRetrievalStrategy.getSids(authentication); |
| 113 |
|
|
| 114 |
0 |
if (acl.isGranted(new Permission[] {BasePermission.ADMINISTRATION}, sids, false)) { |
| 115 |
0 |
return; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
0 |
throw new AccessDeniedException( |
| 119 |
|
"Principal does not have required ACL permissions to perform requested operation"); |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
public void setSidRetrievalStrategy(SidRetrievalStrategy sidRetrievalStrategy) { |
| 123 |
0 |
Assert.notNull(sidRetrievalStrategy, "SidRetrievalStrategy required"); |
| 124 |
0 |
this.sidRetrievalStrategy = sidRetrievalStrategy; |
| 125 |
0 |
} |
| 126 |
|
} |