Beispiel #1
0
        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);
        }
Beispiel #2
0
        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"
            });
        }
Beispiel #3
0
        public List <NoteModel> GetNotes(IPwdManService pwdManService, string authenticationToken)
        {
            logger.LogDebug("Get notes...");
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var notes     = dbContext.DbNotes.Where((n) => n.DbUserId == user.Id).OrderByDescending((n) => n.ModifiedUtc);
            var ret       = new List <NoteModel>();

            foreach (var note in notes)
            {
                ret.Add(new NoteModel {
                    Id = note.Id, Title = note.Title
                });
            }
            return(ret);
        }
Beispiel #4
0
        public bool DeleteNote(IPwdManService pwdManService, string authenticationToken, long id)
        {
            logger.LogDebug("Delete note ID {id}", id);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var note      = dbContext.DbNotes.SingleOrDefault(
                (n) => n.Id == id && n.DbUserId == user.Id);

            if (note != null)
            {
                dbContext.DbNotes.Remove(note);
                dbContext.SaveChanges();
                return(true);
            }
            return(false);
        }
Beispiel #5
0
        public bool RenameItem(IPwdManService pwdManService, string authenticationToken, long id, string name)
        {
            logger.LogDebug("Rename item ID {id} to '{name}'...", id, name);
            name = name.Trim();
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var docItem   = GetItemById(dbContext, user, id);

            if (docItem != null && docItem.Name != name)
            {
                docItem.Name = name;
                dbContext.SaveChanges();
                return(true);
            }
            return(false);
        }
Beispiel #6
0
        public long AddNote(IPwdManService pwdManService, string authenticationToken, NoteModel noteModel)
        {
            logger.LogDebug("Add note...");
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var note      = new DbNote
            {
                DbUserId    = user.Id,
                Title       = noteModel.Title,
                Content     = noteModel.Content,
                ModifiedUtc = DateTime.UtcNow
            };

            dbContext.DbNotes.Add(note);
            dbContext.SaveChanges();
            return(note.Id);
        }
Beispiel #7
0
        public List <ItemModel> GetItems(IPwdManService pwdManService, string authenticationToken, long?id)
        {
            logger.LogDebug("Get items for item ID {id}...", id);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var ret       = new List <ItemModel>();
            var docItem   = GetItemById(dbContext, user, id);

            if (docItem != null)
            {
                // current item, path items and all direct children
                ret.Add(ConvertToItemModel(docItem));
                ret.AddRange(ConvertToItemModel(GetPath(dbContext, user, docItem)));
                ret.AddRange(ConvertToItemModel(GetChildren(dbContext, user, docItem)));
            }
            return(ret);
        }
Beispiel #8
0
        public DateTime?UpdateNote(IPwdManService pwdManService, string authenticationToken, NoteModel noteModel)
        {
            logger.LogDebug("Update note ID {noteModel.Id}...", noteModel.Id);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var note      = dbContext.DbNotes.SingleOrDefault(
                (n) => n.Id == noteModel.Id && n.DbUserId == user.Id);

            if (note != null && (note.Title != noteModel.Title || note.Content != noteModel.Content))
            {
                note.Title       = noteModel.Title;
                note.Content     = noteModel.Content;
                note.ModifiedUtc = DateTime.UtcNow;
                dbContext.SaveChanges();
                return(DbMynaContext.GetUtcDateTime(note.ModifiedUtc).Value);
            }
            return(null);
        }
Beispiel #9
0
        public DiaryEntryModel GetEntry(IPwdManService pwdManService, string authenticationToken, DateTime date)
        {
            logger.LogDebug("Get diary entry for {date}...", date);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();

            date = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, DateTimeKind.Utc);
            var diary = dbContext.DbDiaries.SingleOrDefault((d) => d.DbUserId == user.Id && d.Date == date);

            if (diary != null)
            {
                return(new DiaryEntryModel
                {
                    Date = diary.Date,
                    Entry = diary.Entry
                });
            }
            return(null);
        }
