1 | package com.github.netkorp.telegram.framework.managers; |
||
0 ignored issues
–
show
Code Smell
introduced
by
![]() |
|||
2 | |||
3 | import com.github.netkorp.telegram.framework.commands.interfaces.Command; |
||
4 | import org.springframework.beans.factory.annotation.Autowired; |
||
5 | import org.springframework.beans.factory.annotation.Value; |
||
6 | import org.springframework.context.annotation.Lazy; |
||
7 | import org.springframework.stereotype.Service; |
||
8 | |||
9 | import java.util.LinkedList; |
||
10 | import java.util.List; |
||
11 | |||
12 | /** |
||
13 | * Provides the component for securing the bot by containing the list of authorized chat identifications. |
||
14 | */ |
||
15 | @Service |
||
16 | public class SecurityManager { |
||
0 ignored issues
–
show
|
|||
17 | |||
18 | /** |
||
19 | * The list of authorized chat identification. |
||
20 | */ |
||
21 | private final List<Long> authorizedChats; |
||
0 ignored issues
–
show
|
|||
22 | |||
23 | /** |
||
24 | * The component for managing all of the available commands in the bot. |
||
25 | */ |
||
26 | private final CommandManager commandManager; |
||
0 ignored issues
–
show
|
|||
27 | |||
28 | /** |
||
29 | * Constructs a new {@link SecurityManager} instance with the list of the authorized chat identifications. |
||
30 | * |
||
31 | * @param authorizedChats the list of the authorized chat identifications. |
||
32 | */ |
||
33 | @Autowired |
||
34 | public SecurityManager(@Value("${telegram.authorized.idChat}") String authorizedChats, @Lazy CommandManager commandManager) { |
||
0 ignored issues
–
show
|
|||
35 | this.authorizedChats = new LinkedList<>(); |
||
0 ignored issues
–
show
|
|||
36 | this.commandManager = commandManager; |
||
37 | |||
38 | for (String chatID : authorizedChats.split(",")) { |
||
0 ignored issues
–
show
|
|||
39 | try { |
||
0 ignored issues
–
show
|
|||
40 | this.authorizedChats.add(Long.parseLong(chatID)); |
||
0 ignored issues
–
show
|
|||
41 | } catch (Exception ex) { |
||
0 ignored issues
–
show
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.
When instantiating a new Exception, you can set another Exception as its cause. See the Oracle documentation on Throwables. Usage example throw new Exception("Exception Message", originalException);
Complete Example: class ReThrowException {
public static void throwsException() {
try {
throw new Exception("I am the original exception");
} catch (final Exception e) {
throw new RuntimeException("I am the new exception", e);
}
}
public static void main(String[] args) {
try {
throwsException();
}
catch (final RuntimeException e) {
System.out.println(e.getMessage());
System.out.println("and my cause is: " + e.getCause().getMessage());
e.printStackTrace();
}
}
}
![]() |
|||
42 | // Do nothing |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Returns {@code true} if the chat identification is authorized. |
||
49 | * |
||
50 | * @param chatId the chat identification of the person from whom you want to know if it is authorized. |
||
51 | * @return {@code true} if the chat identification is authorized; {@code false} otherwise. |
||
52 | */ |
||
53 | public boolean isAuthorized(Long chatId) { |
||
0 ignored issues
–
show
|
|||
54 | return this.authorizedChats.contains(chatId); |
||
0 ignored issues
–
show
|
|||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Returns {@code true} if the chat identification is authorized to invoke the command. |
||
59 | * |
||
60 | * @param chatId the chat identification of the person from whom you want to know if it is authorized to invoke the command. |
||
0 ignored issues
–
show
|
|||
61 | * @param command the command to be invoked. |
||
62 | * @return {@code true} if the chat identification is authorized to invoke the command; {@code false} otherwise. |
||
63 | */ |
||
64 | public boolean isAuthorized(Long chatId, Command command) { |
||
0 ignored issues
–
show
|
|||
65 | if (commandManager.isNonSecureCommand(command)) { |
||
0 ignored issues
–
show
|
|||
66 | return true; |
||
0 ignored issues
–
show
|
|||
67 | } |
||
68 | |||
69 | return this.authorizedChats.contains(chatId); |
||
70 | } |
||
71 | } |
||
72 |