wip mifosx-195 add/remove READ_{reportName} permissions when add/remove non-core reports

This commit is contained in:
John Woodlock
2013-04-17 12:23:55 +01:00
parent e5b0c31200
commit 3001cc3582
3 changed files with 68 additions and 39 deletions
@@ -243,4 +243,9 @@ public class Report extends AbstractPersistable<Long> {
"Validation errors exist.", dataValidationErrors); "Validation errors exist.", dataValidationErrors);
} }
} }
public String getReportName() {
return reportName;
}
} }
@@ -16,6 +16,9 @@ import org.mifosplatform.infrastructure.dataqueries.domain.ReportRepository;
import org.mifosplatform.infrastructure.dataqueries.exception.ReportNotFoundException; import org.mifosplatform.infrastructure.dataqueries.exception.ReportNotFoundException;
import org.mifosplatform.infrastructure.dataqueries.serialization.ReportCommandFromApiJsonDeserializer; import org.mifosplatform.infrastructure.dataqueries.serialization.ReportCommandFromApiJsonDeserializer;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.useradministration.domain.Permission;
import org.mifosplatform.useradministration.domain.PermissionRepository;
import org.mifosplatform.useradministration.exception.PermissionNotFoundException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -33,15 +36,18 @@ public class ReportWritePlatformServiceImpl implements
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer; private final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer;
private final ReportRepository reportRepository; private final ReportRepository reportRepository;
private final PermissionRepository permissionRepository;
@Autowired @Autowired
public ReportWritePlatformServiceImpl( public ReportWritePlatformServiceImpl(
final PlatformSecurityContext context, final PlatformSecurityContext context,
final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer, final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer,
final ReportRepository reportRepository) { final ReportRepository reportRepository,
final PermissionRepository permissionRepository) {
this.context = context; this.context = context;
this.fromApiJsonDeserializer = fromApiJsonDeserializer; this.fromApiJsonDeserializer = fromApiJsonDeserializer;
this.reportRepository = reportRepository; this.reportRepository = reportRepository;
this.permissionRepository = permissionRepository;
} }
@Transactional @Transactional
@@ -54,8 +60,10 @@ public class ReportWritePlatformServiceImpl implements
this.fromApiJsonDeserializer.validate(command.json()); this.fromApiJsonDeserializer.validate(command.json());
final Report report = Report.fromJson(command); final Report report = Report.fromJson(command);
final Permission permission = new Permission("report",
report.getReportName(), "READ");
this.reportRepository.save(report); this.reportRepository.save(report);
this.permissionRepository.save(permission);
return new CommandProcessingResultBuilder() return new CommandProcessingResultBuilder()
.withCommandId(command.commandId()) .withCommandId(command.commandId())
@@ -110,7 +118,14 @@ public class ReportWritePlatformServiceImpl implements
"Core Reports Can't be Deleted", ""); "Core Reports Can't be Deleted", "");
} }
final Permission permission = this.permissionRepository.findOneByCode("READ" + "_" + report.getReportName());
if (permission == null) {
throw new PermissionNotFoundException("READ" + "_" + report.getReportName());
}
this.reportRepository.delete(report); this.reportRepository.delete(report);
this.permissionRepository.delete(permission);
return new CommandProcessingResultBuilder().withEntityId(reportId) return new CommandProcessingResultBuilder().withEntityId(reportId)
.build(); .build();
@@ -15,51 +15,60 @@ import org.springframework.data.jpa.domain.AbstractPersistable;
@Table(name = "m_permission") @Table(name = "m_permission")
public class Permission extends AbstractPersistable<Long> { public class Permission extends AbstractPersistable<Long> {
@Column(name = "grouping", nullable = false, length = 45) @Column(name = "grouping", nullable = false, length = 45)
private final String grouping; private final String grouping;
@Column(name = "code", nullable = false, length = 100) @Column(name = "code", nullable = false, length = 100)
private final String code; private final String code;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Column(name = "entity_name", nullable = true, length = 100) @Column(name = "entity_name", nullable = true, length = 100)
private final String entityName; private final String entityName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Column(name = "action_name", nullable = true, length = 100) @Column(name = "action_name", nullable = true, length = 100)
private final String actionName; private final String actionName;
@Column(name = "can_maker_checker", nullable = false) @Column(name = "can_maker_checker", nullable = false)
private boolean canMakerChecker; private boolean canMakerChecker;
protected Permission() { public Permission(final String grouping, final String entityName,
this.grouping = null; final String actionName) {
this.code = null; this.grouping = grouping;
this.entityName = null; this.entityName = entityName;
this.actionName = null; this.actionName = actionName;
this.canMakerChecker = false; this.code = actionName + "_" + entityName;
} this.canMakerChecker = false;
}
public boolean hasCode(final String checkCode) { protected Permission() {
return this.code.equalsIgnoreCase(checkCode); this.grouping = null;
} this.entityName = null;
this.actionName = null;
this.code = null;
this.canMakerChecker = false;
}
public String getCode() { public boolean hasCode(final String checkCode) {
return this.code; return this.code.equalsIgnoreCase(checkCode);
} }
public boolean hasMakerCheckerEnabled() { public String getCode() {
return this.canMakerChecker; return this.code;
} }
public String getGrouping() { public boolean hasMakerCheckerEnabled() {
return this.grouping; return this.canMakerChecker;
} }
public boolean enableMakerChecker(final boolean canMakerChecker) { public String getGrouping() {
final boolean isUpdatedValueSame = (this.canMakerChecker == canMakerChecker); return this.grouping;
this.canMakerChecker = canMakerChecker; }
return !isUpdatedValueSame; public boolean enableMakerChecker(final boolean canMakerChecker) {
} final boolean isUpdatedValueSame = (this.canMakerChecker == canMakerChecker);
this.canMakerChecker = canMakerChecker;
return !isUpdatedValueSame;
}
} }