Ejemplo n.º 1
0
        public async Task <IActionResult> EditGroup(int departmentGroupId)
        {
            if (!await _authorizationService.CanUserEditDepartmentGroupAsync(UserId, departmentGroupId))
            {
                Unauthorized();
            }

            EditGroupView model = new EditGroupView();

            model.Users = await _departmentsService.GetAllUsersForDepartmentAsync(DepartmentId);

            model.Groups = await _departmentGroupsService.GetAllGroupsForDepartmentAsync(DepartmentId);

            model.EditGroup = await _departmentGroupsService.GetGroupByIdAsync(departmentGroupId);

            model.InternalDispatchEmail = $"{model.EditGroup.DispatchEmail}@{Config.InboundEmailConfig.GroupsDomain}";
            model.Latitude  = model.EditGroup.Latitude;
            model.Longitude = model.EditGroup.Longitude;
            model.What3Word = model.EditGroup.What3Words;

            List <DepartmentGroup> groups = new List <DepartmentGroup>();

            groups.Add(new DepartmentGroup {
                DepartmentGroupId = -1, Name = "None"
            });
            groups.AddRange(model.Groups.Where(x => x.Type.HasValue && x.Type.Value == (int)DepartmentGroupTypes.Station));
            model.StationGroups = new SelectList(groups, "DepartmentGroupId", "Name");

            if (model.EditGroup.Address != null && model.EditGroup.Type.HasValue && model.EditGroup.Type.Value == (int)DepartmentGroupTypes.Station)
            {
                model.Address1   = model.EditGroup.Address.Address1;
                model.City       = model.EditGroup.Address.City;
                model.Country    = model.EditGroup.Address.Country;
                model.PostalCode = model.EditGroup.Address.PostalCode;
                model.State      = model.EditGroup.Address.State;
            }

            if (!String.IsNullOrWhiteSpace(model.EditGroup.PrinterData))
            {
                var printer = JsonConvert.DeserializeObject <DepartmentGroupPrinter>(model.EditGroup.PrinterData);

                model.PrinterId   = printer.PrinterId.ToString();
                model.PrinterName = printer.PrinterName;
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public IActionResult EditGroup(int departmentGroupId)
        {
            if (!_authorizationService.CanUserEditDepartmentGroup(UserId, departmentGroupId))
            {
                Unauthorized();
            }

            EditGroupView model = new EditGroupView();

            model.Users                 = _departmentsService.GetAllUsersForDepartment(DepartmentId);
            model.Groups                = _departmentGroupsService.GetAllGroupsForDepartment(DepartmentId);
            model.EditGroup             = _departmentGroupsService.GetGroupById(departmentGroupId);
            model.InternalDispatchEmail = $"{model.EditGroup.DispatchEmail}@{Config.InboundEmailConfig.GroupsDomain}";
            model.Latitude              = model.EditGroup.Latitude;
            model.Longitude             = model.EditGroup.Longitude;
            model.What3Word             = model.EditGroup.What3Words;

            List <DepartmentGroup> groups = new List <DepartmentGroup>();

            groups.Add(new DepartmentGroup {
                DepartmentGroupId = -1, Name = "None"
            });
            groups.AddRange(model.Groups.Where(x => x.Type.HasValue && x.Type.Value == (int)DepartmentGroupTypes.Station));
            model.StationGroups = new SelectList(groups, "DepartmentGroupId", "Name");

            if (model.EditGroup.Address != null && model.EditGroup.Type.HasValue && model.EditGroup.Type.Value == (int)DepartmentGroupTypes.Station)
            {
                model.Address1   = model.EditGroup.Address.Address1;
                model.City       = model.EditGroup.Address.City;
                model.Country    = model.EditGroup.Address.Country;
                model.PostalCode = model.EditGroup.Address.PostalCode;
                model.State      = model.EditGroup.Address.State;
            }

            return(View(model));
        }
Ejemplo n.º 3
0
        public IActionResult EditGroup(EditGroupView model, IFormCollection collection)
        {
            if (!_authorizationService.CanUserEditDepartmentGroup(UserId, model.EditGroup.DepartmentGroupId))
            {
                Unauthorized();
            }

            model.Users  = _departmentsService.GetAllUsersForDepartment(DepartmentId);
            model.Groups = _departmentGroupsService.GetAllGroupsForDepartment(DepartmentId);

            List <DepartmentGroup> groups = new List <DepartmentGroup>();

            groups.Add(new DepartmentGroup {
                DepartmentGroupId = -1, Name = "None"
            });
            groups.AddRange(model.Groups.Where(x => x.Type.HasValue && x.Type.Value == (int)DepartmentGroupTypes.Station));
            model.StationGroups = new SelectList(groups, "DepartmentGroupId", "Name");

            var group = _departmentGroupsService.GetGroupById(model.EditGroup.DepartmentGroupId);

            if (String.IsNullOrWhiteSpace(group.DispatchEmail))
            {
                group.DispatchEmail = RandomGenerator.GenerateRandomString(6, 6, false, true, false, true, true, false, null);
            }

            if (String.IsNullOrWhiteSpace(group.MessageEmail))
            {
                group.MessageEmail = RandomGenerator.GenerateRandomString(6, 6, false, true, false, true, true, false, null);
            }

            var auditEvent = new AuditEvent();

            auditEvent.DepartmentId = DepartmentId;
            auditEvent.UserId       = UserId;
            auditEvent.Type         = AuditLogTypes.GroupChanged;
            auditEvent.Before       = group.CloneJson();

            group.Name = model.EditGroup.Name;

            var groupAdmins = new List <string>();
            var groupUsers  = new List <string>();
            var allUsers    = new List <string>();

            if (collection.ContainsKey("groupAdmins"))
            {
                groupAdmins.AddRange(collection["groupAdmins"].ToString().Split(char.Parse(",")));
            }

            if (collection.ContainsKey("groupUsers"))
            {
                groupUsers.AddRange(collection["groupUsers"].ToString().Split(char.Parse(",")));
            }

            allUsers.AddRange(groupAdmins);
            allUsers.AddRange(groupUsers);

            if (model.EditGroup.Type.HasValue && model.EditGroup.Type.Value == (int)DepartmentGroupTypes.Station)
            {
                if (String.IsNullOrWhiteSpace(model.What3Word))
                {
                    if (String.IsNullOrEmpty(model.Latitude) && String.IsNullOrEmpty(model.Longitude))
                    {
                        if (String.IsNullOrEmpty(model.Address1))
                        {
                            ModelState.AddModelError("Address1", string.Format("The Address field is required for station groups"));
                        }

                        if (String.IsNullOrEmpty(model.City))
                        {
                            ModelState.AddModelError("City", string.Format("The City field is required for station groups"));
                        }

                        if (String.IsNullOrEmpty(model.Country))
                        {
                            ModelState.AddModelError("Country", string.Format("The Country field is required for station groups"));
                        }

                        if (String.IsNullOrEmpty(model.PostalCode))
                        {
                            ModelState.AddModelError("PostalCode", string.Format("The Postal Code field is required for station groups"));
                        }

                        if (String.IsNullOrEmpty(model.State))
                        {
                            ModelState.AddModelError("State", string.Format("The State field is required for station groups"));
                        }
                    }
                }
                else
                {
                    var result = _geoLocationProvider.GetCoordinatesFromW3W(model.What3Word);

                    if (result == null)
                    {
                        ModelState.AddModelError("What3Word", string.Format("The What3Words address entered was incorrect."));
                    }
                    else
                    {
                        model.Latitude  = result.Latitude.ToString();
                        model.Longitude = result.Longitude.ToString();
                    }
                }
            }

            if (ModelState.IsValid)
            {
                model.EditGroup.DepartmentId = DepartmentId;
                List <DepartmentGroupMember> users = new List <DepartmentGroupMember>();

                foreach (var user in allUsers)
                {
                    if (users.All(x => x.UserId != user))
                    {
                        var dgm = new DepartmentGroupMember();
                        dgm.DepartmentId = DepartmentId;
                        dgm.UserId       = user;

                        if (groupAdmins.Contains(user))
                        {
                            dgm.IsAdmin = true;
                        }

                        users.Add(dgm);
                    }
                }

                if (model.EditGroup.Type.HasValue && model.EditGroup.Type.Value == (int)DepartmentGroupTypes.Station)
                {
                    if (group.Address == null)
                    {
                        group.Address = new Address();
                    }

                    if (String.IsNullOrEmpty(model.Latitude) && String.IsNullOrEmpty(model.Longitude))
                    {
                        group.Address.Address1   = model.Address1;
                        group.Address.City       = model.City;
                        group.Address.Country    = model.Country;
                        group.Address.PostalCode = model.PostalCode;
                        group.Address.State      = model.State;
                    }
                    else
                    {
                        group.Address   = null;
                        group.Latitude  = model.Latitude;
                        group.Longitude = model.Longitude;
                    }

                    if (!String.IsNullOrWhiteSpace(model.What3Word))
                    {
                        group.What3Words = model.What3Word;
                    }

                    group.ParentDepartmentGroupId = null;
                    group.Parent = null;
                }
                else
                {
                    group.Address = null;
                }

                if (model.EditGroup.ParentDepartmentGroupId <= 0)
                {
                    group.ParentDepartmentGroupId = null;
                }

                if (!String.IsNullOrWhiteSpace(model.PrinterApiKey) && !String.IsNullOrWhiteSpace(model.PrinterId))
                {
                    var printer = new DepartmentGroupPrinter();
                    printer.PrinterId   = int.Parse(model.PrinterId);
                    printer.PrinterName = model.PrinterName;
                    printer.ApiKey      = SymmetricEncryption.Encrypt(model.PrinterApiKey, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase);

                    group.PrinterData = JsonConvert.SerializeObject(printer);
                }
                group.DispatchToPrinter = model.EditGroup.DispatchToPrinter;

                group.Members = users;
                _departmentGroupsService.Update(group);

                auditEvent.After = group.CloneJson();
                _eventAggregator.SendMessage <AuditEvent>(auditEvent);

                return(RedirectToAction("Index", "Groups", new { Area = "User" }));
            }

            model.EditGroup = group;

            return(View(model));
        }