Exemplo n.º 1
0
        public IActionResult RoleAssign(string Id)
        {
            TempData["userId"] = Id;
            AppUser user = _userManager.FindByIdAsync(Id).Result;

            ViewBag.userName = user.UserName;
            IQueryable <AppRole> roles = _roleManager.Roles;

            List <string> userRoles = _userManager.GetRolesAsync(user).Result as List <string>;

            List <RoleAssignModel> roleAssignModels = new List <RoleAssignModel>();

            foreach (var role in roles)
            {
                RoleAssignModel r = new RoleAssignModel();
                r.RoleId   = role.Id;
                r.RoleName = role.Name;
                if (userRoles.Contains(role.Name))
                {
                    r.Exist = true;
                }
                else
                {
                    r.Exist = false;
                }
                roleAssignModels.Add(r);
            }

            return(View(roleAssignModels));
        }
        public async Task <IActionResult> RoleAssign(RoleAssignModel roleAssignModel)
        {
            var user = await _userManager.FindByIdAsync(roleAssignModel.UserID);

            var currentUser = await _userManager.GetUserAsync(User);


            foreach (UserRole role in roleAssignModel.UserRoles)
            {
                if (role.Exist == false && role.NewValue == true)
                {
                    await _userManager.AddToRoleAsync(user, role.RoleName);
                }
                else if (role.Exist == true && role.NewValue == false)
                {
                    await _userManager.RemoveFromRoleAsync(user, role.RoleName);
                }
            }

            await _signInManager.SignOutAsync();

            await _signInManager.SignInAsync(currentUser, true);

            TempData["UserStatus"] = "RoleAssigned";
            return(RedirectToAction("Users"));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> RoleAssign(RoleAssignModel roleAssignModel)
        {
            var user = await _userManager.FindByIdAsync(roleAssignModel.UserID);

            if (user == null)
            {
                return(RedirectToAction("Users"));
            }

            foreach (Role role in roleAssignModel.Roles)
            {
                if (role.Exist)
                {
                    await _userManager.AddToRoleAsync(user, role.RoleName);
                }
                else
                {
                    await _userManager.RemoveFromRoleAsync(user, role.RoleName);
                }
            }

            await _signinManager.SignOutAsync();

            await _signinManager.SignInAsync(user, true);

            return(RedirectToAction("Users"));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> RoleAssign(string UserID)
        {
            var user = await _userManager.FindByIdAsync(UserID);

            if (user == null)
            {
                return(RedirectToAction("Users"));
            }
            //TÜM ROLLER
            List <AppRole> AllRoles = _roleManager.Roles.ToList();

            //KULLANICININ SAHİP OLDUĞU ROLLER
            var UserRoles = await _userManager.GetRolesAsync(user) as List <string>;

            RoleAssignModel roleAssignModel = new RoleAssignModel {
                UserID = user.Id, Username = user.UserName
            };


            foreach (var role in AllRoles)
            {
                roleAssignModel.Roles.Add(new Role
                {
                    RoleID   = role.Id,
                    RoleName = role.Name,
                    Exist    = UserRoles.Contains(role.Name)? true : false
                });
            }

            return(View(roleAssignModel));
        }
Exemplo n.º 5
0
        public async Task <string> RoleAssign(RoleAssignModel model)
        {
            if (await CheckUserAndRole(model.AccessId, CommonConstants.ADMIN) == false)
            {
                throw new ServiceException("Tài khoản không có quyền truy cập");
            }
            var user = await _userManager.FindByIdAsync(model.UserId.ToString());

            if (user == null)
            {
                throw new ServiceException("User doesn't exist");
            }
            var role = await _roleManager.FindByNameAsync(model.Role);

            if (role == null)
            {
                throw new ServiceException("Role không tồn tại");
            }
            var roleInUser = (await _userManager.GetRolesAsync(user)).FirstOrDefault();

            if (roleInUser != null)
            {
                await _userManager.RemoveFromRoleAsync(user, roleInUser);
            }
            await _userManager.AddToRoleAsync(user, model.Role);

            return((await _userManager.GetRolesAsync(user)).FirstOrDefault());
        }
Exemplo n.º 6
0
        public ActionResult RoleAssignment(RoleAssignModel viewModel)
        {
            if (!Request.IsAuthenticated || User.IsInRole("citizen") || User.IsInRole("crew_member") || User.IsInRole("crew_leader"))
            {
                return(RedirectToAction("Index"));
            }
            RoleAssignModel newModel = new RoleAssignModel();

            newModel.users = potholeDAL.GetAllUsers();
            return(View(newModel));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> RoleAssign(RoleAssignModel model)
        {
            var session = HttpContext.Session;

            model.AccessId = Convert.ToInt64(session.GetString(CommonConstants.USER_SESSION));
            var result = await userService.RoleAssign(model);

            if (result == null)
            {
                BadRequest();
            }
            return(Ok(result));
        }
        public async Task <IActionResult> AssignRole(int id)
        {
            var user = _userManager.Users.FirstOrDefault(I => I.Id == id);

            TempData["UserId"] = user.Id;
            var roles     = _roleManager.Roles.ToList();
            var userRoles = await _userManager.GetRolesAsync(user);

            List <RoleAssignModel> models = new List <RoleAssignModel>();

            foreach (var item in roles)
            {
                RoleAssignModel model = new RoleAssignModel();
                model.RoleId = item.Id;
                model.Name   = item.Name;
                model.Exists = userRoles.Contains(item.Name);
                models.Add(model);
            }
            return(View(models));
        }
        public async Task <IActionResult> RoleAssign(string UserID)
        {
            var user = await _userManager.FindByIdAsync(UserID);

            //USER NULL MI
            if (user == null)
            {
                TempData["UserStatus"] = "UserNotFound";
                return(RedirectToAction("Users"));
            }

            //TÜM ROLLER
            List <Role> AllRoles = _roleManager.Roles.ToList();


            //KULLANICININ ROLLERİ
            var userRoles = await _userManager.GetRolesAsync(user) as List <string>;


            RoleAssignModel roleAssignModel = new RoleAssignModel()
            {
                UserID = user.Id, NameLastname = user.Name + " " + user.Lastname
            };

            foreach (Role role in AllRoles)
            {
                roleAssignModel.UserRoles.Add(new UserRole()
                {
                    RoleID   = role.Id,
                    RoleName = role.Name,
                    Exist    = userRoles.Contains(role.Name)?true:false
                });
            }

            return(View(roleAssignModel));
        }