rbac修改与删除之前的简单rbac

This commit is contained in:
liyaobang 2025-04-08 16:17:34 +08:00
parent 9f7c6206ca
commit 3401a3b254
7 changed files with 27 additions and 245 deletions

View File

@ -1,84 +0,0 @@
<configuration>
<log4net>
<!--错误日志类-->
<logger name="logerror">
<!--定义记录的日志级别-->
<level value="ALL" />
<!--记录到哪个介质中去-->
<appender-ref ref="ErrorAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
<!--运行信息日志类-->
<logger name="loginfo">
<level value="ALL" />
<appender-ref ref="InfoAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
<!--操作日志类-->
<logger name="logoperate">
<level value="ALL" />
<appender-ref ref="OperateAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
<!--错误日志附加介质-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
<param name="File" value="\\Log\\Error" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="10MB" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
<param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="RollingStyle" value="Date" />
<!--信息日志布局-->
<layout type="log4net.Layout.PatternLayout">
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />-->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!--信息日志附加介质-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="\\Log\\Info" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="10MB" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
<param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="RollingStyle" value="Date" />
<!--信息日志布局-->
<layout type="log4net.Layout.PatternLayout">
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />-->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!--警告日志附加介质-->
<appender name="OperateAppender" type="log4net.Appender.RollingFileAppender">
<!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
<param name="File" value="\\Log\\Operate" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="10MB" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
<param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="RollingStyle" value="Date" />
<!--信息日志布局-->
<layout type="log4net.Layout.PatternLayout">
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />-->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
</configuration>

View File

