Exemplo n.º 1
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Create([Required] string name)
        {
            if (roleManager.Roles.Any(e => e.Name == name))
            {
                ViewBag.ErrorMessage = "This role already exists";
                return(RedirectToAction("Index"));
            }

            if (ModelState.IsValid)
            {
                IdentityResult result = await roleManager.CreateAsync(new IdentityRole(name));

                if (result.Succeeded)
                {
                    UsersUnderRole usersUnder = new UsersUnderRole {
                        RoleName = name
                    };
                    await dbContext.UnderRole.AddAsync(usersUnder);

                    await dbContext.SaveChangesAsync();

                    return(RedirectToAction("Index", roleManager.Roles.ToList()));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }

            return(View(name));
        }
Exemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Delete(string id)
        {
            IdentityRole role = await roleManager.FindByIdAsync(id);

            if (role != null && role.Name != "Admin")
            {
                IdentityResult result = await roleManager.DeleteAsync(role);

                if (result.Succeeded)
                {
                    UsersUnderRole underRole = dbContext.UnderRole.SingleOrDefault(e => e.RoleName.Equals(role.Name));

                    dbContext.UnderRole.Remove(underRole);

                    return(RedirectToAction("Index", roleManager.Roles.ToList()));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            else
            {
                ModelState.AddModelError("", "No Role found");
            }
            return(View("Index", roleManager.Roles.ToList()));
        }
Exemplo n.º 3
0
        public async Task <ViewResult> ManageUsers()
        {
            var     userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            AppUser user   = await userManager.FindByIdAsync(userId);

            List <AppUser> users = new List <AppUser>();

            if (await userManager.IsInRoleAsync(user, "Admin"))
            {
                users = userManager.Users.ToList();
            }
            else if (await userManager.IsInRoleAsync(user, "User"))
            {
            }
            else
            {
                foreach (var role in roleManager.Roles.ToList())
                {
                    if (await userManager.IsInRoleAsync(user, role.Name))
                    {
                        ViewBag.Message = "User is in role other than user";
                        UsersUnderRole theUsers = dbContext.UnderRole.SingleOrDefault(e => e.RoleName.Equals(role.Name));

                        if (theUsers == null || theUsers.UsersUnder == null || theUsers.UsersUnder.Length == 0)
                        {
                            continue;
                        }
                        string usersUnder = theUsers.UsersUnder;
                        foreach (var thing in usersUnder.Split(',').ToList())
                        {
                            var finalUser = userManager.Users.SingleOrDefault(f => f.UserName.Equals(thing));
                            if (finalUser != null)
                            {
                                users.Add(finalUser);
                            }
                        }
                    }
                }
            }

            return(View(users));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddUsersUnderRole(RoleModificationModel model)
        {
            IdentityResult result;
            List <String>  usersUnderRole = new List <String>();

            UsersUnderRole underRole = dbContext.UnderRole.SingleOrDefault(e => e.RoleName.Equals(model.RoleName));

            if (underRole == null)
            {
                return(View("Index", roleManager.Roles.ToList()));
            }

            if (underRole.UsersUnder != null)
            {
                var currentUsers = underRole.UsersUnder.Split(',');

                foreach (var user in currentUsers)
                {
                    usersUnderRole.Add(user);
                }
            }

            foreach (string userId in model.IdsToAdd ?? new string[] { })
            {
                AppUser user = await userManager.FindByIdAsync(userId);

                if (user != null)
                {
                    if (usersUnderRole.Contains(user.UserName))
                    {
                        continue;
                    }
                    else
                    {
                        usersUnderRole.Add(user.UserName);
                    }
                }
            }

            foreach (string userId in model.IdsToDelete ?? new string[] { })
            {
                AppUser user = await userManager.FindByIdAsync(userId);

                if (user != null)
                {
                    if (usersUnderRole.Contains(user.UserName))
                    {
                        usersUnderRole.Remove(user.UserName);
                    }
                }
            }

            StringBuilder sb = new StringBuilder();

            foreach (var user in usersUnderRole)
            {
                sb.Append(user);
                sb.Append(",");
            }

            if (sb.ToString().Length > 0)
            {
                underRole.UsersUnder = sb.ToString().Substring(0, sb.ToString().Length - 1);
            }
            else
            {
                underRole.UsersUnder = sb.ToString();
            }


            dbContext.UnderRole.Update(underRole);
            await dbContext.SaveChangesAsync();


            return(View("Index", roleManager.Roles.ToList()));
        }