Esempio n. 1
0
        public async Task <bool> CancelSharingForUser(Guid shareId, string ownerUsername)
        {
            string ownerId = (await _databaseContext.Users
                              .FirstOrDefaultAsync(_ => _.UserName == ownerUsername))?
                             .Id;

            ShareForUser shareForUserToDelete = await _databaseContext.ShareForUsers
                                                .Include(_ => _.Resource)
                                                .FirstOrDefaultAsync(_ => _.ID == shareId && _.OwnerID == ownerId);

            if (shareForUserToDelete == null)
            {
                return(false);
            }

            shareForUserToDelete.Resource.IsSharedForUsers = false;
            shareForUserToDelete.Resource.IsShared         = shareForUserToDelete.Resource.IsSharedForEveryone;

            await _databaseContext.SaveChangesAsync();

            _databaseContext.ShareForUsers.Remove(shareForUserToDelete);

            await _databaseContext.SaveChangesAsync();

            return(!await _databaseContext.ShareForUsers.AnyAsync(_ => _.ID == shareId));
        }
Esempio n. 2
0
        public async Task <StatusCode <NoteOut> > GetByIdAndUser(Guid noteId, string username)
        {
            string userId = (await _databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == username))?
                            .Id;

            Note note = await _databaseContext.Notes
                        .Include(_ => _.ShareEveryone)
                        .Include(_ => _.ShareForUsers)
                        .FirstOrDefaultAsync(_ => _.ID == noteId);

            if (note == null)
            {
                return(new StatusCode <NoteOut>(StatusCodes.Status404NotFound, $"Note {noteId} not found"));
            }

            if (note.OwnerID == userId)
            {
                return(new StatusCode <NoteOut>(StatusCodes.Status200OK, _mapper.Map <NoteOut>(note)));
            }

            if (note.IsShared)
            {
                if (note.IsSharedForEveryone)
                {
                    ShareEveryone share = note.ShareEveryone;

                    if (share != null)
                    {
                        if ((share.ExpirationDateTime == null ||
                             (share.ExpirationDateTime != null && share.ExpirationDateTime >= DateTime.Now)) &&
                            (share.DownloadLimit == null ||
                             (share.DownloadLimit != null && share.DownloadLimit > 0)))
                        {
                            return(new StatusCode <NoteOut>(StatusCodes.Status200OK, _mapper.Map <NoteOut>(note)));
                        }
                    }
                }
                else if (note.IsSharedForUsers)
                {
                    ShareForUser share = note.ShareForUsers.FirstOrDefault(_ => _.SharedForUserID == userId && _.ResourceID == noteId);

                    if (share != null)
                    {
                        if (share.ExpirationDateTime == null ||
                            (share.ExpirationDateTime != null && share.ExpirationDateTime >= DateTime.Now))
                        {
                            return(new StatusCode <NoteOut>(StatusCodes.Status200OK, _mapper.Map <NoteOut>(note)));
                        }
                    }
                }
            }

            return(new StatusCode <NoteOut>(StatusCodes.Status404NotFound, $"Note {noteId} not found"));
        }
Esempio n. 3
0
        public async void Returns_OkObjectResult200AndNoteOut_when_NoteExistsAndIsSharedForLoggedUserAndExpirationDateTimeNotEnds()
        {
            IDatabaseContext    databaseContext     = DatabaseTestHelper.GetContext();
            MapperConfiguration mapperConfiguration = new MapperConfiguration(conf =>
            {
                conf.AddProfile(new Note_to_NoteOut());
            });
            IMapper mapper = mapperConfiguration.CreateMapper();

            INotesService notesService = new NotesService(databaseContext, mapper);

            string userId = (await databaseContext.Users.FirstOrDefaultAsync(_ => _.UserName == ADMIN_USERNAME)).Id;

            string ownerId = Guid.NewGuid().ToString();

            Note note = new Note
            {
                ResourceType     = DAO.Models.Base.ResourceType.NOTE,
                Title            = "Note's title",
                Content          = "Note's content",
                OwnerID          = ownerId,
                CreatedDateTime  = DateTime.Now,
                IsShared         = true,
                IsSharedForUsers = true
            };

            await databaseContext.Notes.AddAsync(note);

            ShareForUser shareForUser = new ShareForUser
            {
                CreatedDateTime    = DateTime.Now,
                ResourceID         = note.ID,
                OwnerID            = ownerId,
                SharedForUserID    = userId,
                ExpirationDateTime = DateTime.Now.AddDays(1)
            };

            await databaseContext.ShareForUsers.AddAsync(shareForUser);

            await databaseContext.SaveChangesAsync();

            Assert.True(databaseContext.Notes
                        .Any(_ => _.ID == note.ID));

            Assert.True(databaseContext.ShareForUsers
                        .Any(_ => _.ID == shareForUser.ID));

            StatusCode <NoteOut> status = await notesService.GetByIdAndUser(note.ID, ADMIN_USERNAME);

            Assert.NotNull(status);
            Assert.Equal(StatusCodes.Status200OK, status.Code);
            Assert.NotNull(status.Body);
            Assert.IsType <NoteOut>(status.Body);
        }
