/// <summary> /// Checks if the specified permission is "implied" by /// this object. /// <P> /// More specifically, this method returns true if: /// <ul> /// <li> <i>p</i>'s class is the same as this object's class, and /// <li> <i>p</i>'s name equals or (in the case of wildcards) /// is implied by this object's /// name. For example, "a.b.*" implies "a.b.c". /// </ul> /// </summary> /// <param name="p"> the permission to check against. /// </param> /// <returns> true if the passed permission is equal to or /// implied by this permission, false otherwise. </returns> public override bool Implies(Permission p) { if ((p == null) || (p.GetType() != this.GetType())) { return(false); } BasicPermission that = (BasicPermission)p; if (this.Wildcard) { if (that.Wildcard) { // one wildcard can imply another return(that.Path.StartsWith(Path)); } else { // make sure ap.path is longer so a.b.* doesn't imply a.b return((that.Path.Length() > this.Path.Length()) && that.Path.StartsWith(this.Path)); } } else { if (that.Wildcard) { // a non-wildcard can't imply a wildcard return(false); } else { return(this.Path.Equals(that.Path)); } } }
/// <summary> /// Checks two BasicPermission objects for equality. /// Checks that <i>obj</i>'s class is the same as this object's class /// and has the same name as this object. /// <P> </summary> /// <param name="obj"> the object we are testing for equality with this object. </param> /// <returns> true if <i>obj</i>'s class is the same as this object's class /// and has the same name as this BasicPermission object, false otherwise. </returns> public override bool Equals(Object obj) { if (obj == this) { return(true); } if ((obj == null) || (obj.GetType() != this.GetType())) { return(false); } BasicPermission bp = (BasicPermission)obj; return(Name.Equals(bp.Name)); }
/// <summary> /// Adds a permission to the BasicPermissions. The key for the hash is /// permission.path. /// </summary> /// <param name="permission"> the Permission object to add. /// </param> /// <exception cref="IllegalArgumentException"> - if the permission is not a /// BasicPermission, or if /// the permission is not of the /// same Class as the other /// permissions in this collection. /// </exception> /// <exception cref="SecurityException"> - if this BasicPermissionCollection object /// has been marked readonly </exception> public override void Add(Permission permission) { if (!(permission is BasicPermission)) { throw new IllegalArgumentException("invalid permission: " + permission); } if (ReadOnly) { throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection"); } BasicPermission bp = (BasicPermission)permission; // make sure we only add new BasicPermissions of the same class // Also check null for compatibility with deserialized form from // previous versions. if (PermClass == null) { // adding first permission PermClass = bp.GetType(); } else { if (bp.GetType() != PermClass) { throw new IllegalArgumentException("invalid permission: " + permission); } } lock (this) { Perms[bp.CanonicalName] = permission; } // No sync on all_allowed; staleness OK if (!All_allowed) { if (bp.CanonicalName.Equals("*")) { All_allowed = true; } } }
/// <summary> /// Check and see if this set of permissions implies the permissions /// expressed in "permission". /// </summary> /// <param name="permission"> the Permission object to compare /// </param> /// <returns> true if "permission" is a proper subset of a permission in /// the set, false if not. </returns> public override bool Implies(Permission permission) { if (!(permission is BasicPermission)) { return(false); } BasicPermission bp = (BasicPermission)permission; // random subclasses of BasicPermission do not imply each other if (bp.GetType() != PermClass) { return(false); } // short circuit if the "*" Permission was added if (All_allowed) { return(true); } // strategy: // Check for full match first. Then work our way up the // path looking for matches on a.b..* String path = bp.CanonicalName; //System.out.println("check "+path); Permission x; lock (this) { x = Perms[path]; } if (x != null) { // we have a direct hit! return(x.Implies(permission)); } // work our way up the tree... int last, offset; offset = path.Length() - 1; while ((last = path.LastIndexOf(".", offset)) != -1) { path = path.Substring(0, last + 1) + "*"; //System.out.println("check "+path); lock (this) { x = Perms[path]; } if (x != null) { return(x.Implies(permission)); } offset = last - 1; } // we don't have to check for "*" as it was already checked // at the top (all_allowed), so we just return false return(false); }