Exemplo n.º 1
0
        public async Task <ActionResult> AddUser(AddUserModel model)
        {
            ViewBag.MenuItem = CurrentMenuItem;

            ArrayList errors = new ArrayList();

            if (ModelState.IsValid)
            {
                // Validation
                if (String.IsNullOrEmpty(model.UserName.Trim()))
                {
                    errors.Add("User Name is mandatory");
                }
                if (model.SelectedRoles == null)
                {
                    errors.Add("At least one role must be selected");
                }
                if (model.SelectedFacilities == null)
                {
                    errors.Add("At least one facility must be selected");
                }
                if (unitOfWork.Repository <User>().Queryable().Any(u => u.UserName == model.UserName))
                {
                    errors.Add("A user with the specified username already exists.");
                }

                if (errors.Count == 0)
                {
                    var user = new UserInfo()
                    {
                        FirstName = model.FirstName, LastName = model.LastName, UserName = model.UserName, Email = model.Email, Password = model.Password
                    };
                    IdentityResult result = await userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        var roleIdsInt64 = model.SelectedRoles.ConvertAll <Int64>(Convert.ToInt64).ToArray();

                        var roles = unitOfWork.Repository <Role>().Queryable()
                                    .Where(r => roleIdsInt64.Contains(r.Id))
                                    .Select(rk => rk.Key)
                                    .ToArray();

                        IdentityResult roleResult = await userManager.AddToRolesAsync(user.Id, roles);

                        if (roleResult.Succeeded)
                        {
                            var utemp            = unitOfWork.Repository <User>().Queryable().SingleOrDefault(u => u.Id == user.Id);
                            var facilityIdsInt64 = model.SelectedFacilities.ConvertAll <Int64>(Convert.ToInt64).ToArray();

                            ArrayList deleteCollection = new ArrayList();
                            foreach (UserFacility uf in utemp.Facilities)
                            {
                                if (!facilityIdsInt64.Contains(uf.Facility.Id))
                                {
                                    // remove
                                    deleteCollection.Add(uf);
                                }
                            }
                            ArrayList addCollection = new ArrayList();
                            foreach (Int64 id in facilityIdsInt64)
                            {
                                if (!utemp.Facilities.Any(uf => uf.Id == id))
                                {
                                    // add
                                    addCollection.Add(id);
                                }
                            }

                            foreach (UserFacility uf in deleteCollection)
                            {
                                utemp.Facilities.Remove(uf);
                            }
                            foreach (Int64 id in addCollection)
                            {
                                var uf = new UserFacility()
                                {
                                    Facility = unitOfWork.Repository <Facility>().Queryable().SingleOrDefault(f => f.Id == id), User = utemp
                                };
                                utemp.Facilities.Add(uf);
                            }
                            utemp.Active = true;
                            unitOfWork.Repository <User>().Update(utemp);
                            unitOfWork.Complete();

                            HttpCookie cookie = new HttpCookie("PopUpMessage");
                            cookie.Value = "User record added successfully";
                            Response.Cookies.Add(cookie);

                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            AddErrors(roleResult);
                        }
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
                else // if (errors.Count == 0)
                {
                    AddErrors(errors);
                }
            }

            ViewBag.Roles = unitOfWork.Repository <Role>().Queryable().ToList().Select(r => new SelectListItem {
                Value = r.Id.ToString(), Text = r.Name
            }).ToList();

            ViewBag.Facilities = unitOfWork.Repository <Facility>().Queryable().OrderBy(f => f.FacilityName).ToList().Select(f => new SelectListItem {
                Value = f.Id.ToString(), Text = f.FacilityName
            }).ToList();

            return(View(model));
        }