Beispiel #10
0
        public int DeleteItems(IPwdManService pwdManService, string authenticationToken, long parentId, List <long> delIds)
        {
            logger.LogDebug("Delete {cnt} item(s) in parent ID {parentId}...", delIds.Count, parentId);
            var user        = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext   = pwdManService.GetDbContext();
            var removeItems = new List <DbDocItem>();
            var parent      = GetItemById(dbContext, user, parentId);

            if (parent != null)
            {
                // delete only items that are contained in the parent folder
                var delItems = dbContext.DbDocItems.Where(
                    item =>
                    item.OwnerId == user.Id &&
                    item.ParentId == parentId &&
                    delIds.Contains(item.Id)).ToList();
                foreach (var delItem in delItems)
                {
                    removeItems.Add(delItem);
                    parent.Children -= 1;
                    // add all children of a folder is deleted
                    if (delItem.Type == DbDocItemType.Folder)
                    {
                        removeItems.AddRange(GetAllChildren(dbContext, user, delItem));
                    }
                }
            }
            foreach (var delItem in removeItems)
            {
                if (delItem.ContentId.HasValue)
                {
                    dbContext.DbDocContents.Remove(new DbDocContent {
                        Id = delItem.ContentId.Value
                    });
                }
            }
            if (removeItems.Any())
            {
                dbContext.DbDocItems.RemoveRange(removeItems);
                dbContext.SaveChanges();
            }
            return(removeItems.Count);
        }
Beispiel #11
0
        public NoteModel GetNote(IPwdManService pwdManService, string authenticationToken, long id)
        {
            logger.LogDebug("Get note for ID {id}...", id);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var note      = dbContext.DbNotes.SingleOrDefault(
                (n) => n.Id == id && n.DbUserId == user.Id);

            if (note != null)
            {
                return(new NoteModel
                {
                    Id = note.Id,
                    LastModifiedUtc = DbMynaContext.GetUtcDateTime(note.ModifiedUtc).Value,
                    Title = note.Title,
                    Content = note.Content
                });
            }
            return(null);
        }
Beispiel #12
0
        public ItemModel CreateVolume(IPwdManService pwdManService, string authenticationToken, string name)
        {
            logger.LogDebug("Create volume '{name}'...", name);
            name = name.Trim();
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var docItem   = GetVolume(dbContext, user);

            if (docItem == null)
            {
                docItem = new DbDocItem {
                    Name    = name,
                    Type    = DbDocItemType.Volume,
                    OwnerId = user.Id
                };
                dbContext.DbDocItems.Add(docItem);
                dbContext.SaveChanges();
            }
            return(ConvertToItemModel(docItem));
        }
Beispiel #13
0
        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);
        }
Beispiel #14
0
        public List <int> GetDaysWithEntries(IPwdManService pwdManService, string authenticationToken, DateTime date)
        {
            logger.LogDebug("Get days in month with diary entries for {date}...", date);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var first     = new DateTime(date.Year, date.Month, 1, 0, 0, 0, DateTimeKind.Utc);
            var last      = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month), 0, 0, 0, DateTimeKind.Utc);
            var diaries   = dbContext.DbDiaries
                            .Where((d) => d.DbUserId == user.Id &&
                                   d.Date >= first && d.Date <= last &&
                                   d.Entry.Trim().Length > 0)
                            .OrderBy((d) => d.Date);
            var ret = new List <int>();

            foreach (var diary in diaries)
            {
                ret.Add(diary.Date.Day);
            }
            return(ret);
        }
Beispiel #15
0
        public void SetEntry(IPwdManService pwdManService, string authenticationToken, DiaryEntryModel model)
        {
            logger.LogDebug("Set diary entry  for {date}...", model.Date);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var date      = new DateTime(model.Date.Year, model.Date.Month, model.Date.Day, 0, 0, 0, DateTimeKind.Utc);
            var diary     = dbContext.DbDiaries.SingleOrDefault((d) => d.DbUserId == user.Id && d.Date == date);

            if (diary == null)
            {
                diary = new DbDiary {
                    DbUserId = user.Id, Date = date, Entry = model.Entry
                };
                dbContext.DbDiaries.Add(diary);
            }
            else
            {
                diary.Entry = model.Entry;
            }
            dbContext.SaveChanges();
        }
