|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace ScrewTurn.Wiki.AclEngine { |
| 7 | + |
| 8 | + /// <summary> |
| 9 | + /// Implements tools for evaluating permissions. |
| 10 | + /// </summary> |
| 11 | + public static class AclEvaluator { |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Decides whether a user, member of some groups, is authorized to perform an action on a resource. |
| 15 | + /// </summary> |
| 16 | + /// <param name="resource">The resource.</param> |
| 17 | + /// <param name="action">The action on the resource.</param> |
| 18 | + /// <param name="user">The user, in the form 'U.Name'.</param> |
| 19 | + /// <param name="groups">The groups the user is member of, in the form 'G.Name'.</param> |
| 20 | + /// <param name="entries">The available ACL entries for the resource.</param> |
| 21 | + /// <returns>The positive, negative, or indeterminate result.</returns> |
| 22 | + /// <exception cref="ArgumentNullException">If <paramref name="resource"/>, <paramref name="action"/>, <paramref name="user"/>, <paramref name="groups"/> or <paramref name="entries"/> are <c>null</c>.</exception> |
| 23 | + /// <exception cref="ArgumentException">If <paramref name="resource"/>, <paramref name="action"/>, <paramref name="user"/> are empty, or if <paramref name="action"/> equals <see cref="AclEntry.FullControlAction"/>.</exception> |
| 24 | + public static Authorization AuthorizeAction(string resource, string action, string user, string[] groups, AclEntry[] entries) { |
| 25 | + if(resource == null) throw new ArgumentNullException("resource"); |
| 26 | + if(resource.Length == 0) throw new ArgumentException("Resource cannot be empty", "resource"); |
| 27 | + if(action == null) throw new ArgumentNullException("action"); |
| 28 | + if(action.Length == 0) throw new ArgumentException("Action cannot be empty", "action"); |
| 29 | + if(action == AclEntry.FullControlAction) throw new ArgumentException("Action cannot be the FullControl flag", "action"); |
| 30 | + if(user == null) throw new ArgumentNullException("user"); |
| 31 | + if(user.Length == 0) throw new ArgumentException("User cannot be empty", "user"); |
| 32 | + if(groups == null) throw new ArgumentNullException("groups"); |
| 33 | + if(entries == null) throw new ArgumentNullException("entries"); |
| 34 | + |
| 35 | + // Simple ACL model |
| 36 | + // Sort entries so that FullControl ones are at the bottom |
| 37 | + // First look for an entry specific for the user |
| 38 | + // If not found, look for a group that denies the permission |
| 39 | + |
| 40 | + AclEntry[] sortedEntries = new AclEntry[entries.Length]; |
| 41 | + Array.Copy(entries, sortedEntries, entries.Length); |
| 42 | + |
| 43 | + Array.Sort(sortedEntries, delegate(AclEntry x, AclEntry y) { |
| 44 | + return x.Action.CompareTo(y.Action); |
| 45 | + }); |
| 46 | + Array.Reverse(sortedEntries); |
| 47 | + |
| 48 | + foreach(AclEntry entry in sortedEntries) { |
| 49 | + if(entry.Resource == resource && (entry.Action == action || entry.Action == AclEntry.FullControlAction) && entry.Subject == user) { |
| 50 | + if(entry.Value == Value.Grant) return Authorization.Granted; |
| 51 | + else if(entry.Value == Value.Deny) return Authorization.Denied; |
| 52 | + else throw new NotSupportedException("Entry value not supported"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // For each group, a decision is made |
| 57 | + Dictionary<string, bool> groupFullControlGrant = new Dictionary<string, bool>(); |
| 58 | + Dictionary<string, bool> groupExplicitGrant = new Dictionary<string, bool>(); |
| 59 | + Dictionary<string, bool> groupFullControlDeny = new Dictionary<string, bool>(); |
| 60 | + |
| 61 | + foreach(string group in groups) { |
| 62 | + foreach(AclEntry entry in entries) { |
| 63 | + |
| 64 | + if(entry.Resource == resource && entry.Subject == group) { |
| 65 | + if(!groupFullControlGrant.ContainsKey(group)) { |
| 66 | + groupFullControlGrant.Add(group, false); |
| 67 | + groupExplicitGrant.Add(group, false); |
| 68 | + groupFullControlDeny.Add(group, false); |
| 69 | + } |
| 70 | + |
| 71 | + if(entry.Action == action) { |
| 72 | + // Explicit action |
| 73 | + if(entry.Value == Value.Grant) { |
| 74 | + // An explicit grant only wins if there are no other explicit deny |
| 75 | + groupExplicitGrant[group] = true; |
| 76 | + } |
| 77 | + else if(entry.Value == Value.Deny) { |
| 78 | + // An explicit deny wins over all other entries |
| 79 | + return Authorization.Denied; |
| 80 | + } |
| 81 | + } |
| 82 | + else if(entry.Action == AclEntry.FullControlAction) { |
| 83 | + // Full control, lower priority |
| 84 | + if(entry.Value == Value.Deny) { |
| 85 | + groupFullControlDeny[group] = true; |
| 86 | + } |
| 87 | + else if(entry.Value == Value.Grant) { |
| 88 | + groupFullControlGrant[group] = true; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Any explicit grant found at this step wins, because all explicit deny have been processed previously |
| 96 | + bool tentativeGrant = false; |
| 97 | + bool tentativeDeny = false; |
| 98 | + foreach(string group in groupFullControlGrant.Keys) { |
| 99 | + if(groupExplicitGrant[group]) return Authorization.Granted; |
| 100 | + |
| 101 | + if(groupFullControlGrant[group] && !groupFullControlDeny[group]) tentativeGrant = true; |
| 102 | + if(!groupFullControlGrant[group] && groupFullControlDeny[group]) tentativeDeny = true; |
| 103 | + } |
| 104 | + if(tentativeGrant && !tentativeDeny) return Authorization.Granted; |
| 105 | + else if(tentativeDeny) return Authorization.Denied; |
| 106 | + else return Authorization.Unknown; |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Lists legal authorization values. |
| 113 | + /// </summary> |
| 114 | + public enum Authorization { |
| 115 | + /// <summary> |
| 116 | + /// Authorization granted. |
| 117 | + /// </summary> |
| 118 | + Granted, |
| 119 | + /// <summary> |
| 120 | + /// Authorization denied. |
| 121 | + /// </summary> |
| 122 | + Denied, |
| 123 | + /// <summary> |
| 124 | + /// No information available. |
| 125 | + /// </summary> |
| 126 | + Unknown |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments