Esempio n. 1
0
        public static IEnumerable <ConnectionProfileInfo> GetProfiles(string dbType, bool isSampleMode = false)
        {
            IEnumerable <ConnectionProfileInfo> profiles = Enumerable.Empty <ConnectionProfileInfo>();

            string filePath = ProfilePath;

            if (File.Exists(filePath))
            {
                profiles = ((IEnumerable <ConnectionProfileInfo>)JsonConvert.DeserializeObject(File.ReadAllText(ProfilePath), typeof(IEnumerable <ConnectionProfileInfo>)))
                           .Where(item => (item.DatabaseType == dbType || string.IsNullOrEmpty(dbType)));

                if (!isSampleMode)
                {
                    var accountProfiles = AccountProfileManager.GetProfiles(dbType);
                    foreach (var profile in profiles)
                    {
                        AccountProfileInfo accountProfile = accountProfiles.FirstOrDefault(item => item.Id == profile.AccountProfileId);

                        if (accountProfile != null)
                        {
                            profile.ConnectionInfo          = GetConnectionInfo(accountProfile);
                            profile.ConnectionInfo.Database = profile.Database;
                        }
                    }
                }
            }

            return(profiles.OrderBy(item => item.Name));
        }
Esempio n. 2
0
        public static ConnectionInfo GetConnectionInfo(string dbType, string profileName)
        {
            ConnectionInfo connectionInfo = null;

            string filePath = ProfilePath;

            if (File.Exists(filePath))
            {
                IEnumerable <ConnectionProfileInfo> profiles = (IEnumerable <ConnectionProfileInfo>)JsonConvert.DeserializeObject(File.ReadAllText(ProfilePath), typeof(IEnumerable <ConnectionProfileInfo>));

                ConnectionProfileInfo profile = profiles.FirstOrDefault(item => item.DatabaseType == dbType.ToString() && profileName == item.Name);

                if (profile != null)
                {
                    AccountProfileInfo accountProfile = AccountProfileManager.GetProfiles(dbType).FirstOrDefault(item => item.Id == profile.AccountProfileId);

                    if (accountProfile != null)
                    {
                        connectionInfo = GetConnectionInfo(accountProfile);
                    }

                    if (connectionInfo != null)
                    {
                        connectionInfo.Database = profile.Database;
                    }
                }
            }

            return(connectionInfo);
        }
Esempio n. 3
0
        private static ConnectionInfo GetConnectionInfo(AccountProfileInfo accountProfile)
        {
            ConnectionInfo connectionInfo = new ConnectionInfo();

            ObjectHelper.CopyProperties(accountProfile, connectionInfo);

            return(connectionInfo);
        }
        public static Guid Save(AccountProfileInfo info, bool rememberPassword)
        {
            AccountProfileInfo cloneInfo = ObjectHelper.CloneObject <AccountProfileInfo>(info);

            if (!rememberPassword || info.IntegratedSecurity)
            {
                cloneInfo.Password = "";
            }
            else if (rememberPassword && !string.IsNullOrEmpty(info.Password))
            {
                cloneInfo.Password = AesHelper.Encrypt(info.Password);
            }

            List <AccountProfileInfo> profiles = new List <AccountProfileInfo>();

            if (File.Exists(ProfilePath))
            {
                profiles = (List <AccountProfileInfo>)JsonConvert.DeserializeObject(File.ReadAllText(ProfilePath), typeof(List <AccountProfileInfo>));
            }

            AccountProfileInfo oldProfile = profiles.FirstOrDefault(item => item.Id == info.Id);

            Guid id = info.Id;

            if (oldProfile == null)
            {
                profiles.Add(cloneInfo);
            }
            else
            {
                id = oldProfile.Id;

                ObjectHelper.CopyProperties(info, oldProfile);

                if (rememberPassword && !string.IsNullOrEmpty(info.Password))
                {
                    oldProfile.Password = AesHelper.Encrypt(info.Password);
                }
            }

            File.WriteAllText(ProfilePath, JsonConvert.SerializeObject(profiles, Formatting.Indented));

            return(id);
        }
Esempio n. 5
0
        public static string Save(ConnectionProfileInfo info, bool rememberPassword)
        {
            ConnectionInfo connectionInfo = ObjectHelper.CloneObject <ConnectionInfo>(info.ConnectionInfo);

            info.Database = connectionInfo.Database;

            if (!rememberPassword)
            {
                connectionInfo.Password = "";
            }

            string encrptedPassword = "";

            if (rememberPassword && !string.IsNullOrEmpty(connectionInfo.Password))
            {
                encrptedPassword = AesHelper.Encrypt(connectionInfo.Password);
            }

            string profileName = info.Name;

            if (string.IsNullOrEmpty(profileName))
            {
                profileName = info.ConnectionDescription;
            }

            var accountProfiles = AccountProfileManager.GetProfiles(info.DatabaseType);

            AccountProfileInfo accountProfile = accountProfiles.FirstOrDefault(item => item.Server == connectionInfo.Server &&
                                                                               item.Port == connectionInfo.Port &&
                                                                               item.UserId == connectionInfo.UserId &&
                                                                               item.IntegratedSecurity == connectionInfo.IntegratedSecurity);

            bool changed = false;

            if (accountProfile != null)
            {
                if (!accountProfile.IntegratedSecurity && accountProfile.Password != encrptedPassword)
                {
                    changed = true;
                    accountProfile.Password = connectionInfo.Password;
                }
            }
            else
            {
                changed        = true;
                accountProfile = new AccountProfileInfo()
                {
                    DatabaseType = info.DatabaseType
                };
                ObjectHelper.CopyProperties(connectionInfo, accountProfile);
            }

            if (changed)
            {
                AccountProfileManager.Save(accountProfile, rememberPassword);
            }

            List <ConnectionProfileInfo> profiles = new List <ConnectionProfileInfo>();

            if (File.Exists(ProfilePath))
            {
                profiles = (List <ConnectionProfileInfo>)JsonConvert.DeserializeObject(File.ReadAllText(ProfilePath), typeof(List <ConnectionProfileInfo>));
            }

            ConnectionProfileInfo oldProfile = profiles.FirstOrDefault(item => item.Name == info.Name && item.DatabaseType == info.DatabaseType);

            if (oldProfile == null)
            {
                info.AccountProfileId = accountProfile.Id;
                profiles.Add(info);
            }
            else
            {
                ObjectHelper.CopyProperties(info, oldProfile);
            }

            File.WriteAllText(ProfilePath, JsonConvert.SerializeObject(profiles, Formatting.Indented));

            return(profileName);
        }