public bool SaveEncryptedData(IEnumerable <EncryptedData> ecnryptedDatas)
        {
            if (ecnryptedDatas == null)
            {
                throw new ArgumentNullException("ecnryptedDatas");
            }

            lock (SyncRoot)
            {
                using var tx = FilesDbContext.Database.BeginTransaction();
                foreach (var test in ecnryptedDatas)
                {
                    if (string.IsNullOrEmpty(test.PublicKey) ||
                        string.IsNullOrEmpty(test.FileHash) ||
                        string.IsNullOrEmpty(test.Data))
                    {
                        continue;
                    }
                    var encryptedData = new DbEncryptedData
                    {
                        PublicKey = test.PublicKey,
                        FileHash  = test.FileHash,
                        Data      = test.Data
                    };

                    FilesDbContext.AddOrUpdate(r => r.EncryptedData, encryptedData);
                }

                tx.Commit();
            }
            return(true);
        }
Esempio n. 2
0
        public void AddLink(string sourceId, string linkedId)
        {
            FilesDbContext.AddOrUpdate(r => r.FilesLink, new DbFilesLink()
            {
                TenantId  = TenantID,
                SourceId  = sourceId,
                LinkedId  = linkedId,
                LinkedFor = AuthContext.CurrentAccount.ID
            });

            FilesDbContext.SaveChanges();
        }
Esempio n. 3
0
        public void SaveToken(Token token)
        {
            var dbFilesThirdpartyApp = new DbFilesThirdpartyApp
            {
                App      = token.App,
                Token    = EncryptToken(token),
                UserId   = AuthContext.CurrentAccount.ID,
                TenantId = TenantManager.GetCurrentTenant().TenantId
            };

            FilesDbContext.AddOrUpdate(r => r.ThirdpartyApp, dbFilesThirdpartyApp);
            FilesDbContext.SaveChanges();
        }
Esempio n. 4
0
        private Tag SaveTag(Tag t, Dictionary <string, int> cacheTagId, DateTime createOn)
        {
            var cacheTagIdKey = string.Join("/", new[] { TenantID.ToString(), t.Owner.ToString(), t.TagName, ((int)t.TagType).ToString(CultureInfo.InvariantCulture) });

            if (!cacheTagId.TryGetValue(cacheTagIdKey, out var id))
            {
                id = FilesDbContext.Tag
                     .Where(r => r.Owner == t.Owner)
                     .Where(r => r.Name == t.TagName)
                     .Where(r => r.Flag == t.TagType)
                     .Select(r => r.Id)
                     .FirstOrDefault();

                if (id == 0)
                {
                    var toAdd = new DbFilesTag
                    {
                        Id       = 0,
                        Name     = t.TagName,
                        Owner    = t.Owner,
                        Flag     = t.TagType,
                        TenantId = TenantID
                    };

                    toAdd = FilesDbContext.Tag.Add(toAdd).Entity;
                    FilesDbContext.SaveChanges();
                    id = toAdd.Id;
                }

                cacheTagId.Add(cacheTagIdKey, id);
            }

            t.Id = id;

            var linkToInsert = new DbFilesTagLink
            {
                TenantId  = TenantID,
                TagId     = id,
                EntryId   = MappingID(t.EntryId, true).ToString(),
                EntryType = t.EntryType,
                CreateBy  = AuthContext.CurrentAccount.ID,
                CreateOn  = createOn,
                TagCount  = t.Count
            };

            FilesDbContext.AddOrUpdate(r => r.TagLink, linkToInsert);
            FilesDbContext.SaveChanges();

            return(t);
        }
