com.github.netkorp.telegram.framework.managers.SecurityManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
eloc 17
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A SecurityManager(String,CommandManager) 0 9 3
A isAuthorized(Long) 0 2 1
A isAuthorized(Long,Command) 0 6 2
1
package com.github.netkorp.telegram.framework.managers;
0 ignored issues
show
Code Smell introduced by
It is a best practice to supply a copyright/licence header in your code. Your organisation probably has a template for that.
Loading history...
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
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
17
18
    /**
19
     * The list of authorized chat identification.
20
     */
21
    private final List<Long> authorizedChats;
0 ignored issues
show
introduced by
Annotate this member with "@Autowired", "@Resource", "@Inject", or "@Value", or remove it.
Loading history...
Coding Style introduced by
Make this line start at column 3.
Loading history...
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
introduced by
Annotate this member with "@Autowired", "@Resource", "@Inject", or "@Value", or remove it.
Loading history...
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
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
Coding Style introduced by
This line is 129 characters long, which is over the set limit of 120 characters.
Loading history...
35
        this.authorizedChats = new LinkedList<>();
0 ignored issues
show
Coding Style introduced by
Make this line start at column 5.
Loading history...
36
        this.commandManager = commandManager;
37
38
        for (String chatID : authorizedChats.split(",")) {
0 ignored issues
show
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
39
            try {
0 ignored issues
show
Coding Style introduced by
Make this line start at column 7.
Loading history...
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
40
                this.authorizedChats.add(Long.parseLong(chatID));
0 ignored issues
show
Coding Style introduced by
Make this line start at column 9.
Loading history...
41
            } catch (Exception ex) {
0 ignored issues
show
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
Coding Style introduced by
Move this "catch" keyword to a new dedicated line.
Loading history...
introduced by
Catch a list of specific exception subtypes instead.
Loading history...
Best Practice introduced by
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();
        }
    }
}
Loading history...
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
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
54
        return this.authorizedChats.contains(chatId);
0 ignored issues
show
Coding Style introduced by
Make this line start at column 5.
Loading history...
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
Coding Style introduced by
This line is 129 characters long, which is over the set limit of 120 characters.
Loading history...
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
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
65
        if (commandManager.isNonSecureCommand(command)) {
0 ignored issues
show
Coding Style introduced by
Make this line start at column 5.
Loading history...
Coding Style introduced by
Move this left curly brace to the beginning of next line of code.
Loading history...
66
            return true;
0 ignored issues
show
Coding Style introduced by
Make this line start at column 7.
Loading history...
67
        }
68
69
        return this.authorizedChats.contains(chatId);
70
    }
71
}
72