Ejemplo n.º 1
0
        public async Task <ActionResult> ManageComboItemsforUser(string id, string[] ComboItems, FormCollection collection)
        {
            MyIdentityManager  myIdentityManager  = new MyIdentityManager();
            MyComboItemManager myComboItemManager = new MyComboItemManager();

            if (ComboItems != null)
            {
                foreach (var comboItem in ComboItems)
                {
                    if (comboItem.IsNullOrWhiteSpace())
                    {
                        return(HttpNotFound("AddNewRoleToUser: a role in the roles table is null or whitespace!"));
                    }
                    //if role exists and we are not trying to add the user to an non existing role in the db
                }
            }// if ComboItems==null is checked by UpdateComboItemsforUser, and there is an action for that

            if (id.IsNullOrWhiteSpace())
            {
                return(HttpNotFound("User Id Is null! 4956"));
            }

            //find user
            var user = myIdentityManager.SearchUserById(id);

            if (user == null)
            {
                return(HttpNotFound("User with id " + id + "not found!"));
            }
            //if (ComboItems == null) { return HttpNotFound("ManageRolesForUser: ComboItems table zero"); }

            //Logic to update with new selection of ComboItems
            bool result = myComboItemManager.UpdateComboItemsforUser(id, ComboItems);

            myComboItemManager.DisposeAll();
            myIdentityManager.Dispose();
            if (result == true)
            {
                return(Json(new { success = true }));
            }
            return(HttpNotFound("AddNewRoleToUser: Error adding role: " + " to user with id " + id));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> UserEdit([Bind
                                                       (Include = "EnrollmentDate,Email,Id,EmailConfirmed," +
                                                                  "PhoneNumber,PhoneNumberConfirmed," +
                                                                  "TwoFactorEnabled,LockoutEnabled," +
                                                                  "AccessFailedCount,UserName," +
                                                                  "Address," +
                                                                  "LockoutEndDateUtc,ComboItems,")]
                                                  ApplicationUser applicationUser, string[] ComboItems)
        {
            if (ModelState.IsValid)
            {
                MyIdentityManager  myIdentityManager  = new MyIdentityManager();
                MyComboItemManager myComboItemManager = new MyComboItemManager();
                //get the user based on the id
                var userEdited = myIdentityManager.SearchUserById(applicationUser.Id);

                myComboItemManager.UpdateComboItemsforUser(applicationUser.Id, ComboItems);

                userEdited.EnrollmentDate       = applicationUser.EnrollmentDate;
                userEdited.Email                = applicationUser.Email;
                userEdited.Address              = applicationUser.Address;
                userEdited.EmailConfirmed       = applicationUser.EmailConfirmed;
                userEdited.PhoneNumber          = applicationUser.PhoneNumber;
                userEdited.PhoneNumberConfirmed = applicationUser.PhoneNumberConfirmed;
                userEdited.TwoFactorEnabled     = applicationUser.TwoFactorEnabled;
                userEdited.LockoutEnabled       = applicationUser.LockoutEnabled;
                userEdited.LockoutEndDateUtc    = applicationUser.LockoutEndDateUtc;
                userEdited.AccessFailedCount    = applicationUser.AccessFailedCount;
                userEdited.UserName             = applicationUser.UserName;
                var userEditResult = myIdentityManager.UpdateUser(userEdited);

                if (!userEditResult.Succeeded)
                {
                    return(HttpNotFound("not updated"));
                }

                myComboItemManager.DisposeAll();
                return(Json(new { success = true }));
            }
            return(HttpNotFound("Not Valid mod"));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> AddNewUser(
            [Bind(
                 Include =
                     "Email,Password,UserName,EnrollmentDate,PhoneNumber,PhoneNumberConfirmed" +
                     ",TwoFactorEnabled,Address,LockoutEnabled,LockoutEndDateUtc,AccessFailedCount,EmailConfirmed,ComboItemsStrTable"
                 )] AddNewUserViewModel addNewUserViewModel)
        {
            MyIdentityManager myIdentityManager = new MyIdentityManager();

            if (!ModelState.IsValid)  //if the modelstate is not valid, pass the errors to a string, and display via httpnotfount. Not the best way, but works
            {
                #region error reporting
                var modelStateerrors = ModelState.Where(x => x.Value.Errors.Count > 0)
                                       .Select(x => new { x.Key, x.Value.Errors }).ToArray();
                string errorList = "";


                if (modelStateerrors != null)
                {
                    foreach (var modelerr in modelStateerrors)
                    {
                        foreach (var modelerro in modelerr.Errors)
                        {
                            errorList = errorList + " | " + modelerro.ErrorMessage;
                        }
                    }
                }
                //return PartialView("CustomError", errorList);
                #endregion
            }
            //our user, to ASPNET user. Relying to ASPNET's input error checks etc
            DateTime temp = DateTime.Now;
            var      user = new ApplicationUser {
                Email                = addNewUserViewModel.Email,
                UserName             = addNewUserViewModel.Email,
                Address              = addNewUserViewModel.Address,
                EnrollmentDate       = addNewUserViewModel.EnrollmentDate ?? temp, //if the user leaves that blank, fill it with datetime.now
                PhoneNumber          = addNewUserViewModel.PhoneNumber,
                PhoneNumberConfirmed = addNewUserViewModel.PhoneNumberConfirmed,
                TwoFactorEnabled     = addNewUserViewModel.TwoFactorEnabled,
                LockoutEnabled       = addNewUserViewModel.LockoutEnabled,
                LockoutEndDateUtc    = addNewUserViewModel.LockoutEndDateUtc,
                AccessFailedCount    = addNewUserViewModel.AccessFailedCount ?? 0, //if the user leaves that blank, make it 0
                EmailConfirmed       = addNewUserViewModel.EmailConfirmed
            };
            var  createUserResult         = myIdentityManager.CreateNewUser(user, addNewUserViewModel.Password);
            bool addComboItemToUserResult = false;
            if (createUserResult.Succeeded)
            {
                //foreach (var selectListItem in addNewUserViewModel.ComboItems)
                //{
                //    MyComboItemManager myComboItemManager = new MyComboItemManager();
                //    if (selectListItem.Selected)
                //    {
                //        addComboItemToUserResult = myComboItemManager.AddComboItemToUser(user.Id, selectListItem.Value);
                //    }
                //}

                //if (addComboItemToUserResult)
                // the above is the proper way. DropDownList returns a string[] instead a fking IEnumerable<SelectListItem>. FK that

                MyComboItemManager myComboItemManager = new MyComboItemManager();
                addComboItemToUserResult = myComboItemManager.UpdateComboItemsforUser(user.Id, addNewUserViewModel.ComboItemsStrTable);
                myComboItemManager.DisposeAll();
                myIdentityManager.Dispose();
                return(Json(new { success = true }));
                //else
                //    return HttpNotFound("Could not add Comboitems to User");
            }
            else
            {
                return(HttpNotFound("User data not valid, please try again"));
            }
        }