Esempio n. 1
0
    protected void AddRole_Click(object sender, EventArgs e)
    {
        string userName = UserName.Text;

        string roleName;
        bool   addOk = true;

        DropDownList roles = UserRoleView.FooterRow.FindControl("UserRolesDDL") as DropDownList;

        roleName = roles.SelectedValue;

        foreach (GridViewRow row in UserRoleView.Rows)
        {
            if (((Label)row.FindControl("RoleName")).Text == roleName)
            {
                addOk = false;
            }
        }

        if (addOk == true)
        {
            UserManager.AddUserToRole(userName, roleName);
            DataBindRoleList();
            Response.Write("<script>alert('Role has been updated.');</script>");
        }

        UsersRolesController controller = new UsersRolesController();
        List <IdentityRole>  userRole   = controller.Get_UserRole(UserIdField.Value);

        UserRoleView.DataSource = userRole;
        UserRoleView.ShowFooter = true;
        UserRoleView.DataBind();
    }
Esempio n. 2
0
        public ActionResult Update(UserRoleView userRole)
        {
            if (userRole.RoleId == userRole.UpdateRoleId)
            {
                return(Content("更新失败"));
            }
            //先把要更新角色的用户找出来
            var user = db.Users.FirstOrDefault(r => r.Id == userRole.UserId);
            //var module = db.Modules.FirstOrDefault(r => r.Id == roleModule.Moduleid);
            //构造一个原来的角色
            var role = new Role {
                Id = userRole.RoleId
            };

            db.Roles.Attach(role);

            //构造一个要更新的模块
            var updaterole = new Role {
                Id = userRole.UpdateRoleId
            };

            db.Roles.Attach(updaterole);
            //更新就是先删除后添加
            user.Roles.Remove(role);
            user.Roles.Add(updaterole);

            if (db.SaveChanges() == 0)
            {
                return(Json(new { code = 400 }));
            }
            return(Json(new { code = 200 }));
        }
Esempio n. 3
0
    protected void User_ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        // this gives you Row for your list items
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;

        // Retrieve the DropDownList control from the current row.
        Label  userID = (Label)dataItem.FindControl("UserNameID");
        string userId = userID.Text;

        UserIdField.Value = userId;

        UsersRolesController controller = new UsersRolesController();

        ApplicationUser user = controller.Get_User(userId);

        UserName.Text  = user.UserName;
        FullName.Text  = user.FullName;
        Institute.Text = user.Institution;
        Email.Text     = " " + user.Email;


        List <IdentityRole> userRole = controller.Get_UserRole(userId);

        UserRoleView.DataSource = userRole;
        UserRoleView.ShowFooter = true;
        UserRoleView.DataBind();
    }
Esempio n. 4
0
        public async Task <IActionResult> EditUser(string roleId)
        {
            ViewBag.roleId = roleId;

            var role = await _roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                return(RedirectToAction("List", "Role"));
            }

            List <User> users = _userManager.Users.ToList();
            var         model = new List <UserRoleView>();

            foreach (var user in users)
            {
                var userRoleView = new UserRoleView
                {
                    UserId   = user.Id,
                    UserName = user.UserName
                };

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

            return(View(model));
        }
Esempio n. 5
0
        // GET: UserRoles
        public ActionResult Index(int?id)
        {
            try
            {
                if (DataUtil.Validation())
                {
                    if (id == null)
                    {
                        id = 0;
                    }

                    UserRoleView objUserRoleView = new UserRoleView();
                    objUserRoleView.userRoleList = db.UserRole.Include(u => u.role).Include(u => u.user).Where(u => u.user.userID == id).ToList();
                    objUserRoleView.roles        = db.Database.SqlQuery <Role>("select r.roleid, r.name, CAST(ISNULL((select 1 from userRoles ur where ur.userID = @userID and ur.roleID = r.roleID),0) AS BIT) selected, r.active, r.createdate, r.createuser, r.updatedate, r.updateuser from roles r where r.active = 1 ", new SqlParameter("@userID", id)).ToList();
                    objUserRoleView.user         = db.User.Find(id);
                    return(View(objUserRoleView));
                }
                else
                {
                    return(RedirectToAction("Login", "Home"));
                }
            }
            catch (Exception ex)
            {
                Error objError = new Error();
                objError.page        = "UserRoles";
                objError.option      = "Index-1";
                objError.date        = DateTime.Now;
                objError.description = ex.Message;
                BaseDataAccess <Error> baseDataAccess = new BaseDataAccess <Error>();
                baseDataAccess.Insert(objError);
                return(RedirectToAction("Error", "Home"));
            }
        }
