public MaintenanceGroupCustomersModel GetMaintenanceGroupCustomersModel(int maintGroupId)
        {
            var model = new MaintenanceGroupCustomersModel {
                CustomerId = maintGroupId
            };
            var mainGroup = RbacEntities.CustomerProfiles.SingleOrDefault(m => m.CustomerId == maintGroupId);

            if (mainGroup != null)
            {
                //get assigned customers: the pems city based on the maintGroupId passed it, it will build all the customers

                var mainGroupCity = new PemsCity(maintGroupId.ToString());
                model.Customers = mainGroupCity.MaintenanceCustomers.Select(x => new MaintenanceGroupCustomerModel {
                    Id = x.Id, DisplayName = x.DisplayName, NewCustomer = false
                }).ToList();

                //get possible customers: add the default item as well
                model.AvailableCustomers = new List <SelectListItem>
                {
                    new SelectListItem {
                        Text = ResourceTypes.DropDownDefault, Value = "-1"
                    }
                };

                //go grab all the customers
                model.AvailableCustomers.AddRange(GetMaintenanceCustomerListItems(maintGroupId));

                GetCustomerBaseModel(maintGroupId, model);
                model.Status      = GetCustomerStatusModel(maintGroupId);
                model.DisplayName = mainGroup.DisplayName;
            }

            return(model);
        }
        private MaintenanceGroupCustomersModel RebuildModel(MaintenanceGroupCustomersModel model, FormCollection formValues)
        {
            var tokenCharacter = new[] { model.Separator };

            // Walk the form fields and set any values in the model to values reflected by
            // the form fields.
            foreach (var formValueKey in formValues.Keys)
            {
                var formKey   = formValueKey.ToString();
                var formValue = formValues[formKey];
                if (formKey.Equals("NewCustomers"))
                {
                    model.NewCustomers = formValue.Split(tokenCharacter, StringSplitOptions.RemoveEmptyEntries).ToList();
                }
            }
            return(model);
        }
        public ActionResult EditMaintenanceGroupCustomers(string submitButton, MaintenanceGroupCustomersModel model, FormCollection formValues)
        {
            // Was action RETURN?
            if (submitButton.Equals("RETURN"))
            {
                return(RedirectToAction("Index", new { rtn = "true" }));
            }

            // Rebuild CustomerAreasModel from form fields
            model = RebuildModel(model, formValues);

            // Save values
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            (new MaintenanceGroupFactory()).SetMaintenanceGroupCustomersModel(model.CustomerId, model);

            // Saved and stay on same page.
            return(RedirectToAction("EditMaintenanceGroupCustomers", new { customerId = model.CustomerId }));
        }
        public ActionResult EditMaintenanceGroupCustomers(int customerId)
        {
            MaintenanceGroupCustomersModel model = (new MaintenanceGroupFactory()).GetMaintenanceGroupCustomersModel(customerId);

            return(View(model));
        }
        public void SetMaintenanceGroupCustomersModel(int maintGroupId, MaintenanceGroupCustomersModel model)
        {
            // Save any new areas.
            if (model.NewCustomers != null)
            {
                var maintGroup = new PemsCity(maintGroupId.ToString());

                foreach (var newCustomerId in model.NewCustomers)
                {
                    //try to parse the id to make sure it came though correctly
                    int newCustID;
                    var parsed = int.TryParse(newCustomerId, out newCustID);

                    if (parsed)
                    {
                        //now lets check to see if this customer is in the system
                        var existingCustomer = RbacEntities.CustomerProfiles.FirstOrDefault(x => x.CustomerId == newCustID);
                        if (existingCustomer != null)
                        {
                            //only do it if it doesnt exist there already
                            var existing = RbacEntities.MaintenanceGroupCustomers.FirstOrDefault(x => x.MaintenanceGroupId == maintGroupId && x.CustomerId == newCustID);
                            if (existing != null)
                            {
                                continue;
                            }
                            RbacEntities.MaintenanceGroupCustomers.Add(new MaintenanceGroupCustomer
                            {
                                MaintenanceGroupId = maintGroupId,
                                CustomerId         = newCustID
                            });
                            RbacEntities.SaveChanges();

                            //roll thorugh all the ussers for the maint group and if htey are a technician, add them to the _main group for the customer.
                            //This way they will not be member, but will be part of a role so they have access log in to the maint for that cuity

                            //we do not need to roll thorugh the users for the customers, since they are not allowed to assign technicians until they are part of the maintenance group
                            //this means that the customer will not have any technicians, so we do not need to worry about adding those users as techs for the maint group,
                            //that will be done when the user is checked as a tech after the customer is assigned to the maint group.
                            //now add all of the users for this maintenance group that are technicians to the _maintenance role for the customer.

                            //we are also only doing this for new customers.

                            //get all the users for the maintenance group
                            var mainGroupMembersUsernames = (new SecurityManager()).GetUsersForCity(maintGroup.InternalName);

                            //get the customer and an auth manager for that customer
                            var customer             = new PemsCity(existingCustomer.CustomerId.ToString());
                            var authorizationManager = new AuthorizationManager(customer);

                            //check to see if they are a technician
                            foreach (var mainGroupMembersUsername in mainGroupMembersUsernames)
                            {
                                if (TechnicianFactory.IsUserTechnician(mainGroupMembersUsername))
                                {
                                    //if they are, add them to the _maint role for the customer
                                    authorizationManager.AddGroupMember(Constants.Security.DefaultMaintenanceGroupName,
                                                                        mainGroupMembersUsername);

                                    //go get the userid from the name
                                    var userId = (new UserFactory()).GetUserId(mainGroupMembersUsername);

                                    //we also need to update the caching table to give the technician access to see the site
                                    (new UserCustomerAccessManager()).AddCustomerAccess(userId, maintGroupId);
                                }
                            }
                        }
                    }
                }
            }
        }