Beispiel #1
0
        /// <summary>
        /// Creates a new Resource.File for a specified resource of type collectionName in the repository.
        /// </summary>
        /// <param name="collectionName">The resource type.</param>
        /// <param name="mimeType">The MIME type of media.</param>
        /// <param name="media">The new File contents.</param>
        /// <param name="fileExtension">The media file extension.</param>
        /// <returns>A SyndicationItem that describes the newly created resource.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty
        /// or mimeType is null/empty or media is null.</exception>
        protected SyndicationItem CreateMedia(string collectionName, string mimeType, byte[] media,
                                              string fileExtension)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (string.IsNullOrEmpty(mimeType))
            {
                throw new ArgumentNullException("mimeType");
            }

            if (null == media)
            {
                throw new ArgumentNullException("media");
            }

            AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken();

            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                if (!authenticatedToken.HasCreatePermission(context))
                {
                    throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED);
                }

                ScholarlyWork resource = CreateScholarlyWork(collectionName);
                resource.DateModified = DateTime.Now;

                Core.File mediaResource = new Core.File();
                mediaResource.MimeType      = mimeType;
                mediaResource.FileExtension = string.IsNullOrEmpty(fileExtension) ?
                                              AtomPubHelper.GetFileExtension(mimeType) : fileExtension;
                context.AddToResources(mediaResource);
                context.SaveChanges();
                resource.Files.Add(mediaResource);

                MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media);
                context.UploadFileContent(mediaResource, mediaStream);
                mediaStream.Close();

                resource.GrantDefaultPermissions(context, authenticatedToken);
                mediaResource.GrantDefaultPermissions(context, authenticatedToken);

                context.SaveChanges();

                return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates 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 CreateUser(ZentityUser user, AuthenticatedToken token, ZentityContext context)
        {
            #region Parameter Validation
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            ValidateParameters(context, token);
            #endregion

            context.MetadataWorkspace.LoadFromAssembly(typeof(Identity).Assembly);

            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            if (context.Resources.OfType <Identity>().Where(res => res.IdentityName == user.LogOnName).Count() == 1)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, ConstantStrings.IdentityExists, user.LogOnName));
            }

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

            Identity identity = new Identity();
            identity.Title        = user.Profile.FirstName + " " + user.Profile.LastName;
            identity.IdentityName = user.LogOnName;
            context.AddToResources(identity);
            return(context.SaveChanges() == 0 ? false : true);
        }
