.NET 云原生架构师训练营(权限系统 代码实现 Store.EntityFramework)--学习笔记
目录
- 开发任务
- 代码实现
开发任务
- DotNetNB.Security.Core:定义 core,models,Istore;实现 default memory store
- DotNetNB.Security.Store.EntityFramework:基于 mysql 创建 PermissionStore 和 ResourceStore
代码实现
我们需要在 ResourceProviderHostedService 中读取所有的 Resource,将 Resource 转换为 Permission,再将 Permission 分配给 Role
创建 Permission 的 model
namespace DotNetNB.Security.Core.Models
{
public class Permission
{
public string Key { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public IEnumerable<Resource> Resources { get; set; }
}
}
在 IPermissionManager 接口中定义 CreateAsync 方法
namespace DotNetNB.Security.Core
{
public interface IPermissionManager
{
public Task CreateAsync(string key, string displayName, string description, IEnumerable<string> resources);
}
}
IResourceManager 接口添加通过 key 获取 resource 的方法 GetByKeysAsync
public Task<IEnumerable<Resource>> GetByKeysAsync(IEnumerable<string> resources);
PermissionManager 里面需要用到 Store,先定义接口 IPermissionStore
using DotNetNB.Security.Core.Models;
namespace DotNetNB.Security.Core.Store
{
public interface IPermissionStore
{
public Task CreateAsync(Permission permission);
}
}
在 PermissionManager 里面创建一个 permission,通过 ResourceManager 获取到所有 resources 赋值给 permission,再将 permission 存储到 PermissionStore
using DotNetNB.Security.Core.Models;
using DotNetNB.Security.Core.Store;
namespace DotNetNB.Security.Core;
public class PermissionManager: IPermissionManager
{
private readonly IResourceManager _resourceManager;
private readonly IPermissionStore _permissionStore;
public PermissionManager(IResourceManager resourceManager, IPermissionStore permissionStore)
{
_resourceManager = resourceManager;
_permissionStore = permissionStore;
}
public async Task CreateAsync(string key, string displayName, string description, IEnumerable<string> resourceKeys)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var permission = new Permission { Key = key, DisplayName = displayName, Description = description };
var resources = await _resourceManager.GetByKeysAsync(resourceKeys);
permission.Resources = resources;
await _permissionStore.CreateAsync(permission);
}
}
创建 DefaultPermissionStore 实现 IPermissionStore
using DotNetNB.Security.Core.Models;
namespace DotNetNB.Security.Core.Store
{
public class DefaultPermissionStore : IPermissionStore
{
private List<Permission> _list;
public DefaultPermissionStore()
{
_list = new List<Permission>();
}
public async Task CreateAsync(Permission permission)
{
_list.Add(permission);
}
}
}
IPermissionStore 接口添加获取 permission 列表的方法
public Task<IEnumerable<Permission>> GetAllAsync();
在 DefaultPermissionStore 中直接将列表返回即可
public async Task<IEnumerable<Permission>> GetAllAsync()
{
return _list;
}
在 PermissionManager 中通过 PermissionStore 获取 Permission 列表返回
public async Task<IEnumerable<Permission>> GetAllAsync()
{
return await _permissionStore.GetAllAsync();
}
在 IPermissionStore 中添加 GetByKeyAsync 方法,在 PermissionManager 中用于校验 key 是否存在对应的 Permission
public Task<Permission> GetByKeyAsync(string key);
在 DefaultPermissionStore 中实现 GetByKeyAsync 方法
public async Task<Permission> GetByKeyAsync(string key)
{
return _list.SingleOrDefault(r => r.Key == key);
}
在 PermissionManager 中校验 key 是否存在对应的 Permission
var origin = await _permissionStore.GetByKeyAsync(key);
if (origin != null)
throw new InvalidOperationException("Duplicated permission key found");
IResourceManager 接口添加获取所有 resource 方法 GetAllAsync
public Task<IEnumerable<Resource>> GetAllAsync();
ResourceManager 里面需要用到 Store,先定义接口 IResourceStore
using DotNetNB.Security.Core.Models;
namespace DotNetNB.Security.Core.Store
{
public interface IResourceStore
{
public Task CreateAsync(Resource resource);
public Task CreateAsync(IEnumerable<Resource> resources);
public Task<IEnumerable<Resource>> GetAllAsync();
public Task<Resource> GetByKeyAsync(string key);
public Task<IEnumerable<Resource>> GetByKeysAsync(IEnumerable<string> resources);
}
}
创建 DefaultResourceStore 实现 IResourceStore
using DotNetNB.Security.Core.Models;
namespace DotNetNB.Security.Core.Store
{
public class DefaultResourceStore : IResourceStore
{
private readonly List<Resource> _list;
public DefaultResourceStore()
{
_list = new List<Resource>();
}
public async Task CreateAsync(Resource resource)
{
_list.Add(resource);
}
public async Task CreateAsync(IEnumerable<Resource> resources)
{
_list.AddRange(resources);
}
public async Task<IEnumerable<Resource>> GetAllAsync()
{
return _list;
}
public async Task<Resource> GetByKeyAsync(string key)
{
return _list.SingleOrDefault(r => r.Key == key);
}
public async Task<IEnumerable<Resource>> GetByKeysAsync(IEnumerable<string> resources)
{
return _list.Where(r => resources.Contains(r.Key));
}
}
}
在 ResourceManager 中通过 ResourceStore 创建存储获取 Resource,创建的时候判断是否已经存在 Resource
using DotNetNB.Security.Core.Models;
using DotNetNB.Security.Core.Store;
namespace DotNetNB.Security.Core
{
public class ResourceManager : IResourceManager
{
private readonly IResourceStore _resourceStore;
public ResourceManager(IResourceStore resourceStore)
{
_resourceStore = resourceStore;
}
public async Task CreateAsync(Resource resource)
{
var origin = await _resourceStore.GetByKeyAsync(resource.Key);
if (origin != null)
throw new InvalidOperationException("Duplicated resource key found");
await _resourceStore.CreateAsync(resource);
}
public async Task CreateAsync(IEnumerable<Resource> resources)
{
var origins = await _resourceStore.GetByKeysAsync(resources.Select(r => r.Key));
if (origins.Any())
throw new InvalidOperationException($"Duplicated resource key found:{string.Concat(origins.Select(o => o.Key), ",")}");
await _resourceStore.CreateAsync(resources);
}
public async Task<IEnumerable<Resource>> GetAllAsync()
{
return await _resourceStore.GetAllAsync();
}
public async Task<IEnumerable<Resource>> GetByKeysAsync(IEnumerable<string> resources)
{
return await _resourceStore.GetByKeysAsync(resources);
}
}
}
GitHub源码链接:
https://github.com/MingsonZheng/dotnetnb.security
课程链接
https://appsqsyiqlk5791.h5.xiaoeknow.com/v1/course/video/v_5f39bdb8e4b01187873136cf?type=2
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。
.NET 云原生架构师训练营(权限系统 代码实现 Store.EntityFramework)--学习笔记的更多相关文章
- .NET 云原生架构师训练营(设计原则与模式)--学习笔记
在复杂系统的架构设计中引入设计原则与模式,能够极大降低复杂系统开发.和维护的成本 目录 几个问题 为什么要学习设计模式 优良架构设计的具体指标 理解复杂系统 面向对象思想(指导复杂系统的分析.设计.实 ...
- .NET 云原生架构师训练营(KestrelServer源码分析)--学习笔记
目录 目标 源码 目标 理解 KestrelServer 如何接收网络请求,网络请求如何转换成 http request context(C# 可识别) 源码 https://github.com/d ...
- .NET 云原生架构师训练营(系统架构)--学习笔记
目录 对外展现的功能 内部功能 功能交互与价值通路 系统架构 目标 认识系统的价值通路 认识功能架构,通过把功能结构与形式结构结合来描述系统架构 受益原则 好的架构必须使人受益,要想把架构做好,就要专 ...
- .NET 云原生架构师训练营(模块一 架构师与云原生)--学习笔记
目录 什么是软件架构 软件架构的基本思路 单体向分布式演进.云原生.技术中台 1.1 什么是软件架构 1.1.1 什么是架构? Software architecture = {Elements, F ...
- .NET 云原生架构师训练营(权限系统 RGCA 架构设计)--学习笔记
目录 项目核心内容 实战目标 RGCA 四步架构法 项目核心内容 无代码埋点实现对所有 API Action 访问控制管理 对 EF Core 实体新增.删除.字段级读写控制管理 与 Identity ...
- .NET 云原生架构师训练营(权限系统 RGCA 开发任务)--学习笔记
目录 目标 模块拆分 OPM 开发任务 目标 基于上一讲的模块划分做一个任务拆解,根据任务拆解实现功能 模块拆分 模块划分已经完成了边界的划分,边界内外职责清晰 OPM 根据模块拆分画出 OPM(Ob ...
- .NET 云原生架构师训练营(建立系统观)--学习笔记
目录 目标 ASP .NET Core 什么是系统 什么是系统思维 系统分解 什么是复杂系统 作业 目标 通过整体定义去认识系统 通过分解去简化对系统的认识 ASP .NET Core ASP .NE ...
- .NET 云原生架构师训练营(权限系统 代码实现 ActionAccess)--学习笔记
目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...
- .NET 云原生架构师训练营(权限系统 代码实现 Identity)--学习笔记
目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...
- .NET 云原生架构师训练营(权限系统 代码重构)--学习笔记
目录 模块拆分 代码重构 模块拆分 代码重构 AuthenticationController PermissionController IAuthorizationMiddlewareResultH ...
随机推荐
- SpringBoot学习笔记五之管理员后台维护
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6803544440112677379/ 首先完成分页 引入PageHelper(之前已经添加过了) 在spring- ...
- 修正了Model1模式,进入如今盛行的的Model2模式,也就是MVC模式
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6513668601843548675/ 1.<JSP页面实际上就是Servlet> 2.<JSP页 ...
- Centos安装与配置
一.安装 默认安装 二.配置 配置网卡 BOOTPROTO=none ONBOOT=yes IPADDR=xxx.xxx.x.xx PREFIX=24 GATEWAY=xxx.xxx.x.x DNS1 ...
- cesium加载gltf模型点击以及列表点击定位弹窗
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 之 ...
- Docker环境安装,基本命令集合
一.docker安装 1).卸载旧的安装包 centos7默认安装的docker版本是1.13.1,卸载它,安装新的版本. root用户下,一次把这坨命令复制进去 yum remove docker ...
- Spring Boot Starter 和 ABP Module
Spring Boot 和 ABP 都是模块化的系统,分别是Java 和.NET 可以对比的框架.模块系统是就像乐高玩具一样,一块一块零散积木堆积起一个精彩的世界.每种积木的形状各不相同,功能各不相同 ...
- BAT经典面试题之redis的热KEY问题怎么解决
引言 讲了几天的数据库系列的文章,大家一定看烦了,其实还没讲完...(以下省略一万字).今天我们换换口味,来写redis方面的内容,谈谈热key问题如何解决.其实热key问题说来也很简单,就是瞬间有几 ...
- Serverless计算
云服务的演化历程 整个it系统服务的搭建,随着时间有多个层级的演化.从最早的内部部署(On-premises) 到基于云的Iaas,Paas,Saas,Baas, Faas.服务的构建对开发者越来友好 ...
- ansible roles实践 zookeeper集群部署
1.下载解压 wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11. ...
- ES_AutoCheck.sh
#!/bin/bash #@es_check #@date 2019/11/26 #@auth tigergao status=`curl -s GET "http://172.16.71. ...