Exemplo n.º 1
0
        public async Task <IActionResult> RoleUser(string Id)
        {
            var role = await _roleManager.FindByIdAsync(Id);

            List <RoleUserViewModel> model = new List <RoleUserViewModel>();

            if (role == null)
            {
                return(NotFound());
            }

            foreach (var user in _userManager.Users)
            {
                var modelItem = new RoleUserViewModel();
                modelItem.RoleID       = Id;
                modelItem.UserFullName = user.FirstName + ' ' + user.LastName;
                modelItem.UserID       = user.Id;

                if (await _userManager.IsInRoleAsync(user, role.Name))
                {
                    modelItem.IsSelected = true;
                }
                else
                {
                    modelItem.IsSelected = false;
                }

                model.Add(modelItem);
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Create()
        {
            RoleUserViewModel ObjRoleUserViewModel = new RoleUserViewModel();
            List <Role>       AllRole = _RoleServices.GetAll().ToList <Role>();

            ObjRoleUserViewModel.NotSpecifiedUser      = AllRole;
            ObjRoleUserViewModel.RolesForSpecifiedUser = new List <Role>();
            return(View(ObjRoleUserViewModel));
        }
Exemplo n.º 3
0
 public ActionResult Edit(RoleUserViewModel ObjRoleUserViewModel)
 {
     if (ModelState.IsValid)
     {
         _UserServices.Update(ObjRoleUserViewModel._User, ObjRoleUserViewModel.SpecifiedSelected);
         return(RedirectToAction("Index"));
     }
     return(View(ObjRoleUserViewModel));
 }
Exemplo n.º 4
0
        public ActionResult Edit(int id)
        {
            User usr = _UserServices.Get(id);
            RoleUserViewModel myviewmodel = new RoleUserViewModel();

            myviewmodel._User                 = usr;
            myviewmodel.NotSpecifiedUser      = GetNotSelectedRole(id);
            myviewmodel.RolesForSpecifiedUser = usr.Roles.ToList <Role>();
            return(View(myviewmodel));
        }
        public ActionResult RoleUsers(string id)
        {
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new IdentityDbContext(Platform.DAAS.OData.Security.ModuleConfiguration.DefaultIdentityStoreConnectionName)));

            IdentityRole identityRole = roleManager.FindById(id);

            if (identityRole != null)
            {
                var userManager     = new UserManager <IdentityUser>(new UserStore <IdentityUser>(new IdentityDbContext(Platform.DAAS.OData.Security.ModuleConfiguration.DefaultIdentityStoreConnectionName)));
                var securityManager = Provider.SecurityManager();

                var identityUsers = userManager.Users; //userManager.Users.Where(u => !securityManager.IsSupperUser(u.Id));

                RoleUserViewModel viewModel = new RoleUserViewModel()
                {
                    ID        = identityRole.Id,
                    Name      = identityRole.Name,
                    RoleUsers = new List <UserViewModel>(),
                    Users     = new List <UserViewModel>()
                };

                if (identityUsers != null)
                {
                    foreach (var user in identityUsers.ToArray())
                    {
                        if (!securityManager.IsSupperUser(user.Id))
                        {
                            viewModel.Users.Add(new UserViewModel()
                            {
                                ID       = user.Id,
                                UserName = user.UserName,
                                //Description = user.Description,
                                Email = user.Email
                            });

                            if (userManager.IsInRole(user.Id, identityRole.Name))
                            {
                                viewModel.RoleUsers.Add(new UserViewModel()
                                {
                                    ID       = user.Id,
                                    UserName = user.UserName,
                                    //Description = user.Description,
                                    Email = user.Email
                                });
                            }
                        }
                    }
                }

                return(View(viewModel));
            }

            return(View());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> EditRolesInUser(string userId)
        {
            ViewBag.userId = userId;
            var result = await _userManager.FindByIdAsync(userId);

            if (result == null)
            {
                ViewBag.ErrorMessage = $"User with id :{userId} cannot be found";
                return(View("NotFound"));
            }
            //This to ensure we wait for the 1st database call
            //to finish, before we make another call to scoped identity
            var roles = await _roleManager.Roles.ToListAsync();

            //RoleUser --> RoleUserModel
            var model = new List <RoleUserModel>();

            foreach (var role in roles)
            {
                var roleUserModel = new RoleUserModel
                {
                    RoleId   = role.Id,
                    RoleName = role.Name
                };
                //
                if (await _userManager.IsInRoleAsync(result, role.Name))
                {
                    roleUserModel.IsSelected = true;
                }
                else
                {
                    roleUserModel.IsSelected = false;
                }

                //userRoleModel.IsSelected = (await _userManager.IsInRoleAsync(user, result.Name)) ? true : false;
                model.Add(roleUserModel);
            }

            //UserRoleModel --> UserRoleViewModel
            var roleUserViewModel = new List <RoleUserViewModel>();

            foreach (var role in model)
            {
                var roleUser = new RoleUserViewModel
                {
                    RoleId     = role.RoleId,
                    RoleName   = role.RoleName,
                    IsSelected = role.IsSelected
                };

                roleUserViewModel.Add(roleUser);
            }
            return(View(roleUserViewModel));
        }
Exemplo n.º 7
0
 public ActionResult Create(RoleUserViewModel ObjRoleUserViewModel)
 {
     if (ModelState.IsValid)
     {
         //for role
         List <Role> lstRol = new List <Role>();
         foreach (int x in ObjRoleUserViewModel.SpecifiedSelected)
         {
             lstRol.Add(new Role(x));
         }
         _UserServices.Add(ObjRoleUserViewModel._User, lstRol);
         return(RedirectToAction("Index"));
     }
     return(View("Create", ObjRoleUserViewModel));
 }
Exemplo n.º 8
0
        public async Task <ActionResult> Manager()
        {
            if (User.Identity.IsAuthenticated)
            {
                //var managerUser = UserManager.Users.SingleOrDefault(u => u.Id == HttpContext.User.Identity.GetUserId());
                var roleUser = new RoleUserViewModel();


                //roleUser.ManagerUser = managerUser;
                roleUser.ApplicationUsers = await GetUsersInRoleAsync();

                return(View(roleUser));
            }

            return(RedirectToAction("Login", "Account"));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> EditUsersInRole(string roleId)
        {
            try
            {
                ViewBag.roleId = roleId;

                var role = await _roleManager.FindByIdAsync(roleId);

                if (role == null)
                {
                    ViewBag.ErrorMessage = $"Role with Id = {roleId} cannot be found!";
                    return(View("NotFound"));
                }

                var model     = new List <RoleUserViewModel>();
                var listUsers = await _userManager.Users.ToListAsync();

                foreach (var user in listUsers)
                {
                    var roleUserViewModel = new RoleUserViewModel()
                    {
                        UserId   = user.Id,
                        UserName = user.UserName,
                    };

                    var isInRole = await _userManager.IsInRoleAsync(user, role.Name);

                    if (isInRole)
                    {
                        roleUserViewModel.IsSelected = true;
                    }
                    else
                    {
                        roleUserViewModel.IsSelected = false;
                    }

                    model.Add(roleUserViewModel);
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"When trying to update an user");
                throw;
            }
        }
Exemplo n.º 10
0
        public async Task <IActionResult> AddUserToRole(RoleUserViewModel roleUserView)
        {
            var role = await _roleManager.FindByIdAsync(roleUserView.RoleId);

            var user = await _userManager.FindByIdAsync(roleUserView.UserId);

            if (role != null && user != null)
            {
                var result = await _userManager.AddToRoleAsync(user, role.Name);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError(string.Empty, "添加角色用户出错");
            }
            ModelState.AddModelError(string.Empty, "用户或者角色未找到");
            return(View(roleUserView));
        }
Exemplo n.º 11
0
        public Result <RoleUserViewModel> GetUsersViewModel(string id)
        {
            SelectSpecification <RoleEntity, RoleUserViewModel> roleSpecification = new SelectSpecification <RoleEntity, RoleUserViewModel>();

            roleSpecification.AddFilter(x => x.Id == id);
            roleSpecification.AddSelect(x => new RoleUserViewModel(
                                            x.Id,
                                            x.Name));

            RoleUserViewModel roleUser = _roleRepository.Get(roleSpecification);

            if (roleUser == null)
            {
                _logger.LogWarning($"No Role. RoleId {id}");
                return(Result.Fail <RoleUserViewModel>("no_role", "No Role"));
            }

            return(Result.Ok(roleUser));
        }
        } // end public ManageUsersModel(RoleManager<IdentityRole> roleMgr, UserManager<VerityUser> userMrg)

        public async Task<IActionResult> OnGetAsync(string id)
        {
            IdentityRole role = await roleManager.FindByIdAsync(id);
            List<Core3IDUser> members = new List<Core3IDUser>();
            List<Core3IDUser> nonMembers = new List<Core3IDUser>();
            foreach (Core3IDUser user in userManager.Users)
            {
                var list = await userManager.IsInRoleAsync(user, role.Name) ? members : nonMembers;
                list.Add(user);
            } // end foreach (Core3IDUser user in userManager.Users)

            RoleUserViewModel = new RoleUserViewModel
            {
                Role = role,
                Members = members,
                NonMembers = nonMembers
            };

            return Page();
        } // end public async Task<IActionResult> OnGetAsync(string id)
Exemplo n.º 13
0
        public ActionResult RolesUsuario(string id)
        {
            var roles = roleManager.Roles;
            var user  = UserManager.FindById(id);

            var model = new RoleUserViewModel
            {
                UserId    = user.Id,
                Email     = user.Email,
                UserName  = user.UserName,
                UserPhone = user.PhoneNumber
            };

            foreach (var rol in roles)
            {
                if (UserManager.IsInRole(user.Id, rol.Name))
                {
                    model.Roles.Add(rol.Name);
                }
            }
            return(View(model));
        }
Exemplo n.º 14
0
        public async Task <IActionResult> DeleteUserFromRole(string roleId)
        {
            var role = await _roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                return(RedirectToAction("Index"));
            }
            var vm = new RoleUserViewModel
            {
                RoleId = role.Id
            };
            var users = await _userManager.Users.ToListAsync();

            foreach (var item in users)
            {
                if (await _userManager.IsInRoleAsync(item, role.Name))
                {
                    vm.Users.Add(item);
                }
            }
            return(View(vm));
        }
