1
|
|
|
package com.github.netkorp.telegram.framework.managers; |
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 { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The list of authorized chat identification. |
20
|
|
|
*/ |
21
|
|
|
private final List<Long> authorizedChats; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The component for managing all of the available commands in the bot. |
25
|
|
|
*/ |
26
|
|
|
private final CommandManager commandManager; |
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) { |
35
|
|
|
this.authorizedChats = new LinkedList<>(); |
36
|
|
|
this.commandManager = commandManager; |
37
|
|
|
|
38
|
|
|
for (String chatID : authorizedChats.split(",")) { |
39
|
|
|
try { |
40
|
|
|
this.authorizedChats.add(Long.parseLong(chatID)); |
41
|
|
|
} catch (Exception ex) { |
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) { |
54
|
|
|
return this.authorizedChats.contains(chatId); |
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. |
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) { |
65
|
|
|
if (commandManager.isNonSecureCommand(command)) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return this.authorizedChats.contains(chatId); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|