Пример #1
0
        public ActionResult Edit(DatabaseConnectionViewModel viewModel)
        {
            DatabaseConnection existingConnection = db.DatabaseConnections.Find(viewModel.DatabaseConnectionID);

            if (existingConnection == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                string error;
                if (TryMapViewModel(viewModel, ref existingConnection, out error) == false)
                {
                    ModelState.AddModelError("ssh-key-issue", "There was a problem with the SSH key file, please upload it again.");
                    viewModel.SshKeyFileID = null;
                    return(View(viewModel));
                }

                // save to database
                db.Entry(existingConnection).State = EntityState.Modified;
                db.SaveChanges();

                SaveSecureInformation(existingConnection, viewModel);

                return(RedirectToAction("Details", new { id = existingConnection.DatabaseConnectionID }));
            }

            return(View(viewModel));
        }
Пример #2
0
        // GET: DatabaseConnections/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            var database = db.DatabaseConnections.FirstOrDefault(dc => dc.DatabaseConnectionID == id.Value);

            if (database == null)
            {
                return(NotFound());
            }

            var userPermissions = db.UserDatabaseConnections
                                  .Where(uc => uc.ApplicationUserID == CurrentUser.Id)
                                  .ToList();

            if (PermissionMgr.UserCanModifyDatabase(userPermissions, database) == false && database.OrganisationId != CurrentUser.OrganisationId)
            {
                return(NotFound());
            }

            ViewBag.hasExistingKeyFile = database.UseSshKey;

            var viewModel = new DatabaseConnectionViewModel(database);

            if (viewModel.SshPort == null)
            {
                viewModel.SshPort = 22;
            }

            return(View(viewModel));
        }
Пример #3
0
        public ActionResult Create(DatabaseConnectionViewModel viewModel)
        {
            ViewBag.TestMessage = "";
            ViewBag.WillUpgrade = false;

            if (ModelState.IsValid)
            {
                DatabaseConnection connection = new DatabaseConnection
                {
                    CreatedOn      = DateTime.Now,
                    OrganisationId = CurrentUser.OrganisationId
                };

                string error;
                if (TryMapViewModel(viewModel, ref connection, out error) == false)
                {
                    ModelState.AddModelError("ssh-key-issue", "There was a problem with the SSH key file, please upload it again.");
                    viewModel.SshKeyFileID = null;
                    return(View(viewModel));
                }

                db.DatabaseConnections.Add(connection);

                db.SaveChanges();

                SaveSecureInformation(connection, viewModel);

                return(RedirectToAction("Details", new { id = connection.DatabaseConnectionID }));
            }

            return(View(viewModel));
        }
Пример #4
0
        private void SaveSecureInformation(DatabaseConnection connection, DatabaseConnectionViewModel viewModel)
        {
            if (viewModel.DbPssword != null)
            {
                _passwordManager.SetSecret(SecretType.DatabasePassword.ToString() + "_" + connection.DatabaseConnectionID, viewModel.DbPssword);
            }

            if (connection.UseSsh && connection.UseSshKey == false && viewModel.SshPassword != null)
            {
                _passwordManager.SetSecret(SecretType.SSHPassword.ToString() + "_" + connection.DatabaseConnectionID, viewModel.SshPassword);
            }

            if (connection.UseSsh == false || connection.UseSshKey)
            {
                _passwordManager.DeleteSecret(SecretType.SSHPassword.ToString() + "_" + connection.DatabaseConnectionID);
            }
        }
 public DatabaseConnectionDialog(DatabaseConnectionViewModel viewModel)
 {
     _viewModel = viewModel;
     this.InitializeComponent();
 }
Пример #6
0
        private bool TryMapViewModel(DatabaseConnectionViewModel viewModel, ref DatabaseConnection connection, out string error)
        {
            error = null;

            // map fields
            connection.DatabaseName = viewModel.DatabaseName;
            connection.Name         = viewModel.Name;
            connection.Port         = viewModel.Port;
            connection.Server       = viewModel.Server;
            connection.Type         = viewModel.Type;
            connection.Username     = viewModel.Username;
            connection.Description  = viewModel.Description;

            if (connection.Type == DatabaseType.MySQL || connection.Type == DatabaseType.PostgreSQL)
            {
                connection.UseSsh = viewModel.UseSsh;
            }
            else
            {
                connection.UseSsh = false;
            }

            if (connection.UseSsh)
            {
                connection.SshServer   = viewModel.SshServer;
                connection.SshPort     = viewModel.SshPort;
                connection.SshUsername = viewModel.SshUsername;
                connection.UseSshKey   = viewModel.UseSshKey;

                if (connection.UseSshKey)
                {
                    if (connection.SshKeyFileID != viewModel.SshKeyFileID)
                    {
                        var userId = _userManager.GetUserId(User);

                        var newSshKey = db.SshKeyFiles.SingleOrDefault(k => k.Id == viewModel.SshKeyFileID && k.CreatedBy.Id == userId);
                        if (newSshKey == null)
                        {
                            error = "There was a problem with the new SSH key file, please upload it again.";
                            viewModel.SshKeyFileID = null;
                            return(false);
                        }
                        else
                        {
                            connection.SshKeyFileID = newSshKey.Id;
                            connection.SshKeyFile   = newSshKey;
                        }
                    }
                }
                else
                {
                    connection.SshKeyFileID = null;
                    connection.SshKeyFile   = null;
                }
            }
            else
            {
                connection.SshUsername = null;
                connection.SshServer   = null;
                connection.SshPort     = null;
            }

            return(true);
        }