Browse Source

refactor crud utils; added auth options dbo

tags/v0.6.0
Robin Thoni 7 years ago
parent
commit
15136fd711
30 changed files with 326 additions and 160 deletions
  1. 2
    2
      Luticate2.Auth/Business/LuBusinessExtensions.cs
  2. 3
    3
      Luticate2.Auth/Business/LuGroupsBusiness.cs
  3. 24
    7
      Luticate2.Auth/Business/LuTokensBusiness.cs
  4. 9
    3
      Luticate2.Auth/Business/LuUsersBusiness.cs
  5. 6
    2
      Luticate2.Auth/Controllers/LuAuthExtensions.cs
  6. 22
    8
      Luticate2.Auth/Controllers/LuUsersController.cs
  7. 7
    0
      Luticate2.Auth/Dbo/Basic/LuAuthOptionsDbo.cs
  8. 1
    1
      Luticate2.Auth/Dbo/Users/LuUsersTokenDbo.cs
  9. 8
    4
      Luticate2.Auth/Interfaces/Tokens/ILuTokensBusiness.cs
  10. 4
    0
      Luticate2.Auth/Interfaces/Users/ILuUsersBusiness.cs
  11. 22
    2
      Luticate2.Auth/Middlewares/LuLoggedUserMiddleware.cs
  12. 19
    15
      Luticate2.Auth/Middlewares/LuPermissionMiddleware.cs
  13. 38
    16
      Luticate2.Utils/Business/LuCrudBusiness.cs
  14. 44
    0
      Luticate2.Utils/Business/LuCrudWebSocketNotificationsBusiness.cs
  15. 0
    49
      Luticate2.Utils/Business/LuNotificationsBusiness.cs
  16. 23
    0
      Luticate2.Utils/Business/LuUtilsBusinessExtensions.cs
  17. 25
    0
      Luticate2.Utils/Business/LuWebSocketNotificationsBusiness.cs
  18. 2
    1
      Luticate2.Utils/Controllers/LuUtilsExtensions.cs
  19. 11
    0
      Luticate2.Utils/Dbo/Basic/LuUtilsConstants.cs
  20. 13
    0
      Luticate2.Utils/Interfaces/ILuCrudNotifications.cs
  21. 7
    0
      Luticate2.Utils/Interfaces/ILuCrudWebSocketNotificationsBusiness.cs
  22. 2
    12
      Luticate2.Utils/Interfaces/ILuNotificationsBusiness.cs
  23. 7
    0
      Luticate2.Utils/Interfaces/ILuWebSocketNotificationsBusiness.cs
  24. 4
    13
      TestAuth/Tests.cs
  25. 1
    1
      WebApiAuth/Startup.cs
  26. 0
    15
      WebApiUtils/Business/PkGuidBusiness.cs
  27. 16
    0
      WebApiUtils/Business/PkGuidsBusiness.cs
  28. 3
    3
      WebApiUtils/Business/UploadBusiness.cs
  29. 2
    2
      WebApiUtils/Controllers/PkGuidController.cs
  30. 1
    1
      WebApiUtils/Startup.cs

+ 2
- 2
Luticate2.Auth/Business/LuBusinessExtensions.cs View File

@@ -39,13 +39,13 @@ namespace Luticate2.Auth.Business
39 39
             };//TODO
40 40
         }
41 41
 