Esempio n. 5
0
        protected object MappingID(object id, bool saveIfNotExist)
        {
            if (id == null)
            {
                return(null);
            }

            var isNumeric = int.TryParse(id.ToString(), out var n);

            if (isNumeric)
            {
                return(n);
            }

            object result;

            if (id.ToString().StartsWith("sbox") ||
                id.ToString().StartsWith("box") ||
                id.ToString().StartsWith("dropbox") ||
                id.ToString().StartsWith("spoint") ||
                id.ToString().StartsWith("drive") ||
                id.ToString().StartsWith("onedrive"))
            {
                result = Regex.Replace(BitConverter.ToString(Hasher.Hash(id.ToString(), HashAlg.MD5)), "-", "").ToLower();
            }
            else
            {
                result = Query(FilesDbContext.ThirdpartyIdMapping)
                         .Where(r => r.HashId == id.ToString())
                         .Select(r => r.Id)
                         .FirstOrDefault();
            }

            if (saveIfNotExist)
            {
                var newItem = new DbFilesThirdpartyIdMapping
                {
                    Id     = id.ToString(),
                    HashId = result.ToString()
                };

                FilesDbContext.AddOrUpdate(r => r.ThirdpartyIdMapping, newItem);
            }

            return(result);
        }
        public virtual int SaveProviderInfo(string providerKey, string customerTitle, AuthData authData, FolderType folderType)
        {
            ProviderTypes prKey;

            try
            {
                prKey = (ProviderTypes)Enum.Parse(typeof(ProviderTypes), providerKey, true);
            }
            catch (Exception)
            {
                throw new ArgumentException("Unrecognize ProviderType");
            }

            authData = GetEncodedAccesToken(authData, prKey);

            if (!CheckProviderInfo(ToProviderInfo(0, prKey, customerTitle, authData, SecurityContext.CurrentAccount.ID, folderType, TenantUtil.DateTimeToUtc(TenantUtil.DateTimeNow()))))
            {
                throw new UnauthorizedAccessException(string.Format(FilesCommonResource.ErrorMassage_SecurityException_Auth, providerKey));
            }
            var dbFilesThirdpartyAccount = new DbFilesThirdpartyAccount
            {
                Id         = 0,
                TenantId   = TenantID,
                Provider   = providerKey,
                Title      = Global.ReplaceInvalidCharsAndTruncate(customerTitle),
                UserName   = authData.Login ?? "",
                Password   = EncryptPassword(authData.Password),
                FolderType = folderType,
                CreateOn   = TenantUtil.DateTimeToUtc(TenantUtil.DateTimeNow()),
                UserId     = SecurityContext.CurrentAccount.ID,
                Token      = EncryptPassword(authData.Token ?? ""),
                Url        = authData.Url ?? ""
            };

            return(FilesDbContext.AddOrUpdate(r => r.ThirdpartyAccount, dbFilesThirdpartyAccount).Id);
        }
Esempio n. 7
0
        public void SetShare(FileShareRecord r)
        {
            if (r.Share == FileShare.None)
            {
                var entryId = (MappingID(r.EntryId) ?? "").ToString();
                if (string.IsNullOrEmpty(entryId))
                {
                    return;
                }

                using (var tx = FilesDbContext.Database.BeginTransaction())
                {
                    var files = new List <string>();

                    if (r.EntryType == FileEntryType.Folder)
                    {
                        var folders = new List <string>();
                        if (int.TryParse(entryId, out var intEntryId))
                        {
                            var foldersInt = FilesDbContext.Tree
                                             .Where(r => r.ParentId.ToString() == entryId)
                                             .Select(r => r.FolderId)
                                             .ToList();

                            folders.AddRange(foldersInt.Select(folderInt => folderInt.ToString()));
                            files.AddRange(Query(FilesDbContext.Files).Where(r => foldersInt.Any(a => a == r.FolderId)).Select(r => r.Id.ToString()));
                        }
                        else
                        {
                            folders.Add(entryId);
                        }

                        var toDelete = FilesDbContext.Security
                                       .Where(a => a.TenantId == r.Tenant)
                                       .Where(a => folders.Any(b => b == a.EntryId))
                                       .Where(a => a.EntryType == FileEntryType.Folder)
                                       .Where(a => a.Subject == r.Subject);

                        FilesDbContext.Security.RemoveRange(toDelete);
                        FilesDbContext.SaveChanges();
                    }
                    else
                    {
                        files.Add(entryId);
                    }

                    if (0 < files.Count)
                    {
                        var toDelete = FilesDbContext.Security
                                       .Where(a => a.TenantId == r.Tenant)
                                       .Where(a => files.Any(b => b == a.EntryId))
                                       .Where(a => a.EntryType == FileEntryType.File)
                                       .Where(a => a.Subject == r.Subject);

                        FilesDbContext.Security.RemoveRange(toDelete);
                        FilesDbContext.SaveChanges();
                    }

                    tx.Commit();
                }
            }
            else
            {
                var toInsert = new DbFilesSecurity
                {
                    TenantId  = r.Tenant,
                    EntryId   = MappingID(r.EntryId, true).ToString(),
                    EntryType = r.EntryType,
                    Subject   = r.Subject,
                    Owner     = r.Owner,
                    Security  = r.Share,
                    TimeStamp = DateTime.UtcNow
                };

                FilesDbContext.AddOrUpdate(r => r.Security, toInsert);
                FilesDbContext.SaveChanges();
            }
        }