コード例 #1
0
        public ActionResult Create(CreateUserModel userModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    User user = userModel.ToEntity();
                    user.PasswordSalt = Util.RandomPasswordSalt();
                    user.Password     = Util.ComputeMD5Hash(user.Password + user.PasswordSalt);
                    user.Created      = DateTime.Now;
                    user.Modified     = user.Created;

                    userService.AddUser(user);

                    if (userModel.RoleId != null)
                    {
                        foreach (int roleId in userModel.RoleId)
                        {
                            authorService.AddUserRole(user.Id, roleId);
                        }
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    ErrorNotification(e);
                }
            }
            else
            {
                AddModelStateErrors();
            }

            IEnumerable <Role> roles = authorService.GetAllRoles();

            userModel.Roles = new MultiSelectList(roles, "Id", "Name");

            Title = "Thêm mới tài khoản hệ thống";
            return(View(userModel));
        }
コード例 #2
0
        public virtual JsonResult SetRoles()
        {
            var userId = Request.Form.Get <long>("userId", 0);

            if (userId <= 0)
            {
                return(Json(new { MessageType = 0, MessageContent = "请选择某个用户设置角色!" }));
            }
            var roleIdStr = Request.Form.Get("roleId");

            if (string.IsNullOrEmpty(roleIdStr))
            {
                return(Json(new { MessageType = 0, MessageContent = "请至少分配一项角色!" }));
            }
            var roleIds = roleIdStr.Split(',');

            _authorizationService.DeleteUserRole(userId);
            foreach (var userRole in roleIds.Select(item => new UserRole(userId, Convert.ToInt32(item))))
            {
                _authorizationService.AddUserRole(userRole); //todo 处理单个数据异常情况
            }
            return(Json(new { MessageType = 1, MessageContent = "设置成功" }));
        }