42
-        public static LuUsersToken ToUserToken(this LuTokensDbo dbo)
42
+        public static LuUsersTokenDbo ToUserToken(this LuTokensDbo dbo)
43 43
         {
44 44
             if (dbo == null)
45 45
             {
46 46
                 return null;
47 47
             }
48
-            return new LuUsersToken
48
+            return new LuUsersTokenDbo
49 49
             {
50 50
                 Data = dbo.Data,
51 51
                 NotAfter = dbo.NotAfter,

+ 3
- 3
Luticate2.Auth/Business/LuGroupsBusiness.cs View File

@@ -1,15 +1,15 @@
1
-using Luticate2.Auth.DataAccess;
1
+using System;
2
+using Luticate2.Auth.DataAccess;
2 3
 using Luticate2.Auth.Dbo.Groups;
3 4
 using Luticate2.Auth.Interfaces.Groups;
4 5
 using Luticate2.Utils.Business;
5
-using Luticate2.Utils.Interfaces;
6 6
 
7 7
 namespace Luticate2.Auth.Business
8 8
 {
9 9
     public class LuGroupsBusiness : LuCrudBusiness<LuGroupsDataAccess, LuGroupsAddDbo, LuGroupsDbo, LuGroupsAddDbo, string>,
10 10
         ILuGroupsBusiness
11 11
     {
12
-        public LuGroupsBusiness(LuGroupsDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness) : base(dataAccess, notificationsBusiness)
12
+        public LuGroupsBusiness(LuGroupsDataAccess dataAccess, IServiceProvider serviceProvider) : base(dataAccess, serviceProvider)
13 13
         {
14 14
         }
15 15
     }

+ 24
- 7
Luticate2.Auth/Business/LuTokensBusiness.cs View File

@@ -1,10 +1,13 @@
1 1
 using System;
2
+using System.Linq;
2 3
 using System.Security.Cryptography;
3 4
 using Luticate2.Auth.DataAccess;
4 5
 using Luticate2.Auth.Dbo.Tokens;
5 6
 using Luticate2.Auth.Dbo.Users;
6 7
 using Luticate2.Auth.Interfaces.Tokens;
7 8
 using Luticate2.Utils.Business;
9
+using Luticate2.Utils.Dbo.Basic;
10
+using Luticate2.Utils.Dbo.PaginatedRequest;
8 11
 using Luticate2.Utils.Dbo.Result;
9 12
 using Luticate2.Utils.Interfaces;
10 13
 using Luticate2.Utils.Utils;
@@ -15,17 +18,17 @@ namespace Luticate2.Auth.Business
15 18
     {
16 19
         private readonly IDateTime _dateTime;
17 20
 
18
-        public LuTokensBusiness(LuTokensDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness, IDateTime dateTime) : base(dataAccess, notificationsBusiness)
21
+        public LuTokensBusiness(LuTokensDataAccess dataAccess, IServiceProvider serviceProvider, IDateTime dateTime) : base(dataAccess, serviceProvider)
19 22
         {
20 23
             _dateTime = dateTime;
21 24
         }
22 25
 
23
-        public LuResult<LuUsersToken> GetToken(string token)
26
+        public LuResult<LuUsersTokenDbo> GetToken(string token)
24 27
         {
25 28
             return GetSingleById(token).To(dbo => dbo.ToUserToken());
26 29
         }
27 30
 
28
-        public string GenerateId()
31
+        public string GenerateId()//TODO
29 32
         {
30 33
             var token = new byte[50];
31 34
             using (var rng = RandomNumberGenerator.Create())
@@ -35,7 +38,7 @@ namespace Luticate2.Auth.Business
35 38
             return Convert.ToBase64String(token).Trim('=');
36 39
         }
37 40
 
38
-        public LuResult<string> RegisterToken(LuUsersToken token)
41
+        public LuResult<string> RegisterToken(LuUsersTokenDbo token)
39 42
         {
40 43
             string id;
41 44
             LuResult<LuTokensDbo> tokenRes;
@@ -60,12 +63,12 @@ namespace Luticate2.Auth.Business
60 63
             });
61 64
         }
62 65
 
63
-        public LuResult<LuUsersToken> UnRegisterToken(string token)
66
+        public LuResult<LuUsersTokenDbo> UnRegisterToken(string token)
64 67
         {
65 68
             return this.DeleteSingleByIdDbo(token).To(dbo => dbo.ToUserToken());
66 69
         }
67 70
 
68
-        public bool IsTokenValid(LuUsersToken token)
71
+        public bool IsTokenValid(LuUsersTokenDbo token)
69 72
         {
70 73
             var now = _dateTime.Now;
71 74
             return (token.NotBefore == null || now >= token.NotBefore) &&
@@ -74,7 +77,7 @@ namespace Luticate2.Auth.Business
74 77
 
75 78
         public LuResult<string> GenerateToken(LuUsersDbo user)
76 79
         {
77
-            var token = new LuUsersToken
80
+            var token = new LuUsersTokenDbo
78 81
             {
79 82
                 Data = null,//TODO
80 83
                 NotAfter = null,
@@ -84,5 +87,19 @@ namespace Luticate2.Auth.Business
84 87
 
85 88
             return RegisterToken(token);
86 89
         }
90
+
91
+        public LuResult<LuPaginatedDbo<LuUsersTokenDbo>> GetTokensForUser(LuUsersDbo user, LuPaginatedRequestDbo paginatedRequestDbo)
92
+        {
93
+            if (paginatedRequestDbo.Filter.Filters.ContainsKey("userId"))
94
+            {
95
+                paginatedRequestDbo.Filter.Filters["userId"] = user.Id;
96
+            }
97
+            else
98
+            {
99
+                paginatedRequestDbo.Filter.Filters.Add("userId", user.Id);
100
+            }
101
+            return GetMultiple(paginatedRequestDbo)
102
+                .To(dbo => dbo.To(dbos => dbos.Select(tokensDbo => tokensDbo.ToUserToken()).ToList()));
103
+        }
87 104
     }
88 105
 }

+ 9
- 3
Luticate2.Auth/Business/LuUsersBusiness.cs View File

@@ -1,5 +1,4 @@
1 1
 using System;
2
-using System.Collections.Generic;
3 2
 using System.Security.Cryptography;
4 3
 using System.Text;
5 4
 using Luticate2.Auth.DataAccess;
@@ -7,6 +6,8 @@ using Luticate2.Auth.Dbo.Users;
7 6
 using Luticate2.Auth.Interfaces.Tokens;
8 7
 using Luticate2.Auth.Interfaces.Users;
9 8
 using Luticate2.Utils.Business;
9
+using Luticate2.Utils.Dbo.Basic;
10
+using Luticate2.Utils.Dbo.PaginatedRequest;
10 11
 using Luticate2.Utils.Dbo.Result;
11 12
 using Luticate2.Utils.Interfaces;
12 13
 using Luticate2.Utils.Utils;
@@ -19,8 +20,8 @@ namespace Luticate2.Auth.Business
19 20
         private readonly ILuTokensBusiness _luTokensBusiness;
20 21
         private readonly ILuLoggedUserAccessor _luLoggedUserAccessor;
21 22
 
22
-        public LuUsersBusiness(LuUsersDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness,
23
-            ILuTokensBusiness luTokensBusiness, ILuLoggedUserAccessor luLoggedUserAccessor) : base(dataAccess, notificationsBusiness)
23
+        public LuUsersBusiness(LuUsersDataAccess dataAccess, IServiceProvider serviceProvider,
24
+            ILuTokensBusiness luTokensBusiness, ILuLoggedUserAccessor luLoggedUserAccessor) : base(dataAccess, serviceProvider)
24 25
         {
25 26
             _luTokensBusiness = luTokensBusiness;
26 27
             _luLoggedUserAccessor = luLoggedUserAccessor;
@@ -148,5 +149,10 @@ namespace Luticate2.Auth.Business
148 149
             }
149 150
             return base.DeleteSingleById(id, returnFunc);
150 151
         }
152
+
153
+        public LuResult<LuPaginatedDbo<LuUsersTokenDbo>> Sessions(LuPaginatedRequestDbo paginatedRequestDbo)
154
+        {
155
+            return _luTokensBusiness.GetTokensForUser(_luLoggedUserAccessor.GetLoggedUser().ToLite(), paginatedRequestDbo);
156
+        }
151 157
     }
