MIFOSX-258: return roles of authenticated user in response of authentication api.

This commit is contained in:
Keith Woodlock
2013-03-20 01:13:34 +00:00
parent 2c8efb4977
commit 3e8d9f1c13
7 changed files with 54 additions and 43 deletions
+16 -11
View File
@@ -989,8 +989,7 @@
<tr>
<td><a href="#authentication">AUTHENTICATION</a></td>
<td>authentication</td>
<td><a href="#authenticate_request">Verify
Authentication</a></td>
<td><a href="#authenticate_request">Verify Authentication</a></td>
<td></td>
<td></td>
<td></td>
@@ -9423,8 +9422,7 @@ GET https://DomainName/api/v1/reports/{reportName}
<div class="method-section">
<div class="method-description">
<h4>Verify authentication</h4>
<p>Authenticates the credentials provided and returns the set
of permissions allowed.</p>
<p>Authenticates the credentials provided and returns the set roles and permissions allowed.</p>
</div>
<div class="method-example">
<code class="method-declaration">
@@ -9437,13 +9435,20 @@ No Request Body
</code>
<code class="method-response">
{
"username": "mifos",
"userId": 1,
"base64EncodedAuthenticationKey": "bWlmb3M6cGFzc3dvcmQ=",
"authenticated": true,
"permissions": [
"ALL_FUNCTIONS"
]
"username": "mifos",
"userId": 1,
"base64EncodedAuthenticationKey": "bWlmb3M6cGFzc3dvcmQ=",
"authenticated": true,
"roles": [
{
"id": 1,
"name": "Super user",
"description": "This role provides all application permissions."
}
],
"permissions": [
"ALL_FUNCTIONS"
]
}
</code>
<code class="method-request">
@@ -17,7 +17,9 @@ import javax.ws.rs.core.MediaType;
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
import org.mifosplatform.infrastructure.security.data.AuthenticatedUserData;
import org.mifosplatform.useradministration.data.RoleData;
import org.mifosplatform.useradministration.domain.AppUser;
import org.mifosplatform.useradministration.service.RoleReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
@@ -36,13 +38,15 @@ public class AuthenticationApiResource {
private final DaoAuthenticationProvider customAuthenticationProvider;
private final ToApiJsonSerializer<AuthenticatedUserData> apiJsonSerializerService;
private final RoleReadPlatformService roleReadPlatformService;
@Autowired
public AuthenticationApiResource(
@Qualifier("customAuthenticationProvider") final DaoAuthenticationProvider customAuthenticationProvider,
final ToApiJsonSerializer<AuthenticatedUserData> apiJsonSerializerService) {
final ToApiJsonSerializer<AuthenticatedUserData> apiJsonSerializerService, final RoleReadPlatformService roleReadPlatformService) {
this.customAuthenticationProvider = customAuthenticationProvider;
this.apiJsonSerializerService = apiJsonSerializerService;
this.roleReadPlatformService = roleReadPlatformService;
}
@POST
@@ -64,7 +68,9 @@ public class AuthenticationApiResource {
AppUser principal = (AppUser) authenticationCheck.getPrincipal();
byte[] base64EncodedAuthenticationKey = Base64.encode(username + ":" + password);
authenticatedUserData = new AuthenticatedUserData(username, permissions, principal.getId(), new String(
Collection<RoleData> roles = this.roleReadPlatformService.retrieveAll();
authenticatedUserData = new AuthenticatedUserData(username, roles, permissions, principal.getId(), new String(
base64EncodedAuthenticationKey));
}
@@ -7,6 +7,8 @@ package org.mifosplatform.infrastructure.security.data;
import java.util.Collection;
import org.mifosplatform.useradministration.data.RoleData;
/**
* Immutable data object for authentication.
*/
@@ -21,6 +23,8 @@ public class AuthenticatedUserData {
@SuppressWarnings("unused")
private final boolean authenticated;
@SuppressWarnings("unused")
private final Collection<RoleData> roles;
@SuppressWarnings("unused")
private final Collection<String> permissions;
public AuthenticatedUserData(final String username, final Collection<String> permissions) {
@@ -28,15 +32,17 @@ public class AuthenticatedUserData {
this.userId = null;
this.base64EncodedAuthenticationKey = null;
this.authenticated = false;
this.roles = null;
this.permissions = permissions;
}
public AuthenticatedUserData(final String username, final Collection<String> permissions, final Long userId,
final String base64EncodedAuthenticationKey) {
public AuthenticatedUserData(final String username, final Collection<RoleData> roles, final Collection<String> permissions,
final Long userId, final String base64EncodedAuthenticationKey) {
this.username = username;
this.userId = userId;
this.base64EncodedAuthenticationKey = base64EncodedAuthenticationKey;
this.authenticated = true;
this.roles = roles;
this.permissions = permissions;
}
}
@@ -104,7 +104,7 @@ public class RolesApiResource {
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
final Collection<RoleData> roles = this.roleReadPlatformService.retrieveAllRoles();
final Collection<RoleData> roles = this.roleReadPlatformService.retrieveAll();
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, roles, RESPONSE_DATA_PARAMETERS);
@@ -135,7 +135,7 @@ public class RolesApiResource {
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
RoleData role = this.roleReadPlatformService.retrieveRole(roleId);
RoleData role = this.roleReadPlatformService.retrieveOne(roleId);
if (settings.isCommandIdPassed()) {
final CommandSourceData entry = this.commandSourceReadPlatformService.retrieveById(settings.getCommandId());
final RoleCommand changesOnly = this.fromApiJsonDeserializer.commandFromApiJson(entry.json());
@@ -172,7 +172,7 @@ public class RolesApiResource {
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
final RoleData role = this.roleReadPlatformService.retrieveRole(roleId);
final RoleData role = this.roleReadPlatformService.retrieveOne(roleId);
final Collection<PermissionData> permissionUsageData = this.permissionReadPlatformService.retrieveAllRolePermissions(roleId);
Collection<PermissionData> currentChanges = null;
@@ -76,7 +76,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
public AppUserData retrieveNewUserDetails() {
final Collection<OfficeData> offices = this.officeReadPlatformService.retrieveAllOfficesForDropdown();
final Collection<RoleData> availableRoles = this.roleReadPlatformService.retrieveAllRoles();
final Collection<RoleData> availableRoles = this.roleReadPlatformService.retrieveAll();
return AppUserData.template(offices, availableRoles);
}
@@ -89,7 +89,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
final AppUser user = this.appUserRepository.findOne(userId);
if (user == null || user.isDeleted()) { throw new UserNotFoundException(userId); }
Collection<RoleData> availableRoles = this.roleReadPlatformService.retrieveAllRoles();
Collection<RoleData> availableRoles = this.roleReadPlatformService.retrieveAll();
Collection<RoleData> selectedUserRoles = new ArrayList<RoleData>();
Set<Role> userRoles = user.getRoles();
@@ -11,7 +11,7 @@ import org.mifosplatform.useradministration.data.RoleData;
public interface RoleReadPlatformService {
Collection<RoleData> retrieveAllRoles();
Collection<RoleData> retrieveAll();
RoleData retrieveRole(Long roleId);
RoleData retrieveOne(Long roleId);
}
@@ -11,12 +11,10 @@ import java.util.Collection;
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.useradministration.data.RoleData;
import org.mifosplatform.useradministration.domain.Role;
import org.mifosplatform.useradministration.domain.RoleRepository;
import org.mifosplatform.useradministration.exception.RoleNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
@@ -25,35 +23,31 @@ import org.springframework.stereotype.Service;
public class RoleReadPlatformServiceImpl implements RoleReadPlatformService {
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
private final RoleRepository roleRepository;
private final RoleMapper roleRowMapper;
@Autowired
public RoleReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource,
final RoleRepository roleRepository) {
this.context = context;
this.roleRepository = roleRepository;
public RoleReadPlatformServiceImpl(final TenantAwareRoutingDataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.roleRowMapper = new RoleMapper();
}
@Override
public Collection<RoleData> retrieveAllRoles() {
context.authenticatedUser();
public Collection<RoleData> retrieveAll() {
final String sql = "select " + roleRowMapper.schema() + " order by r.id";
final RoleMapper mapper = new RoleMapper();
final String sql = "select " + mapper.schema() + " order by r.id";
return this.jdbcTemplate.query(sql, mapper, new Object[] {});
return this.jdbcTemplate.query(sql, this.roleRowMapper);
}
@Override
public RoleData retrieveRole(final Long id) {
context.authenticatedUser();
public RoleData retrieveOne(final Long id) {
final Role role = this.roleRepository.findOne(id);
if (role == null) { throw new RoleNotFoundException(id); }
return role.toData();
try {
final String sql = "select " + roleRowMapper.schema() + " where r.id=?";
return this.jdbcTemplate.queryForObject(sql, this.roleRowMapper, new Object[] { id });
} catch (EmptyResultDataAccessException e) {
throw new RoleNotFoundException(id);
}
}
protected static final class RoleMapper implements RowMapper<RoleData> {