Пример #1
0
        public async Task <RoleDTO> GetRoleInfoAsync(Guid id)
        {
            Role roleFromDB = await _roleRepository.FirstOrDefaultAsync(id);

            if (roleFromDB == null)
            {
                throw new InvalidOperationException("角色不存在");
            }
            var result = _mapper.Map <RoleDTO>(roleFromDB);
            List <WebMenuAuthority> allWebMenuAuthorities = await _webMenuAuthorityRepository.GetAllInfoFromCacheAsync();

            List <RoleWebMenuAuthority> roleOwnedWebMenus = await _roleWebMenuAuthorityRepository.WhereAsync(m => m.RoleID == id).ToList();

            Guid[] roleHasWebMenuID = roleOwnedWebMenus.Select(m => m.WebMenuAuthorityID).ToArray();
            result.WebMenuAuthorityTreeList = TreeHelper.GetTreeList <RoleWebMenuAuthorityTreeDTO, WebMenuAuthority, Guid>(allWebMenuAuthorities, null,
                                                                                                                           webMenuAuthority =>
            {
                var temp   = webMenuAuthority.CopyProperties <RoleWebMenuAuthorityTreeDTO>(nameof(WebMenuAuthority.Child), nameof(WebMenuAuthority.RoleWebMenuAuthorities));
                temp.Owned = roleHasWebMenuID.Contains(webMenuAuthority.ID);
                return(temp);
            });
            List <APIAuthority> allAPIAuthorities = await _apiAuthorityRepository.GetAllInfoFromCacheAsync();

            List <RoleAPIAuthority> roleOwnedAPIs = await _roleAPIAuthorityRepository.WhereAsync(m => m.RoleID == id).ToList();

            Guid[] roleHasAPIID = roleOwnedAPIs.Select(m => m.APIAuthorityID).ToArray();
            result.APIAuthorityTreeList = TreeHelper.GetTreeList <RoleAPIAuthorityTreeDTO, APIAuthority, Guid>(allAPIAuthorities, null,
                                                                                                               apiAuthority =>
            {
                var temp   = apiAuthority.CopyProperties <RoleAPIAuthorityTreeDTO>(nameof(APIAuthority.Child), nameof(APIAuthority.RoleAPIAuthorities));
                temp.Owned = roleHasAPIID.Contains(apiAuthority.ID);
                return(temp);
            });
            List <ActionAuthority> allActionAuthorities = await _actionAuthorityRepository.WhereAsync(m => true).ToList();

            List <RoleActionAuthority> roleOwnedActions = await _roleActionAuthorityRepository.WhereAsync(m => m.RoleID == id).ToList();

            Guid[] roleHasActionID = roleOwnedActions.Select(m => m.ActionAuthorityID).ToArray();
            result.ActionAuthorityList = allActionAuthorities.Select(m => new RoleActionAuthorityListDTO
            {
                ActionGroupCode = m.ActionGroupCode,
                Code            = m.Code,
                ID    = m.ID,
                Name  = m.Name,
                Owned = roleHasActionID.Contains(m.ID)
            }).ToList();
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 编辑角色功能权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="actionAuthorityIDs"></param>
        /// <returns></returns>
        private async Task EditRoleActionAuthorities(Role role, Guid[] actionAuthorityIDs)
        {
            role.RoleActionAuthorities = await _roleActionAuthorityRepository.WhereAsync(m => m.RoleID == role.ID).ToList();

            List <Guid> roleActionAuthorityIDs = role.RoleActionAuthorities.Select(m => m.ActionAuthorityID).ToList();
            List <Guid> deleteIDs = roleActionAuthorityIDs.Except(actionAuthorityIDs).ToList();
            List <RoleActionAuthority> deleteModel = role.RoleActionAuthorities.Where(m => deleteIDs.Contains(m.ActionAuthorityID)).ToList();

            foreach (RoleActionAuthority roleActionAuthority in deleteModel)
            {
                _authorityUnitOfWork.RegisterDelete(roleActionAuthority);
            }
            List <Guid> addIDs = actionAuthorityIDs.Except(roleActionAuthorityIDs).ToList();

            AddRoleActionAuthorities(role, addIDs);
        }