Esempio n. 6
0
        public ActionResult Index(UserRoleView objUserRoleView)
        {
            try
            {
                if (DataUtil.Validation())
                {
                    UserRoleView objUserRoleView2 = new UserRoleView();
                    try
                    {
                        if (Request.Form["ActionForm"] == "Registrar")
                        {
                            var RemoveAll = db.UserRole.Where(x => x.userID == objUserRoleView.user.userID);
                            db.UserRole.RemoveRange(RemoveAll);
                            db.SaveChanges();

                            UserRole objUserRole = new UserRole();
                            foreach (var item in objUserRoleView.roles)
                            {
                                if (item.selected)
                                {
                                    objUserRole.userID = objUserRoleView.user.userID;
                                    objUserRole.roleID = item.roleID;
                                    db.UserRole.Add(objUserRole);
                                    db.SaveChanges();
                                }
                            }



                            objUserRoleView2.userRoleList = db.UserRole.Include(u => u.role).Include(u => u.user).Where(u => u.user.userID == objUserRoleView.user.userID).ToList();
                            objUserRoleView2.roles        = db.Database.SqlQuery <Role>("select r.roleid, r.name, CAST(ISNULL((select 1 from userRoles ur where ur.userID = @userID and ur.roleID = r.roleID),0) AS BIT) selected, r.active, r.createdate, r.createuser, r.updatedate, r.updateuser from roles r where r.active = 1 ", new SqlParameter("@userID", objUserRoleView.user.userID)).ToList();
                            objUserRoleView2.user         = db.User.Find(objUserRoleView.user.userID);

                            ModelState.Clear();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    return(View(objUserRoleView2));
                }
                else
                {
                    return(RedirectToAction("Login", "Home"));
                }
            }
            catch (Exception ex)
            {
                Error objError = new Error();
                objError.page        = "UserRoles";
                objError.option      = "Index-2";
                objError.date        = DateTime.Now;
                objError.description = ex.Message;
                BaseDataAccess <Error> baseDataAccess = new BaseDataAccess <Error>();
                baseDataAccess.Insert(objError);
                return(RedirectToAction("Error", "Home"));
            }
        }
Esempio n. 7
0
        public IActionResult UserEdit(string id)
        {
            var model = new UserRoleView();

            model.AllRoles  = AccountLogic.GetRoles();
            model.UserRoles = AccountLogic.GetUserRoles(id);
            model.UserId    = id;

            return(View(model));
        }
        /// <summary>
        /// Users the role.
        /// </summary>
        /// <returns></returns>
        public ActionResult UserRole()
        {
            var objUserRoleView = new UserRoleView
            {
                RolesList = new List <Role>(), //objRoleBal.GetAllRolesByCorporateFacility(corporateId, facilityId),
                //UsersList = new List<Users>(), //objUsersBal.GetAllUsersByCorporateIdFacilityId(corporateId, facilityId),
                UserID = 0
            };

            return(View(objUserRoleView));
        }
 public void ChangeUserRole()
 {
     _userRoleView         = new UserRoleView(this);
     _userRoleView.Topmost = true;
     _userRoleView.Roles   = new ObservableCollection <string>();
     foreach (var role in _model.GetRoles())
     {
         _userRoleView.Roles.Add(role);
     }
     _userRoleView.ShowDialog();
 }
Esempio n. 10
0
        public ActionResult Edit(UserRoleView userRole)
        {
            userRole.UserName = db.Users.FirstOrDefault(r => r.Id == userRole.UserId).Username;
            userRole.RoleName = db.Roles.FirstOrDefault(r => r.Id == userRole.RoleId).Name;

            ViewBag.ModuleOption = from r in db.Roles
                                   select new System.Web.Mvc.SelectListItem {
                Text = r.Name, Value = r.Id.ToString()
            };
            return(View(userRole));
        }
        public CommonApiResponse <UserRole> Post(Guid userId, Guid roleId)
        {
            jwt = ViewBag.Jwt;
            CoreFaces.Helper.Result <UserRole> result = _userRoleService.UserAddRole(jwt.UserId, userId, roleId);
            UserRole     userRole      = result.Data;
            UserRoleView _userRoleView = _userRoleService.UserRoleToUserRoleView(userRole);

            _userRoleView.Name = _roleService.GetById(userRole.RoleId).Name;

            //return CommonApiResponse<UserRole>.Create(Response, userRole, result);
            return(CommonApiResponse <UserRole> .Create(Response, System.Net.HttpStatusCode.OK, true, userRole, null));
        }
        public UserRoleView UserRoleToUserRoleView(UserRole userRole)
        {
            UserRoleView userRoleView = new UserRoleView();

            userRoleView.Id         = userRole.Id;
            userRoleView.OwnerId    = userRole.OwnerId;
            userRoleView.RoleId     = userRole.RoleId;
            userRoleView.StatusId   = userRole.StatusId;
            userRoleView.UserId     = userRoleView.UserId;
            userRoleView.CreateDate = userRole.CreateDate;
            userRoleView.UpdateDate = userRole.UpdateDate;


            return(userRoleView);
        }
        public List <UserRoleView> GetByUserId(Guid userId)
        {
            List <UserRole>     userRoleList     = _userRoleRepository.GetByUserId(userId);
            List <UserRoleView> userRoleViewList = new List <UserRoleView>();

            foreach (UserRole userRole in userRoleList)
            {
                Role         role         = _roleRepository.GetById(userRole.RoleId);
                UserRoleView userRoleView = UserRoleToUserRoleView(userRole);
                userRoleView.Name = role.Name;

                userRoleViewList.Add(userRoleView);
            }

            return(userRoleViewList);
        }
Esempio n. 14
0
        public async Task <IActionResult> Delete(string id)
        {
            var user = await _userManager.FindByIdAsync(id);

            var roles = await _userManager.GetRolesAsync(user);

            var userView = new UserRoleView()
            {
                Id            = user.Id,
                UserName      = user.UserName,
                Email         = user.Email,
                RolesAssigned = roles
            };

            ViewData["Roles"] = new SelectList(userView.RolesAssigned);

            return(View(userView));
        }
Esempio n. 15
0
        public async Task <IActionResult> Delete(string id, UserRoleView user)
        {
            if (ModelState.IsValid)
            {
                var toBeDeleted = user.RoleToBeDeleted;

                if (user.UserName == "a" || toBeDeleted.ToUpper() == "ADMIN")
                {
                    ModelState.AddModelError(string.Empty, "Cannot remove Admin role from User 'a'");
                    ViewData["Roles"] = new SelectList(_roleManager.Roles.ToList());
                    return(View(user));
                }

                if (id != user.Id)
                {
                    return(NotFound());
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        var currentUser = await _userManager.FindByIdAsync(user.Id);

                        var result = await _userManager.RemoveFromRoleAsync(currentUser, toBeDeleted);
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!UserExists(user.Id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction("Index"));
                }
            }

            return(View());
        }
Esempio n. 16
0
        // GET: UserRoles
        public ActionResult Index(int?id)
        {
            if (DataUtil.Validation())
            {
                if (id == null)
                {
                    id = 0;
                }

                UserRoleView objUserRoleView = new UserRoleView();
                objUserRoleView.userRoleList = db.UserRole.Include(u => u.role).Include(u => u.user).Where(u => u.user.userID == id).ToList();
                objUserRoleView.roles        = db.Database.SqlQuery <Role>("select r.roleid, r.name, CAST(ISNULL((select 1 from userRoles ur where ur.userID = @userID and ur.roleID = r.roleID),0) AS BIT) selected, r.active, r.createdate, r.createuser, r.updatedate, r.updateuser from roles r where r.active = 1 ", new SqlParameter("@userID", id)).ToList();
                objUserRoleView.user         = db.User.Find(id);
                return(View(objUserRoleView));
            }
            else
            {
                return(RedirectToAction("Login", "Home"));
            }
        }
Esempio n. 17
0
        public ActionResult Insert(UserRoleView userRole)
        {
            //先把要添加角色的用户找出来
            var user = db.Users.FirstOrDefault(r => r.Id == userRole.UserId);

            var role = new Role {
                Id = userRole.RoleId
            };

            //构造一个要添加的权限模块
            db.Roles.Attach(role);

            //吧这个要添加的权限模块,add到角色的模块集合中
            user.Roles.Add(role);

            if (db.SaveChanges() == 0)
            {
                return(Json(new { code = 400 }));
            }
            return(Json(new { code = 200 }));
        }
Esempio n. 18
0
        public async Task <ActionResult> Index()
        {
            var users = _userManager.Users.ToList();

            var usersList = new List <UserRoleView>();

            foreach (ApplicationUser usr in users)
            {
                var roles = await _userManager.GetRolesAsync(usr);

                var userView = new UserRoleView()
                {
                    Id            = usr.Id,
                    UserName      = usr.UserName,
                    Email         = usr.Email,
                    RolesAssigned = roles
                };

                usersList.Add(userView);
            }

            return(View(usersList));
        }
Esempio n. 19
0
    protected void RemoveRoleButton_Click(object sender, EventArgs e)
    {
        string userName = UserName.Text;

        //Get the button that raised the event
        LinkButton deleteButton = (LinkButton)sender;

        //Get the row that contains this button
        GridViewRow row = (GridViewRow)deleteButton.NamingContainer;

        //get the role from the row
        string role = ((Label)row.FindControl("RoleName")).Text;

        //remove the role from the user
        UserManager.RemoveUserFromRole(userName, role);

        UsersRolesController controller = new UsersRolesController();
        List <IdentityRole>  userRole   = controller.Get_UserRole(UserIdField.Value);

        UserRoleView.DataSource = userRole;
        UserRoleView.ShowFooter = true;
        UserRoleView.DataBind();
        Response.Write("<script>alert('Role has been removed.');</script>");
    }
Esempio n. 20
0
 public ViewModelInternalUser FindById(Func <ViewModelInternalUser, bool> predicate)
 => UserRoleView.AsEnumerable().Where(predicate: predicate).SingleOrDefault();
 public void ChangeUserRole()
 {
     _userRoleView = new UserRoleView(this);
     _userRoleView.Show();
     _userRoleView.Output = _model.GetRoleMenu();
 }
Esempio n. 22
0
        public async Task <ControllerResponseDTO <UserRoleView> > DeleteUserFromUserRoleView(UserRoleView userRoleView)
        {
            var path = "DeleteUserByUserRoleView";

            return(await Post(path, userRoleView));
        }
Esempio n. 23
0
        public async Task <ControllerResponseDTO <UserRoleView> > DeleteUserByUserRoleView([FromBody] UserRoleView userRoleView)
        {
            var user = await UserManager.FindByEmailAsync(userRoleView.Email);

            var deletionResult = await UserManager.DeleteAsync(user);

            if (deletionResult.Succeeded)
            {
                return(new ControllerResponseDTO <UserRoleView>()
                {
                    Content = userRoleView
                });
            }
            else
            {
                return(new ControllerResponseDTO <UserRoleView>()
                {
                    Content = userRoleView,
                    ErrorMessage = deletionResult.Errors.First().Description
                });
            }
        }
Esempio n. 24
0
 public async void DeleteUser(UserRoleView userRoleView)
 {
     await UserService.DeleteUserFromUserRoleView(userRoleView);
 }