Пример #1
0
        //
        // GET: /RBAC/Role/Detail
        /// <summary>
        /// Details the specified role name.
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <returns>Task&lt;ActionResult&gt;.</returns>
        public async Task <ActionResult> Detail(string roleName)
        {
            using (RoleServiceClient client = new RoleServiceClient())
            {
                MethodReturnResult <Role> result = await client.GetAsync(roleName ?? string.Empty);

                if (result.Code == 0)
                {
                    RoleViewModel viewModel = new RoleViewModel()
                    {
                        RoleName    = result.Data.Key,
                        CreateTime  = result.Data.CreateTime,
                        Creator     = result.Data.Creator,
                        Description = result.Data.Description,
                        Editor      = result.Data.Editor,
                        EditTime    = result.Data.EditTime
                    };
                    return(PartialView("_RoleInfoPartial", viewModel));
                }
                else
                {
                    ModelState.AddModelError("", result.Message);
                }
            }
            return(PartialView("_RoleInfoPartial"));
        }
Пример #2
0
        public async Task <ActionResult> SaveModify(RoleViewModel model
                                                    , SetRoleUserViewModel roleUser
                                                    , SetRoleResourceViewModel roleResource)
        {
            MethodReturnResult rst = new MethodReturnResult();

            try {
                using (RoleServiceClient client = new RoleServiceClient())
                {
                    MethodReturnResult <Role> result = await client.GetAsync(model.RoleName);

                    if (result.Code > 0)
                    {
                        return(Json(result));
                    }
                    //修改角色基本信息。
                    result.Data.Description = model.Description;
                    result.Data.Editor      = User.Identity.Name;
                    result.Data.EditTime    = DateTime.Now;
                    rst = await client.ModifyAsync(result.Data);

                    if (rst.Code > 0)
                    {
                        return(Json(rst));
                    }
                    //设置角色用户
                    IList <UserInRole> lstUserInRole = new List <UserInRole>();
                    if (roleUser.Users != null)
                    {
                        foreach (string loginName in roleUser.Users)
                        {
                            lstUserInRole.Add(new UserInRole()
                            {
                                Key = new UserInRoleKey()
                                {
                                    LoginName = loginName, RoleName = model.RoleName
                                },
                                Editor   = User.Identity.Name,
                                EditTime = DateTime.Now
                            });
                        }
                    }
                    rst = await client.SetRoleUserAsync(model.RoleName, lstUserInRole);

                    if (rst.Code > 0)
                    {
                        return(Json(rst));
                    }
                    //设置角色资源
                    IList <RoleOwnResource> lstRoleOwnResource = new List <RoleOwnResource>();
                    if (roleResource.Resources != null)
                    {
                        foreach (string val in roleResource.Resources)
                        {
                            string[]     vals = val.Split('$');
                            ResourceType type = (ResourceType)Enum.Parse(typeof(ResourceType), vals[0]);
                            string       code = string.Concat(vals.Skip(1));

                            lstRoleOwnResource.Add(new RoleOwnResource()
                            {
                                Key = new RoleOwnResourceKey()
                                {
                                    RoleName = model.RoleName, ResourceCode = code, ResourceType = type
                                },
                                Editor   = User.Identity.Name,
                                EditTime = DateTime.Now
                            });
                        }
                    }
                    rst = await client.SetRoleResourceAsync(model.RoleName, lstRoleOwnResource);

                    if (rst.Code > 0)
                    {
                        return(Json(rst));
                    }

                    if (rst.Code == 0)
                    {
                        rst.Message = string.Format(StringResource.Role_SaveModify_Success, model.RoleName);
                    }
                    return(Json(rst));
                }
            }
            catch (Exception ex)
            {
                rst.Code    = 1000;
                rst.Message = ex.Message;
            }
            return(Json(rst));
        }