/// <summary> /// Uploads the associated file to temporary container. /// </summary> /// <param name="fileDetail">Details of the associated file.</param> /// <returns>True if content is uploaded; otherwise false.</returns> public OperationStatus UploadTemporaryFile(FileDetail fileDetail) { OperationStatus operationStatus = null; // Make sure file detail is not null this.CheckNotNull(() => new { fileDetail }); var fileBlob = new BlobDetails() { BlobID = fileDetail.AzureID.ToString(), Data = fileDetail.DataStream, MimeType = fileDetail.MimeType }; try { _blobDataRepository.UploadTemporaryFile(fileBlob); operationStatus = OperationStatus.CreateSuccessStatus(); } catch (Exception) { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } return(operationStatus); }
/// <summary> /// Updates the all the entries for the given Content with all the details. /// </summary> /// <param name="contentId">Content ID.</param> /// <param name="details">Details provided.</param> /// <returns>True if content was updated; otherwise false.</returns> private OperationStatus UpdateAllOffensiveContentEntry(long contentId, OffensiveEntry details) { OperationStatus status = null; try { var offensiveContents = _offensiveContentRepository.GetItems(oc => oc.ContentID == contentId && oc.OffensiveStatusID == (int)OffensiveStatusType.Flagged, null, false); if (offensiveContents != null && offensiveContents.Any()) { foreach (var item in offensiveContents) { item.OffensiveStatusID = (int)details.Status; item.Justification = details.Justification; item.ReviewerID = details.ReviewerID; item.ReviewerDatetime = DateTime.UtcNow; _offensiveContentRepository.Update(item); } } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } // Status will be null if all sub communities and contents have been deleted. // If one them is not deleted then the status will have the exception details. status = status ?? OperationStatus.CreateSuccessStatus(); return(status); }
/// <summary> /// Checks the file from azure which is identified by file name. /// </summary> /// <param name="fileName"> /// file name. /// </param> /// <returns> /// Operation Status /// </returns> public OperationStatus CheckIfAssetExists(string fileName) { OperationStatus operationStatus = null; var fileBlob = new BlobDetails() { BlobID = fileName, }; try { if (_blobDataRepository.CheckIfAssetExists(fileBlob)) { operationStatus = OperationStatus.CreateSuccessStatus(); } else { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } } catch (Exception ex) { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage, ex); } return(operationStatus); }
public Task <OperationStatus> UpdateFeaturedCommunities(IEnumerable <AdminEntityDetails> communities, long userId, int?categoryId) { OperationStatus status = null; try { // TODO: To find a better way of updating and deleting the featured communities. if (_userRepository.IsSiteAdmin(userId)) { IEnumerable <FeaturedCommunities> results; if (categoryId.HasValue) { results = _featuredCommunitiesRepository.GetItems(fc => fc.CategoryID == categoryId, null, false); } else { results = _featuredCommunitiesRepository.GetItems(fc => int.Equals(fc.CategoryID, categoryId), null, false); } if (results != null && results.Any()) { foreach (var item in results) { _featuredCommunitiesRepository.Delete(item); } } if (communities != null && communities.Any()) { foreach (var item in communities) { var community = new FeaturedCommunities() { CategoryID = categoryId, CommunityID = item.EntityID, SortOrder = item.SortOrder, UpdatedByID = userId, UpdatedDatetime = DateTime.UtcNow }; _featuredCommunitiesRepository.Add(community); } } _featuredCommunitiesRepository.SaveChanges(); } else { status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } status = status ?? OperationStatus.CreateSuccessStatus(); return(Task.FromResult(status)); }
/// <summary> /// Uploads file to azure. /// </summary> /// <param name="fileDetails">Details of the file.</param> public OperationStatus UploadAsset(FileDetail fileDetails) { OperationStatus operationStatus = null; this.CheckNotNull(() => new { fileDetails }); var fileBlob = new BlobDetails() { BlobID = fileDetails.Name, Data = fileDetails.DataStream, MimeType = fileDetails.MimeType }; try { if (_blobDataRepository.UploadAsset(fileBlob)) { operationStatus = OperationStatus.CreateSuccessStatus(); } else { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } } catch (Exception) { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } return(operationStatus); }
public OperationStatus UpdateStaticContent(StaticContentDetails staticContentDetails) { OperationStatus status = null; try { Expression <Func <StaticContent, bool> > condition = content => content.TypeID == (int)staticContentDetails.TypeID && content.IsDeleted == false; var contentValue = _staticContentRepository.GetItem(condition); if (contentValue != null) { contentValue.Content = staticContentDetails.Content; contentValue.ModifiedByID = staticContentDetails.ModifiedByID; contentValue.ModifiedDatetime = DateTime.UtcNow; _staticContentRepository.Update(contentValue); _staticContentRepository.SaveChanges(); } else { status = OperationStatus.CreateFailureStatus(string.Format(CultureInfo.CurrentCulture, Resources.StaticContentTypeNotFound, staticContentDetails.TypeID)); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } status = status ?? OperationStatus.CreateSuccessStatus(); return(status); }
/// <summary> /// Move Home video from temp to correct container. /// </summary> /// <param name="fileDetails">Details of the video.</param> public OperationStatus MoveTempFile(FileDetail fileDetails) { OperationStatus operationStatus = null; this.CheckNotNull(() => new { fileDetails }); // Move Home video. if (fileDetails.AzureID != null) { // Move the video file from temporary container to file container. try { if (MoveAssetFile(fileDetails)) { operationStatus = OperationStatus.CreateSuccessStatus(); } else { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } } catch (Exception) { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } } return(operationStatus); }
public OperationStatus ReportOffensiveCommunity(ReportEntityDetails offensiveCommunityDetails) { OperationStatus status = null; this.CheckNotNull(() => new { reportEntityDetails = offensiveCommunityDetails }); try { var offensiveCommunity = new OffensiveCommunities(); Mapper.Map(offensiveCommunityDetails, offensiveCommunity); offensiveCommunity.ReportedDatetime = DateTime.UtcNow; _offensiveCommunitiesRepository.Add(offensiveCommunity); _offensiveCommunitiesRepository.SaveChanges(); } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } // Status will be null if all sub communities and contents have been deleted. // If one them is not deleted then the status will have the exception details. status = status ?? OperationStatus.CreateSuccessStatus(); return(status); }
private OperationStatus DeleteContentRecursively(long contentId, long profileId, bool isOffensive, OffensiveEntry offensiveDetails) { OperationStatus status = null; try { // Get the current content from DB. var content = _contentRepository.GetItem((c) => c.ContentID == contentId && c.IsDeleted == false); if (content != null) { var userRole = GetContentUserRole(content, profileId); if (CanEditDeleteContent(content, profileId, userRole)) { content.IsDeleted = true; content.IsOffensive = isOffensive; content.DeletedByID = profileId; content.DeletedDatetime = DateTime.UtcNow; // Update all the offensive entity entries if the content is being deleted. UpdateAllOffensiveContentEntry(content.ContentID, offensiveDetails); foreach (var contentRelation in content.ContentRelation) { contentRelation.Content1.IsDeleted = true; contentRelation.Content1.IsOffensive = isOffensive; contentRelation.Content1.DeletedByID = profileId; contentRelation.Content1.DeletedDatetime = DateTime.UtcNow; } _contentRepository.Update(content); // Save changes to the database _contentRepository.SaveChanges(); status = OperationStatus.CreateSuccessStatus(); } else { // In case if user is reader or visitor, he should not be allowed to delete the content. status = OperationStatus.CreateFailureStatus("User does not have permission for deleting the content."); } } else { status = OperationStatus.CreateFailureStatus(string.Format(CultureInfo.CurrentCulture, "Content with ID '{0}' was not found", contentId)); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } return(status); }
/// <summary> /// Approves or declines a permission request of a user for a community and adds the user role of the community /// to the user communities table. /// </summary> /// <param name="permissionItem">Permission item with details about the request</param> /// <param name="updatedById">User who is updating the permission request</param> public OperationStatus UpdateUserPermissionRequest(PermissionItem permissionItem, long updatedById) { var operationStatus = OperationStatus.CreateSuccessStatus(); var permissionRequest = EarthOnlineDbContext.PermissionRequest.Where((PermissionRequest pr) => pr.UserID == permissionItem.UserID && pr.CommunityID == permissionItem.CommunityID && pr.Approved == null).FirstOrDefault(); // Make sure permissionRequest is not null this.CheckNotNull(() => new { permissionRequest }); // Update the status of the permission request as approved or rejected. permissionRequest.Approved = permissionItem.Approved; permissionRequest.RespondedByID = updatedById; permissionRequest.RespondedDate = DateTime.UtcNow; // Check if any existing user community role is already there for the user for the same community. var existingUserCommunityRole = DbSet.Where((UserCommunities uc) => uc.UserID == permissionItem.UserID && uc.CommunityId == permissionItem.CommunityID).FirstOrDefault(); // If the request is approved and also there are no roles for the same community and same user (not approved by anyone else) or // the new role is higher than the existing role, // only then add the user and his role for the community in user communities list. if (permissionItem.Approved == true) { if (existingUserCommunityRole != null && existingUserCommunityRole.Role.RoleID >= (int)permissionItem.Role) { operationStatus.Succeeded = false; operationStatus.CustomErrorMessage = true; operationStatus.ErrorMessage = Resources.MembershipExistsErrorMessage; // Note that changes to permission request is not saved to the DB. return(operationStatus); } else { permissionItem.IsInherited = false; // Take the request which is being approved as only User Role which needs to be updated for the community // whose request is getting approved and also for their children recursively. UpdateCommunityPermission(permissionRequest.Community, permissionItem, false); } } else { // To update the permission request. Update(permissionRequest.Community.UserCommunities.FirstOrDefault()); } SaveChanges(); return(operationStatus); }
public Task <OperationStatus> PromoteAsSiteAdmin(IEnumerable <long> adminUsers, long updatedById) { OperationStatus operationStatus = null; try { if (_userRepository.IsSiteAdmin(updatedById)) { var userTypes = _userTypeRepository.GetAll(null); // Mark the user who have been demoted from the site administrators as Regular user's. Expression <Func <User, bool> > removedUserCondition = (user) => (user.UserID != updatedById && user.UserTypeID == (int)UserTypes.SiteAdmin && !adminUsers.Contains(user.UserID)); var removedAdmins = _userRepository.GetItems(removedUserCondition, null, false); IEnumerable <UserType> userTypesEnumerable = userTypes as UserType[] ?? userTypes.ToArray(); foreach (var removedUser in removedAdmins) { removedUser.UserType = userTypesEnumerable.FirstOrDefault(type => type.UserTypeID == (int)UserTypes.Regular); _userRepository.Update(removedUser); } // Mark the user who have been promoted as site administrators. Expression <Func <User, bool> > promotedUserCondition = (user) => (user.UserID != updatedById && adminUsers.Contains(user.UserID) && user.UserTypeID != (int)UserTypes.SiteAdmin); var promotedAdmins = _userRepository.GetItems(promotedUserCondition, null, false); foreach (var promotedUser in promotedAdmins) { promotedUser.UserType = userTypesEnumerable.FirstOrDefault(type => type.UserTypeID == (int)UserTypes.SiteAdmin); _userRepository.Update(promotedUser); } _userRepository.SaveChanges(); } else { operationStatus = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError); } } catch (Exception exception) { operationStatus = OperationStatus.CreateFailureStatus(exception); } operationStatus = operationStatus ?? OperationStatus.CreateSuccessStatus(); return(Task.FromResult(operationStatus)); }
public Task <OperationStatus> UpdateAllOffensiveContentEntry(OffensiveEntry details) { this.CheckNotNull(() => new { details = details }); OperationStatus status = null; try { if (_userRepository.IsSiteAdmin(details.ReviewerID)) { var offensiveContents = _offensiveContentRepository.GetItems(oc => oc.ContentID == details.EntityID && oc.OffensiveStatusID == (int)OffensiveStatusType.Flagged, null, false); if (offensiveContents != null && offensiveContents.Any()) { foreach (var item in offensiveContents) { item.OffensiveStatusID = (int)details.Status; item.Justification = details.Justification; item.ReviewerID = details.ReviewerID; item.ReviewerDatetime = DateTime.UtcNow; _offensiveContentRepository.Update(item); } _offensiveContentRepository.SaveChanges(); } } else { status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } // Status will be null if all sub communities and contents have been deleted. // If one them is not deleted then the status will have the exception details. status = status ?? OperationStatus.CreateSuccessStatus(); return(Task.FromResult(status)); }
/// <summary> /// Deletes the file from azure which is identified by file name. /// </summary> /// <param name="fileName"> /// file name. /// </param> /// <returns> /// Operation Status /// </returns> public OperationStatus DeleteAsset(string fileName) { OperationStatus operationStatus = null; var fileBlob = new BlobDetails() { BlobID = fileName, }; try { _blobDataRepository.DeleteAsset(fileBlob); operationStatus = OperationStatus.CreateSuccessStatus(); } catch (Exception) { operationStatus = OperationStatus.CreateFailureStatus(Resources.UnknownErrorMessage); } return(operationStatus); }
/// <summary> /// Sets the given access type for the specified Content. /// </summary> /// <param name="contentId">Content Id</param> /// <param name="userId">User Identity</param> /// <param name="accessType">Access type of the Content.</param> /// <returns>Status of the operation. Success, if succeeded. Failure message and exception details in case of exception.</returns> public OperationStatus SetContentAccessType(long contentId, long userId, AccessType accessType) { OperationStatus status = null; try { if (_userRepository.IsSiteAdmin(userId)) { // Get the current content from DB. var content = _contentRepository.GetItem((c) => c.ContentID == contentId); // Make sure content exists this.CheckNotNull(() => new { content }); content.AccessTypeID = (int)accessType; content.IsOffensive = (accessType == AccessType.Private); var offensiveDetails = new OffensiveEntry() { EntityID = contentId, ReviewerID = userId, Status = OffensiveStatusType.Offensive }; UpdateAllOffensiveContentEntry(contentId, offensiveDetails); _contentRepository.Update(content); _contentRepository.SaveChanges(); // Create Success message if set access type is successful. status = OperationStatus.CreateSuccessStatus(); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } return(status); }
public OperationStatus UpdateOffensiveContentEntry(OffensiveEntry details) { this.CheckNotNull(() => new { details = details }); OperationStatus status = null; try { if (_userRepository.IsSiteAdmin(details.ReviewerID)) { var offensiveContents = _offensiveContentRepository.GetItem(oc => oc.OffensiveContentID == details.EntryID); if (offensiveContents != null) { offensiveContents.OffensiveStatusID = (int)details.Status; offensiveContents.Justification = details.Justification; offensiveContents.ReviewerID = details.ReviewerID; offensiveContents.ReviewerDatetime = DateTime.UtcNow; _offensiveContentRepository.Update(offensiveContents); _offensiveContentRepository.SaveChanges(); } } else { status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } // Status will be null if all sub communities and contents have been deleted. // If one them is not deleted then the status will have the exception details. status = status ?? OperationStatus.CreateSuccessStatus(); return(status); }
/// <summary> /// Checks if the user is Site Admin /// </summary> /// <param name="userId">ID of the user.</param> /// <returns>True if user is site admin;Otherwise false.</returns> public OperationStatus IsSiteAdmin(long userId) { OperationStatus operationStatus = null; try { if (_userRepository.IsSiteAdmin(userId)) { operationStatus = OperationStatus.CreateSuccessStatus(); } else { operationStatus = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError); } } catch (Exception exception) { operationStatus = OperationStatus.CreateFailureStatus(exception); } return(operationStatus); }
public HttpResponseMessage Publish() { string message = string.Empty; HttpStatusCode status = HttpStatusCode.OK; PostFileModel dataDetail = new PostFileModel(); try { diagnostics.WriteInformationTrace(TraceEventId.Flow, "Into publish"); if (HttpContext.Current.Request["PostFileData"] != null) { var postFileString = HttpContext.Current.Request["PostFileData"]; if (postFileString != null) { //diagnostics.WriteInformationTrace(TraceEventId.Flow, "Into publishing3"); /**************************************************** * TODO: Try catch block below is required to handle the case where the * clients send post request with JSON payload as plain text. * The API needs to be refactored to take the model as input * and have the MVC framework resolve/deserialize the payload * into model object. * **************************************************/ PostFileModel postFileData = default(PostFileModel); try { postFileData = Helper.DeSerializeObject <PostFileModel>(postFileString.DecodeFrom64(), "postFile"); } catch (Exception) { // If the data is not base 64 encoded using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(postFileString))) { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(PostFileModel)); postFileData = (PostFileModel)jsonSerializer.ReadObject(stream); } } DM.Repository repository = repositoryService.GetRepositoryById(postFileData.SelectedRepositoryId); this.fileService = this.fileServiceFactory.GetFileService(repository.BaseRepository.Name); // Get the AuthToken from the request. TODO need to move below code to SkyDriveFileController. AuthToken userAuthToken = postFileData.UserAuthToken; userAuthToken.UserId = this.user.UserId; userAuthToken.RespositoryId = repository.RepositoryId; // Save the file details to db and publish logic this.fileService.SaveFile(postFileData); return(Request.CreateResponse <OperationStatus>(HttpStatusCode.OK, OperationStatus.CreateSuccessStatus())); } } } catch (ArgumentNullException ane) { diagnostics.WriteErrorTrace(TraceEventId.Exception, ane); status = HttpStatusCode.BadRequest; if (ane.ParamName.Equals("fileService")) { message = MessageStrings.File_Service_Is_Null; } else { message = ane.Message + ane.StackTrace + ane.GetType().ToString(); } } catch (Exception ex) { diagnostics.WriteErrorTrace(TraceEventId.Exception, ex); message = ex.Message + ex.StackTrace + ex.GetType().ToString(); if (null != ex.InnerException) { message += ex.InnerException.Message + ex.InnerException.StackTrace + ex.InnerException.GetType().ToString(); } status = HttpStatusCode.InternalServerError; } return(Request.CreateErrorResponse(status, message)); }
/// <summary> /// Un-deletes the specified content from the Earth database so that it is again accessible in the site. /// </summary> /// <param name="contentId">Content Id</param> /// <param name="userId">User Identity</param> /// <returns>Status of the operation. Success, if succeeded. Failure message and exception details in case of exception.</returns> public OperationStatus UnDeleteOffensiveContent(long contentId, long userId) { OperationStatus status = null; try { if (_userRepository.IsSiteAdmin(userId)) { // Get the current content from DB. var content = _contentRepository.GetItem((c) => c.ContentID == contentId); if (content != null) { content.IsDeleted = false; // We need to mark the community as not offensive as the Admin is marking the content as undeleted. content.IsOffensive = false; // We need to mark the DeletedBy as null as we are Undoing the delete operation. // Also DeleteBy filed will be used to check if the user has explicitly deleted the content. content.User2 = null; var parentCommunity = Enumerable.ElementAt(content.CommunityContents, 0).Community; if ((parentCommunity.CommunityTypeID == (int)CommunityTypes.Community || parentCommunity.CommunityTypeID == (int)CommunityTypes.Folder) && parentCommunity.IsDeleted == true) { // Get the "None" (User) community of the user who uploaded the Content. var noneCommunity = _communityRepository.GetItem( c => c.CreatedByID == content.CreatedByID && c.CommunityTypeID == (int)CommunityTypes.User); this.CheckNotNull(() => new { noneCommunity }); // Remove the existing relation content.CommunityContents.Remove(Enumerable.ElementAt(content.CommunityContents, 0)); // Set the None community as parent community. var noneCommunityContent = new CommunityContents() { CommunityID = noneCommunity.CommunityID, Content = content }; content.CommunityContents.Add(noneCommunityContent); } foreach (var contentRelation in content.ContentRelation) { contentRelation.Content1.IsDeleted = false; contentRelation.Content1.User2 = null; } _contentRepository.Update(content); // Save changes to the database _contentRepository.SaveChanges(); status = OperationStatus.CreateSuccessStatus(); } else { status = OperationStatus.CreateFailureStatus(string.Format(CultureInfo.CurrentCulture, "Content with ID '{0}' was not found", contentId)); } } else { status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError); } } catch (Exception exception) { status = OperationStatus.CreateFailureStatus(exception); } return(status); }