152 158
 }

+ 6
- 2
Luticate2.Auth/Controllers/LuAuthExtensions.cs View File

@@ -2,6 +2,7 @@
2 2
 using Luticate2.Auth.Attributes.EntityAccessors;
3 3
 using Luticate2.Auth.Business;
4 4
 using Luticate2.Auth.DataAccess;
5
+using Luticate2.Auth.Dbo.Basic;
5 6
 using Luticate2.Auth.Dbo.Users;
6 7
 using Luticate2.Auth.Interfaces.Groups;
7 8
 using Luticate2.Auth.Interfaces.Permissions;
@@ -28,9 +29,10 @@ namespace Luticate2.Auth.Controllers
28 29
         public const string TokenCookieName = "luticate2-token";
29 30
 
30 31
         public static IServiceCollection AddLuticateAuth(this IServiceCollection services,
31
-            Action<LuUtilsOptionsDbo> optionsDelegate, Action<DbContextOptionsBuilder> optionsAction)
32
+            Action<LuUtilsOptionsDbo> utilsOptionsDelegate, Action<DbContextOptionsBuilder> optionsAction,
33
+            Action<LuAuthOptionsDbo> authOptionsDelegate)
32 34
         {
33
-            services.AddLuticateUtils(optionsDelegate);
35
+            services.AddLuticateUtils(utilsOptionsDelegate);
34 36
 
35 37
             services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
36 38
 
@@ -60,6 +62,8 @@ namespace Luticate2.Auth.Controllers
60 62
                 optionsAction.Invoke(options);
61 63
             }, ServiceLifetime.Transient);
62 64
 
65
+            services.Configure(authOptionsDelegate);
66
+
63 67
             return services;
64 68
         }
65 69
 

+ 22
- 8
Luticate2.Auth/Controllers/LuUsersController.cs View File

@@ -1,13 +1,13 @@
1 1
 using System.ComponentModel.DataAnnotations;
2 2
 using Luticate2.Auth.Attributes;
3 3
 using Luticate2.Auth.Business;
4
+using Luticate2.Auth.Dbo.Basic;
4 5
 using Luticate2.Auth.Dbo.Permissions;
5 6
 using Luticate2.Auth.Dbo.Users;
6 7
 using Luticate2.Auth.Interfaces.Users;
7 8
 using Luticate2.Utils.Controllers;
8 9
 using Luticate2.Utils.Dbo.Basic;
9 10
 using Luticate2.Utils.Dbo.PaginatedRequest;
10
-using Luticate2.Utils.Dbo.Result;
11 11
 using Luticate2.Utils.Utils;
12 12
 using Microsoft.AspNetCore.Http;
13 13
 using Microsoft.AspNetCore.Mvc;
@@ -25,10 +25,13 @@ namespace Luticate2.Auth.Controllers
25 25
         private const string WritePermission = LuPermissions.LuGroupsWrite;
26 26
 
27 27
         private readonly ILuUsersBusiness _busines;
28
+        private readonly LuAuthOptionsDbo _luAuthOptions;
28 29
 
29
-        public LuUsersController(ILuUsersBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(luUtilsOptionsDbo)
30
+        public LuUsersController(ILuUsersBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo,
31
+            IOptions<LuAuthOptionsDbo> luAuthOptions) : base(luUtilsOptionsDbo)
30 32
         {
31 33
             _busines = busines;
34
+            _luAuthOptions = luAuthOptions.Value;
32 35
         }
33 36
 
34 37
         [HttpGet("{id}")]
@@ -75,22 +78,27 @@ namespace Luticate2.Auth.Controllers
75 78
                 Response.Cookies.Append(LuAuthExtensions.TokenCookieName, loginRes.Data.Token, new CookieOptions
76 79
                 {
77 80
                     HttpOnly = true,
78
-                    Secure = true
81
+                    Secure = _luAuthOptions.SecureCookies
79 82
                 });
80 83
             }
81 84
             return Handle(loginRes);
82 85
         }
83 86
 
84 87
         [HttpPost("logout")]
85
-        public LuApiWrapperDbo<bool> Logout()
88
+        public LuApiWrapperDbo<bool> Logout(/*string token*/)
86 89
         {
87
-            var token = HttpContext.GetLuUserToken();
88
-            if (token != null)
90
+//            var t = token;
91
+//            if (t == null)
92
+//            {
93
+//                t = HttpContext.GetLuUserToken();
94
+//            }
95
+            var t = HttpContext.GetLuUserToken();
96
+            var logoutRes = _busines.Logout(t);
97
+            if (logoutRes/* && (token == null || t == token)*/)
89 98
             {
90 99
                 Response.Cookies.Delete(LuAuthExtensions.TokenCookieName);
91
-                return Handle(_busines.Logout(token));
92 100
             }
93
-            return Handle(LuResult<bool>.Ok(true));
101
+            return Handle(logoutRes);
94 102
         }
95 103
 
96 104
         [HttpGet("me")]
@@ -98,5 +106,11 @@ namespace Luticate2.Auth.Controllers
98 106
         {
99 107
             return Handle(_busines.Me().ToLite());
100 108
         }
109
+
110
+        [HttpGet("sessions")]
111
+        public LuApiWrapperDbo<LuPaginatedDbo<LuUsersTokenDbo>> Sessions(LuPaginatedRequestDbo paginatedRequestDbo)
112
+        {
113
+            return Handle(_busines.Sessions(paginatedRequestDbo));
114
+        }
101 115
     }
