增加应用、角色组、角色控制器及绑定控制器

This commit is contained in:
falcon 2020-04-15 17:54:33 +08:00
parent 7289a12c8a
commit 691c778bbb
5 changed files with 310 additions and 0 deletions

View File

@ -0,0 +1,70 @@
using System;
using System.Linq;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
/// <summary>
/// 应用角色组对应
/// </summary>
public class App_RoleGroupController:ApiControllerBase<App_RoleGroupController>
{
public App_RoleGroupController(ILogger<App_RoleGroupController> logger,IServiceProvider service) : base(logger,service) {
}
/// <summary>
/// 绑定指定应用和角色组
/// </summary>
/// <param name="appId">应用编号</param>
/// <param name="roleGroupId">角色组编号</param>
/// <returns>是否成功</returns>
[HttpPost]
public bool Binding(int appId,int roleGroupId) {
var aQu = this.Db.Apps.Where(m => m.Id == appId);
if(!aQu.Any()) {
throw new Exception("没有找到指定的应用");
}
var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色组");
}
var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId);
if(!arQu.Any()) {
var model = new App_RoleGroup {
AppId = appId,
RoleGroupId = roleGroupId,
};
this.Db.Entry(model).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
}
return true;
}
/// <summary>
/// 解除指定应用和角色组绑定
/// </summary>
/// <param name="appId">应用编号</param>
/// <param name="roleGroupId">角色组编号</param>
/// <returns>解绑是否成功</returns>
[HttpPost]
public bool Unbind(int appId,int roleGroupId) {
var aQu = this.Db.Apps.Where(m => m.Id == appId);
if(!aQu.Any()) {
throw new Exception("没有找到指定的应用");
}
var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色组");
}
var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId);
if(arQu.Any()) {
foreach(var item in arQu) {
this.Db.Entry(item).State = EntityState.Deleted;
}
this.Db.SaveChangesAsync().Wait();
}
return true;
}
}
}

View File

@ -2,6 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
@ -13,6 +17,50 @@ namespace FAuth.Controllers.api
{
public RoleController(ILogger<RoleController> logger,IServiceProvider service) : base(logger,service) {
}
/// <summary>
/// 添加新角色
/// </summary>
/// <param name="roleName">角色名称</param>
/// <param name="description">角色说明</param>
/// <returns>角色信息</returns>
[HttpPost]
[ProducesResponseType(typeof(Roles),200)]
public Roles AddNewRole(string roleName,string description) {
if(roleName.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(roleName));
}
var qu = this.Db.Roles.Where(m => m.Name == roleName);
if(qu.Any()) {
throw new Exception($"角色{roleName}已经存在,不可以重复创建!");
}
var newRole = new Roles {
Name = roleName,
Description = description,
};
this.Db.Entry(newRole).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
return newRole;
}
/// <summary>
/// 查询角色是否注册
/// </summary>
/// <param name="roleName">角色名称</param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(Roles),200)]
public Roles GetApps(string roleName) {
if(string.IsNullOrEmpty(roleName))
throw new ArgumentException("message",nameof(roleName));
var qu = this.Db.Roles.Where(m => m.Name == roleName);
if(qu.Any()) {
return qu.First();
} else {
throw new Exception("没有找到该角色");
}
}
}
}

View File

@ -1,4 +1,9 @@
using System;
using System.Linq;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
@ -10,6 +15,50 @@ namespace FAuth.Controllers.api
{
public RoleGroupController(ILogger<RoleGroupController> logger,IServiceProvider service) : base(logger,service) {
}
/// <summary>
/// 添加新角色组
/// </summary>
/// <param name="name">角色组名称</param>
/// <param name="description">角色组说明</param>
/// <returns>角色组信息</returns>
[HttpPost]
[ProducesResponseType(typeof(RoleGroup),200)]
public RoleGroup AddNew(string name,string description) {
if(name.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(name));
}
var qu = this.Db.RoleGroups.Where(m => m.Name == name);
if(qu.Any()) {
throw new Exception($"角色组{name}已经存在,不可以重复创建!");
}
var model = new RoleGroup {
Name = name,
Description = description,
};
this.Db.Entry(model).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
return model;
}
/// <summary>
/// 查询角色组是否注册
/// </summary>
/// <param name="name">角色组名称</param>
/// <returns>角色组信息</returns>
[HttpPost]
[ProducesResponseType(typeof(RoleGroup),200)]
public RoleGroup GetApps(string name) {
if(string.IsNullOrEmpty(name))
throw new ArgumentException("message",nameof(name));
var qu = this.Db.RoleGroups.Where(m => m.Name == name);
if(qu.Any()) {
return qu.First();
} else {
throw new Exception("没有找到该角色");
}
}
}
}

