Search in sources :

Example 1 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class Upgrade3 method upgrade.

@Override
protected void upgrade() throws Exception {
    OutputStream out = createUpdateLogOutputStream();
    // Create permission columns
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), addPermissionsSQL);
    scripts.put(DatabaseType.H2.name(), addPermissionsSQL);
    scripts.put(DatabaseType.MSSQL.name(), addPermissionsSQL);
    scripts.put(DatabaseType.POSTGRES.name(), addPermissionsSQL);
    runScript(scripts, out);
    // Convert permissions into roles
    // Move current permissions to roles
    ejt.query("SELECT id, togglePermission FROM maintenanceEvents", rs -> {
        int voId = rs.getInt(1);
        // Add role/mapping
        MangoPermission togglePermissions = PermissionMigration.parseLegacyPermission(rs.getString(2));
        Integer toggle = getOrCreatePermission(togglePermissions).getId();
        ejt.update("UPDATE maintenanceEvents SET togglePermissionId=? WHERE id=?", toggle, voId);
    });
    // Modify permission columns
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), permissionsNotNullMySQL);
    scripts.put(DatabaseType.H2.name(), permissionsNotNullSQL);
    scripts.put(DatabaseType.MSSQL.name(), permissionsNotNullSQL);
    scripts.put(DatabaseType.POSTGRES.name(), permissionsNotNullSQL);
    runScript(scripts, out);
    scripts = new HashMap<>();
    // Drop the togglePermissions
    scripts.put(DatabaseType.MYSQL.name(), dropTogglePermission);
    scripts.put(DatabaseType.H2.name(), dropTogglePermission);
    scripts.put(DatabaseType.MSSQL.name(), dropTogglePermission);
    scripts.put(DatabaseType.POSTGRES.name(), dropTogglePermission);
    runScript(scripts, out);
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 2 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class MaintenanceEventDao method savePreRelationalData.

@Override
public void savePreRelationalData(MaintenanceEventVO existing, MaintenanceEventVO vo) {
    MangoPermission togglePermission = permissionService.findOrCreate(vo.getTogglePermission());
    vo.setTogglePermission(togglePermission);
}
Also used : MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 3 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class MaintenanceEventDao method mapRecord.

@Override
public MaintenanceEventVO mapRecord(Record record) {
    MaintenanceEventVO me = new MaintenanceEventVO();
    me.setId(record.get(table.id));
    me.setXid(record.get(table.xid));
    me.setName(record.get(table.alias));
    me.setAlarmLevel(AlarmLevels.fromValue(record.get(table.alarmLevel)));
    me.setScheduleType(record.get(table.scheduleType));
    me.setDisabled(charToBool(record.get(table.disabled)));
    me.setActiveYear(record.get(table.activeYear));
    me.setActiveMonth(record.get(table.activeMonth));
    me.setActiveDay(record.get(table.activeDay));
    me.setActiveHour(record.get(table.activeHour));
    me.setActiveMinute(record.get(table.activeMinute));
    me.setActiveSecond(record.get(table.activeSecond));
    me.setActiveCron(record.get(table.activeCron));
    me.setInactiveYear(record.get(table.inactiveYear));
    me.setInactiveMonth(record.get(table.inactiveMonth));
    me.setInactiveDay(record.get(table.inactiveDay));
    me.setInactiveHour(record.get(table.inactiveHour));
    me.setInactiveMinute(record.get(table.inactiveMinute));
    me.setInactiveSecond(record.get(table.inactiveSecond));
    me.setInactiveCron(record.get(table.inactiveCron));
    me.setTimeoutPeriods(record.get(table.timeoutPeriods));
    me.setTimeoutPeriodType(record.get(table.timeoutPeriodType));
    me.setTogglePermission(new MangoPermission(record.get(table.togglePermissionId)));
    return me;
}
Also used : MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 4 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class Upgrade4 method upgrade.

