001package jmri.jmrit.permission;
002
003import java.util.*;
004
005import jmri.*;
006import jmri.PermissionValue;
007
008/**
009 * A role in the permission system.
010 *
011 * @author Daniel Bergqvist (C) 2024
012 */
013public class DefaultRole implements Role {
014
015    public static final DefaultRole ROLE_GUEST =
016            new DefaultRole(Bundle.getMessage("Role_Guest"),50,"GUEST");
017
018    public static final DefaultRole ROLE_REMOTE_GUEST =
019            new DefaultRole(Bundle.getMessage("Role_Remote_Guest"),40,"REMOTE_GUEST");
020
021    public static final DefaultRole ROLE_STANDARD_USER =
022            new DefaultRole(Bundle.getMessage("Role_StandardUser"),10,"STANDARD_USER");
023
024    public static final DefaultRole ROLE_ADMIN =
025            new DefaultRole(Bundle.getMessage("Role_Admin"),100,"ADMIN");
026
027    private final String _name;
028    private final boolean _systemRole;
029    private final int _priority;
030    private final String _systemName;
031
032    private final Map<Permission, PermissionValue> _permissions =
033            new TreeMap<>((a,b) -> {return a.getName().compareTo(b.getName());});
034
035
036    public DefaultRole(DefaultRole r) {
037        this._name = r._name;
038        this._systemRole = r._systemRole;
039        this._priority = r._priority;
040        this._systemName = r._systemName;
041        this._permissions.putAll(r._permissions);
042    }
043
044    public DefaultRole(String name) {
045        this._name = name;
046        this._priority = 0;
047        this._systemRole = false;
048        this._systemName = null;
049    }
050
051    public DefaultRole(String name, int priority, String systemName) {
052        this._name = name;
053        this._priority = priority;
054        this._systemRole = priority != 0;
055        this._systemName = systemName;
056    }
057
058    @Override
059    public String getName() {
060        return this._name;
061    }
062
063    @Override
064    public boolean isSystemRole() {
065        return this._systemRole;
066    }
067
068    @Override
069    public int getPriority() {
070        return this._priority;
071    }
072
073    @Override
074    public String getSystemName() {
075        return this._systemName;
076    }
077
078    @Override
079    public Map<Permission,PermissionValue> getPermissions() {
080        return Collections.unmodifiableMap(_permissions);
081    }
082
083    @Override
084    public PermissionValue getPermissionValue(Permission permission) {
085        return _permissions.getOrDefault(permission, permission.getDefaultPermission(this));
086    }
087
088    @Override
089    public void setPermission(Permission permission, PermissionValue value) {
090        if (! InstanceManager.getDefault(PermissionManager.class)
091                .ensureAtLeastPermission(PermissionsSystemAdmin.PERMISSION_EDIT_PREFERENCES,
092                        BooleanPermission.BooleanValue.TRUE)) {
093            return;
094        }
095        _permissions.put(permission, value);
096    }
097
098    void setPermissionWithoutCheck(Permission permission, PermissionValue value) {
099        _permissions.put(permission, value);
100    }
101
102    @Override
103    public boolean isGuestRole() {
104        return this == ROLE_GUEST;
105    }
106
107    @Override
108    public boolean isStandardUserRole() {
109        return this == ROLE_STANDARD_USER;
110    }
111
112    @Override
113    public boolean isAdminRole() {
114        return this == ROLE_ADMIN;
115    }
116
117}