102 116
 }

+ 7
- 0
Luticate2.Auth/Dbo/Basic/LuAuthOptionsDbo.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Auth.Dbo.Basic
2
+{
3
+    public class LuAuthOptionsDbo
4
+    {
5
+        public bool SecureCookies { get; set; }
6
+    }
7
+}

Luticate2.Auth/Dbo/Users/LuUsersToken.cs → Luticate2.Auth/Dbo/Users/LuUsersTokenDbo.cs View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
3 3
 
4 4
 namespace Luticate2.Auth.Dbo.Users
5 5
 {
6
-    public class LuUsersToken
6
+    public class LuUsersTokenDbo
7 7
     {
8 8
         public string UserId { get; set; }
9 9
 

+ 8
- 4
Luticate2.Auth/Interfaces/Tokens/ILuTokensBusiness.cs View File

@@ -1,18 +1,22 @@
1 1
 using Luticate2.Auth.Dbo.Users;
2
+using Luticate2.Utils.Dbo.Basic;
3
+using Luticate2.Utils.Dbo.PaginatedRequest;
2 4
 using Luticate2.Utils.Dbo.Result;
3 5
 
4 6
 namespace Luticate2.Auth.Interfaces.Tokens
5 7
 {
6 8
     public interface ILuTokensBusiness
7 9
     {
8
-        LuResult<LuUsersToken> GetToken(string token);
10
+        LuResult<LuUsersTokenDbo> GetToken(string token);
9 11
 
10
-        LuResult<string> RegisterToken(LuUsersToken token);
12
+        LuResult<string> RegisterToken(LuUsersTokenDbo token);
11 13
 
12
-        LuResult<LuUsersToken> UnRegisterToken(string token);
14
+        LuResult<LuUsersTokenDbo> UnRegisterToken(string token);
13 15
 
14
-        bool IsTokenValid(LuUsersToken token);
16
+        bool IsTokenValid(LuUsersTokenDbo token);
15 17
 
16 18
         LuResult<string> GenerateToken(LuUsersDbo user);
19
+
20
+        LuResult<LuPaginatedDbo<LuUsersTokenDbo>> GetTokensForUser(LuUsersDbo user, LuPaginatedRequestDbo paginatedRequestDbo);
17 21
     }
18 22
 }

+ 4
- 0
Luticate2.Auth/Interfaces/Users/ILuUsersBusiness.cs View File

@@ -1,4 +1,6 @@
1 1
 using Luticate2.Auth.Dbo.Users;
2
+using Luticate2.Utils.Dbo.Basic;
3
+using Luticate2.Utils.Dbo.PaginatedRequest;
2 4
 using Luticate2.Utils.Dbo.Result;
3 5
 using Luticate2.Utils.Interfaces;
4 6
 
@@ -23,5 +25,7 @@ namespace Luticate2.Auth.Interfaces.Users
23 25
         LuResult<LuUsersFullDbo> Edit(string id, LuUsersEditDbo user);
24 26
 
25 27
         LuResult<LuUsersFullDbo> Me();
28
+
29
+        LuResult<LuPaginatedDbo<LuUsersTokenDbo>> Sessions(LuPaginatedRequestDbo paginatedRequestDbo);
26 30
     }
27 31
 }

+ 22
- 2
Luticate2.Auth/Middlewares/LuLoggedUserMiddleware.cs View File

@@ -1,10 +1,10 @@
1 1
 using System;
2
-using Luticate2.Auth.Business;
3 2
 using Luticate2.Auth.Controllers;
4 3
 using Luticate2.Auth.Interfaces.Tokens;
5 4
 using Luticate2.Auth.Interfaces.Users;
6 5
 using Luticate2.Utils.Dbo.Result;
7 6
 using Luticate2.Utils.Utils;
7
+using Microsoft.AspNetCore.Mvc.Controllers;
8 8
 using Microsoft.AspNetCore.Mvc.Filters;
9 9
 
10 10
 namespace Luticate2.Auth.Middlewares
@@ -23,9 +23,29 @@ namespace Luticate2.Auth.Middlewares
23 23
 
24 24
         public void OnActionExecuting(ActionExecutingContext context)
25 25
         {
26
+            var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
27
+            if (actionDescriptor == null)
28
+            {
29
+                LuResult<bool>.Error(LuStatus.InternalError,
30
+                    $"{nameof(LuLoggedUserMiddleware)}: actionDescriptor == null").Throw();
31
+                return;
32
+            }
33
+            var isLogin = false;
34
+            var isLogout = false;
35
+            if (actionDescriptor.ControllerTypeInfo.AsType() == typeof(LuUsersController))
36
+            {
37
+                if (actionDescriptor.ActionName == nameof(LuUsersController.Login))
38
+                {
39
+                    isLogin = true;
40
+                }
41
+                else if (actionDescriptor.ActionName == nameof(LuUsersController.Logout))
42
+                {
43
+                    isLogout = true;
44
+                }
45
+            }
26 46
             var token = context.HttpContext.GetLuUserToken();
27 47
             var userId = Guid.Empty.ToDbo();
28
-            if (token != null)
48
+            if (token != null && !isLogout && !isLogin)//TODO
29 49
             {
30 50
                 var tokenRes = _luTokensBusiness.GetToken(token);
31 51
                 if (tokenRes.Status == LuStatus.NotFound)

+ 19
- 15
Luticate2.Auth/Middlewares/LuPermissionMiddleware.cs View File

@@ -5,6 +5,7 @@ using Luticate2.Auth.Interfaces.Permissions;
5 5
 using Luticate2.Auth.Interfaces.Users;
6 6
 using Luticate2.Utils.Controllers;
7 7
 using Luticate2.Utils.Dbo.Result;
8
+using Microsoft.AspNetCore.Authorization.Infrastructure;
8 9
 using Microsoft.AspNetCore.Mvc.Controllers;
9 10
 using Microsoft.AspNetCore.Mvc.Filters;
10 11
 using Microsoft.Extensions.DependencyInjection;
@@ -25,23 +26,26 @@ namespace Luticate2.Auth.Middlewares
25 26
         public void OnActionExecuting(ActionExecutingContext context)
26 27
         {
27 28
             var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
28
-            if (actionDescriptor != null)
29
+            if (actionDescriptor == null)
29 30
             {
30
-                var attributes = actionDescriptor.MethodInfo.GetCustomAttributes<LuPermissionAttribute>();
31
-                foreach (var attribute in attributes)
31
+                LuResult<bool>.Error(LuStatus.InternalError,
32
+                    $"{nameof(LuPermissionMiddleware)}: actionDescriptor == null").Throw();
33
+                return;
34
+            }
35
+            var attributes = actionDescriptor.MethodInfo.GetCustomAttributes<LuPermissionAttribute>();
36
+            foreach (var attribute in attributes)
37
+            {
38
+                var srcAccessor = _serviceProvider.GetService(attribute.SrcEntityAccessor) as ILuAttrEntityAccessor;
39
+                var dstAccessor = _serviceProvider.GetService(attribute.DstEntityAccessor) as ILuAttrEntityAccessor;
40
+                var srcEntity = srcAccessor?.GetEntity(context, attribute.Id);
41
+                var dstEntity = dstAccessor?.GetEntity(context, attribute.Id);
42
+                var permissionValue = _luPermissionsBusiness.GetPermissionEffectiveValue(attribute.PermissionName,
43
+                    attribute.SrcEntityType, srcEntity, attribute.DstEntityType, dstEntity).ThrowIfNotSuccess();
44
+                if (!permissionValue.Data)
32 45
                 {
33
-                    var srcAccessor = _serviceProvider.GetService(attribute.SrcEntityAccessor) as ILuAttrEntityAccessor;
34
-                    var dstAccessor = _serviceProvider.GetService(attribute.DstEntityAccessor) as ILuAttrEntityAccessor;
35
-                    var srcEntity = srcAccessor?.GetEntity(context, attribute.Id);
36
-                    var dstEntity = dstAccessor?.GetEntity(context, attribute.Id);
37
-                    var permissionValue = _luPermissionsBusiness.GetPermissionEffectiveValue(attribute.PermissionName,
38
-                        attribute.SrcEntityType, srcEntity, attribute.DstEntityType, dstEntity).ThrowIfNotSuccess();
39
-                    if (!permissionValue.Data)
40
-                    {
41
-                        var user = _serviceProvider.GetService<ILuLoggedUserAccessor>().GetLoggedUser();
42
-                        LuResult<object>.Error(LuStatus.PermissionError,$"user: {user?.Id} {user?.Username}",
43
-                            "Permission denied").Throw();
44
-                    }
46
+                    var user = _serviceProvider.GetService<ILuLoggedUserAccessor>().GetLoggedUser();
47
+                    LuResult<object>.Error(LuStatus.PermissionError,$"user: {user?.Id} {user?.Username}",
48
+                        "Permission denied").Throw();
45 49
                 }
46 50
             }
47 51
         }

+ 38
- 16
Luticate2.Utils/Business/LuCrudBusiness.cs View File

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using System.Collections.Generic;
2 3
 using Luticate2.Utils.Dbo.Basic;
3 4
 using Luticate2.Utils.Dbo.PaginatedRequest;
4 5
 using Luticate2.Utils.Dbo.Result;
@@ -17,20 +18,20 @@ namespace Luticate2.Utils.Business
17 18
     {
18 19
         protected readonly TDataAccess DataAccess;
19 20
 
20
-        private readonly ILuNotificationsBusiness _notificationsBusiness;
21
+        protected readonly IServiceProvider ServiceProvider;
21 22
 
22
-        protected virtual string EntityType { get; set; }
23
+        private readonly IList<Type> _listeners = new List<Type>();
23 24
 
24
-        protected virtual Func<string, TDboRead, bool> NotifyCreateFilter { get; set; }
25
-
26
-        protected virtual Func<string, TDboRead, TDboRead, bool> NotifyUpdateFilter { get; set; }
27
-
28
-        protected virtual Func<string, TDboRead, bool> NotifyDeleteFilter { get; set; }
29
-
30
-        protected LuCrudBusiness(TDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness)
25
+        protected LuCrudBusiness(TDataAccess dataAccess, IServiceProvider serviceProvider)
31 26
         {
32 27
             DataAccess = dataAccess;
33
-            _notificationsBusiness = notificationsBusiness;
28
+            ServiceProvider = serviceProvider;
29
+        }
30
+
31
+        public void AddListener<T>()
32
+            where T : ILuCrudNotifications
33
+        {
34
+            _listeners.Add(typeof(T));
34 35
         }
35 36
 
36 37
         protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
@@ -69,8 +70,7 @@ namespace Luticate2.Utils.Business
69 70
             var newEntity = DataAccess.AddDbo(res.Data);
70 71
             if (newEntity)
71 72
             {
72
-                _notificationsBusiness.NotifyCreate(EntityType, newEntity.Data,
73
-                    NotifyCreateFilter != null ? s => NotifyCreateFilter(s, newEntity.Data) : (Func<string, bool>) null);
73
+                NotifyCreate(newEntity.Data);
74 74
                 return LuResult<T>.Ok(returnFunc(newEntity.Data));
75 75
             }
76 76
             return newEntity.To<T>();
@@ -109,8 +109,7 @@ namespace Luticate2.Utils.Business
109 109
             var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
110 110
             if (editedEntity)
111 111
             {
112
-                _notificationsBusiness.NotifyUpdate(EntityType, originalEntity.Data, editedEntity.Data,
113
-                    NotifyUpdateFilter != null ? s => NotifyUpdateFilter(s, originalEntity.Data, editedEntity.Data) : (Func<string, bool>) null);
112
+                NotifyUpdate(originalEntity.Data, editedEntity.Data);
114 113
                 return LuResult<T>.Ok(returnFunc(editedEntity.Data));
115 114
             }
116 115
             return editedEntity.To<T>();
@@ -129,10 +128,33 @@ namespace Luticate2.Utils.Business
129 128
             var res = DataAccess.DeleteSingleById(id, returnFunc);
130 129
             if (res)
131 130
             {
132
-                _notificationsBusiness.NotifyDelete(EntityType, originalEntity.Data,
133
-                    NotifyDeleteFilter != null ? s => NotifyDeleteFilter(s, originalEntity.Data) : (Func<string, bool>) null);
131
+                NotifyDelete(originalEntity.Data);
134 132
             }
135 133
             return res;
136 134
         }
135
+
136
+        private void ForeachListener(Action<ILuCrudNotifications> action)
137
+        {
138
+            foreach (var listener in _listeners)
139
+            {
140
+                var instance = ServiceProvider.GetService(listener) as ILuCrudNotifications;
141
+                action(instance);
142
+            }
143
+        }
144
+
145
+        private void NotifyCreate(object newEntity)
146
+        {
147
+            ForeachListener(notifications => notifications.NotifyCreate(GetType(), newEntity));
148
+        }
149
+
150
+        private void NotifyUpdate(object oldEntity, object newEntity)
151
+        {
152
+            ForeachListener(notifications => notifications.NotifyUpdate(GetType(), oldEntity, newEntity));
153
+        }
154
+
155
+        private void NotifyDelete( object oldEntity)
156
+        {
157
+            ForeachListener(notifications => notifications.NotifyDelete(GetType(), oldEntity));
158
+        }
137 159
     }
138 160
 }