Beispiel #3
0
        /// <summary>
        /// Removes the identity from group.
        /// </summary>
        /// <param name="identity">The identity.</param>
        /// <param name="group">The group.</param>
        /// <param name="authenticatedToken">The authenticated token.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.Boolean; true if successful, false otherwise.</returns>
        private static bool RemoveIdentityFromGroup(
            Identity identity,
            Group group,
            AuthenticatedToken authenticatedToken,
            ZentityContext context)
        {
            if (!DataAccess.IsAdmin(authenticatedToken.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            Identity originalIdentity = GetIdentity(identity.Id, context);
            Group    originalGroup    = GetGroup(group.Id, context);

            if (originalIdentity == null || originalGroup == null)
            {
                throw new ArgumentException(ConstantStrings.InvalidIdentityOrGroup);
            }

            originalIdentity.Groups.Load();
            if (!originalIdentity.Groups.Contains(originalGroup))
            {
                return(true);
            }

            if (group.GroupName == AdminGroupName)
            {
                ZentityUserAdmin admin = new ZentityUserAdmin(authenticatedToken);
                admin.UnsetAdmin(identity.IdentityName);
            }

            originalIdentity.Groups.Remove(originalGroup);
            return(context.SaveChanges() == 0 ? false : true);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new resource of type collectionName in the repository.
        /// </summary>
        /// <param name="collectionName">The resource type.</param>
        /// <param name="atomEntry">Information about the resource.</param>
        /// <returns>A SyndicationItem that describes the newly created resource.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty
        /// or atomEntry is null/empty .</exception>
        SyndicationItem IAtomPubStoreWriter.CreateMember(string collectionName, AtomEntryDocument atomEntry)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (null == atomEntry)
            {
                throw new ArgumentNullException("atomEntry");
            }

            AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken();

            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                if (!authenticatedToken.HasCreatePermission(context))
                {
                    throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED);
                }

                ScholarlyWork resource = CreateScholarlyWork(collectionName);
                context.AddToResources(resource);
                ZentityAtomPubStoreWriter.UpdateResourceProperty(context, resource, atomEntry);

                resource.GrantDefaultPermissions(context, authenticatedToken);

                context.SaveChanges();

                return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Updates the metadata for the specified resource.
        /// </summary>
        /// <param name="collectionName">Name of the target collection.</param>
        /// <param name="memberResourceId">The Id of the member resource.</param>
        /// <param name="atomEntry">Describes the resource to be modified.</param>
        /// <returns>A SyndicationItem that describes the updated resource.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty
        /// or atomEntry is null.</exception>
        /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception>
        SyndicationItem IAtomPubStoreWriter.UpdateMemberInfo(string collectionName, string memberResourceId, AtomEntryDocument atomEntry)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (string.IsNullOrEmpty(memberResourceId))
            {
                throw new ArgumentNullException("memberResourceId");
            }

            if (null == atomEntry)
            {
                throw new ArgumentNullException("atomEntry");
            }

            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                ScholarlyWork resource = (ScholarlyWork)AtomPubHelper.GetMember(context, collectionName, memberResourceId, "Update");
                resource.Files.Load();

                // Bug Fix : 177689 - AtomPub (M2): Author node is appending after every PUT request instead of overwriting it.
                resource.Authors.Load();
                resource.Contributors.Load();

                UpdateResourceProperty(context, resource, atomEntry);
                context.SaveChanges();

                return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource));
            }
        }
 /// <summary>
 /// Adds the file resource.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="resource">The resource.</param>
 /// <returns>The added <see cref="Core.File"/> type.</returns>
 private static Core.File AddFileResource(ZentityContext context, ScholarlyWork resource)
 {
     Core.File mediaResource = new Core.File();
     context.AddToResources(mediaResource);
     context.SaveChanges();
     resource.Files.Add(mediaResource);
     return(mediaResource);
 }
Beispiel #7
0
        /// <summary>
        /// Updates the Resource.File of the specified resource.
        /// </summary>
        /// <param name="collectionName">The type of the resource.</param>
        /// <param name="memberResourceId">The resource whose File needs to be updated.</param>
        /// <param name="mimeType">The MIME type of media.</param>
        /// <param name="media">The new File contents.</param>
        /// <returns>A SyndicationItem that describes the updated resource.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty
        /// or media is null.</exception>
        /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception>
        SyndicationItem IAtomPubStoreWriter.UpdateMedia(string collectionName, string memberResourceId, string mimeType, byte[] media)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (null == media)
            {
                throw new ArgumentNullException("media");
            }

            if (string.IsNullOrEmpty(memberResourceId))
            {
                throw new ArgumentNullException("memberResourceId");
            }

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

            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                Type collectionType = CoreHelper.GetSystemResourceType(collectionName);
                // Prepare a query to get a resource with specified Id and specified type.
                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 mediaResource = query.FirstOrDefault();

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

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

                mediaResource.Resources.Load();
                ScholarlyWork resource = (ScholarlyWork)mediaResource.Resources.First();
                resource.DateModified       = DateTime.Now;
                mediaResource.MimeType      = mimeType;
                mediaResource.FileExtension = AtomPubHelper.GetFileExtension(mimeType);

                MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media);
                context.UploadFileContent(mediaResource, mediaStream);

                // Bug Fix : 180811 - Save Changes once mime type and contents are set.
                context.SaveChanges();

                return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource));
            }
        }
Beispiel #8
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);
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Updates 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 UpdateGroup(Group group, AuthenticatedToken token, ZentityContext context)
        {
            context.MetadataWorkspace.LoadFromAssembly(typeof(Group).Assembly);

            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

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

            if (originalGroup == null)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentUICulture,
                                                ConstantStrings.GroupDoesNotExist,
                                                group.GroupName));
            }

            if (group.GroupName != originalGroup.GroupName)
            {
                if (context.Resources.OfType <Group>()
                    .Where(res => res.GroupName == group.GroupName).Count() > 0)
                {
                    throw new ArgumentException(string.Format(
                                                    CultureInfo.CurrentUICulture,
                                                    ConstantStrings.GroupUpdateException,
                                                    group.GroupName));
                }
            }

            originalGroup.GroupName   = group.GroupName;
            originalGroup.Title       = group.Title;
            originalGroup.Description = group.Description;
            originalGroup.Uri         = group.Uri;

            return(context.SaveChanges() == 0 ? false : true);
        }
