Ver código fonte

added luresult helper

tags/v0.6.0
Robin Thoni 7 anos atrás
pai
commit
d039c7437e

+ 1
- 0
Luticate2.Auth/Business/LuPermissionsBusiness.cs Ver arquivo

@@ -15,6 +15,7 @@ namespace Luticate2.Auth.Business
15 15
         public LuResult<bool> GetPermissionEffectiveValue(string permissionName, string srcEntityType, object srcEntity,
16 16
             string dstEntityType, object dstEntity)
17 17
         {
18
+//            throw new Exception($"{permissionName} {srcEntityType} {srcEntity} {dstEntityType} {dstEntity}");
18 19
             return LuResult<bool>.Ok(true);
19 20
         }
20 21
     }

+ 5
- 0
Luticate2.Auth/Business/LuUsersBusiness.cs Ver arquivo

@@ -27,6 +27,11 @@ namespace Luticate2.Auth.Business
27 27
             return LuResult<UsersToken>.Ok(new UsersToken());//TODO
28 28
         }
29 29
 
30
+        public LuResult<bool> IsTokenValid(UsersToken token)
31
+        {
32
+            return LuResult<bool>.Ok(true);
33
+        }
34
+
30 35
         public LuResult<UsersDbo> GetSingleById(string id)
31 36
         {
32 37
             return LuResult<UsersDbo>.Ok(new UsersDbo

+ 0
- 1
Luticate2.Auth/Controllers/LuAuthExtensions.cs Ver arquivo

@@ -80,7 +80,6 @@ namespace Luticate2.Auth.Controllers
80 80
 
81 81
         public static void SetLuLoggedUser(this HttpContext context, UsersDbo user)
82 82
         {
83
-            throw new Exception($"{user.Id} {user.Username}");
84 83
             context.GetLuItems()[LuticateItemsLoggedUser] = user;
85 84
         }
86 85
     }

+ 2
- 0
Luticate2.Auth/Interfaces/Users/ILuUsersBusiness.cs Ver arquivo

@@ -11,6 +11,8 @@ namespace Luticate2.Auth.Interfaces.Users
11 11
 
12 12
         LuResult<UsersToken> UnRegisterToken(string token);
13 13
 
14
+        LuResult<bool> IsTokenValid(UsersToken token);
15
+
14 16
         LuResult<UsersDbo> GetSingleById(string id);
15 17
     }
16 18
 }

+ 6
- 8
Luticate2.Auth/Middlewares/LuLoggedUserMiddleware.cs Ver arquivo

@@ -29,20 +29,18 @@ namespace Luticate2.Auth.Middlewares
29 29
                 var tokenRes = _luUsersBusiness.GetToken(token);
30 30
                 if (tokenRes.Status == LuStatus.NotFound)
31 31
                 {
32
-                    throw new LuResultException(LuResult<object>.Error(LuStatus.LoginError, $"{token}", "Invalid session"));
32
+                    LuResult<object>.Error(LuStatus.LoginError, $"token: {token}", "Invalid session").Throw();
33 33
                 }
34
-                if (!tokenRes)
34
+                tokenRes.ThrowIfNotSuccess();
35
+                var tokenValidityRes = _luUsersBusiness.IsTokenValid(tokenRes.Data).ThrowIfNotSuccess();
36
+                if (!tokenValidityRes.Data)
35 37
                 {
36
-                    throw new LuResultException(tokenRes.To<object>());
38
+                    LuResult<object>.Error(LuStatus.LoginError, $"token: {token}", "Invalid session").Throw();
37 39
                 }
38 40
                 userId = tokenRes.Data.UserId;
39 41
             }
40 42
 
41
-            var userRes = _luUsersBusiness.GetSingleById(userId);
42
-            if (!userRes)
43
-            {
44
-                throw new LuResultException(userRes.To<object>());
45
-            }
43
+            var userRes = _luUsersBusiness.GetSingleById(userId).ThrowIfNotSuccess();
46 44
 
47 45
             context.HttpContext.SetLuLoggedUser(userRes.Data);
48 46
         }

+ 3
- 7
Luticate2.Auth/Middlewares/LuPermissionMiddleware.cs Ver arquivo

@@ -35,16 +35,12 @@ namespace Luticate2.Auth.Middlewares
35 35
                     var srcEntity = srcAccessor?.GetEntity(context, attribute.Id);
36 36
                     var dstEntity = dstAccessor?.GetEntity(context, attribute.Id);
37 37
                     var permissionValue = _luPermissionsBusiness.GetPermissionEffectiveValue(attribute.PermissionName,
38
-                        attribute.SrcEntityType, srcEntity, attribute.DstEntityType, dstEntity);
39
-                    if (!permissionValue)
40
-                    {
41
-                        throw new LuResultException(permissionValue.To<object>());
42
-                    }
38
+                        attribute.SrcEntityType, srcEntity, attribute.DstEntityType, dstEntity).ThrowIfNotSuccess();
43 39
                     if (!permissionValue.Data)