+ 44
- 0
Luticate2.Utils/Business/LuCrudWebSocketNotificationsBusiness.cs View File

@@ -0,0 +1,44 @@
1
+using System;
2
+using Luticate2.Utils.Interfaces;
3
+
4
+namespace Luticate2.Utils.Business
5
+{
6
+    public class LuCrudWebSocketNotificationsBusiness : ILuCrudWebSocketNotificationsBusiness
7
+    {
8
+        private readonly ILuWebSocketNotificationsBusiness _luWebSocketNotificationsBusiness;
9
+
10
+        public LuCrudWebSocketNotificationsBusiness(ILuWebSocketNotificationsBusiness luWebSocketNotificationsBusiness)
11
+        {
12
+            _luWebSocketNotificationsBusiness = luWebSocketNotificationsBusiness;
13
+        }
14
+
15
+        public string GetEntityType(Type crudType)
16
+        {
17
+            var name = crudType.Name;
18
+            if (name.EndsWith("Business"))
19
+            {
20
+                name = name.Remove(name.Length - 8);
21
+            }
22
+            if (name.Length > 0 && char.IsUpper(name[0]))
23
+            {
24
+                name = char.ToLower(name[0]) + name.Remove(0, 1);
25
+            }
26
+            return name;
27
+        }
28
+
29
+        public void NotifyCreate(Type crudType, object newEntity)
30
+        {
31
+            _luWebSocketNotificationsBusiness.NotifyCreate(GetEntityType(crudType), newEntity);
32
+        }
33
+
34
+        public void NotifyUpdate(Type crudType, object oldEntity, object newEntity)
35
+        {
36
+            _luWebSocketNotificationsBusiness.NotifyUpdate(GetEntityType(crudType), oldEntity, newEntity);
37
+        }
38
+
39
+        public void NotifyDelete(Type crudType, object oldEntity)
40
+        {
41
+            _luWebSocketNotificationsBusiness.NotifyDelete(GetEntityType(crudType), oldEntity);
42
+        }
43
+    }
44
+}

+ 0
- 49
Luticate2.Utils/Business/LuNotificationsBusiness.cs View File

@@ -1,49 +0,0 @@
1
-using System;
2
-using System.Linq;
3
-using Luticate2.Utils.Hubs;
4
-using Luticate2.Utils.Interfaces;
5
-using Microsoft.AspNetCore.SignalR;
6
-using Microsoft.AspNetCore.SignalR.Infrastructure;
7
-
8
-namespace Luticate2.Utils.Business
9
-{
10
-    public class LuNotificationsBusiness : ILuNotificationsBusiness
11
-    {
12
-        private readonly LuHubConnectionTracker _connectionTracker;
13
-        private readonly IHubContext _hubContext;
14
-
15
-        public const string EventCreate = "EVENT_CREATE";
16
-
17
-        public const string EventUpdate = "EVENT_UPDATE";
18
-
19
-        public const string EventDelete = "EVENT_DELETE";
20
-
21
-        public LuNotificationsBusiness(IConnectionManager connectionManager, LuHubConnectionTracker connectionTracker)
22
-        {
23
-            _connectionTracker = connectionTracker;
24
-            _hubContext = connectionManager.GetHubContext<LuNotificationsHub>();
25
-        }
26
-
27
-        public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
28
-        {
29
-            var connectionsIds = _connectionTracker.Get<LuNotificationsHub>();
30
-            var selectedIds = filter != null ? connectionsIds.Where(filter).ToList() : connectionsIds;
31
-            _hubContext.Clients.Clients(selectedIds).notify(eventName, entityType, oldEntity, newEntity);
32
-        }
33
-
34
-        public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
35
-        {
36
-            Notify(EventCreate, entityType, null, newEntity, filter);
37
-        }
38
-
39
-        public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
40
-        {
41
-            Notify(EventUpdate, entityType, oldEntity, newEntity, filter);
42
-        }
43
-
44
-        public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
45
-        {
46
-            Notify(EventDelete, entityType, oldEntity, null, filter);
47
-        }
48
-    }
49
-}

