示例#1
0
        public ActionResult ManageRoles(List <string> userIds, string roleName)
        {
            //Use our RoleHelper to actually assign the role to the person or persons selected
            //This action is intended to operate on the selected Users.
            //If no Users were selected then there is nothing to do
            if (userIds != null)
            {
                //This loop removes all the selected Users from the Role they occupy
                //If they don't currently occupy a Role then I juess we are wasting time
                //trying to remove them from no role
                foreach (var userId in userIds)
                {
                    //First I need to determine what Role if any the user is in
                    var userRole = roleHelper.ListUserRoles(userId).FirstOrDefault();

                    if (!string.IsNullOrEmpty(userRole))
                    {
                        //Second I have to remove each of the selected users from their current Role

                        roleHelper.RemoveUserFromRole(userId, userRole);
                    }

                    //Then I will add each selected user to the selected Role


                    if (!string.IsNullOrEmpty(roleName))
                    {
                        roleHelper.AddUserToRole(userId, roleName);
                    }
                }
            }

            return(RedirectToAction("ManageRoles"));
        }
        public async Task <ActionResult> Create([Bind(Include = "Id,HouseName,Greeting,Created")] Household household)
        {
            if (ModelState.IsValid)
            {
                household.Created = DateTime.Now;
                db.Households.Add(household);
                db.SaveChanges();

                var user = db.Users.Find(User.Identity.GetUserId());
                user.HouseholdId = household.Id;
                rolesHelper.AddUserToRole(user.Id, "Head");
                db.SaveChanges();

                await HttpContextBaseExtension.RefreshAuthentication(HttpContext, user);

                return(RedirectToAction("Index", "Home"));
            }
            return(RedirectToAction("Dashboard"));
        }
        public ActionResult ManageUserRole(string id, string roleName)
        {
            foreach (var role in roleHelper.ListUserRoles(id))
            {
                roleHelper.RemoveUserFromRole(id, role);
            }

            if (!string.IsNullOrEmpty(roleName))
            {
                roleHelper.AddUserToRole(id, roleName);
            }

            TempData["Message"] = "The user's role has been updated.";
            return(RedirectToAction("Index"));
        }
        public ActionResult ManageRoles(List <string> userIds, string roleName)
        {
            if (userIds == null)
            {
                return(RedirectToAction("ManageRoles"));
            }

            foreach (var userId in userIds)
            {
                foreach (var role in roleHelper.ListUserRoles(userId).ToList())
                {
                    roleHelper.RemoveUserFromRole(userId, role);
                }

                if (!string.IsNullOrEmpty(roleName))
                {
                    roleHelper.AddUserToRole(userId, roleName);
                }
            }
            return(RedirectToAction("ManageRoles"));
        }
        public ActionResult ManageRoles(List <string> userIds, string roleName)
        {
            //This action is intended to operate on the selected Users.
            //If no Users were selected then there is nothing to do
            if (userIds != null)
            {
                foreach (var userId in userIds)
                {
                    //var currentRole = roleHelper.ListUserRoles(userId).FirstOrDefault();
                    //roleHelper.RemoveUserFromRole(userId, currentRole);

                    roleHelper.RemoveUserFromRole(userId, roleHelper.ListUserRoles(userId).FirstOrDefault());

                    if (!string.IsNullOrEmpty(roleName))
                    {
                        roleHelper.AddUserToRole(userId, roleName);
                    }
                }
            }

            return(RedirectToAction("ManageRoles"));
        }
示例#6
0
        public ActionResult ManageRoles(List <string> userIds, string roleName)
        {
            if (userIds == null || roleName == null)
            {
                return(RedirectToAction("ManageRoles"));
            }

            if (userIds != null)
            {
                foreach (var userId in userIds)
                {
                    var userRole = rolesHelper.ListUserRoles(userId).FirstOrDefault();

                    if (userRole != null)
                    {
                        var currentRole = rolesHelper.ListUserRoles(userId).FirstOrDefault();
                        rolesHelper.RemoveUserFromRole(userId, currentRole);
                    }
                    rolesHelper.AddUserToRole(userId, roleName);
                }
            }
            return(RedirectToAction("ManageRoles"));
        }
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model, int?houseHoldId)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName       = model.Email,
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                    Email          = model.Email,
                    EmailConfirmed = true,
                    AvatarPath     = "/Avatars/download.jpg",
                    HouseholdId    = houseHoldId,
                };

                if (model.Avatar != null)
                {
                    var justFileName = Path.GetFileNameWithoutExtension(model.Avatar.FileName);
                    justFileName = StringUtilities.URLFriendly(justFileName);
                    justFileName = $"{justFileName}-{DateTime.Now.Ticks}";
                    justFileName = $"{justFileName}{Path.GetExtension(model.Avatar.FileName)}";

                    var fileName = Path.GetFileName(model.Avatar.FileName);
                    model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), justFileName));
                    user.AvatarPath = $"/Avatars/{justFileName}";
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    if (houseHoldId != null && !string.IsNullOrEmpty(model.Code))
                    {
                        rolehelper.AddUserToRole(user.Id, "Member");
                    }
                    try
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                        //var from = $"Complication Station<{WebConfigurationManager.AppSettings["emailfrom"]}>";
                        //var emailAddress = WebConfigurationManager.AppSettings["EmailFrom"];
                        //var emailFrom = $"Falco Issue Tracker <{emailAddress}>";

                        //var email = new MailMessage(emailFrom, model.Email)
                        //{
                        //    Subject = "Confirm Email",
                        //    Body = $"Please confirm your account by clicking <a href=\"{callbackUrl}\">here</a>",
                        //    IsBodyHtml = true
                        //};
                        var EmailSvc = new EmailService();
                        //await EmailSvc.SendAsync(email);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }
                    //TempData["UnconfirmedEmail"] = "Please check your email and verify your account.";
                    if (string.IsNullOrEmpty(model.Code))
                    {
                        return(RedirectToAction("Dashboard", "Households"));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }