FalconSSO/FAuth/Controllers/api/Role_UserController.cs

73 lines
2.7 KiB
C#

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;
}
}
}