Esempio n. 4
0
        public async Task <StatusCode <ShareForUserOut> > ShareForUser(ShareForUserIn shareForUserIn, string ownerUsername)
        {
            if (shareForUserIn.Username == ownerUsername)
            {
                return(new StatusCode <ShareForUserOut>(StatusCodes.Status404NotFound, $"You cannot share your own file with yourself"));
            }

            if (!await _databaseContext.Users.AnyAsync(_ => _.UserName == shareForUserIn.Username))
            {
                return(new StatusCode <ShareForUserOut>(StatusCodes.Status404NotFound, $"User {shareForUserIn.Username} not found"));
            }

            string ownerId = (await _databaseContext.Users
                              .FirstOrDefaultAsync(_ => _.UserName == ownerUsername))?
                             .Id;

            ResourceAbstract resourceAbstract = await _databaseContext.ResourceAbstracts
                                                .Include(_ => _.ShareForUsers)
                                                .FirstOrDefaultAsync(_ => _.ID == shareForUserIn.ResourceId && _.OwnerID == ownerId);

            if (resourceAbstract == null)
            {
                return(new StatusCode <ShareForUserOut>(StatusCodes.Status404NotFound, $"Resource {shareForUserIn.ResourceId} not found"));
            }

            string userId = (await _databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == shareForUserIn.Username))?
                            .Id;

            ShareForUser shareForUser = await _databaseContext.ShareForUsers
                                        .FirstOrDefaultAsync(_ => _.OwnerID == ownerId && _.ResourceID == shareForUserIn.ResourceId && _.SharedForUserID == userId);

            if (shareForUser == null)
            {
                shareForUser = new ShareForUser
                {
                    CreatedDateTime    = DateTime.Now,
                    ExpirationDateTime = shareForUserIn.ExpirationDateTime,
                    OwnerID            = ownerId,
                    ResourceID         = shareForUserIn.ResourceId,
                    SharedForUserID    = userId
                };

                resourceAbstract.IsShared         = true;
                resourceAbstract.IsSharedForUsers = true;
                resourceAbstract.ShareForUsers.Add(shareForUser);
            }
            else
            {
                shareForUser.LastModifiedDateTime = DateTime.Now;
                shareForUser.ExpirationDateTime   = shareForUserIn.ExpirationDateTime;
            }

            if (shareForUser.ExpirationDateTime.HasValue)
            {
                shareForUser.ExpirationDateTime = shareForUser.ExpirationDateTime.Value.AddHours(1);
            }

            await _databaseContext.SaveChangesAsync();

            ShareForUserOut result = _mapper.Map <ShareForUserOut>(shareForUser);

            return(new StatusCode <ShareForUserOut>(StatusCodes.Status200OK, result));
        }
