コード例 #1
0
        public IList <Permission> GetAllPermissions()
        {
            var permissions = new List <Permission>();

            //加入导航权限
            permissions.AddRange(_menuManager.GetAllMenuPermissions());
            //todo:加入菜单对应的资源权限
            //加入模块权限
            //permissions.AddRange(_moduleInfoManager.GetAllModulePermissions().Result);
            return(permissions);
        }
コード例 #2
0
        /// <summary>
        /// 导航权限设置
        /// </summary>
        /// <param name="type">Roles:为角色分配权限,Users:为用户分配权限</param>
        /// <param name="permissionNames"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task AssignAllMenuPermission(string type, string[] permissionNames, int id)
        {
            IList <Permission> allPermissions = _menuManager.GetAllMenuPermissions();

            if (type == "Roles")
            {
                //给角色分配权限
                var role = await _roleManager.GetByIdAsync(id);

                foreach (var permission in allPermissions)
                {
                    if (permissionNames.Contains(permission.Name))
                    {
                        await _roleManager.GrantPermissionAsync(role, permission);
                    }
                    else
                    {
                        await _roleManager.ProhibitPermissionAsync(role, permission);
                    }
                }
            }
            else if (type == "User")
            {
                //给用户分配权限
                var user = await _userManager.GetByIdAsync(id);

                foreach (var permission in allPermissions)
                {
                    if (permissionNames.Contains(permission.Name))
                    {
                        await _userManager.GrantPermissionAsync(user, permission);
                    }
                    else
                    {
                        await _userManager.ProhibitPermissionAsync(user, permission);
                    }
                }
            }
        }