@Override
protected void upgrade() throws Exception {
    try (OutputStream out = createUpdateLogOutputStream()) {
        // Get a reference to the superadmin permission in DB
        MangoPermission superadmin = getOrCreatePermissionNoCache(MangoPermission.superadminOnly());
        ejt.query("SELECT id, typeName, typeRef1 FROM events WHERE typeName=?", new Object[] { MaintenanceEventType.TYPE_NAME }, rs -> {
            int eventId = rs.getInt(1);
            Integer voId = rs.getInt(3);
            // Find the permission id for this me
            Integer permissionId = maintenanceEventPermissionMap.computeIfAbsent(voId, (k) -> {
                // Build the permission for this event
                Set<Role> allRequired = new HashSet<>();
                List<Integer> dataPointIds = ejt.queryForList(SELECT_POINT_IDS, new Object[] { k }, Integer.class);
                for (Integer dpId : dataPointIds) {
                    MangoPermission dataPointPermission = dataPointPermissionMap.computeIfAbsent(dpId, (pointId) -> {
                        Integer id = ejt.queryForInt("SELECT readPermissionId from dataPoints where id=?", new Object[] { pointId }, Common.NEW_ID);
                        if (id == Common.NEW_ID) {
                            return superadmin;
                        } else {
                            return permissionMap.computeIfAbsent(id, (pId) -> {
                                MangoPermission p = get(pId);
                                if (p == null) {
                                    return superadmin;
                                } else {
                                    return p;
                                }
                            });
                        }
                    });
                    dataPointPermission.getRoles().stream().forEach(minterm -> allRequired.addAll(minterm));
                }
                List<Integer> dataSourceIds = ejt.queryForList(SELECT_DATA_SOURCE_IDS, new Object[] { k }, Integer.class);
                for (Integer dsId : dataSourceIds) {
                    MangoPermission dataSourcePermission = dataSourcePermissionMap.computeIfAbsent(dsId, (sourceId) -> {
                        Integer id = ejt.queryForInt("SELECT readPermissionId from dataSources where id=?", new Object[] { sourceId }, Common.NEW_ID);
                        if (id == Common.NEW_ID) {
                            return superadmin;
                        } else {
                            return permissionMap.computeIfAbsent(id, (pId) -> {
                                MangoPermission p = get(pId);
                                if (p == null) {
                                    return superadmin;
                                } else {
                                    return p;
                                }
                            });
                        }
                    });
                    dataSourcePermission.getRoles().stream().forEach(minterm -> allRequired.addAll(minterm));
                }
                MangoPermission permission;
                if (allRequired.size() == 0) {
                    permission = MangoPermission.superadminOnly();
                } else {
                    permission = MangoPermission.requireAllRoles(allRequired);
                }
                return getOrCreatePermission(permission).getId();
            });
            ejt.update("UPDATE events SET readPermissionId=? WHERE id=?", permissionId, eventId);
        });
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) OutputStream(java.io.OutputStream) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 5 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class SystemMetricsRestController method query.

@ApiOperation(value = "Get the current value for all System Metrics", notes = "TBD Add RQL Support to this endpoint")
@RequestMapping(method = RequestMethod.GET)
public List<ValueMonitorModel> query(@AuthenticationPrincipal User user) {
    MangoPermission permission = definition.getPermission();
    service.ensurePermission(user, permission);
    return Common.MONITORED_VALUES.getMonitors().stream().sorted(Comparator.comparing(a -> a.getName().translate(user.getTranslations()))).map(ValueMonitorModel::new).collect(Collectors.toList());
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) ValueMonitorModel(com.infiniteautomation.mango.rest.latest.model.ValueMonitorModel) Common(com.serotonin.m2m2.Common) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) SystemMetricsReadPermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.SystemMetricsReadPermissionDefinition) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) ApiParam(io.swagger.annotations.ApiParam) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) ValueMonitor(com.infiniteautomation.mango.monitor.ValueMonitor) ApiOperation(io.swagger.annotations.ApiOperation) List(java.util.List) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) Api(io.swagger.annotations.Api) Comparator(java.util.Comparator) User(com.serotonin.m2m2.vo.User) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)93 Role (com.serotonin.m2m2.vo.role.Role)22 HashSet (java.util.HashSet)21 Set (java.util.Set)18 HashMap (java.util.HashMap)13 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)12 PermissionDefinition (com.serotonin.m2m2.module.PermissionDefinition)11 JsonException (com.serotonin.json.JsonException)10 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)9 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)8 JsonValue (com.serotonin.json.type.JsonValue)7 ArrayList (java.util.ArrayList)7 JsonArray (com.serotonin.json.type.JsonArray)6 JsonObject (com.serotonin.json.type.JsonObject)6 User (com.serotonin.m2m2.vo.User)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4