/// <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); }
/// <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)); } }
/// <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); }
/// <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)); } }
/// <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); }