/// <summary>
 /// Checks the access.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="resourceOperation">The resource operation.</param>
 /// <param name="roles">The roles.</param>
 /// <returns>是否驗證成功</returns>
 public override bool CheckAccess(System.Security.Claims.AuthorizationContext context, Core.Common.Security.ResourceOperation resourceOperation, string[] roles)
 {
     ResourceOperationPermission rop = new ResourceOperationPermission();
     bool result = false;
     result = rop.Authorize(resourceOperation, roles);
     return result;
 }
        /// <summary>
        /// Checks the access.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="resourceOperation">The resource operation.</param>
        /// <param name="roles">The roles.</param>
        /// <returns>是否驗證成功</returns>
        public override bool CheckAccess(System.Security.Claims.AuthorizationContext context, Core.Common.Security.ResourceOperation resourceOperation, string[] roles)
        {
            ResourceOperationPermission rop = new ResourceOperationPermission();
            bool result = false;

            result = rop.Authorize(resourceOperation, roles);
            return(result);
        }
        //
        // GET: /Roles/Details/5
        public async Task<ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var role = await RoleManager.FindByIdAsync(id);
            // Get the list of Users in this Role
            var users = new List<ApplicationUser>();

            // Get the list of Users in this Role
            foreach (var user in UserManager.Users.ToList())
            {
                if (await UserManager.IsInRoleAsync(user.Id, role.Name))
                {
                    users.Add(user);
                }
            }

            var resourceOperation = ResourceCollection.ResourceOperation.DistinctBy(x => x.ResourceName).ToList();

            ResourceOperationPermission rop = new ResourceOperationPermission();

            var permissions = rop.GetCurrentRolePermission(role.Name, role.Id, resourceOperation);

            var viewModel = new Detail()
            {
                ResourceOperation = permissions.ResourceOperation,
                RoleId = permissions.RoleId,
                RoleName = permissions.RoleName,
                Users = users.ToDictionary(x => x.Email, x => x.Email)
            };

            return View(viewModel);
        }
        public async Task<ActionResult> DeleteConfirmed(string id, string deleteUser)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                var role = await RoleManager.FindByIdAsync(id);
                if (role == null)
                {
                    return HttpNotFound();
                }
                IdentityResult result;
                if (deleteUser != null)
                {
                    result = await RoleManager.DeleteAsync(role);
                }
                else
                {
                    result = await RoleManager.DeleteAsync(role);
                }
                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", result.Errors.First());
                    return View();
                }

                ResourceOperationPermission rop = new ResourceOperationPermission();
                rop.DeleteRolePermissionTable(role.Id);

                return RedirectToAction("Index").WithSuccess("刪除群組成功");
            }
            return View();
        }
        public async Task<ActionResult> Edit(Edit viewModel)
        {
            if (ModelState.IsValid)
            {
                var role = await RoleManager.FindByIdAsync(viewModel.RoleId);
                role.Name = viewModel.RoleName;
                await RoleManager.UpdateAsync(role);

                ResourceOperationPermission rop = new ResourceOperationPermission();

                rop.SaveCurrentRolePermission(viewModel);

                return RedirectToAction("Index").WithSuccess("修改群組成功");
            }
            return View();
        }
        //
        // 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 };

            var resourceOperation = ResourceCollection.ResourceOperation.DistinctBy(x => x.ResourceName).ToList();

            ResourceOperationPermission rop = new ResourceOperationPermission();

            var viewModel = rop.GetCurrentRolePermission(role.Name, role.Id, resourceOperation);

            return View(viewModel);
        }
        public async Task<ActionResult> Create(Create roleViewModel)
        {
            if (ModelState.IsValid)
            {
                var role = new IdentityRole(roleViewModel.RoleName);
                var roleresult = await RoleManager.CreateAsync(role);
                if (!roleresult.Succeeded)
                {
                    ModelState.AddModelError("", roleresult.Errors.First());
                    return View(roleViewModel);
                }
                roleViewModel.RoleId = role.Id;

                ResourceOperationPermission rop = new ResourceOperationPermission();

                rop.SaveCurrentRolePermission(roleViewModel);
                return RedirectToAction("Index").WithSuccess("新增群組成功");
            }

            return View(roleViewModel);
        }