Exemplo n.º 15
0
        public async Task <IActionResult> DeleteUserFromRole(RoleUserViewModel roleUserView)
        {
            var role = await _roleManager.FindByIdAsync(roleUserView.RoleId);

            var user = await _userManager.FindByIdAsync(roleUserView.UserId);

            if (role != null && user != null)
            {
                if (!await _userManager.IsInRoleAsync(user, role.Name))
                {
                    ModelState.AddModelError(string.Empty, "用户不在角色里");
                    return(View(roleUserView));
                }
                var result = await _userManager.RemoveFromRoleAsync(user, role.Name);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError(string.Empty, "删除角色用户出错");
            }
            ModelState.AddModelError(string.Empty, "用户或者角色未找到");
            return(View(roleUserView));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> AddUserToRole(string roleId)
        {
            var role = await _roleManager.FindByIdAsync(roleId);

            if (role != null)
            {
                var roleUserViewModel = new RoleUserViewModel
                {
                    RoleId = role.Id
                };
                var users = await _userManager.Users.ToListAsync();

                foreach (var user in users)
                {
                    if (!await _userManager.IsInRoleAsync(user, role.Name))
                    {
                        roleUserViewModel.Users.Add(user);
                    }
                }
                return(View(roleUserViewModel));
            }

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 17
0
        public async Task <ResponseContainer> CreateOrUpdateRole(RoleUserViewModel vm)
        {
            ResponseContainer response = new ResponseContainer();

            var lst = new List <RoleUserViewModel>();

            if (vm.RolesId != null)
            {
                foreach (var item in vm.RolesId)
                {
                    var model = new RoleUserViewModel();
                    model.Id     = item;
                    model.UserId = vm.UserId;
                    //model.Name = "A";
                    lst.Add(model);
                }

                var resul = await _apiFactory.PostAsync <List <RoleUserViewModel>, bool>(lst, "Authentication/CreateMultiRole", HostConstants.ApiAuthentication, _userSession.BearerToken);

                response.Activity = "Chỉnh sửa quyền";
                response.Action   = "create";
            }
            else
            {
                var model = new RoleUserViewModel()
                {
                    UserId = vm.UserId
                };
                lst.Add(model);
                var resul = await _apiFactory.PostAsync <List <RoleUserViewModel>, bool>(lst, "Authentication/CreateMultiRole", HostConstants.ApiAuthentication, _userSession.BearerToken);

                response.Activity = "Chỉnh sửa quyền";
                response.Action   = "create";
            }
            return(response);
        }
        /// <summary>
        /// 保存角色用户关系
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult SaveRoleUsers(RoleUserViewModel model)
        {
            return(ExecuteFunctionRun(() =>
            {
                ActionResult result = new ActionResult(true, "msgGlobalString.SaveSucced");
                List <RoleUserViewModel> listusers = new List <RoleUserViewModel>();
                try
                {
                    OThinker.Organization.OrgPost post = this.Engine.Organization.GetUnit(model.RoleID) as OThinker.Organization.OrgPost;
                    if (post != null)
                    {
                        if (model.UserID != null)
                        {
                            var ids = model.UserID.Split(';');
                            for (int i = 0; i < ids.Length; i++)
                            {
                                OThinker.Organization.OrgStaff staff = new OThinker.Organization.OrgStaff()
                                {
                                    ObjectID = string.IsNullOrEmpty(model.ObjectID) ? "" : model.ObjectID,
                                    OUScope = model.ManagerScopeIds == null ? new string[] { } : model.ManagerScopeIds.Split(';'),
                                    UserID = ids[i],
                                    ParentObjectID = model.RoleID,
                                    ParentIndex = model.Sortkey, //ParentIndex 存储排序键
                                    Description = model.Description
                                };

                                //新增
                                if (string.IsNullOrEmpty(staff.ObjectID))
                                {
                                    //新增时判断是否有添加过
                                    if (post.ChildList != null && post.ChildList.Count() > 0)
                                    {
                                        bool flag = false;
                                        foreach (OThinker.Organization.OrgStaff sf in post.ChildList)
                                        {
                                            if (sf.UserID == ids[i])
                                            {
                                                flag = true;
                                                break;
                                            }
                                        }

                                        if (flag)
                                        {
                                            result.Success = false;
                                            result.Message = "用户[" + this.Engine.Organization.GetName(ids[i]) + "]已经存在记录,请选择编辑!";
                                            return Json(result);
                                        }
                                    }

                                    staff.ObjectID = Guid.NewGuid().ToString();
                                    post.AddChildUnit(staff);
                                }
                                //更新
                                else
                                {
                                    post.UpdateChildUnit(staff);
                                }
                            }
                        }


                        post.ChildList = post.ChildList.OrderBy(c => c.ParentIndex).ToArray();
                        this.Engine.Organization.UpdateUnit(this.UserValidator.UserID, post);
                    }

                    return Json(result);
                }
                catch (Exception ex)
                {
                    result = new ActionResult(false, "保存失败," + ex.Message);
                    return Json(result);
                }
            }));
        }