예제 #1
0
        /// <summary>
        /// This method grants authorization for repository level (non-resource-specific) predicates, e.g. Create.
        /// </summary>
        /// <param name="authorizingPredicateUri">Uri for a non-resource specific predicate, e.g. Create</param>
        /// <param name="context">ZentityContext object</param>
        /// <returns>True if the current identity instance has authorization for the given predicate, false otherwise.</returns>
        public bool GrantAuthorization(string authorizingPredicateUri, ZentityContext context)
        {
            #region Parameter Validation
            ValidateParameters(authorizingPredicateUri, context);
            #endregion
            try
            {
                Relationship existingRelationship = context.Relationships.Where(rel =>
                                                                                rel.Subject.Id == this.Id &&
                                                                                rel.Object.Id == this.Id &&
                                                                                rel.Predicate.Uri.Equals(authorizingPredicateUri, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                if (existingRelationship == null)
                {
                    // Check if relationship is present in context in added state
                    foreach (ObjectStateEntry objectStateEntry in
                             context.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
                    {
                        existingRelationship = objectStateEntry.Entity as Relationship;

                        //// Check if relationship is same on which Grant is requested
                        if (existingRelationship != null &&
                            (existingRelationship.Subject.Id == this.Id &&
                             existingRelationship.Object.Id == this.Id &&
                             existingRelationship.Predicate.Uri.Equals(authorizingPredicateUri, StringComparison.OrdinalIgnoreCase)))
                        {
                            return(true);
                        }
                    }

                    Relationship relationship = new Relationship();
                    relationship.Subject   = this;
                    relationship.Object    = this;
                    relationship.Predicate = context.Predicates.Where(s => s.Uri == authorizingPredicateUri).First <Predicate>();

                    context.AddToRelationships(relationship);
                }
                else
                {
                    // Check if relationship is present in context in added, modified or deleted state
                    foreach (ObjectStateEntry objectStateEntry in
                             context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted
                                                                              | EntityState.Modified))
                    {
                        Relationship relationshipEntity = objectStateEntry.Entity as Relationship;

                        if (relationshipEntity != null &&
                            relationshipEntity.Id == existingRelationship.Id)
                        {
                            //// If its in deleted state, make it in unchanged
                            if (objectStateEntry.State == EntityState.Deleted)
                            {
                                objectStateEntry.AcceptChanges();
                            }
                            else
                            {
                                //// If modified, then add a new relationship
                                Relationship relationship = new Relationship();
                                relationship.Subject   = this;
                                relationship.Object    = this;
                                relationship.Predicate = context.Predicates.Where(s => s.Uri == authorizingPredicateUri).First <Predicate>();

                                context.AddToRelationships(relationship);
                            }

                            break;
                        }
                    }
                }

                return(true);
            }
            catch (Exception exception)
            {
                throw new AuthorizationException(Resources.GrantAuthorizationException, exception);
            }
        }