public bool UpdateMarkdown(IPwdManService pwdManService, string authenticationToken, long id, string markdown) { logger.LogDebug("Update markdown for item ID {id}...", id); var user = pwdManService.GetUserFromToken(authenticationToken); if (!pwdManService.HasRole(user, "usermanager")) { throw new AccessDeniedPermissionException(); } var dbContext = pwdManService.GetDbContext(); var docItem = dbContext.DbDocItems. Include(item => item.Content). SingleOrDefault(item => item.OwnerId == user.Id && item.Id == id); if (docItem == null || docItem.Type != DbDocItemType.Item || !docItem.Name.EndsWith(".md")) { throw new InvalidParameterException(); } var sum = dbContext.DbDocItems.Where(item => item.Type == DbDocItemType.Item && item.OwnerId == user.Id).Sum(item => item.Size); sum -= docItem.Size; var bytes = Encoding.UTF8.GetBytes(markdown); var size = bytes.Length; if (sum + size > user.StorageQuota) { throw new StorageQuotaExceededException(); } docItem.Size = size; docItem.Content.Data = bytes; dbContext.SaveChanges(); return(true); }
public DownloadResult DownloadDocument(IPwdManService pwdManService, string authenticationToken, long id) { logger.LogDebug("Download content for item ID {id}...", id); var dbContext = pwdManService.GetDbContext(); var docItem = dbContext.DbDocItems .SingleOrDefault(item => item.Type == DbDocItemType.Item && item.Id == id); if (docItem == null || !docItem.ContentId.HasValue) { throw new AccessDeniedPermissionException(); } if (!AccessRole.IsEverbody(docItem.AccessRole)) { var user = pwdManService.GetUserFromToken(authenticationToken); if (AccessRole.IsOwner(docItem.AccessRole)) { if (user.Id != docItem.OwnerId) { throw new AccessDeniedPermissionException(); } } else if (!pwdManService.HasRole(user, docItem.AccessRole)) { throw new AccessDeniedPermissionException(); } } var docContent = dbContext.DbDocContents.Single(c => c.Id == docItem.ContentId); return(new DownloadResult { Stream = new MemoryStream(docContent.Data), FileName = docItem.Name, ContentType = "application/octet-stream" }); }
public bool SetFolderAccessRole(IPwdManService pwdManService, string authenticationToken, long id, string accessRole) { logger.LogDebug("Update item access for ID {id} to '{access}'...", id, accessRole); var user = pwdManService.GetUserFromToken(authenticationToken); if (!pwdManService.HasRole(user, "usermanager")) { throw new AccessDeniedPermissionException(); } var dbContext = pwdManService.GetDbContext(); var docItem = GetItemById(dbContext, user, id); if (docItem != null && docItem.Type == DbDocItemType.Folder && docItem.AccessRole != accessRole) { docItem.AccessRole = accessRole; dbContext.SaveChanges(); return(true); } return(false); }