Browse Source

添加授权

DOVER-GLOBAL\11047086 5 months ago
parent
commit
6d9650f696

+ 41 - 0
FuelCloud/Fuel.Application/Authorization/Authorization.cs

@@ -0,0 +1,41 @@
+using Fuel.Application.Service;
+using FuelServer.Core.Entity;
+using Microsoft.Extensions.DependencyInjection;
+using Org.BouncyCastle.Asn1.Ocsp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Fuel.Core
+{
+    public  class Authorization
+    {
+        private  static IUserService _userService;
+
+        public  Authorization(IUserService userService)
+        {
+            _userService = ServiceLocator.GetRequiredService<IUserService>();
+        }
+        public static List<string> GetPermissions()
+        {
+            var permissions = _userService.GetUserPermissions();
+            return permissions;
+        }
+    }
+    public static class ServiceLocator
+    {
+        public static IServiceProvider ServiceProvider { get; set; }
+
+        public static T GetRequiredService<T>() where T : notnull
+        {
+            if (ServiceProvider == null)
+            {
+                throw new InvalidOperationException("");
+            }
+            return ServiceProvider.GetRequiredService<T>();
+        }
+    }
+
+}

+ 2 - 2
FuelCloud/Fuel.Application/Authorization/PermissionHandler.cs

@@ -34,8 +34,8 @@ namespace Fuel.Application.Authorization
                 return;
             }
 
-            var userId = userIdClaim.Value;
-
+           // var userId = userIdClaim.Value;
+            int userId = int.TryParse(userIdClaim.Value, out int number) ? number : 0;
             // 查询用户的权限列表
             var permissions = await _userService.GetUserPermissions(userId);
 

+ 1 - 1
FuelCloud/Fuel.Application/Service/IUserService.cs

@@ -9,7 +9,7 @@ namespace Fuel.Application.Service
 {
     public interface  IUserService
     {
-        Task<IEnumerable<string>> GetUserPermissions(string userId);
+        List<string> GetUserPermissions(int userId = 1);
         users ValidateCredentials(string username, string password);
     }
 }

+ 24 - 12
FuelCloud/Fuel.Application/Service/UserService.cs

@@ -1,4 +1,5 @@
-using FuelServer.Core.Entity;
+using Fuel.Core.Entity;
+using FuelServer.Core.Entity;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -9,23 +10,34 @@ namespace Fuel.Application.Service
 {
     public class UserService : IUserService
     {
-        private readonly EntityHelper _entityHelper;
-        public UserService(EntityHelper entityHelper)
+        public readonly IFreeSql _fsql;
+        public UserService(IFreeSql fsql)
         {
-            _entityHelper = entityHelper;
+            _fsql = fsql;
         }
         public users ValidateCredentials(string username, string password)
         {
-            return _entityHelper.GetEntitiesAsync<users>(_ => _.Account == username && _.Password == password).Result.FirstOrDefault(); ;
+            return _fsql.Select<users>().Where(_ => _.Account == username && _.Password == password).First();
         }
-        public async Task<IEnumerable<string>> GetUserPermissions(string userId)
+        /// <summary>
+        /// 获取权限
+        /// </summary>
+        /// <param name="userId">1 超级管理员</param>
+        /// <returns></returns>
+        public async Task<IEnumerable<string>> GetUserPermissions(int userId = 1)
         {
-            // 这里是模拟数据,实际应用中应该从数据库或其他来源获取权限信息
-            return await Task.FromResult(new List<string>
-        {
-            "Admin:Index:GET",
-            "Admin:Edit:POST"
-            // 更多权限...
+              var Permission =  _fsql.Select<users, AdUserRole,AdRolePermission,AdPermission>()
+    .LeftJoin((a, b, c,d) => a.Id == b.UserId)
+    .LeftJoin((a, b, c, d) => b.RoleId == c.RoleId)
+    .LeftJoin((a, b, c, d) => c.PermissionId == d.Id)
+    .Where((a, b, c, d) => a.Id == userId)
+    .ToList((a, b, c, d) => new { d });
+            var permissionList = new List<string>();
+            foreach (var permission in Permission)
+            {
+                permissionList.Add(permission.d.Code);
+            }
+            return permissionList;
         });
         }
     }

+ 1 - 1
FuelCloud/src/Fuel.Payment.Server/Program.cs

@@ -55,7 +55,7 @@ builder.Services.UseRedisClient(redisOptions);
 // 动态添加基于权限的策略
 void AddPermissionPolicies(AuthorizationOptions options)
 {
-    // 获取所有可能的权限字符串(这里只是一个例子,你应该根据实际情况实现)
+    // 获取权限点
     var permissions = Authorization.GetPermissions();
 
     foreach (var permission in permissions)

+ 0 - 19
FuelCloud/src/FuelServer.Core/Authorization.cs

@@ -1,19 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Fuel.Core
-{
-    public  class Authorization
-    {
-        public static IEnumerable<string> GetPermissions()
-        {
-            return new List<string>
-    {
-        "Nozzle:uploadNozzle:POST",
-    };
-        }
-    }
-}

+ 55 - 0
FuelCloud/src/FuelServer.Core/Entity/AdUserRole.cs

@@ -0,0 +1,55 @@
+using FreeSql.DatabaseModel;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using FreeSql.DataAnnotations;
+namespace Fuel.Core.Entity
+{
+    /// <summary>
+	/// 用户角色
+	/// </summary>
+    [JsonObject(MemberSerialization.OptIn), Table(Name = "ad_user_role", DisableSyncStructure = true)]
+    public partial class AdUserRole
+    {
+        /// <summary>
+		/// 主键Id
+		/// </summary>
+		[JsonProperty, Column(IsPrimary = true)]
+        public long Id { get; set; }
+
+
+        /// <summary>
+        /// 创建时间
+        /// </summary>
+        [JsonProperty, Column(DbType = "datetime")]
+        public DateTime? CreatedTime { get; set; }
+
+        /// <summary>
+        /// 创建者Id
+        /// </summary>
+        [JsonProperty]
+        public long? CreatedUserId { get; set; }
+
+        /// <summary>
+        /// 创建者
+        /// </summary>
+        [JsonProperty, Column(StringLength = 50)]
+        public string CreatedUserName { get; set; }
+
+        /// <summary>
+        /// 用户Id
+        /// </summary>
+        [JsonProperty]
+        public long UserId { get; set; }
+
+        /// <summary>
+        /// 角色Id
+        /// </summary>
+        [JsonProperty]
+        public long RoleId { get; set; }
+    }
+}