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)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(Activity activity)
        {
            var currentUser = await _userManager.GetCurrentUser(Context);

            if (currentUser == null || !await _userManager.IsTenantAdmin(currentUser))
            {
                return(new HttpUnauthorizedResult());
            }

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

            if (ModelState.IsValid)
            {
                await _dataAccess.AddActivity(activity);

                return(RedirectToAction("Index"));
            }
            return(View(activity));
        }
Exemplo n.º 3
0
        public static PrepOpsTask ToModel(this TaskViewModel task, IPrepOpsDataAccess dataAccess)
        {
            var activity = dataAccess.GetActivity(task.ActivityId);

            if (activity == null)
                return null;

            bool newTask = true;
            PrepOpsTask dbtask;

            if (task.Id == 0)
            {
                dbtask = new PrepOpsTask();
            }
            else
            {
                dbtask = dataAccess.GetTask(task.Id);
                newTask = false;
            }

            // dbtask.Id = task.Id;
            dbtask.Description = task.Description;
            dbtask.Activity = activity;
            dbtask.EndDateTimeUtc = task.EndDateTime.HasValue ? task.EndDateTime.Value.UtcDateTime : new Nullable<DateTime>();
            dbtask.StartDateTimeUtc = task.EndDateTime.HasValue ? task.StartDateTime.Value.UtcDateTime : new Nullable<DateTime>();
            dbtask.Name = task.Name;

            // Workaround:  POST is bringing in empty AssignedVolunteers.  Clean this up. Discussing w/ Kiran Challa.
            // Workaround: the if statement is superflous, and should go away once we have the proper fix referenced above.
            if (task.AssignedVolunteers != null)
            {
                var bogusAssignedVolunteers = (from assignedVolunteer in task.AssignedVolunteers
                                               where string.IsNullOrEmpty(assignedVolunteer.UserId)
                                               select assignedVolunteer).ToList();
                foreach (var bogus in bogusAssignedVolunteers)
                {
                    task.AssignedVolunteers.Remove(bogus);
                }
            }
            // end workaround

            if (task.AssignedVolunteers != null && task.AssignedVolunteers.Count > 0)
            {
                var taskUsersList = task.AssignedVolunteers.Select(tvm => new TaskUsers
                {
                    Task = dbtask,
                    User = dataAccess.GetUser(tvm.UserId)
                }).ToList();

                // We may be updating an existing task
                if (newTask || dbtask.AssignedVolunteers.Count == 0)
                {
                    dbtask.AssignedVolunteers = taskUsersList;
                }
                else
                {
                    // Can probably rewrite this more efficiently.
                    foreach (TaskUsers taskUsers in taskUsersList)
                    {
                        if (!(from entry in dbtask.AssignedVolunteers
                              where entry.User.Id == taskUsers.User.Id
                              select entry).Any())
                        {
                            dbtask.AssignedVolunteers.Add(taskUsers);
                        }
                    }
                }
            }

            return dbtask;
        }
Exemplo n.º 4
0
        public static PrepOpsTask ToModel(this TaskViewModel task, IPrepOpsDataAccess dataAccess)
        {
            var activity = dataAccess.GetActivity(task.ActivityId);

            if (activity == null)
            {
                return(null);
            }

            bool        newTask = true;
            PrepOpsTask dbtask;

            if (task.Id == 0)
            {
                dbtask = new PrepOpsTask();
            }
            else
            {
                dbtask  = dataAccess.GetTask(task.Id);
                newTask = false;
            }

            // dbtask.Id = task.Id;
            dbtask.Description      = task.Description;
            dbtask.Activity         = activity;
            dbtask.EndDateTimeUtc   = task.EndDateTime.HasValue ? task.EndDateTime.Value.UtcDateTime : new Nullable <DateTime>();
            dbtask.StartDateTimeUtc = task.EndDateTime.HasValue ? task.StartDateTime.Value.UtcDateTime : new Nullable <DateTime>();
            dbtask.Name             = task.Name;

            // Workaround:  POST is bringing in empty AssignedVolunteers.  Clean this up. Discussing w/ Kiran Challa.
            // Workaround: the if statement is superflous, and should go away once we have the proper fix referenced above.
            if (task.AssignedVolunteers != null)
            {
                var bogusAssignedVolunteers = (from assignedVolunteer in task.AssignedVolunteers
                                               where string.IsNullOrEmpty(assignedVolunteer.UserId)
                                               select assignedVolunteer).ToList();
                foreach (var bogus in bogusAssignedVolunteers)
                {
                    task.AssignedVolunteers.Remove(bogus);
                }
            }
            // end workaround


            if (task.AssignedVolunteers != null && task.AssignedVolunteers.Count > 0)
            {
                var taskUsersList = task.AssignedVolunteers.Select(tvm => new TaskUsers
                {
                    Task = dbtask,
                    User = dataAccess.GetUser(tvm.UserId)
                }).ToList();

                // We may be updating an existing task
                if (newTask || dbtask.AssignedVolunteers.Count == 0)
                {
                    dbtask.AssignedVolunteers = taskUsersList;
                }
                else
                {
                    // Can probably rewrite this more efficiently.
                    foreach (TaskUsers taskUsers in taskUsersList)
                    {
                        if (!(from entry in dbtask.AssignedVolunteers
                              where entry.User.Id == taskUsers.User.Id
                              select entry).Any())
                        {
                            dbtask.AssignedVolunteers.Add(taskUsers);
                        }
                    }
                }
            }

            return(dbtask);
        }