+ 23
- 0
Luticate2.Utils/Business/LuUtilsBusinessExtensions.cs View File

@@ -0,0 +1,23 @@
1
+using Luticate2.Utils.Dbo.Basic;
2
+using Luticate2.Utils.Interfaces;
3
+
4
+namespace Luticate2.Utils.Business
5
+{
6
+    public static class LuUtilsBusinessExtensions
7
+    {
8
+        public static void NotifyCreate(this ILuNotificationsBusiness business, string entityType, object newEntity)
9
+        {
10
+            business.Notify(LuUtilsConstants.EventCreate, entityType, null, newEntity);
11
+        }
12
+
13
+        public static void NotifyUpdate(this ILuNotificationsBusiness business, string entityType, object oldEntity, object newEntity)
14
+        {
15
+            business.Notify(LuUtilsConstants.EventUpdate, entityType, oldEntity, newEntity);
16
+        }
17
+
18
+        public static void NotifyDelete(this ILuNotificationsBusiness business, string entityType, object oldEntity)
19
+        {
20
+            business.Notify(LuUtilsConstants.EventDelete, entityType, oldEntity, null);
21
+        }
22
+    }
23
+}

+ 25
- 0
Luticate2.Utils/Business/LuWebSocketNotificationsBusiness.cs View File

@@ -0,0 +1,25 @@
1
+using Luticate2.Utils.Hubs;
2
+using Luticate2.Utils.Interfaces;
3
+using Microsoft.AspNetCore.SignalR;
4
+using Microsoft.AspNetCore.SignalR.Infrastructure;
5
+
6
+namespace Luticate2.Utils.Business
7
+{
8
+    public class LuWebSocketNotificationsBusiness : ILuWebSocketNotificationsBusiness
9
+    {
10
+        private readonly LuHubConnectionTracker _connectionTracker;
11
+        private readonly IHubContext _hubContext;
12
+
13
+        public LuWebSocketNotificationsBusiness(IConnectionManager connectionManager, LuHubConnectionTracker connectionTracker)
14
+        {
15
+            _connectionTracker = connectionTracker;
16
+            _hubContext = connectionManager.GetHubContext<LuNotificationsHub>();
17
+        }
18
+
19
+        public void Notify(string eventName, string entityType, object oldEntity, object newEntity)
20
+        {
21
+            var connectionsIds = _connectionTracker.Get<LuNotificationsHub>();
22
+            _hubContext.Clients.Clients(connectionsIds).notify(eventName, entityType, oldEntity, newEntity);
23
+        }
24
+    }
25
+}

+ 2
- 1
Luticate2.Utils/Controllers/LuUtilsExtensions.cs View File

@@ -43,7 +43,8 @@ namespace Luticate2.Utils.Controllers
43 43
 
44 44
             services.TryAddScoped<LuEfTransactionScope>();
45 45
             services.TryAddSingleton<LuHubConnectionTracker>();
46
-            services.TryAddSingleton<ILuNotificationsBusiness, LuNotificationsBusiness>();
46
+            services.TryAddSingleton<ILuWebSocketNotificationsBusiness, LuWebSocketNotificationsBusiness>();
47
+            services.TryAddSingleton<ILuCrudWebSocketNotificationsBusiness, LuCrudWebSocketNotificationsBusiness>();
47 48
             services.Configure(optionsDelegate);
48 49
             return services;
49 50
         }

+ 11
- 0
Luticate2.Utils/Dbo/Basic/LuUtilsConstants.cs View File

@@ -0,0 +1,11 @@
1
+namespace Luticate2.Utils.Dbo.Basic
2
+{
3
+    public class LuUtilsConstants
4
+    {
5
+        public const string EventCreate = "EVENT_CREATE";
6
+
7
+        public const string EventUpdate = "EVENT_UPDATE";
8
+
9
+        public const string EventDelete = "EVENT_DELETE";
10
+    }
11
+}

+ 13
- 0
Luticate2.Utils/Interfaces/ILuCrudNotifications.cs View File

@@ -0,0 +1,13 @@
1
+using System;
2
+
3
+namespace Luticate2.Utils.Interfaces
4
+{
5
+    public interface ILuCrudNotifications
6
+    {
7
+        void NotifyCreate(Type crudType, object newEntity);
8
+
9
+        void NotifyUpdate(Type crudType, object oldEntity, object newEntity);
10
+
11
+        void NotifyDelete(Type crudType, object oldEntity);
12
+    }
13
+}

+ 7
- 0
Luticate2.Utils/Interfaces/ILuCrudWebSocketNotificationsBusiness.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Utils.Interfaces
2
+{
3
+    public interface ILuCrudWebSocketNotificationsBusiness : ILuCrudNotifications
4
+    {
5
+
6
+    }
7
+}

+ 2
- 12
Luticate2.Utils/Interfaces/ILuNotificationsBusiness.cs View File

@@ -1,17 +1,7 @@
1
-using System;
2
-
3
-namespace Luticate2.Utils.Interfaces
1
+namespace Luticate2.Utils.Interfaces
4 2
 {
5 3
     public interface ILuNotificationsBusiness
6 4
     {
7
-        void Notify(string eventName, string entityType, object oldEntity, object newEntity,
8
-            Func<string, bool> filter = null);
9
-
10
-        void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null);
11
-
12
-        void NotifyUpdate(string entityType, object oldEntity, object newEntity,
13
-            Func<string, bool> filter = null);
14
-
15
-        void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null);
5
+        void Notify(string eventName, string entityType, object oldEntity, object newEntity);
16 6
     }