Esempio n. 5
0
        public async Task <StatusCode <FileOut> > GetByIdAndUser(Guid id, string username)
        {
            string userId = (await _databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == username))?.Id;

            ResourceAbstract fileAbstract = await _databaseContext.ResourceAbstracts
                                            .Include(_ => _.ParentDirectory)
                                            .Include(_ => _.ShareEveryone)
                                            .Include(_ => _.ShareForUsers)
                                            .FirstOrDefaultAsync(_ => _.ID == id && (_.ResourceType == ResourceType.FILE || _.ResourceType == ResourceType.DIRECTORY));

            if (fileAbstract == null)
            {
                return(new StatusCode <FileOut>(StatusCodes.Status404NotFound, $"File {id} not found"));
            }

            if (fileAbstract.OwnerID == userId)
            {
                FileOut result = _mapper.Map <FileOut>(fileAbstract);
                if (fileAbstract is File)
                {
                    result.FileSizeBytes = (fileAbstract as File).FileSizeBytes;
                }
                return(new StatusCode <FileOut>(StatusCodes.Status200OK, result));
            }

            if (fileAbstract.IsShared)
            {
                if (fileAbstract.IsSharedForEveryone)
                {
                    ShareEveryone share = fileAbstract.ShareEveryone;

                    if (share != null)
                    {
                        if ((share.ExpirationDateTime == null ||
                             (share.ExpirationDateTime != null && share.ExpirationDateTime >= DateTime.Now)) &&
                            (share.DownloadLimit == null ||
                             (share.DownloadLimit != null && share.DownloadLimit > 0)))
                        {
                            FileOut result = _mapper.Map <FileOut>(fileAbstract);
                            if (fileAbstract is File)
                            {
                                result.FileSizeBytes = (fileAbstract as File).FileSizeBytes;
                            }
                            return(new StatusCode <FileOut>(StatusCodes.Status200OK, result));
                        }
                    }
                }
                else if (fileAbstract.IsSharedForUsers)
                {
                    ShareForUser share = fileAbstract.ShareForUsers.FirstOrDefault(_ => _.SharedForUserID == userId && _.ResourceID == id);

                    if (share != null)
                    {
                        if (share.ExpirationDateTime == null ||
                            (share.ExpirationDateTime != null && share.ExpirationDateTime >= DateTime.Now))
                        {
                            FileOut result = _mapper.Map <FileOut>(fileAbstract);
                            if (fileAbstract is File)
                            {
                                result.FileSizeBytes = (fileAbstract as File).FileSizeBytes;
                            }
                            return(new StatusCode <FileOut>(StatusCodes.Status200OK, result));
                        }
                    }
                }
            }

            return(new StatusCode <FileOut>(StatusCodes.Status404NotFound, $"File {id} not found"));
        }
Esempio n. 6
0
        public async Task <StatusCode <DownloadFileInfo> > DownloadByIdAndUser(Guid id, string username)
        {
            string userId = (await _databaseContext.Users.FirstOrDefaultAsync(_ => _.UserName == username))?.Id;

            File fileToDownload = await _databaseContext.Files
                                  .Include(_ => _.ShareEveryone)
                                  .Include(_ => _.ShareForUsers)
                                  .FirstOrDefaultAsync(_ => _.ID == id);

            if (fileToDownload == null)
            {
                return(new StatusCode <DownloadFileInfo>(StatusCodes.Status404NotFound, $"File {id} not found"));
            }

            if (fileToDownload.OwnerID == userId)
            {
                byte[] fileContent = System.IO.File.ReadAllBytes(fileToDownload.Path);

                DownloadFileInfo downloadFileInfo = new DownloadFileInfo(fileToDownload.Name, fileContent);

                return(new StatusCode <DownloadFileInfo>(StatusCodes.Status200OK, downloadFileInfo));
            }

            if (fileToDownload.IsShared)
            {
                if (fileToDownload.IsSharedForEveryone)
                {
                    ShareEveryone share = fileToDownload.ShareEveryone;

                    if (share != null)
                    {
                        if ((share.ExpirationDateTime == null ||
                             (share.ExpirationDateTime != null && share.ExpirationDateTime >= DateTime.Now)) &&
                            (share.DownloadLimit == null ||
                             (share.DownloadLimit != null && share.DownloadLimit > 0)))
                        {
                            if (share.DownloadLimit != null && share.DownloadLimit > 0)
                            {
                                --share.DownloadLimit;
                                await _databaseContext.SaveChangesAsync();
                            }

                            byte[] fileContent = System.IO.File.ReadAllBytes(fileToDownload.Path);

                            DownloadFileInfo downloadFileInfo = new DownloadFileInfo(fileToDownload.Name, fileContent);

                            return(new StatusCode <DownloadFileInfo>(StatusCodes.Status200OK, downloadFileInfo));
                        }
                    }
                }
                else if (fileToDownload.IsSharedForUsers)
                {
                    ShareForUser share = fileToDownload.ShareForUsers.FirstOrDefault(_ => _.SharedForUserID == userId && _.ResourceID == id);

                    if (share != null)
                    {
                        if (share.ExpirationDateTime == null ||
                            (share.ExpirationDateTime != null && share.ExpirationDateTime >= DateTime.Now))
                        {
                            byte[] fileContent = System.IO.File.ReadAllBytes(fileToDownload.Path);

                            DownloadFileInfo downloadFileInfo = new DownloadFileInfo(fileToDownload.Name, fileContent);

                            return(new StatusCode <DownloadFileInfo>(StatusCodes.Status200OK, downloadFileInfo));
                        }
                    }
                }
            }

            return(new StatusCode <DownloadFileInfo>(StatusCodes.Status404NotFound, $"File {id} not found"));
        }