View File

@ -0,0 +1,71 @@
using System;
using System.Linq;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
/// <summary>
/// 角色组角色对应
/// </summary>
public class RoleGroup_RoleController:ApiControllerBase<RoleGroup_RoleController>
{
public RoleGroup_RoleController(ILogger<RoleGroup_RoleController> logger,IServiceProvider service) : base(logger,service) {
}
/// <summary>
/// 绑定角色组和角色
/// </summary>
/// <param name="roleGroupId">角色组编号</param>
/// <param name="roleId">角色组</param>
/// <returns>是否成功</returns>
[HttpPost]
public bool Binding(int roleGroupId,int roleId) {
var rgQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rgQu.Any()) {
throw new Exception("没有找到指定的角色组");
}
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
}
var rgr = this.Db.RoleGroup_Roles.Where(m => m.RoleGroupId == roleGroupId && m.RoleId == roleId);
if(!rgr.Any()) {
this.Db.RoleGroup_Roles.Add(new RoleGroup_Role {
RoleGroupId = roleGroupId,
RoleId = roleId,
});
this.Db.SaveChangesAsync().Wait();
}
return true;
}
/// <summary>
/// 角色组角色解除绑定
/// </summary>
/// <param name="roleGroupId">角色组编号</param>
/// <param name="roleId">角色编号</param>
/// <returns>是否成功</returns>
[HttpPost]
public bool Unbind(int roleGroupId,int roleId) {
var rgQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rgQu.Any()) {
throw new Exception("没有找到指定的角色组");
}
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
}
var rgr = this.Db.RoleGroup_Roles.Where(m => m.RoleGroupId == roleGroupId && m.RoleId == roleId);
if(rgr.Any()) {
foreach(var item in rgr) {
this.Db.Entry(item).State = EntityState.Deleted;
}
this.Db.SaveChangesAsync().Wait();
}
return true;
}
}
}

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
/// <summary>
/// 角色用户绑定控制器
/// </summary>
public class Role_UserController:ApiControllerBase<Role_UserController>
{
public Role_UserController(ILogger<Role_UserController> logger,IServiceProvider service) : base(logger,service) {
}
/// <summary>
/// 绑定指定角色和用户
/// </summary>
/// <param name="roleId">角色编号</param>
/// <param name="userId">用户编号</param>
/// <returns>是否成功</returns>
[HttpPost]
public bool Binding(int roleId,int userId) {
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
}
var uQu = this.Db.Users.Where(m => m.Id == userId);
if(!uQu.Any()) {
throw new Exception("没有找到指定的用户");
}
var ruQu = this.Db.Role_Users.Where(m => m.RoleId == roleId && m.UserId == userId);
if(!ruQu.Any()) {
var model = new Role_User {
RoleId = roleId,
UserId = userId,
};
this.Db.Entry(model).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
}
return true;
}
/// <summary>
/// 解除指定应用和角色组绑定
/// </summary>
/// <param name="roleId"></param>
/// <param name="userId"></param>
/// <returns>解绑是否成功</returns>
[HttpPost]
public bool Unbind(int roleId,int userId) {
var rQu = this.Db.Roles.Where(m => m.Id == roleId);
if(!rQu.Any()) {
throw new Exception("没有找到指定的角色");
}
var uQu = this.Db.Users.Where(m => m.Id == userId);
if(!uQu.Any()) {
throw new Exception("没有找到指定的用户");
}
var ruQu = this.Db.Role_Users.Where(m => m.RoleId == roleId && m.UserId == userId);
if(ruQu.Any()) {
foreach(var item in ruQu) {
this.Db.Entry(item).State = EntityState.Deleted;
}
this.Db.SaveChangesAsync().Wait();
}
return true;
}
}
}