public CampaignViewModel(Campaign campaign)
        {
            if (campaign == null)
                return;

            Id = campaign.Id;
            Name = campaign.Name;
            Description = campaign.Description;
            ManagingTenantName = campaign.ManagingTenant.Name;
            ManagingTenantId = campaign.ManagingTenant.Id;
            StartDate = new DateTimeOffset(campaign.StartDateTimeUtc, TimeSpan.Zero);
            EndDate = new DateTimeOffset(campaign.EndDateTimeUtc, TimeSpan.Zero);
            Activities = campaign.Activities != null ? campaign.Activities.ToViewModel() : Enumerable.Empty<ActivityViewModel>();
        }
        public async Task<IActionResult> Create(Campaign campaign)
        {
            var currentUser = await _userManager.GetCurrentUser(Context);
            if (currentUser == null)
            {
                return new HttpUnauthorizedResult();
            }

            // Workaround: Sometimes AssociatedTenant is missing when using UserManager
            var currentUserWithAssociatedTenant = _dataAccess.GetUser(currentUser.Id);
            if (!await UserIsTenantAdminOfCampaign(currentUserWithAssociatedTenant, campaign))
            {
                return new HttpUnauthorizedResult();
            }

            if (ModelState.IsValid)
            {
                await _dataAccess.AddCampaign(campaign);
                return RedirectToAction("Index");
            }

            return WithTenants(View(campaign));
        }
 Task IPrepOpsDataAccess.UpdateCampaign(Campaign value)
 {
     _dbContext.Campaigns.Update(value);
     return _dbContext.SaveChangesAsync();
 }
 private async Task<bool> UserIsTenantAdminOfCampaign(ApplicationUser user, Campaign campaignToCheck)
 {
     return await _userManager.IsSiteAdmin(user) ||
         ((user.AssociatedTenant != null) && (campaignToCheck.ManagingTenantId == user.AssociatedTenant.Id));
 }
        public async Task<IActionResult> Edit(Campaign campaign)
        {
            var currentUser = await _userManager.GetCurrentUser(Context);
            if (currentUser == null)
            {
                return new HttpUnauthorizedResult();
            }

            if (_dataAccess.GetUser(currentUser.Id).AssociatedTenant.Id != campaign.ManagingTenantId)
            {
                return new HttpUnauthorizedResult();
            }

            if (ModelState.IsValid)
            {
                await _dataAccess.UpdateCampaign(campaign);
                return RedirectToAction("Index");
            }
            return View(campaign);
        }