001package jmri;
002
003/**
004 * A manager for permissions.
005 *
006 * @author Daniel Bergqvist (C) 2024
007 */
008public interface PermissionManager {
009
010    interface LoginListener {
011        void loginLogout(boolean isLogin);
012    }
013
014    Role addRole(String name)
015            throws RoleAlreadyExistsException;
016
017    void removeRole(String name)
018            throws RoleDoesNotExistException;
019
020    User addUser(String username, String password)
021            throws UserAlreadyExistsException;
022
023    void removeUser(String username)
024            throws UserDoesNotExistException;
025
026    void changePassword(String newPassword, String oldPassword);
027
028    boolean login(String username, String password);
029
030    void logout();
031
032    boolean isLoggedIn();
033
034    boolean isCurrentUser(String username);
035
036    boolean isCurrentUser(User user);
037
038    String getCurrentUserName();
039
040    boolean isGuestUser(User user);
041
042    void addLoginListener(LoginListener listener);
043
044    boolean isEnabled();
045
046    void setEnabled(boolean enabled);
047
048    boolean isAllowEmptyPasswords();
049
050    void setAllowEmptyPasswords(boolean value);
051
052    boolean hasPermission(Permission permission);
053
054    /**
055     * Checks if the current user has the permission.
056     * If not, show a message dialog if not headless. Otherwise log a message.
057     * @param permission the permission to check
058     * @return true if the user has the permission, false otherwise
059     */
060    boolean checkPermission(Permission permission);
061
062    void registerOwner(PermissionOwner owner);
063
064    void registerPermission(Permission permission);
065
066    void storePermissionSettings();
067
068
069    public static class RoleAlreadyExistsException extends JmriException {
070        public RoleAlreadyExistsException() {
071            super(Bundle.getMessage("PermissionManager_RoleAlreadyExistsException"));
072        }
073    }
074
075    public static class RoleDoesNotExistException extends JmriException {
076        public RoleDoesNotExistException() {
077            super(Bundle.getMessage("PermissionManager_RoleDoesNotExistException"));
078        }
079    }
080
081    public static class UserAlreadyExistsException extends JmriException {
082        public UserAlreadyExistsException() {
083            super(Bundle.getMessage("PermissionManager_UserAlreadyExistsException"));
084        }
085    }
086
087    public static class UserDoesNotExistException extends JmriException {
088        public UserDoesNotExistException() {
089            super(Bundle.getMessage("PermissionManager_UserDoesNotExistException"));
090        }
091    }
092
093    public static class BadUserOrPasswordException extends JmriException {
094        public BadUserOrPasswordException() {
095            super(Bundle.getMessage("PermissionManager_BadUserOrPasswordException"));
096        }
097    }
098
099    public static class BadPasswordException extends JmriException {
100        public BadPasswordException() {
101            super(Bundle.getMessage("PermissionManager_BadPasswordException"));
102        }
103    }
104
105}