Beispiel #11
0
        /// <summary>
        /// Creates 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 CreateGroup(Group group, AuthenticatedToken token, ZentityContext context)
        {
            #region Parameter Validation
            ValidateGroup(group);
            ValidateParameters(context, token);
            #endregion

            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            Group existingGroup = context.Resources.OfType <Group>()
                                  .Where(res => res.GroupName == group.GroupName).FirstOrDefault();

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

            context.AddToResources(group);
            return(context.SaveChanges() == 0 ? false : true);
        }
Beispiel #12
0
        /// <summary>
        /// Updates the identity.
        /// </summary>
        /// <param name="identity">The identity.</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 UpdateIdentity(Identity identity, AuthenticatedToken token, ZentityContext context)
        {
            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            Identity originalIdentity = GetIdentity(identity.Id, context);

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

            originalIdentity.Title       = identity.Title;
            originalIdentity.Description = identity.Description;
            originalIdentity.Uri         = identity.Uri;

            return(context.SaveChanges() == 0 ? false : true);
        }
Beispiel #13
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);
        }
Beispiel #14
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);
            }
        }
        /// <summary>
        /// Creates a new Resource.File for a specified resource of type collectionName in the repository.
        /// </summary>
        /// <param name="collectionName">The resource type.</param>
        /// <param name="mimeType">The MIME type of media.</param>
        /// <param name="media">The new File contents.</param>
        /// <param name="fileExtension">The media file extension.</param>
        /// <returns>A SyndicationItem that describes the newly created resource.</returns>
        SyndicationItem IAtomPubStoreWriter.CreateMedia(string collectionName, string mimeType,
                                                        byte[] media, string fileExtension)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (string.IsNullOrEmpty(mimeType))
            {
                throw new ArgumentNullException("mimeType");
            }

            if (null == media)
            {
                throw new ArgumentNullException("media");
            }

            if (SwordConstants.ZipContentType != mimeType)
            {
                return(base.CreateMedia(collectionName, mimeType, media, fileExtension));
            }

            // Convert byte array to stream.
            MemoryStream mediaStream    = ZentityAtomPubStoreWriter.GetMediaStream(media);
            string       extractionPath = ExtractZipContent(mediaStream);

            HttpContext.Current.Items[SwordConstants.ZipExtractedPath] = extractionPath;

            // Get the path of METS xml file.
            string metsFilePath = extractionPath + "\\" + SwordConstants.MetsDocumentName;

            if (!System.IO.File.Exists(metsFilePath))
            {
                //string errorMessage = string.Format(CultureInfo.CurrentCulture,
                //                                    Properties.Resources.SWORD_MISSING_METS_DOCUMENT,
                //                                    SwordConstants.MetsDocumentName);
                //throw new MetsException(errorMessage);
                return(base.CreateMedia(collectionName, mimeType, media, fileExtension));
            }

            AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken();

            using (ZentityContext zentityContext = CoreHelper.CreateZentityContext())
            {
                if (!authenticatedToken.HasCreatePermission(zentityContext))
                {
                    throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED);
                }

                // Generate METS document from given METS xml file.
                MetsDocument document = new MetsDocument(metsFilePath);

                // Create resource of specified collection type.
                ScholarlyWork resource = CreateScholarlyWork(collectionName);
                resource.DateModified = DateTime.Now;

                // Upload the zip file contents as media for main resource.
                // This will be required in AtomPub get requests and further use.
                Core.File mediaResource = AddFileResource(zentityContext, resource, mediaStream);
                mediaResource.MimeType      = mimeType;
                mediaResource.FileExtension = AtomPubHelper.GetFileExtension(mimeType);
                // close the stream
                mediaStream.Close();

                AddChildResources(extractionPath, document, resource, zentityContext);

                resource.GrantDefaultPermissions(zentityContext, authenticatedToken);
                mediaResource.GrantDefaultPermissions(zentityContext, authenticatedToken);

                // Save all changes at the end
                zentityContext.SaveChanges();

                return(ZentityAtomPubStoreReader.GenerateSyndicationItem(base.BaseUri, resource));
            }
        }