@ -13,13 +13,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Content Include="Configs\config.json"> <Compile Remove="Services\AuthService.cs" />
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Configs\log4net.config" /> <Content Include="db\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
@ -55,4 +55,14 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
<ItemGroup>
<None Include="db\db.sqlite">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -3,7 +3,7 @@ using DH.RBAC.Logic.Sys;
using DH.RBAC.Model.Sys; using DH.RBAC.Model.Sys;
using DH.RBAC.Utility.Other; using DH.RBAC.Utility.Other;
using DHSoftware.Models; using DHSoftware.Models;
using DHSoftware.Services;
namespace DHSoftware namespace DHSoftware
{ {

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Data.Entity;
using System.Drawing; using System.Drawing;
using System.Reflection;
using System.Windows.Forms; using System.Windows.Forms;
using AntdUI; using AntdUI;
using DH.Commons.Base; using DH.Commons.Base;
@ -13,17 +15,21 @@ using DH.RBAC.Utility.Other;
using DHSoftware.Utils; using DHSoftware.Utils;
using DHSoftware.Views; using DHSoftware.Views;
using Microsoft.VisualBasic.Logging; using Microsoft.VisualBasic.Logging;
using Newtonsoft.Json;
using GlobalConfig = DH.RBAC.Common.GlobalConfig; using GlobalConfig = DH.RBAC.Common.GlobalConfig;
namespace DHSoftware namespace DHSoftware
{ {
internal static class Program internal static class Program
{ {
private static MainWindow mainWindow; private static MainWindow mainWindow;
/// <summary>
/// 应用程序的主入口点。 /// <summary>
/// </summary> /// 应用程序的主入口点。
[STAThread] /// </summary>
[STAThread]
static void Main() static void Main()
{ {
// 必须在第一个窗口创建前调用以下两行 // 必须在第一个窗口创建前调用以下两行
@ -46,10 +52,11 @@ namespace DHSoftware
UpdateStep(10, "正在加载数据库", true); UpdateStep(10, "正在加载数据库", true);
try try
{ {
MyConfig config = File.ReadAllText(MyEnvironment.RootPath("Configs/config.json")).ToObject<MyConfig>(); MyConfig config = File.ReadAllText(MyEnvironment.RootPath("db/config.json")).ToObject<MyConfig>();
GlobalConfig.Config = config; GlobalConfig.Config = config;
string message = ""; string message = "";
bool flag = BaseLogic.InitDB(config.DbType, config.DbHost, config.DbName, config.DbUserName, config.DbPassword, ref message); bool flag = BaseLogic.InitDB(config.DbType, config.DbHost, config.DbName, config.DbUserName, config.DbPassword, ref message);
if (!flag) if (!flag)
{ {
Console.Write(message); Console.Write(message);

View File

@ -1,151 +0,0 @@
using DHSoftware.Models;
using SqlSugar;
namespace DHSoftware.Utils
{
public static class DatabaseUtil
{
private static readonly string DatabasePath = Path.Combine(
Application.StartupPath,
"db",
"RBACSystem.sqlite"
);
public static void InitializeDatabase()
{
EnsureDirectoryExists();
using (var db = GetDatabase())
{
// 检查初始化状态(通过检查是否存在系统表)
bool isInitialized = db.DbMaintenance.IsAnyTable("RolePermission");
if (!isInitialized)
{
// 创建所有表
db.CodeFirst.InitTables(
typeof(User),
typeof(Role),
typeof(Permission),
typeof(UserRole),
typeof(RolePermission)
);
// 初始化基础数据
InitializeSeedData(db);
}
}
}
public static SqlSugarClient GetDatabase()
{
return new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = $"Data Source={DatabasePath};",
DbType = DbType.Sqlite,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
}
private static void EnsureDirectoryExists()
{
var directory = Path.GetDirectoryName(DatabasePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
private static void InitializeSeedData(SqlSugarClient db)
{
// 初始化角色
var adminRole = GetOrCreateRole(db, "admin", "系统管理员");
var userRole = GetOrCreateRole(db, "user", "普通用户");
// 初始化权限
var permissions = new List<Permission>
{
new Permission { Code = "system:access", Name = "访问系统" },
new Permission { Code = "user:view", Name = "查看用户" },
new Permission { Code = "user:edit", Name = "管理用户" },
new Permission { Code = "role:manage", Name = "角色管理" },
new Permission { Code = "system:config", Name = "配置权限" },
new Permission { Code = "system:loadscheme", Name = "加载方案" },
new Permission { Code = "system:addscheme", Name = "新增方案" },
new Permission { Code = "system:deletescheme", Name = "删除方案" }
};
InitializePermissions(db, permissions);
// 分配权限给管理员角色
AssignPermissionsToRole(db, adminRole.Id, permissions.Select(p => p.Code).ToList());
// 创建默认管理员
CreateAdminUser(db);
}
private static Role GetOrCreateRole(SqlSugarClient db, string roleName, string description)
{
var role = db.Queryable<Role>()
.First(r => r.RoleName == roleName);
if (role == null)
{
role = new Role
{
RoleName = roleName,
Description = description
};
role.Id = db.Insertable(role).ExecuteReturnIdentity();
}
return role;
}
private static void InitializePermissions(SqlSugarClient db, List<Permission> permissions)
{
foreach (var p in permissions)
{
if (!db.Queryable<Permission>().Any(x => x.Code == p.Code))
{
db.Insertable(p).ExecuteCommand();
}
}
}
private static void AssignPermissionsToRole(SqlSugarClient db, int roleId, List<string> permissionCodes)
{
var existing = db.Queryable<RolePermission>()
.Where(rp => rp.RoleId == roleId)
.Select(rp => rp.PermissionCode)
.ToList();
foreach (var code in permissionCodes.Except(existing))
{
db.Insertable(new RolePermission
{
RoleId = roleId,
PermissionCode = code
}).ExecuteCommand();
}
}
private static void CreateAdminUser(SqlSugarClient db)
{
if (!db.Queryable<User>().Any(u => u.UserName == "admin"))
{
var admin = new User
{
UserName = "admin",
Password = HashHelper.MD5Encrypt("admin123"),
LastLoginTime = null
};
admin.Id = db.Insertable(admin).ExecuteReturnIdentity();
db.Insertable(new UserRole
{
UserId = admin.Id,
RoleId = db.Queryable<Role>().First(r => r.RoleName == "admin").Id
}).ExecuteCommand();
}
}
}
}

BIN
DHSoftware/db/db.sqlite Normal file

Binary file not shown.