public async Task <bool> UpdateAsync(EmailAccount entity, EmbeddedUser embeddedUser, string ip)
        {
            var update = Builders <EmailAccount> .Update
                         .Set(e => e.Email, entity.Email)
                         .Set(e => e.DisplayName, entity.DisplayName)
                         .Set(e => e.Host, entity.Host)
                         .Set(e => e.Port, entity.Port)
                         .Set(e => e.UserName, entity.UserName)
                         //.Set(e => e.Password, entity.Password)
                         .Set(e => e.EnableSsl, entity.EnableSsl)
                         .Set(e => e.UseDefaultCredentials, entity.UseDefaultCredentials)
                         .Set(e => e.IsDefault, entity.IsDefault);

            var result = await _emailAccounts.UpdatePartiallyAsync(entity.Id, update);

            if (result)
            {
                _memoryCacheService.Remove(DefaultEmailAccount);

                // Activity Log.
                await _activityLogService.CreateAsync(SystemKeyword.UpdateEmailAccount, entity, embeddedUser, ip);
            }

            return(result);
        }
Пример #2
0
        public async Task <bool> DeleteAsync(string id, EmbeddedUser embeddedUser, string ip)
        {
            var entity = await GetByIdAsync(id);

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

            if (entity.IsFile() && File.Exists(entity.PhysicalPath))
            {
                File.Delete(entity.PhysicalPath);
            }
            else
            {
                await EnsureFolderIsEmpty(id);

                // System Directory.
                EnsureNotSystemFolder(entity);
            }

            var result = await _blobs.DeleteAsync(id);

            // Activity Log.
            await _activityLogService.CreateAsync(SystemKeyword.DeleteBlob, entity, embeddedUser, ip);

            return(result);
        }
        public async Task <ActivityLog> CreateAsync(
            string systemKeyword,
            IEntity entity,
            EmbeddedUser embeddedUser,
            string ip)
        {
            var embeddedActivityLogType = await GetEnabledEmbeddedActivityLogTypes(systemKeyword);

            if (embeddedActivityLogType == null)
            {
                return(null);
            }

            var log = new ActivityLog
            {
                EntityName      = entity.GetType().FullName,
                LogValue        = ObjectToJson(entity),
                IP              = ip,
                CreatedOn       = DateTime.UtcNow,
                CreatedBy       = embeddedUser,
                ActivityLogType = embeddedActivityLogType
            };

            return(await _dbContext.ActivityLogs.CreateAsync(log));
        }
Пример #4
0
 public async Task DeleteAsync(string[] ids, EmbeddedUser embeddedUser, string ip)
 {
     foreach (var id in ids)
     {
         await DeleteAsync(id, embeddedUser, ip);
     }
 }
Пример #5
0
        public async Task <NewsItem> CreateAsync(NewsItem entity, EmbeddedUser embeddedUser, string ip)
        {
            await _news.CreateAsync(entity);

            // Activity Log.
            await _activityLogService.CreateAsync(SystemKeyword.CreateNews, entity, embeddedUser, ip);

            return(entity);
        }
Пример #6
0
        public async Task <bool> UpdateAsync(NewsItem entity, EmbeddedUser embeddedUser, string ip)
        {
            var result = await _news.UpdateAsync(entity);

            if (result)
            {
                // Activity Log.
                await _activityLogService.CreateAsync(SystemKeyword.UpdateNews, entity, embeddedUser, ip);
            }

            return(result);
        }
        public async Task <EmailAccount> CreateAsync(EmailAccount entity, EmbeddedUser embeddedUser, string ip)
        {
            await _emailAccounts.CreateAsync(entity);

            if (entity.IsDefault)
            {
                _memoryCacheService.Remove(DefaultEmailAccount);
            }

            // Activity Log.
            await _activityLogService.CreateAsync(SystemKeyword.CreateEmailAccount, entity, embeddedUser, ip);

            return(entity);
        }
Пример #8
0
        public async Task <bool> UpdateFolderAsync(Blob entity, EmbeddedUser embeddedUser, string ip)
        {
            await EnsureNotDuplicatedName(entity.Parent, entity.Name);

            var update = Builders <Blob> .Update
                         .Set(e => e.Name, entity.Name);

            var result = await _blobs.UpdatePartiallyAsync(entity.Id, update);

            // Activity Log.
            await _activityLogService.CreateAsync(SystemKeyword.UpdateBlob, entity, embeddedUser, ip);

            return(result);
        }