44 40
                     {
45 41
                         var user = _serviceProvider.GetService<ILuLoggedUserAccessor>().GetLoggedUser();
46
-                        throw new LuResultException(LuResult<object>.Error(LuStatus.PermissionError,
47
-                            $"user: {user?.Id} {user?.Username}", "Permission denied"));
42
+                        LuResult<object>.Error(LuStatus.PermissionError,$"user: {user?.Id} {user?.Username}",
43
+                            "Permission denied").Throw();
48 44
                     }
49 45
                 }
50 46
             }

+ 2
- 1
Luticate2.Utils/Controllers/LuController.cs Ver arquivo

@@ -28,7 +28,8 @@ namespace Luticate2.Utils.Controllers
28 28
                     version = LuUtilsOptionsDbo.Version
29 29
                 };
30 30
             }
31
-            throw new LuResultException(result.To<object>());
31
+            result.Throw();
32
+            return null;
32 33
         }
33 34
 
34 35
         protected virtual LuApiWrapperDbo<LuBoxedValueDbo<T>> HandleBoxed<T>(LuResult<T> result)

+ 1
- 4
Luticate2.Utils/Controllers/LuFsFilesCrudController.cs Ver arquivo

@@ -34,10 +34,7 @@ namespace Luticate2.Utils.Controllers
34 34
             {
35 35
                 return StatusCode(404);
36 36
             }
37
-            if (!get)
38
-            {
39
-                throw new LuResultException(get.To<object>());
40
-            }
37
+            get.ThrowIfNotSuccess();
41 38
             string type;
42 39
             var contentTypeProvider = new FileExtensionContentTypeProvider();
43 40
             if (!contentTypeProvider.TryGetContentType(id, out type))

+ 2
- 11
Luticate2.Utils/Dbo/PaginatedRequest/LuPaginatedRequestBinder.cs Ver arquivo

@@ -1,5 +1,4 @@
1 1
 using System.Threading.Tasks;
2
-using Luticate2.Utils.Controllers;
3 2
 using Luticate2.Utils.Dbo.Filter;
4 3
 using Luticate2.Utils.Dbo.OrderBy;
5 4
 using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -15,17 +14,9 @@ namespace Luticate2.Utils.Dbo.PaginatedRequest
15 14
             var pageValue = bindingContext.ValueProvider.GetValue("page");
16 15
             var perPageValue = bindingContext.ValueProvider.GetValue("perPage");
17 16
 
18
-            var filter = LuFilterBinder.FromString(filterValue.FirstValue);
19
-            if (!filter)
20
-            {
21
-                throw new LuResultException(filter.To<object>());
22
-            }
17
+            var filter = LuFilterBinder.FromString(filterValue.FirstValue).ThrowIfNotSuccess();
23 18
 
24
-            var orderBy = LuOrderByBinder.FromString(orderByValue.FirstValue);
25
-            if (!orderBy)
26
-            {
27
-                throw new LuResultException(orderBy.To<object>());
28
-            }
19
+            var orderBy = LuOrderByBinder.FromString(orderByValue.FirstValue).ThrowIfNotSuccess();
29 20
 
30 21
             var dbo = new LuPaginatedRequestDbo
31 22
             {

+ 15
- 0
Luticate2.Utils/Dbo/Result/LuResult.cs Ver arquivo

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using Luticate2.Utils.Controllers;
2 3
 
3 4
 namespace Luticate2.Utils.Dbo.Result
4 5
 {
@@ -89,5 +90,19 @@ namespace Luticate2.Utils.Dbo.Result
89 90
         {
90 91
             return res.Success;
91 92
         }
93
+
94
+        public void Throw()
95
+        {
96
+            throw new LuResultException(To<object>());
97
+        }
98
+
99
+        public LuResult<T> ThrowIfNotSuccess()
100
+        {
101
+            if (!this)
102
+            {
103
+                Throw();
104
+            }
105
+            return this;
106
+        }
92 107
     }
93 108
 }

+ 1
- 1
Luticate2.Utils/Middlewares/LuModelStateFilter.cs Ver arquivo

@@ -40,7 +40,7 @@ namespace Luticate2.Utils.Middlewares
40 40
                         errors += error;
41 41
                     }
42 42
                 }
43
-                throw new LuResultException(LuResult<object>.Error(LuStatus.InputError, "Validation error:" + errors, ""));
43
+                LuResult<object>.Error(LuStatus.InputError, "Validation error:" + errors, "").Throw();
44 44
             }
45 45
         }
46 46
 

Carregando…
Cancelar
Salvar