Пример #1
0
        //
        // GET: /Roles/Edit/Admin
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var role = await RoleManager.FindByIdAsync(id);
            if (role == null)
            {
                return HttpNotFound();
            }
            RoleViewModel roleModel = new RoleViewModel { Id = role.Id, Name = role.Name, Description = role.Description };

            var rolPers = await PermissionManager.GetPermissionsAsync(id);
            var pers = PermissionManager.Permissions.OrderBy(x=>x.ControllerKey).ToList();
            roleModel.PerrmissionList = pers.Select(x => new SelectListItem()
                {
                    Selected = rolPers.Contains(x),
                    Text = x.Description,
                    Value = x.Id
                });
            var ctrs = ControllerPermissions;
            foreach (var c in ctrs)
            {
                var perCtr = pers.Where(x=>x.ControllerKey == c.Key).Select(x=>x.Id).ToList();
                roleModel.GroupPerrmissionList.Add(c.Value, roleModel.PerrmissionList.Where(x=>perCtr.Contains(x.Value)));
            }

            var perCtr1 = pers.Where(x =>!ctrs.Keys.Contains(x.ControllerKey)).Select(x => x.Id).ToList();
            roleModel.GroupPerrmissionList.Add("Các chức năng khác", roleModel.PerrmissionList.Where(x => perCtr1.Contains(x.Value)));

            return View(roleModel);
        }
Пример #2
0
        public async Task<ActionResult> Create(RoleViewModel roleViewModel)
        {
            if (ModelState.IsValid)
            {
                var role = new ApplicationRole(roleViewModel.Name);
                // Save the new Description property:
                role.Description = roleViewModel.Description;

                var roleresult = await RoleManager.CreateAsync(role);
                if (!roleresult.Succeeded)
                {
                    ModelState.AddModelError("", roleresult.Errors.First());
                    return View();
                }
                return RedirectToAction("Index");
            }
            return View();
        }