|
14 | 14 | using Microsoft.Extensions.DependencyInjection; |
15 | 15 | using Microsoft.Extensions.Hosting; |
16 | 16 | using Newtonsoft.Json.Linq; |
| 17 | +using TelegramBot.Attributes; |
17 | 18 |
|
18 | 19 | namespace TelegramBot |
19 | 20 | { |
@@ -207,15 +208,29 @@ private async Task HandleRequestAsync(ITelegramUpdateHandler handler, Update upd |
207 | 208 | _logger.LogWarning("Method not found for message: {Text}.", update.Message?.Text); |
208 | 209 | return; |
209 | 210 | } |
| 211 | + if (method.GetCustomAttribute<AuthorizeAttribute>() != null |
| 212 | + || method.DeclaringType?.GetCustomAttribute<AuthorizeAttribute>() != null) |
| 213 | + { |
| 214 | + if (_serviceProvider.GetService<IBotAuthorizationHandler>() is IBotAuthorizationHandler authorizationHandler) |
| 215 | + { |
| 216 | + if (!authorizationHandler.Authorize(user)) |
| 217 | + { |
| 218 | + await authorizationHandler |
| 219 | + .HandleUnauthorized(user) |
| 220 | + .ExecuteResultAsync(new ActionContext(_client, user.Id)); |
| 221 | + return; |
| 222 | + } |
| 223 | + } |
| 224 | + } |
210 | 225 | if (method.ReturnType != typeof(Task<IActionResult>) && method.ReturnType != typeof(IActionResult)) |
211 | 226 | { |
212 | 227 | throw new InvalidOperationException("Invalid return type: " + method.ReturnType.Name); |
213 | 228 | } |
214 | | - if (method.GetParameters().Length != args?.Length) |
| 229 | + if (args != null && method.GetParameters().Length != args?.Length) |
215 | 230 | { |
216 | 231 | throw new InvalidOperationException("Invalid arguments count: " + args?.Length); |
217 | 232 | } |
218 | | - BotControllerBase controller = (BotControllerBase)ActivatorUtilities.CreateInstance(_serviceProvider, method.DeclaringType); |
| 233 | + BotControllerBase controller = (BotControllerBase)ActivatorUtilities.CreateInstance(_serviceProvider, method.DeclaringType!); |
219 | 234 | controller.Update = update; |
220 | 235 | controller.User = user; |
221 | 236 | if (_serviceProvider.GetService<IKeyValueProvider>() is IKeyValueProvider keyValueProvider) |
|
0 commit comments