add service to update can_maker_checker field on permissions

This commit is contained in:
John Woodlock
2012-11-26 21:19:30 +00:00
parent 19fea3ed00
commit 02617ce7f6
23 changed files with 296 additions and 70 deletions
@@ -5,17 +5,23 @@ import java.util.Set;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.mifosng.platform.api.commands.PermissionsCommand;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.PermissionUsageData; import org.mifosng.platform.api.data.PermissionUsageData;
import org.mifosng.platform.api.infrastructure.PortfolioApiDataConversionService;
import org.mifosng.platform.api.infrastructure.PortfolioApiJsonSerializerService; import org.mifosng.platform.api.infrastructure.PortfolioApiJsonSerializerService;
import org.mifosng.platform.infrastructure.api.ApiParameterHelper; import org.mifosng.platform.infrastructure.api.ApiParameterHelper;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.mifosng.platform.user.service.PermissionReadPlatformService; import org.mifosng.platform.user.service.PermissionReadPlatformService;
import org.mifosng.platform.user.service.PermissionWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -27,7 +33,13 @@ public class PermissionsApiResource {
@Autowired @Autowired
private PermissionReadPlatformService permissionReadPlatformService; private PermissionReadPlatformService permissionReadPlatformService;
@Autowired
private PermissionWritePlatformService permissionWritePlatformService;
@Autowired
private PortfolioApiDataConversionService apiDataConversionService;
@Autowired @Autowired
private PortfolioApiJsonSerializerService apiJsonSerializerService; private PortfolioApiJsonSerializerService apiJsonSerializerService;
@@ -43,11 +55,29 @@ public class PermissionsApiResource {
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions); context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
final boolean makerCheckerable = ApiParameterHelper.makerCheckerable(uriInfo.getQueryParameters());
final Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); final Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
final Collection<PermissionUsageData> permissions = this.permissionReadPlatformService.retrieveAllPermissions(); Collection<PermissionUsageData> permissions = null;
if (makerCheckerable) permissions = this.permissionReadPlatformService.retrieveAllMakerCheckerablePermissions();
else permissions = this.permissionReadPlatformService.retrieveAllPermissions();
return this.apiJsonSerializerService.serializePermissionDataToJson(prettyPrint, responseParameters, permissions); return this.apiJsonSerializerService.serializePermissionDataToJson(prettyPrint, responseParameters, permissions);
} }
@PUT
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response updatePermissionsDetails(final String jsonRequestBody) {
final PermissionsCommand command = this.apiDataConversionService.convertApiRequestJsonToPermissionsCommand(jsonRequestBody);
final Long entityId = this.permissionWritePlatformService.updateMakerCheckerPermissions(command);
return Response.ok().entity(new EntityIdentifier(entityId)).build();
}
} }
@@ -15,8 +15,8 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.data.CommandSourceData; import org.mifosng.platform.api.data.CommandSourceData;
import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.PermissionUsageData; import org.mifosng.platform.api.data.PermissionUsageData;
@@ -0,0 +1,26 @@
package org.mifosng.platform.api.commands;
import java.util.Map;
/**
* Immutable command for updating permissions (initially maker-checker).
*/
public class PermissionsCommand {
private final Map<String, Boolean> permissions;
private final transient boolean makerCheckerApproval;
public PermissionsCommand(final Map<String, Boolean> permissionsMap, final boolean makerCheckerApproval) {
this.permissions = permissionsMap;
this.makerCheckerApproval = makerCheckerApproval;
}
public Map<String, Boolean> getPermissions() {
return this.permissions;
}
public boolean isApprovedByChecker() {
return this.makerCheckerApproval;
}
}
@@ -1,4 +1,4 @@
package org.mifosng.platform.accounting.api.commands; package org.mifosng.platform.api.commands;
import java.util.Map; import java.util.Map;
@@ -15,20 +15,17 @@ public class PermissionUsageData {
private final String actionName; private final String actionName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final Boolean selected; private final Boolean selected;
@SuppressWarnings("unused")
private final Boolean canMakerChecker;
public static PermissionUsageData from(final String permissionCode, final boolean isSelected) { public static PermissionUsageData from(final String permissionCode, final boolean isSelected) {
return new PermissionUsageData(null, permissionCode, null, null, isSelected, null); return new PermissionUsageData(null, permissionCode, null, null, isSelected);
} }
public PermissionUsageData(final String grouping, final String code, final String entityName, final String actionName, public PermissionUsageData(final String grouping, final String code, final String entityName, final String actionName,
final Boolean selected, final Boolean canMakerChecker) { final Boolean selected) {
this.grouping = grouping; this.grouping = grouping;
this.code = code; this.code = code;
this.entityName = entityName; this.entityName = entityName;
this.actionName = actionName; this.actionName = actionName;
this.selected = selected; this.selected = selected;
this.canMakerChecker = canMakerChecker;
} }
} }
@@ -1,6 +1,5 @@
package org.mifosng.platform.api.infrastructure; package org.mifosng.platform.api.infrastructure;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.commands.LoanReassignmentCommand; import org.mifosng.platform.api.commands.LoanReassignmentCommand;
@@ -25,7 +24,9 @@ import org.mifosng.platform.api.commands.LoanTransactionCommand;
import org.mifosng.platform.api.commands.NoteCommand; import org.mifosng.platform.api.commands.NoteCommand;
import org.mifosng.platform.api.commands.OfficeCommand; import org.mifosng.platform.api.commands.OfficeCommand;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand; import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.commands.PermissionsCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.SavingAccountCommand; import org.mifosng.platform.api.commands.SavingAccountCommand;
import org.mifosng.platform.api.commands.SavingProductCommand; import org.mifosng.platform.api.commands.SavingProductCommand;
import org.mifosng.platform.api.commands.StaffCommand; import org.mifosng.platform.api.commands.StaffCommand;
@@ -41,6 +42,8 @@ public interface PortfolioApiDataConversionService {
OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json); OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json);
RoleCommand convertApiRequestJsonToRoleCommand(Long resourceIdentifier, String json); RoleCommand convertApiRequestJsonToRoleCommand(Long resourceIdentifier, String json);
PermissionsCommand convertApiRequestJsonToPermissionsCommand(String jsonRequestBody);
RolePermissionCommand convertApiRequestJsonToRolePermissionCommand(Long roleId, String jsonRequestBody); RolePermissionCommand convertApiRequestJsonToRolePermissionCommand(Long roleId, String jsonRequestBody);
@@ -17,7 +17,6 @@ import java.util.Set;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.commands.ChargeCommand; import org.mifosng.platform.api.commands.ChargeCommand;
@@ -42,7 +41,9 @@ import org.mifosng.platform.api.commands.LoanTransactionCommand;
import org.mifosng.platform.api.commands.NoteCommand; import org.mifosng.platform.api.commands.NoteCommand;
import org.mifosng.platform.api.commands.OfficeCommand; import org.mifosng.platform.api.commands.OfficeCommand;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand; import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.commands.PermissionsCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.SavingAccountCommand; import org.mifosng.platform.api.commands.SavingAccountCommand;
import org.mifosng.platform.api.commands.SavingProductCommand; import org.mifosng.platform.api.commands.SavingProductCommand;
import org.mifosng.platform.api.commands.StaffCommand; import org.mifosng.platform.api.commands.StaffCommand;
@@ -256,6 +257,17 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo
// command object // command object
return this.portfolioCommandDeserializerService.deserializeRoleCommand(resourceIdentifier, json, false); return this.portfolioCommandDeserializerService.deserializeRoleCommand(resourceIdentifier, json, false);
} }
@Override
public PermissionsCommand convertApiRequestJsonToPermissionsCommand(final String json) {
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
final Type typeOfMap = new TypeToken<Map<String, Boolean>>() {}.getType();
final Map<String, Boolean> permissionsMap = gsonConverter.fromJson(json, typeOfMap);
return new PermissionsCommand(permissionsMap, false);
}
@Override @Override
public RolePermissionCommand convertApiRequestJsonToRolePermissionCommand(final Long resourceIdentifier, final String json) { public RolePermissionCommand convertApiRequestJsonToRolePermissionCommand(final Long resourceIdentifier, final String json) {
@@ -4,9 +4,9 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.infrastructure.api.JsonParserHelper; import org.mifosng.platform.infrastructure.api.JsonParserHelper;
import org.mifosng.platform.infrastructure.errorhandling.InvalidJsonException; import org.mifosng.platform.infrastructure.errorhandling.InvalidJsonException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -1,8 +1,8 @@
package org.mifosng.platform.api.infrastructure; package org.mifosng.platform.api.infrastructure;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
/** /**
* Service for de-serializing JSON for a command into the platforms internal * Service for de-serializing JSON for a command into the platforms internal
@@ -1,8 +1,8 @@
package org.mifosng.platform.api.infrastructure; package org.mifosng.platform.api.infrastructure;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
/** /**
* Service for serializing commands into another format. * Service for serializing commands into another format.
@@ -2,9 +2,9 @@ package org.mifosng.platform.api.infrastructure;
import java.util.HashSet; import java.util.HashSet;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.infrastructure.api.GoogleGsonSerializerHelper; import org.mifosng.platform.infrastructure.api.GoogleGsonSerializerHelper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -0,0 +1,13 @@
package org.mifosng.platform.exceptions;
/**
* A {@link RuntimeException} thrown when permission resources are not found.
*/
public class PermissionCantBeMakerCheckerableException extends
AbstractPlatformResourceNotFoundException {
public PermissionCantBeMakerCheckerableException(String code) {
super("error.msg.permission.code.not.makercheckerable", "Permission with Code "
+ code + " can't be Maker Checkerable", code);
}
}
@@ -80,6 +80,15 @@ public class ApiParameterHelper {
} }
return template; return template;
} }
public static boolean makerCheckerable(final MultivaluedMap<String, String> queryParams) {
boolean makerCheckerable = false;
if (queryParams.getFirst("makerCheckerable") != null) {
String prettyPrintValue = queryParams.getFirst("makerCheckerable");
makerCheckerable = "true".equalsIgnoreCase(prettyPrintValue);
}
return makerCheckerable;
}
public static boolean genericResultSet(final MultivaluedMap<String, String> queryParams) { public static boolean genericResultSet(final MultivaluedMap<String, String> queryParams) {
boolean genericResultSet = false; boolean genericResultSet = false;
@@ -1,8 +1,8 @@
package org.mifosng.platform.makerchecker.service; package org.mifosng.platform.makerchecker.service;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.infrastructure.PortfolioApiDataConversionService; import org.mifosng.platform.api.infrastructure.PortfolioApiDataConversionService;
import org.mifosng.platform.api.infrastructure.PortfolioCommandDeserializerService; import org.mifosng.platform.api.infrastructure.PortfolioCommandDeserializerService;
import org.mifosng.platform.api.infrastructure.PortfolioCommandSerializerService; import org.mifosng.platform.api.infrastructure.PortfolioCommandSerializerService;
@@ -6,12 +6,12 @@ import org.springframework.security.access.prepost.PreAuthorize;
public interface CodeWritePlatformService { public interface CodeWritePlatformService {
@PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'ORGANISATION_ADMINISTRATION_SUPER_USER', 'CREATE_CODE')") @PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'CREATE_CODE')")
Long createCode(final CodeCommand command); Long createCode(final CodeCommand command);
@PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'ORGANISATION_ADMINISTRATION_SUPER_USER', 'UPDATE_CODE')") @PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'UPDATE_CODE')")
Long updateCode(final CodeCommand command); Long updateCode(final CodeCommand command);
@PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE', 'DELETE_CODE')") @PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'DELETE_CODE')")
EntityIdentifier deleteCode(Long codeId); EntityIdentifier deleteCode(Long codeId);
} }
@@ -172,7 +172,7 @@ public class AppUser extends AbstractAuditableCustom<AppUser, Long> implements P
for (Role role : this.roles) { for (Role role : this.roles) {
Collection<Permission> permissions = role.getPermissions(); Collection<Permission> permissions = role.getPermissions();
for (Permission permission : permissions) { for (Permission permission : permissions) {
grantedAuthorities.add(new SimpleGrantedAuthority(permission.code())); grantedAuthorities.add(new SimpleGrantedAuthority(permission.getCode()));
} }
} }
return grantedAuthorities; return grantedAuthorities;
@@ -10,7 +10,6 @@ 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> {
@SuppressWarnings("unused")
@Column(name = "grouping", nullable = false, length = 45) @Column(name = "grouping", nullable = false, length = 45)
private final String grouping; private final String grouping;
@@ -25,8 +24,9 @@ public class Permission extends AbstractPersistable<Long> {
@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) @SuppressWarnings("unused")
private final boolean canMakerChecker; @Column(name = "can_maker_checker", nullable = false)
private boolean canMakerChecker;
protected Permission() { protected Permission() {
this.grouping = null; this.grouping = null;
@@ -40,11 +40,20 @@ public class Permission extends AbstractPersistable<Long> {
return this.code.equalsIgnoreCase(checkCode); return this.code.equalsIgnoreCase(checkCode);
} }
public String code() { public String getCode() {
return this.code; return this.code;
} }
public boolean hasMakerCheckerEnabled() { public boolean hasMakerCheckerEnabled() {
return this.canMakerChecker; return this.canMakerChecker;
} }
public void setCanMakerChecker(Boolean canMakerChecker) {
this.canMakerChecker = canMakerChecker;
}
public String getGrouping() {
return this.grouping;
}
} }
@@ -7,6 +7,8 @@ import org.mifosng.platform.api.data.PermissionUsageData;
public interface PermissionReadPlatformService { public interface PermissionReadPlatformService {
Collection<PermissionUsageData> retrieveAllPermissions(); Collection<PermissionUsageData> retrieveAllPermissions();
Collection<PermissionUsageData> retrieveAllMakerCheckerablePermissions();
Collection<PermissionUsageData> retrieveAllRolePermissions(Long roleId); Collection<PermissionUsageData> retrieveAllRolePermissions(Long roleId);
} }
@@ -7,70 +7,110 @@ import java.util.Collection;
import org.mifosng.platform.api.data.PermissionUsageData; import org.mifosng.platform.api.data.PermissionUsageData;
import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource; import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class PermissionReadPlatformServiceImpl implements PermissionReadPlatformService { public class PermissionReadPlatformServiceImpl implements
PermissionReadPlatformService {
private final JdbcTemplate jdbcTemplate; private final static Logger logger = LoggerFactory
private final PlatformSecurityContext context; .getLogger(PermissionReadPlatformService.class);
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
@Autowired @Autowired
public PermissionReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource) { public PermissionReadPlatformServiceImpl(
this.context = context; final PlatformSecurityContext context,
this.jdbcTemplate = new JdbcTemplate(dataSource); final TenantAwareRoutingDataSource dataSource) {
} this.context = context;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override @Override
public Collection<PermissionUsageData> retrieveAllPermissions() { public Collection<PermissionUsageData> retrieveAllPermissions() {
context.authenticatedUser(); context.authenticatedUser();
final PermissionUsageDataMapper mapper = new PermissionUsageDataMapper(); final PermissionUsageDataMapper mapper = new PermissionUsageDataMapper();
final String sql = mapper.permissionSchema(); final String sql = mapper.permissionSchema();
logger.info("retrieveAllPermissions: " + sql);
return this.jdbcTemplate.query(sql, mapper, new Object[] {});
}
return this.jdbcTemplate.query(sql, mapper, new Object[] {}); @Override
} public Collection<PermissionUsageData> retrieveAllMakerCheckerablePermissions() {
@Override context.authenticatedUser();
public Collection<PermissionUsageData> retrieveAllRolePermissions(final Long roleId) {
final PermissionUsageDataMapper mapper = new PermissionUsageDataMapper(); final PermissionUsageDataMapper mapper = new PermissionUsageDataMapper();
final String sql = mapper.rolePermissionSchema(); final String sql = mapper.makerCheckerablePermissionSchema();
logger.info("retrieveAllMakerCheckerablePermissions: " + sql);
return this.jdbcTemplate.query(sql, mapper, new Object[] { roleId }); return this.jdbcTemplate.query(sql, mapper, new Object[] {});
} }
private static final class PermissionUsageDataMapper implements RowMapper<PermissionUsageData> { @Override
public Collection<PermissionUsageData> retrieveAllRolePermissions(
final Long roleId) {
@Override final PermissionUsageDataMapper mapper = new PermissionUsageDataMapper();
public PermissionUsageData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { final String sql = mapper.rolePermissionSchema();
logger.info("retrieveAllRolePermissions: " + sql);
final String grouping = rs.getString("grouping"); return this.jdbcTemplate.query(sql, mapper, new Object[] { roleId });
final String code = rs.getString("code"); }
final String entityName = rs.getString("entityName");
final String actionName = rs.getString("actionName");
final Boolean selected = rs.getBoolean("selected");
final Boolean canMakerChecker = rs.getBoolean("canMakerChecker");
return new PermissionUsageData(grouping, code, entityName, actionName, selected, canMakerChecker); private static final class PermissionUsageDataMapper implements
} RowMapper<PermissionUsageData> {
public String permissionSchema() { @Override
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, true as selected, p.can_maker_checker as canMakerChecker" public PermissionUsageData mapRow(final ResultSet rs,
+ " from m_permission p " + " order by p.grouping, ifnull(entity_name, ''), p.code"; @SuppressWarnings("unused") final int rowNum)
} throws SQLException {
public String rolePermissionSchema() { final String grouping = rs.getString("grouping");
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, if(isnull(rp.role_id), false, true) as selected, " final String code = rs.getString("code");
+ " if(isnull(rp.role_id), false, p.can_maker_checker) as canMakerChecker " final String entityName = rs.getString("entityName");
+ " from m_permission p " final String actionName = rs.getString("actionName");
+ " left join m_role_permission rp on rp.permission_id = p.id and rp.role_id = ? " final Boolean selected = rs.getBoolean("selected");
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
} return new PermissionUsageData(grouping, code, entityName,
} actionName, selected);
}
public String permissionSchema() {
/* get all non-CHECKER permissions */
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, true as selected"
+ " from m_permission p "
+ " where code not like '%\\_CHECKER'"
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
}
public String makerCheckerablePermissionSchema() {
/*
* get all 'Maker-Checkerable' permissions - Maintenance permissions
* (i.e. exclude the 'special' grouping, the READ permissions and
* the CHECKER permissions
*/
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, p.can_maker_checker as selected"
+ " from m_permission p "
+ " where grouping != 'special' and code not like 'READ_%' and code not like '%\\_CHECKER'"
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
}
public String rolePermissionSchema() {
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, if(isnull(rp.role_id), false, true) as selected "
+ " from m_permission p "
+ " left join m_role_permission rp on rp.permission_id = p.id and rp.role_id = ? "
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
}
}
} }
@@ -0,0 +1,11 @@
package org.mifosng.platform.user.service;
import org.mifosng.platform.api.commands.PermissionsCommand;
import org.springframework.security.access.prepost.PreAuthorize;
public interface PermissionWritePlatformService {
@PreAuthorize(value = "hasAnyRole('ALL_FUNCTIONS', 'PERMISSIONS_ROLE')")
Long updateMakerCheckerPermissions(PermissionsCommand command);
}
@@ -0,0 +1,74 @@
package org.mifosng.platform.user.service;
import java.util.Collection;
import java.util.Map;
import org.mifosng.platform.api.commands.PermissionsCommand;
import org.mifosng.platform.exceptions.PermissionNotFoundException;
import org.mifosng.platform.security.PlatformSecurityContext;
import org.mifosng.platform.user.domain.Permission;
import org.mifosng.platform.user.domain.PermissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class PermissionWritePlatformServiceJpaRepositoryImpl implements
PermissionWritePlatformService {
private final PlatformSecurityContext context;
private final PermissionRepository permissionRepository;
@Autowired
public PermissionWritePlatformServiceJpaRepositoryImpl(
final PlatformSecurityContext context,
final PermissionRepository permissionRepository) {
this.context = context;
this.permissionRepository = permissionRepository;
}
@Transactional
@Override
public Long updateMakerCheckerPermissions(final PermissionsCommand command) {
context.authenticatedUser();
final Collection<Permission> allPermissions = this.permissionRepository
.findAll();
final Map<String, Boolean> commandPermissions = command
.getPermissions();
for (final String permissionCode : commandPermissions.keySet()) {
final Permission permission = findPermissionByCode(allPermissions,
permissionCode);
if (permission.getCode().endsWith("_CHECKER")
|| permission.getCode().startsWith("READ_")
|| permission.getGrouping().equalsIgnoreCase("special")) {
throw new PermissionNotFoundException(permissionCode);
}
final boolean isSelected = commandPermissions.get(permissionCode)
.booleanValue();
permission.setCanMakerChecker(isSelected);
this.permissionRepository.save(permission);
}
return new Long(0);
}
private Permission findPermissionByCode(
Collection<Permission> allPermissions, String permissionCode) {
if (allPermissions != null) {
for (Permission permission : allPermissions) {
if (permission.hasCode(permissionCode)) {
return permission;
}
}
}
throw new PermissionNotFoundException(permissionCode);
}
}
@@ -1,7 +1,7 @@
package org.mifosng.platform.user.service; package org.mifosng.platform.user.service;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
public interface RoleWritePlatformService { public interface RoleWritePlatformService {
@@ -3,8 +3,8 @@ package org.mifosng.platform.user.service;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.RolePermissionCommand;
import org.mifosng.platform.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException; import org.mifosng.platform.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException;
import org.mifosng.platform.exceptions.PermissionNotFoundException; import org.mifosng.platform.exceptions.PermissionNotFoundException;
import org.mifosng.platform.exceptions.RoleNotFoundException; import org.mifosng.platform.exceptions.RoleNotFoundException;