Пример #9
0
        public async Task <bool> DeleteAsync(string id, EmbeddedUser embeddedUser, string ip)
        {
            var projection = Builders <NewsItem> .Projection
                             .Exclude(e => e.Blob)
                             .Exclude(e => e.Content);

            var oldEntity = await _news.DeleteAsync(id, projection);

            if (oldEntity != null)
            {
                // Activity Log.
                await _activityLogService.CreateAsync(SystemKeyword.DeleteNews, oldEntity, embeddedUser, ip);

                return(true);
            }

            return(false);
        }
        public async Task <bool> DeleteAsync(string id, EmbeddedUser embeddedUser, string ip)
        {
            var oldEntity = await _emailAccounts.DeleteAsync(id, null);

            if (oldEntity != null)
            {
                if (oldEntity.IsDefault)
                {
                    _memoryCacheService.Remove(DefaultEmailAccount);
                }

                // Activity Log.
                await _activityLogService.CreateAsync(SystemKeyword.DeleteEmailAccount, oldEntity, embeddedUser, ip);

                return(true);
            }

            return(false);
        }
Пример #11
0
        public async Task <Blob> CreateFolderAsync(Blob entity, EmbeddedUser embeddedUser, string ip)
        {
            await EnsureNotDuplicatedName(entity.Parent, entity.Name);

            var parentEntity = await GetByIdAsync(entity.Parent);

            if (parentEntity == null)
            {
                throw new BadRequestException(ApiStatusCode.Blob_InvalidParent, InvalidParentField);
            }

            var ancestors = new List <BlobAncestor>(parentEntity.Ancestors ?? new List <BlobAncestor>());

            ancestors.Add(new BlobAncestor(parentEntity.Id, parentEntity.Name));
            entity.Ancestors = ancestors;

            await _blobs.CreateAsync(entity);

            // Activity Log.
            await _activityLogService.CreateAsync(SystemKeyword.CreateBlob, entity, embeddedUser, ip);

            return(entity);
        }
Пример #12
0
        public async Task <IEnumerable <string> > CreateFileAsync(IFormFile[] files, string parent, EmbeddedUser embeddedUser, string ip)
        {
            var parentEntity = string.IsNullOrWhiteSpace(parent) ?
                               await GetSystemFolder(_commonFolderName) :
                               await GetByIdAsync(parent);

            if (parentEntity == null)
            {
                throw new BadRequestException(ApiStatusCode.Blob_InvalidParent, InvalidParentField);
            }

            var rootVirtualPath  = parentEntity.VirtualPath;
            var rootPhysicalPath = parentEntity.PhysicalPath;

            if (string.IsNullOrEmpty(rootPhysicalPath))
            {
                var ancestorId = parentEntity.Ancestors?.FirstOrDefault()?.Id;
                var ancestor   = await GetByIdAsync(ancestorId);

                rootVirtualPath  = ancestor?.VirtualPath;
                rootPhysicalPath = ancestor?.PhysicalPath;
            }

            if (string.IsNullOrEmpty(rootPhysicalPath))
            {
                throw new BadRequestException(ApiStatusCode.Blob_InvalidParent, InvalidParentField);
            }

            var ancestors = new List <BlobAncestor>(parentEntity.Ancestors ?? new List <BlobAncestor>());

            ancestors.Add(new BlobAncestor(parentEntity.Id, parentEntity.Name));

            IList <string> results = new List <string>();
            Blob           entity;
            string         subType, fileName, randomName,
                           folderPhysicalPath, filePhysicalPath, fileVirtualPath;

            foreach (var file in files)
            {
                subType = file.GetSubTypeFromContentType();

                if (string.IsNullOrEmpty(subType))
                {
                    throw new BadRequestException(ApiStatusCode.Blob_InvalidMIMEType, "The MIME type is not valid.");
                }

                fileName =
                    ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Trim('"');
                randomName = GetRandomFileName(fileName);

                folderPhysicalPath = Path.Combine(rootPhysicalPath, subType);
                CreateDirectoryIfNotExist(folderPhysicalPath);

                filePhysicalPath = Path.Combine(folderPhysicalPath, randomName);

                if (File.Exists(filePhysicalPath))
                {
                    throw new BadRequestException(ApiStatusCode.Blob_DuplicatedName, $"The {randomName} was existed.");
                }

                fileVirtualPath = string.IsNullOrEmpty(rootVirtualPath) ?
                                  null :
                                  $"{rootVirtualPath}/{subType}/{randomName}";

                entity = new Blob
                {
                    Name          = fileName,
                    RandomName    = randomName,
                    FileExtension = Path.GetExtension(fileName).ToLowerInvariant(),
                    ContentType   = file.ContentType,
                    VirtualPath   = fileVirtualPath,
                    PhysicalPath  = filePhysicalPath,
                    Parent        = parentEntity.Id,
                    Ancestors     = ancestors
                };

                await Task.WhenAll(_blobs.CreateAsync(entity), file.SaveAsAsync(entity.PhysicalPath));

                // Activity Log.
                await _activityLogService.CreateAsync(SystemKeyword.CreateBlob, entity, embeddedUser, ip);

                results.Add(entity.Id);
            }

            return(results);
        }