예제 #1
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));
        }
예제 #2
0
        // GET: DatabaseConnections/Details/5
        public ActionResult Details(int id)
        {
            var database = db.DatabaseConnections.FirstOrDefault(dc => dc.DatabaseConnectionID == id);

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

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

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

            ViewBag.UserIsOrganisationAdmin     = database.OrganisationId == CurrentUser.OrganisationId;
            ViewBag.UserCanModifyQueries        = PermissionMgr.UserCanModifyQuery(userPermissions, database) || database.OrganisationId == CurrentUser.OrganisationId;
            ViewBag.UserCanModifyDatabase       = PermissionMgr.UserCanModifyDatabase(userPermissions, database) || database.OrganisationId == CurrentUser.OrganisationId;
            ViewBag.UserCanManageDatabaseAccess = PermissionMgr.UserCanManageDatabaseAccess(userPermissions, database) || database.OrganisationId == CurrentUser.OrganisationId;
            ViewBag.UserCanDeleteDatabase       = PermissionMgr.UserCanDeleteDatabase(userPermissions, database) || database.OrganisationId == CurrentUser.OrganisationId;

            var viewModel = new DatabaseConnectionDetailsViewModel
            {
                DatabaseConnectionID = database.DatabaseConnectionID,
                Name         = database.Name,
                Type         = database.Type,
                Server       = database.Server,
                Port         = database.Port,
                Username     = database.Username,
                DatabaseName = database.DatabaseName,
                Description  = database.Description,
                UseSsh       = database.UseSsh,
                SshServer    = database.SshServer,
                SshPort      = database.SshPort,
                SshUsername  = database.SshUsername,
                AccessUsers  = new List <UserDatabaseConnection>()
            };
            List <DatabaseConnectionQueriesDetailsViewModel> viewQueries = new List <DatabaseConnectionQueriesDetailsViewModel>();

            var queries = db.Queries
                          .Include(q => q.CreatedBy)
                          .Include(q => q.LastEditedBy)
                          .Where(q => q.DatabaseConnectionID == database.DatabaseConnectionID);

            foreach (var query in queries)
            {
                DatabaseConnectionQueriesDetailsViewModel queryView = new DatabaseConnectionQueriesDetailsViewModel();
                queryView.QueryID       = query.QueryID;
                queryView.Name          = query.Name;
                queryView.Description   = query.Description;
                queryView.IsSimpleQuery = query.IsSimpleQuery;
                queryView.CreatedBy     = query.CreatedBy;
                queryView.CreatedOn     = query.CreatedOn;
                queryView.LastEditedBy  = query.LastEditedBy;
                queryView.LastEditedOn  = query.LastEditedOn;
                viewQueries.Add(queryView);
            }

            viewModel.SavedQueries = viewQueries.OrderByDescending(q => q.LastEditedOn);

            if (database.Organisation != null)
            {
                viewModel.OrganisationName = database.Organisation.OrganisationName;
            }
            else
            {
                viewModel.OrganisationName = "None";
            }

            if (ViewBag.UserCanModifyDatabase)
            {
                // Don't allow users to manage themselves, things get strange very quickly
                var accessUsers = db.UserDatabaseConnections
                                  .Where(u => u.DatabaseConnection.DatabaseConnectionID == database.DatabaseConnectionID && u.ApplicationUserID != CurrentUser.Id)
                                  .ToList();

                foreach (var accessUser in accessUsers)
                {
                    viewModel.AccessUsers.Add(new UserDatabaseConnection
                    {
                        UserDatabaseConnectionID = accessUser.UserDatabaseConnectionID,
                        DatabaseConnectionID     = accessUser.DatabaseConnectionID,
                        ApplicationUser          = accessUser.ApplicationUser,
                        Type        = accessUser.Type,
                        CreatedBy   = accessUser.CreatedBy,
                        CreatedOn   = accessUser.CreatedOn,
                        InviteEmail = accessUser.InviteEmail,
                    });
                }
            }

            return(View(viewModel));
        }