public async Task <Guid> Copy(Guid projectID) { var existing = await(from p in DataContext.Projects.Include(x => x.DataMarts) where p.ID == projectID select p).SingleOrDefaultAsync(); if (existing == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "The Project could not be found.")); } if (!await DataContext.HasPermissions <Project>(Identity, existing.ID, PermissionIdentifiers.Project.Copy)) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You do not have permission to copy the specified project.")); } string newAcronym = "New " + existing.Acronym; string newName = "New " + existing.Name; while (await(from p in DataContext.Projects where !p.Deleted && (p.Name == newName && p.Acronym == newAcronym) select p).AnyAsync()) { newAcronym = "New " + newAcronym; newName = "New " + newName; } var project = new Project { Acronym = newAcronym, StartDate = DateTime.Today, Name = newName, GroupID = null, Description = existing.Description, Active = true }; DataContext.Projects.Add(project); var existingSecurityGroups = await(from sg in DataContext.SecurityGroups.Include(x => x.Users) where sg.OwnerID == existing.ID orderby sg.ParentSecurityGroupID select sg).ToArrayAsync(); var SecurityGroupMap = new Dictionary <Guid, Guid>(); CopySecurityGroups(existingSecurityGroups, ref SecurityGroupMap, null, project); await DataContext.SaveChangesAsync(); //All of these are done this way with a conditional if because the triggers cause inserts that entity framework is not aware of. Note that they are parameterized to ensure no sql injections. foreach (var user in existingSecurityGroups.SelectMany(u => u.Users).DistinctBy(u => new { u.SecurityGroupID, u.UserID })) { await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM SecurityGroupUsers WHERE UserID = @UserID AND SecurityGroupID = @SecurityGroupID) INSERT INTO SecurityGroupUsers (UserID, SecurityGroupID, Overridden) VALUES (@UserID, @SecurityGroupID, 0)" , new SqlParameter("UserID", user.UserID), new SqlParameter("SecurityGroupID", SecurityGroupMap[user.SecurityGroupID])); } //Data Marts foreach (var existingDataMart in existing.DataMarts) { var dm = new ProjectDataMart { DataMartID = existingDataMart.DataMartID, ProjectID = project.ID }; DataContext.ProjectDataMarts.Add(dm); } //RequestTypes var projRequestTypes = await(from rt in DataContext.ProjectRequestTypes where rt.ProjectID == existing.ID select rt).ToArrayAsync(); foreach (var exisitingRequestType in projRequestTypes) { await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM ProjectRequestTypes WHERE ProjectID = @ProjectID AND RequestTypeID = @RequestTypeID) INSERT INTO ProjectRequestTypes (ProjectID, RequestTypeID) VALUES (@ProjectID, @RequestTypeID)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("RequestTypeID", exisitingRequestType.RequestTypeID)); } var existingSecurityGroupIDs = SecurityGroupMap.Select(gm => gm.Key).ToArray(); //Project Acls var existingProjectAcls = await(from a in DataContext.ProjectAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectAcl in existingProjectAcls) { if (!SecurityGroupMap.ContainsKey(existingProjectAcl.SecurityGroupID)) { SecurityGroupMap.Add(existingProjectAcl.SecurityGroupID, existingProjectAcl.SecurityGroupID); } var count = await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM AclProjects WHERE ProjectID = @ProjectID AND SecurityGroupID = @SecurityGroupID AND PermissionID = @PermissionID) INSERT INTO AclProjects (ProjectID, SecurityGroupID, PermissionID, Allowed, Overridden) VALUES (@ProjectID, @SecurityGroupID, @PermissionID, @Allowed, 1)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("SecurityGroupID", SecurityGroupMap[existingProjectAcl.SecurityGroupID]), new SqlParameter("PermissionID", existingProjectAcl.PermissionID), new SqlParameter("Allowed", existingProjectAcl.Allowed)); } //Project Event Acls var existingProjectEventAcls = await(from a in DataContext.ProjectEvents where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectEventAcl in existingProjectEventAcls) { if (!SecurityGroupMap.ContainsKey(existingProjectEventAcl.SecurityGroupID)) { SecurityGroupMap.Add(existingProjectEventAcl.SecurityGroupID, existingProjectEventAcl.SecurityGroupID); } await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM ProjectEvents WHERE ProjectID = @ProjectID AND SecurityGroupID = @SecurityGroupID AND EventID = @EventID) INSERT INTO ProjectEvents (ProjectID, SecurityGroupID, EventID, Allowed, Overridden) VALUES (@ProjectID, @SecurityGroupID, @EventID, @Allowed, 0)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("SecurityGroupID", SecurityGroupMap[existingProjectEventAcl.SecurityGroupID]), new SqlParameter("EventID", existingProjectEventAcl.EventID), new SqlParameter("Allowed", existingProjectEventAcl.Allowed)); } //Project AclProjectRequestTypes Acls var existingProjectRequestTypesAcls = await(from a in DataContext.ProjectRequestTypeAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectRequestTypesAcl in existingProjectRequestTypesAcls) { if (!SecurityGroupMap.ContainsKey(existingProjectRequestTypesAcl.SecurityGroupID)) { SecurityGroupMap.Add(existingProjectRequestTypesAcl.SecurityGroupID, existingProjectRequestTypesAcl.SecurityGroupID); } var count = await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM AclProjectRequestTypes WHERE ProjectID = @ProjectID AND SecurityGroupID = @SecurityGroupID AND Permission = @Permission AND RequestTypeID = @RequestTypeID) INSERT INTO AclProjectRequestTypes (ProjectID, SecurityGroupID, Permission, RequestTypeID, Overridden) VALUES (@ProjectID, @SecurityGroupID, @Permission, @RequestTypeID, 1)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("SecurityGroupID", SecurityGroupMap[existingProjectRequestTypesAcl.SecurityGroupID]), new SqlParameter("Permission", existingProjectRequestTypesAcl.Permission), new SqlParameter("RequestTypeID", existingProjectRequestTypesAcl.RequestTypeID)); } //Project ProjectRequestTypeWorkflowActivities Acls var existingProjectRequestTypeWorkflowActivitiesAcls = await(from a in DataContext.ProjectRequestTypeWorkflowActivities where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectRequestTypeWorkflowActivitiesAcl in existingProjectRequestTypeWorkflowActivitiesAcls) { if (!SecurityGroupMap.ContainsKey(existingProjectRequestTypeWorkflowActivitiesAcl.SecurityGroupID)) { SecurityGroupMap.Add(existingProjectRequestTypeWorkflowActivitiesAcl.SecurityGroupID, existingProjectRequestTypeWorkflowActivitiesAcl.SecurityGroupID); } var count = await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM AclProjectRequestTypeWorkflowActivities WHERE ProjectID = @ProjectID AND SecurityGroupID = @SecurityGroupID AND PermissionID = @PermissionID AND RequestTypeID = @RequestTypeID AND WorkflowActivityID = @WorkflowActivityID) INSERT INTO AclProjectRequestTypeWorkflowActivities (ProjectID, SecurityGroupID, PermissionID, RequestTypeID, WorkflowActivityID, Allowed, Overridden) VALUES (@ProjectID, @SecurityGroupID, @PermissionID, @RequestTypeID, @WorkflowActivityID, @Allowed, 1)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("SecurityGroupID", SecurityGroupMap[existingProjectRequestTypeWorkflowActivitiesAcl.SecurityGroupID]), new SqlParameter("PermissionID", existingProjectRequestTypeWorkflowActivitiesAcl.PermissionID), new SqlParameter("RequestTypeID", existingProjectRequestTypeWorkflowActivitiesAcl.RequestTypeID), new SqlParameter("WorkflowActivityID", existingProjectRequestTypeWorkflowActivitiesAcl.WorkflowActivityID), new SqlParameter("Allowed", existingProjectRequestTypeWorkflowActivitiesAcl.Allowed)); } //Project DataMart Acls var existingProjectDataMartAcls = await(from a in DataContext.ProjectDataMartAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectDataMartAcl in existingProjectDataMartAcls) { if (!SecurityGroupMap.ContainsKey(existingProjectDataMartAcl.SecurityGroupID)) { SecurityGroupMap.Add(existingProjectDataMartAcl.SecurityGroupID, existingProjectDataMartAcl.SecurityGroupID); } var count = await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM AclProjectDataMarts WHERE ProjectID = @ProjectID AND SecurityGroupID = @SecurityGroupID AND PermissionID = @PermissionID AND DataMartID = @DataMartID) INSERT INTO AclProjectDataMarts (ProjectID, SecurityGroupID, PermissionID, DataMartID, Allowed, Overridden) VALUES (@ProjectID, @SecurityGroupID, @PermissionID, @DataMartID, @Allowed, 1)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("SecurityGroupID", SecurityGroupMap[existingProjectDataMartAcl.SecurityGroupID]), new SqlParameter("PermissionID", existingProjectDataMartAcl.PermissionID), new SqlParameter("DataMartID", existingProjectDataMartAcl.DataMartID), new SqlParameter("Allowed", existingProjectDataMartAcl.Allowed)); } //Project DataMart Request Type Acls var existingProjectDataMartRequestTypeAcls = await(from a in DataContext.ProjectDataMartRequestTypeAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectDataMartRequestTypeAcl in existingProjectDataMartRequestTypeAcls) { if (!SecurityGroupMap.ContainsKey(existingProjectDataMartRequestTypeAcl.SecurityGroupID)) { SecurityGroupMap.Add(existingProjectDataMartRequestTypeAcl.SecurityGroupID, existingProjectDataMartRequestTypeAcl.SecurityGroupID); } var count = await DataContext.Database.ExecuteSqlCommandAsync(@"IF NOT EXISTS(SELECT NULL FROM AclProjectDataMartRequestTypes WHERE ProjectID = @ProjectID AND SecurityGroupID = @SecurityGroupID AND Permission = @Permission AND RequestTypeID = @RequestTypeID AND DataMartID = @DataMartID) INSERT INTO AclProjectDataMartRequestTypes (ProjectID, SecurityGroupID, Permission, RequestTypeID, DataMartID, Overridden) VALUES (@ProjectID, @SecurityGroupID, @Permission, @RequestTypeID, @DataMartID, 1)" , new SqlParameter("ProjectID", project.ID), new SqlParameter("SecurityGroupID", SecurityGroupMap[existingProjectDataMartRequestTypeAcl.SecurityGroupID]), new SqlParameter("Permission", existingProjectDataMartRequestTypeAcl.Permission), new SqlParameter("RequestTypeID", existingProjectDataMartRequestTypeAcl.RequestTypeID), new SqlParameter("DataMartID", existingProjectDataMartRequestTypeAcl.DataMartID)); } await DataContext.SaveChangesAsync(); return(project.ID); }
public async Task <Guid> Copy(Guid projectID) { var existing = await(from p in DataContext.Projects.Include(x => x.DataMarts) where p.ID == projectID select p).SingleOrDefaultAsync(); if (existing == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "The Project could not be found.")); } if (!await DataContext.HasPermissions <Project>(Identity, existing.ID, PermissionIdentifiers.Project.Copy)) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You do not have permission to copy the specified project.")); } string newAcronym = "New " + existing.Acronym; string newName = "New " + existing.Name; while (await(from p in DataContext.Projects where !p.Deleted && (p.Name == newName && p.Acronym == newAcronym) select p).AnyAsync()) { newAcronym = "New " + newAcronym; newName = "New " + newName; } var project = new Project { Acronym = newAcronym, StartDate = DateTime.Today, Name = newName, GroupID = null, Description = existing.Description, Active = true }; DataContext.Projects.Add(project); //Security Groups var existingSecurityGroups = await(from sg in DataContext.SecurityGroups.Include(x => x.Users) where sg.OwnerID == existing.ID select sg).ToArrayAsync(); var SecurityGroupMap = new Dictionary <Guid, Guid>(); foreach (var existingSecurityGroup in existingSecurityGroups) { var sg = new SecurityGroup { Kind = existingSecurityGroup.Kind, Name = "New " + existingSecurityGroup.Name, OwnerID = project.ID, Type = DTO.Enums.SecurityGroupTypes.Project }; DataContext.SecurityGroups.Add(sg); SecurityGroupMap.Add(existingSecurityGroup.ID, sg.ID); foreach (var existingUser in existingSecurityGroup.Users) { DataContext.SecurityGroupUsers.Add(new SecurityGroupUser { Overridden = existingUser.Overridden, SecurityGroupID = sg.ID, UserID = existingUser.UserID }); } } //Data Marts foreach (var existingDataMart in existing.DataMarts) { var dm = new ProjectDataMart { DataMartID = existingDataMart.DataMartID, ProjectID = project.ID }; DataContext.ProjectDataMarts.Add(dm); } //Project Acls var existingProjectAcls = await(from a in DataContext.ProjectAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectAcl in existingProjectAcls) { DataContext.ProjectAcls.Add(new AclProject { Allowed = existingProjectAcl.Allowed, Overridden = existingProjectAcl.Overridden, PermissionID = existingProjectAcl.PermissionID, ProjectID = project.ID, SecurityGroupID = SecurityGroupMap.ContainsKey(existingProjectAcl.SecurityGroupID) ? SecurityGroupMap[existingProjectAcl.SecurityGroupID] : existingProjectAcl.SecurityGroupID }); } //Project Event Acls var existingProjectEventAcls = await(from a in DataContext.ProjectEvents where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectEventAcl in existingProjectEventAcls) { DataContext.ProjectEvents.Add(new ProjectEvent { Allowed = existingProjectEventAcl.Allowed, Overridden = existingProjectEventAcl.Overridden, ProjectID = project.ID, EventID = existingProjectEventAcl.EventID, SecurityGroupID = SecurityGroupMap.ContainsKey(existingProjectEventAcl.SecurityGroupID) ? SecurityGroupMap[existingProjectEventAcl.SecurityGroupID] : existingProjectEventAcl.SecurityGroupID }); } //Project DataMart Acls var existingProjectDataMartAcls = await(from a in DataContext.ProjectDataMartAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectDataMartAcl in existingProjectDataMartAcls) { DataContext.ProjectDataMartAcls.Add(new AclProjectDataMart { Allowed = existingProjectDataMartAcl.Allowed, Overridden = existingProjectDataMartAcl.Overridden, PermissionID = existingProjectDataMartAcl.PermissionID, ProjectID = project.ID, DataMartID = existingProjectDataMartAcl.DataMartID, SecurityGroupID = SecurityGroupMap.ContainsKey(existingProjectDataMartAcl.SecurityGroupID) ? SecurityGroupMap[existingProjectDataMartAcl.SecurityGroupID] : existingProjectDataMartAcl.SecurityGroupID }); } //Project DataMart Event Acls //TODO -Jamie //Project DataMart Request Type Acls var existingProjectDataMartRequestTypeAcls = await(from a in DataContext.ProjectDataMartRequestTypeAcls where a.ProjectID == existing.ID select a).ToArrayAsync(); foreach (var existingProjectDataMartRequestTypeAcl in existingProjectDataMartRequestTypeAcls) { DataContext.ProjectDataMartRequestTypeAcls.Add(new AclProjectDataMartRequestType { Permission = existingProjectDataMartRequestTypeAcl.Permission, Overridden = existingProjectDataMartRequestTypeAcl.Overridden, ProjectID = project.ID, RequestTypeID = existingProjectDataMartRequestTypeAcl.RequestTypeID, DataMartID = existingProjectDataMartRequestTypeAcl.DataMartID, SecurityGroupID = SecurityGroupMap.ContainsKey(existingProjectDataMartRequestTypeAcl.SecurityGroupID) ? SecurityGroupMap[existingProjectDataMartRequestTypeAcl.SecurityGroupID] : existingProjectDataMartRequestTypeAcl.SecurityGroupID }); } await DataContext.SaveChangesAsync(); return(project.ID); }