Browse Source

added luresult helper

tags/v0.6.0
Robin Thoni 7 years ago
parent
commit
d039c7437e

+ 1
- 0
Luticate2.Auth/Business/LuPermissionsBusiness.cs View File

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

+ 5
- 0
Luticate2.Auth/Business/LuUsersBusiness.cs View File

27
             return LuResult<UsersToken>.Ok(new UsersToken());//TODO
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
         public LuResult<UsersDbo> GetSingleById(string id)
35
         public LuResult<UsersDbo> GetSingleById(string id)
31
         {
36
         {
32
             return LuResult<UsersDbo>.Ok(new UsersDbo
37
             return LuResult<UsersDbo>.Ok(new UsersDbo

+ 0
- 1
Luticate2.Auth/Controllers/LuAuthExtensions.cs View File

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

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

11
 
11
 
12
         LuResult<UsersToken> UnRegisterToken(string token);
12
         LuResult<UsersToken> UnRegisterToken(string token);
13
 
13
 
14
+        LuResult<bool> IsTokenValid(UsersToken token);
15
+
14
         LuResult<UsersDbo> GetSingleById(string id);
16
         LuResult<UsersDbo> GetSingleById(string id);
15
     }
17
     }
16
 }
18
 }

+ 6
- 8
Luticate2.Auth/Middlewares/LuLoggedUserMiddleware.cs View File

29
                 var tokenRes = _luUsersBusiness.GetToken(token);
29
                 var tokenRes = _luUsersBusiness.GetToken(token);
30
                 if (tokenRes.Status == LuStatus.NotFound)
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
                 userId = tokenRes.Data.UserId;
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
             context.HttpContext.SetLuLoggedUser(userRes.Data);
45
             context.HttpContext.SetLuLoggedUser(userRes.Data);
48
         }
46
         }

+ 3
- 7
Luticate2.Auth/Middlewares/LuPermissionMiddleware.cs View File

35
                     var srcEntity = srcAccessor?.GetEntity(context, attribute.Id);
35
                     var srcEntity = srcAccessor?.GetEntity(context, attribute.Id);
36
                     var dstEntity = dstAccessor?.GetEntity(context, attribute.Id);
36
                     var dstEntity = dstAccessor?.GetEntity(context, attribute.Id);
37
                     var permissionValue = _luPermissionsBusiness.GetPermissionEffectiveValue(attribute.PermissionName,
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
                     if (!permissionValue.Data)
39
                     if (!permissionValue.Data)
44
                     {
40
                     {
45
                         var user = _serviceProvider.GetService<ILuLoggedUserAccessor>().GetLoggedUser();
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 View File

28
                     version = LuUtilsOptionsDbo.Version
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
         protected virtual LuApiWrapperDbo<LuBoxedValueDbo<T>> HandleBoxed<T>(LuResult<T> result)
35
         protected virtual LuApiWrapperDbo<LuBoxedValueDbo<T>> HandleBoxed<T>(LuResult<T> result)

+ 1
- 4
Luticate2.Utils/Controllers/LuFsFilesCrudController.cs View File

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

+ 2
- 11
Luticate2.Utils/Dbo/PaginatedRequest/LuPaginatedRequestBinder.cs View File

1
 using System.Threading.Tasks;
1
 using System.Threading.Tasks;
2
-using Luticate2.Utils.Controllers;
3
 using Luticate2.Utils.Dbo.Filter;
2
 using Luticate2.Utils.Dbo.Filter;
4
 using Luticate2.Utils.Dbo.OrderBy;
3
 using Luticate2.Utils.Dbo.OrderBy;
5
 using Microsoft.AspNetCore.Mvc.ModelBinding;
4
 using Microsoft.AspNetCore.Mvc.ModelBinding;
15
             var pageValue = bindingContext.ValueProvider.GetValue("page");
14
             var pageValue = bindingContext.ValueProvider.GetValue("page");
16
             var perPageValue = bindingContext.ValueProvider.GetValue("perPage");
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
             var dbo = new LuPaginatedRequestDbo
21
             var dbo = new LuPaginatedRequestDbo
31
             {
22
             {

+ 15
- 0
Luticate2.Utils/Dbo/Result/LuResult.cs View File

1
 using System;
1
 using System;
2
+using Luticate2.Utils.Controllers;
2
 
3
 
3
 namespace Luticate2.Utils.Dbo.Result
4
 namespace Luticate2.Utils.Dbo.Result
4
 {
5
 {
89
         {
90
         {
90
             return res.Success;
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 View File

40
                         errors += error;
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
 

Loading…
Cancel
Save