17 7
 }

+ 7
- 0
Luticate2.Utils/Interfaces/ILuWebSocketNotificationsBusiness.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Utils.Interfaces
2
+{
3
+    public interface ILuWebSocketNotificationsBusiness : ILuNotificationsBusiness
4
+    {
5
+
6
+    }
7
+}

+ 4
- 13
TestAuth/Tests.cs View File

@@ -10,19 +10,7 @@ namespace TestAuth
10 10
 {
11 11
     public class DummyNotificationsBusiness : ILuNotificationsBusiness
12 12
     {
13
-        public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
14
-        {
15
-        }
16
-
17
-        public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
18
-        {
19
-        }
20
-
21
-        public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
22
-        {
23
-        }
24
-
25
-        public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
13
+        public void Notify(string eventName, string entityType, object oldEntity, object newEntity)
26 14
         {
27 15
         }
28 16
     }
@@ -42,6 +30,9 @@ namespace TestAuth
42 30
             }, builder =>
43 31
             {
44 32
                 builder.UseNpgsql(RealDbConnectionString);
33
+            }, dbo =>
34
+            {
35
+                dbo.SecureCookies = false;
45 36
             });
46 37
             return serviceCollection.BuildServiceProvider();
47 38
         }

+ 1
- 1
WebApiAuth/Startup.cs View File

@@ -39,7 +39,7 @@ namespace WebApiAuth
39 39
             services.AddLuticateAuth(options => options.Version = "dev", options =>
40 40
             {
41 41
                 options.UseNpgsql(Configuration.GetConnectionString("default"));
42
-            });
42
+            }, options => options.SecureCookies = false);
43 43
 
44 44
             services.AddMvc()
45 45
                 .AddLuticateAuth();

+ 0
- 15
WebApiUtils/Business/PkGuidBusiness.cs View File

@@ -1,15 +0,0 @@
1
-using Luticate2.Utils.Business;
2
-using Luticate2.Utils.Interfaces;
3
-using TestUtils.DataAccess;
4
-using TestUtils.Dbo.PkGuid;
5
-
6
-namespace WebApiUtils.Business
7
-{
8
-    public class PkGuidBusiness : LuCrudBusiness<LuUtilsPkGuidDataAccess, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, string>
9
-    {
10
-        public PkGuidBusiness(LuUtilsPkGuidDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness) : base(dataAccess, notificationsBusiness)
11
-        {
12
-            EntityType = "pkguids";
13
-        }
14
-    }
15
-}

+ 16
- 0
WebApiUtils/Business/PkGuidsBusiness.cs View File

@@ -0,0 +1,16 @@
1
+using System;
2
+using Luticate2.Utils.Business;
3
+using Luticate2.Utils.Interfaces;
4
+using TestUtils.DataAccess;
5
+using TestUtils.Dbo.PkGuid;
6
+
7
+namespace WebApiUtils.Business
8
+{
9
+    public class PkGuidsBusiness : LuCrudBusiness<LuUtilsPkGuidDataAccess, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, string>
10
+    {
11
+        public PkGuidsBusiness(LuUtilsPkGuidDataAccess dataAccess, IServiceProvider serviceProvider) : base(dataAccess, serviceProvider)
12
+        {
13
+            AddListener<ILuCrudWebSocketNotificationsBusiness>();
14
+        }
15
+    }
16
+}

+ 3
- 3
WebApiUtils/Business/UploadBusiness.cs View File

@@ -1,13 +1,13 @@
1
-using Luticate2.Utils.Business;
1
+using System;
2
+using Luticate2.Utils.Business;
2 3
 using Luticate2.Utils.Dbo.FsFiles;
3
-using Luticate2.Utils.Interfaces;
4 4
 using WebApiUtils.DataAccess;
5 5
 
6 6
 namespace WebApiUtils.Business
7 7
 {
8 8
     public class UploadBusiness : LuCrudBusiness<UploadDataAccess, LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
9 9
     {
10
-        public UploadBusiness(UploadDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness) : base(dataAccess, notificationsBusiness)
10
+        public UploadBusiness(UploadDataAccess dataAccess, IServiceProvider serviceProvider) : base(dataAccess, serviceProvider)
11 11
         {
12 12
         }
13 13
     }

+ 2
- 2
WebApiUtils/Controllers/PkGuidController.cs View File

@@ -6,9 +6,9 @@ using WebApiUtils.Business;
6 6
 
7 7
 namespace WebApiUtils.Controllers
8 8
 {
9
-    public class PkGuidController : LuCrudController<PkGuidBusiness, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, string>
9
+    public class PkGuidController : LuCrudController<PkGuidsBusiness, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, string>
10 10
     {
11
-        public PkGuidController(PkGuidBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(busines, luUtilsOptionsDbo)
11
+        public PkGuidController(PkGuidsBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(busines, luUtilsOptionsDbo)
12 12
         {
13 13
         }
14 14
     }

+ 1
- 1
WebApiUtils/Startup.cs View File

@@ -44,7 +44,7 @@ namespace WebApiUtils
44 44
 
45 45
             services.AddLuticateUtils(options => options.Version = "dev");
46 46
 
47
-            services.AddScoped<PkGuidBusiness>();
47
+            services.AddScoped<PkGuidsBusiness>();
48 48
             services.AddScoped<LuUtilsPkGuidDataAccess>();
49 49
             services.AddScoped<UploadBusiness>();
50 50
             services.AddScoped<UploadDataAccess>();

Loading…
Cancel
Save