Beispiel #16
0
        public int MoveItems(IPwdManService pwdManService, string authenticationToken, long destinationId, List <long> moveIds)
        {
            logger.LogDebug("Move {cnt} item(s) to item ID {destinationId}...", moveIds.Count, destinationId);
            var user        = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext   = pwdManService.GetDbContext();
            int moved       = 0;
            var destination = GetItemById(dbContext, user, destinationId);

            if (destination != null)
            {
                // skip all items that are in destination path
                // skip all items that have already the destination as parent
                // skip the destination item itself
                var destPathIds = GetPath(dbContext, user, destination).Select(item => item.Id);
                var moveItems   = dbContext.DbDocItems.Where(
                    item =>
                    item.OwnerId == user.Id &&
                    moveIds.Contains(item.Id) &&
                    item.Id != destinationId &&
                    item.ParentId != destinationId &&
                    !destPathIds.Contains(item.Id)).ToList();
                foreach (var moveItem in moveItems)
                {
                    var oldParent = GetItemById(dbContext, user, moveItem.ParentId);
                    if (oldParent != null)
                    {
                        oldParent.Children -= 1;
                    }
                    moveItem.ParentId     = destinationId;
                    destination.Children += 1;
                    moved++;
                }
                if (moveItems.Any())
                {
                    dbContext.SaveChanges();
                }
            }
            return(moved);
        }
Beispiel #17
0
        public ItemModel AddFolder(IPwdManService pwdManService, string authenticationToken, long parentId, string name)
        {
            logger.LogDebug("Add folder '{name}' into parent ID {parentId}...", name, parentId);
            name = name.Trim();
            var user       = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext  = pwdManService.GetDbContext();
            var parentItem = GetItemById(dbContext, user, parentId);

            if (parentItem != null)
            {
                var docItem = new DbDocItem
                {
                    Name     = name,
                    Type     = DbDocItemType.Folder,
                    OwnerId  = user.Id,
                    ParentId = parentId
                };
                dbContext.DbDocItems.Add(docItem);
                parentItem.Children += 1;
                dbContext.SaveChanges();
                return(ConvertToItemModel(docItem));
            }
            return(null);
        }
Beispiel #18
0
        public ItemModel UploadDocument(IPwdManService pwdManService, string authenticationToken, long parentId, string name, Stream stream, bool overwrite)
        {
            logger.LogDebug("Upload document with '{name}' into parent ID {parentId}...", name, parentId);
            name = name.Trim();
            var user       = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext  = pwdManService.GetDbContext();
            var sum        = dbContext.DbDocItems.Where(item => item.Type == DbDocItemType.Item && item.OwnerId == user.Id).Sum(item => item.Size);
            var parentItem = GetItemById(dbContext, user, parentId);

            if (parentItem != null)
            {
                DbDocItem docItem = null;
                if (overwrite)
                {
                    docItem = dbContext.DbDocItems.
                              Include(item => item.Content).
                              SingleOrDefault(item =>
                                              item.Type == DbDocItemType.Item &&
                                              item.ParentId == parentId &&
                                              item.OwnerId == user.Id &&
                                              item.Name == name);
                    if (docItem != null)
                    {
                        sum -= docItem.Size;
                    }
                }
                var ms = new MemoryStream();
                stream.CopyTo(ms);
                var size = ms.Length;
                if (sum + size > user.StorageQuota)
                {
                    throw new StorageQuotaExceededException();
                }
                if (docItem != null)
                {
                    docItem.Size         = size;
                    docItem.Content.Data = ms.ToArray();
                    docItem.AccessRole   = parentItem.AccessRole;
                }
                else
                {
                    docItem = new DbDocItem
                    {
                        Name     = name,
                        Type     = DbDocItemType.Item,
                        OwnerId  = user.Id,
                        ParentId = parentId,
                        Size     = size,
                        Content  = new DbDocContent {
                            Data = ms.ToArray()
                        },
                        AccessRole = parentItem.AccessRole
                    };
                    dbContext.DbDocItems.Add(docItem);
                    parentItem.Children += 1;
                }
                dbContext.SaveChanges();
                return(ConvertToItemModel(docItem));
            }
            return(null);
        }