Exemplo n.º 1
0
        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="token">The token.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.Boolean; true if successful, false otherwise.</returns>
        private static bool DeleteUser(ZentityUser user, AuthenticatedToken token, ZentityContext context)
        {
            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            Identity identity = GetIdentity(user.LogOnName, context);

            if (identity == null)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentUICulture,
                                                ConstantStrings.IdentityDoesNotExist,
                                                identity.IdentityName));
            }

            // Remove relationships
            identity.RelationshipsAsObject.Load();
            identity.RelationshipsAsSubject.Load();
            foreach (Relationship relationship in identity.RelationshipsAsObject.Union(
                         identity.RelationshipsAsSubject).ToList())
            {
                context.DeleteObject(relationship);
            }

            // Remove identity
            context.DeleteObject(identity);

            if (context.SaveChanges() == 0)
            {
                return(false);
            }

            if (!user.Unregister())
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deletes the group.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="token">The token.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.Boolean; true if successful, false otherwise.</returns>
        private static bool DeleteGroup(Group group, AuthenticatedToken token, ZentityContext context)
        {
            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            Group existingGroup = GetGroup(group.Id, context);

            if (existingGroup == null)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentUICulture,
                                                ConstantStrings.IdentityDoesNotExist,
                                                existingGroup.GroupName));
            }

            existingGroup.RelationshipsAsObject.Load();
            existingGroup.RelationshipsAsSubject.Load();

            List <Relationship> relationships = existingGroup.RelationshipsAsSubject.ToList();

            foreach (Relationship relationship in relationships)
            {
                context.DeleteObject(relationship);
            }

            relationships = existingGroup.RelationshipsAsObject.ToList();
            foreach (Relationship relationship in relationships)
            {
                context.DeleteObject(relationship);
            }

            context.DeleteObject(existingGroup);
            return(context.SaveChanges() == 0 ? false : true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deletes the specified resource.
        /// </summary>
        /// <param name="collectionName">The type of the resource.</param>
        /// <param name="memberResourceId">The Guid of the resource.</param>
        /// <returns>True if the operation succeeds, False otherwise.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty.</exception>
        /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception>
        bool IAtomPubStoreWriter.DeleteMember(string collectionName, string memberResourceId)
        {
            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                ScholarlyWork resource = (ScholarlyWork)AtomPubHelper.GetMember(context, collectionName, memberResourceId, "Delete");

                // Load to delete all Core.File resource associated to requested scholarlywork.
                if (!resource.Files.IsLoaded)
                {
                    resource.Files.Load();
                }

                Zentity.Core.File[] resourceFiles = resource.Files.ToArray();

                for (int i = 0; i < resourceFiles.Length; i++)
                {
                    DeleteRelationships(context, resourceFiles[i]);
                    context.DeleteObject(resourceFiles[i]);
                }

                DeleteRelationships(context, resource);

                // Delete associated Resource propertes
                resource.ResourceProperties.Load();
                List <ResourceProperty> resProperties = resource.ResourceProperties.ToList();
                foreach (ResourceProperty property in resProperties)
                {
                    resource.ResourceProperties.Remove(property);
                    context.DeleteObject(property);
                }

                context.DeleteObject(resource);
                context.SaveChanges();
                return(true);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Revokes permission on resource for the current identity.
        /// </summary>
        /// <typeparam name="T">Zentity.Core.Resource or its derived type</typeparam>
        /// <param name="authorizingPredicateUri">
        /// Predicate corresponding to the permission.
        /// </param>
        /// <param name="resource">
        /// Resource on which the permission is to be granted.
        /// </param>
        /// <param name="context">
        /// Zentity Object Context.
        /// </param>
        /// <returns>
        /// Boolean indicating whether the permission was successfully revoked or not.
        /// </returns>
        /// <exception cref="AuthorizationException">Exception to indicate invalid information or other general exception.</exception>
        /// <example>
        /// <code>
        /// string authorizingPredicate = "urn:zentity/module/zentity-authorization/predicate/has-read-access";
        /// using (ZentityContext context = new ZentityContext())
        /// {
        ///     Identity identity = context.Resources.OfType&lt;Identity&gt;()
        ///                .Where(tuple => tuple.IdentityName.Equals("User1")).First();
        ///     Resource resource = context.Resources.Where(tuple => tuple.Title.Equals("Resource1")).First();
        ///
        ///     identity.RevokeAuthorization(authorizingPredicate, resource, context);
        ///     context.SaveChanges();
        /// }
        /// </code>
        /// </example>
        public bool RevokeAuthorization <T>(string authorizingPredicateUri, T resource, ZentityContext context) where T : Resource
        {
            #region Parameter Validation
            ValidateParameters(authorizingPredicateUri, context);
            if (resource == null)
            {
                throw new ArgumentNullException("resource");
            }
            #endregion
            try
            {
                Relationship relationship =
                    context.Relationships.Where(rel => rel.Subject.Id == this.Id && rel.Object.Id == resource.Id &&
                                                rel.Predicate.Uri == authorizingPredicateUri).FirstOrDefault();
                if (relationship != null)
                {
                    context.DeleteObject(relationship);
                }
                else
                {
                    // Check if relationship is present in context in added, modified or deleted state
                    foreach (ObjectStateEntry objectStateEntry in
                             context.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
                    {
                        Relationship 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 == resource.Id &&
                             existingRelationship.Predicate.Uri.Equals(authorizingPredicateUri, StringComparison.OrdinalIgnoreCase)))
                        {
                            objectStateEntry.Delete();
                            break;
                        }
                    }
                }

                return(true);
            }
            catch (Exception exception)
            {
                throw new AuthorizationException(Resources.RevokeAuthorizationException, exception);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Deletes the relationships.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="resource">The resource.</param>
        private static void DeleteRelationships(ZentityContext context, Resource resource)
        {
            if (!resource.RelationshipsAsObject.IsLoaded)
            {
                resource.RelationshipsAsObject.Load();
            }

            if (!resource.RelationshipsAsSubject.IsLoaded)
            {
                resource.RelationshipsAsSubject.Load();
            }

            Relationship[] relationships = resource.RelationshipsAsObject
                                           .Union(resource.RelationshipsAsSubject)
                                           .ToArray();

            for (int i = 0; i < relationships.Length; i++)
            {
                context.DeleteObject(relationships[i]);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Deletes the Resource.File for the specified resource.
        /// </summary>
        /// <param name="collectionName">The type of the resource.</param>
        /// <param name="memberResourceId">The Guid of the resource.</param>
        /// <returns>True if the operation succeeds, False otherwise.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty.</exception>
        /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception>
        bool IAtomPubStoreWriter.DeleteMedia(string collectionName, string memberResourceId)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (!AtomPubHelper.IsValidGuid(memberResourceId))
            {
                throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId");
            }

            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                Type   collectionType = CoreHelper.GetSystemResourceType(collectionName);
                string commandText    = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents,
                                                      collectionType.FullName);

                ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context);
                query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId)));

                Core.File mediaFile = query.FirstOrDefault();

                if (null == mediaFile)
                {
                    throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND);
                }

                if (!mediaFile.Authorize("Delete", context, CoreHelper.GetAuthenticationToken()))
                {
                    throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED);
                }

                DeleteRelationships(context, mediaFile);
                context.DeleteObject(mediaFile);
                context.SaveChanges();
                return(true);
            }
        }