From 9472ec236c87264e6ff9d2b8c261b1ccf949e5be Mon Sep 17 00:00:00 2001 From: Vishwas Babu A J Date: Fri, 9 May 2014 14:46:43 +0530 Subject: [PATCH] cleaning up commit for MIFOSX-982 --- .../api/UsersApiResource.java | 28 ++++++---- .../useradministration/domain/AppUser.java | 52 +++++++++---------- ...WritePlatformServiceJpaRepositoryImpl.java | 28 +++++----- .../service/UserDataValidator.java | 14 ++--- 4 files changed, 63 insertions(+), 59 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/UsersApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/UsersApiResource.java index 9ba14eb1b..b539d0e9f 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/UsersApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/UsersApiResource.java @@ -5,6 +5,23 @@ */ package org.mifosplatform.useradministration.api; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriInfo; + import org.mifosplatform.commands.domain.CommandWrapper; import org.mifosplatform.commands.service.CommandWrapperBuilder; import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService; @@ -21,15 +38,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.UriInfo; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - @Path("/users") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @@ -42,7 +50,7 @@ public class UsersApiResource { * {@link AppUserData}. */ private final Set RESPONSE_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "officeId", "officeName", "username", - "firstname", "lastname", "email", "allowedOffices", "availableRoles", "selectedRoles", "staffId")); + "firstname", "lastname", "email", "allowedOffices", "availableRoles", "selectedRoles", "staff")); private final String resourceNameForPermissions = "USER"; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java index b654c207e..d64f849d3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java @@ -7,17 +7,28 @@ package org.mifosplatform.useradministration.domain; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Date; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.UniqueConstraint; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.EnumOptionData; +import org.mifosplatform.infrastructure.core.service.DateUtils; import org.mifosplatform.infrastructure.security.domain.PlatformUser; import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException; import org.mifosplatform.infrastructure.security.service.PlatformPasswordEncoder; @@ -30,8 +41,6 @@ import org.springframework.data.jpa.domain.AbstractPersistable; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; -import org.mifosplatform.infrastructure.core.service.DateUtils; - @Entity @Table(name = "m_appuser", uniqueConstraints = @UniqueConstraint(columnNames = { "username" }, name = "username_org")) @@ -158,7 +167,7 @@ public class AppUser extends AbstractPersistable implements PlatformUser { public void changeOffice(final Office differentOffice) { this.office = differentOffice; } - + public void changeStaff(final Staff differentStaff) { this.staff = differentStaff; } @@ -201,16 +210,10 @@ public class AppUser extends AbstractPersistable implements PlatformUser { } final String staffIdParamName = "staffId"; - if(command.hasParameter(staffIdParamName)) { - if(this.staff==null) { - final Long newValue = command.longValueOfParameterNamed(staffIdParamName); - actualChanges.put(staffIdParamName, newValue); - } else { - if (command.isChangeInLongParameterNamed(staffIdParamName, this.staff.getId())) { - final Long newValue = command.longValueOfParameterNamed(staffIdParamName); - actualChanges.put(staffIdParamName, newValue); - } - } + if (command.hasParameter(staffIdParamName) + && (this.staff == null || command.isChangeInLongParameterNamed(staffIdParamName, this.staff.getId()))) { + final Long newValue = command.longValueOfParameterNamed(staffIdParamName); + actualChanges.put(staffIdParamName, newValue); } final String rolesParamName = "roles"; @@ -343,12 +346,14 @@ public class AppUser extends AbstractPersistable implements PlatformUser { public Office getOffice() { return this.office; } - + public Staff getStaff() { return this.staff; } - public Date getLastTimePasswordUpdated(){ return this.lastTimePasswordUpdated; } + public Date getLastTimePasswordUpdated() { + return this.lastTimePasswordUpdated; + } public boolean canNotApproveLoanInPast() { return hasNotPermissionForAnyOf("ALL_FUNCTIONS", "APPROVEINPAST_LOAN"); @@ -518,27 +523,22 @@ public class AppUser extends AbstractPersistable implements PlatformUser { return staffDisplayName; } - public String getEncodedPassword(final JsonCommand command,final PlatformPasswordEncoder platformPasswordEncoder) - { + public String getEncodedPassword(final JsonCommand command, final PlatformPasswordEncoder platformPasswordEncoder) { final String passwordParamName = "password"; final String passwordEncodedParamName = "passwordEncoded"; String passwordEncodedValue = null; if (command.hasParameter(passwordParamName)) { - if (command.isChangeInPasswordParameterNamed(passwordParamName, this.password - , platformPasswordEncoder,getId())) { + if (command.isChangeInPasswordParameterNamed(passwordParamName, this.password, platformPasswordEncoder, getId())) { - passwordEncodedValue = command.passwordValueOfParameterNamed(passwordParamName, platformPasswordEncoder, - getId()); + passwordEncodedValue = command.passwordValueOfParameterNamed(passwordParamName, platformPasswordEncoder, getId()); } - } - else if (command.hasParameter(passwordEncodedParamName)) { + } else if (command.hasParameter(passwordEncodedParamName)) { if (command.isChangeInStringParameterNamed(passwordEncodedParamName, this.password)) { passwordEncodedValue = command.stringValueOfParameterNamed(passwordEncodedParamName); - } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserWritePlatformServiceJpaRepositoryImpl.java index ed6f5acdd..3cdd1c83d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserWritePlatformServiceJpaRepositoryImpl.java @@ -26,7 +26,6 @@ import org.mifosplatform.organisation.office.domain.OfficeRepository; import org.mifosplatform.organisation.office.exception.OfficeNotFoundException; import org.mifosplatform.organisation.staff.domain.Staff; import org.mifosplatform.organisation.staff.domain.StaffRepositoryWrapper; -import org.mifosplatform.organisation.staff.exception.StaffNotFoundException; import org.mifosplatform.useradministration.api.AppUserApiConstant; import org.mifosplatform.useradministration.domain.AppUser; import org.mifosplatform.useradministration.domain.AppUserPreviousPassword; @@ -64,7 +63,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit private final UserDataValidator fromApiJsonDeserializer; private final AppUserPreviousPasswordRepository appUserPreviewPasswordRepository; private final StaffRepositoryWrapper staffRepositoryWrapper; - + @Autowired public AppUserWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final AppUserRepository appUserRepository, final UserDomainService userDomainService, final OfficeRepository officeRepository, final RoleRepository roleRepository, @@ -100,19 +99,18 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit final String[] roles = command.arrayValueOfParameterNamed("roles"); final Set allRoles = assembleSetOfRoles(roles); - AppUser appUser; + AppUser appUser; final String staffIdParamName = "staffId"; final Long staffId = command.longValueOfParameterNamed(staffIdParamName); - - if(staffId != null) { - final Staff linkedStaff = this.staffRepositoryWrapper.findOneWithNotFoundDetection(staffId); - if(linkedStaff.officeId() != userOffice.getId()) { throw new StaffNotFoundException(staffId); } - appUser = AppUser.fromJson(userOffice,linkedStaff, allRoles, command); - } else { - appUser = AppUser.fromJson(userOffice, null, allRoles, command); + Staff linkedStaff = null; + if (staffId != null) { + linkedStaff = this.staffRepositoryWrapper.findByOfficeWithNotFoundDetection(staffId, userOffice.getId()); } + + appUser = AppUser.fromJson(userOffice, linkedStaff, allRoles, command); + final Boolean sendPasswordToEmail = command.booleanObjectValueOfParameterNamed("sendPasswordToEmail"); this.userDomainService.create(appUser, sendPasswordToEmail); @@ -163,14 +161,12 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit userToUpdate.changeOffice(office); } - + if (changes.containsKey("staffId")) { final Long staffId = (Long) changes.get("staffId"); - final Staff linkedStaff; - if(staffId!=null) { - linkedStaff = this.staffRepositoryWrapper.findOneWithNotFoundDetection(staffId); - } else { - linkedStaff = null; + Staff linkedStaff = null; + if (staffId != null) { + linkedStaff = this.staffRepositoryWrapper.findByOfficeWithNotFoundDetection(staffId, userToUpdate.getOffice().getId()); } userToUpdate.changeStaff(linkedStaff); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/UserDataValidator.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/UserDataValidator.java index 9132095de..f9c8039a1 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/UserDataValidator.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/UserDataValidator.java @@ -81,11 +81,11 @@ public final class UserDataValidator { final Long officeId = this.fromApiJsonHelper.extractLongNamed("officeId", element); baseDataValidator.reset().parameter("officeId").value(officeId).notNull().integerGreaterThanZero(); - if(this.fromApiJsonHelper.parameterExists("staffId", element)) { - final Long staffId = this.fromApiJsonHelper.extractLongNamed("staffId", element); - baseDataValidator.reset().parameter("staffId").value(staffId).notNull().integerGreaterThanZero(); + if (this.fromApiJsonHelper.parameterExists("staffId", element)) { + final Long staffId = this.fromApiJsonHelper.extractLongNamed("staffId", element); + baseDataValidator.reset().parameter("staffId").value(staffId).notNull().integerGreaterThanZero(); } - + final String[] roles = this.fromApiJsonHelper.extractArrayNamed("roles", element); baseDataValidator.reset().parameter("roles").value(roles).arrayNotEmpty(); @@ -111,10 +111,10 @@ public final class UserDataValidator { final Long officeId = this.fromApiJsonHelper.extractLongNamed("officeId", element); baseDataValidator.reset().parameter("officeId").value(officeId).notNull().integerGreaterThanZero(); } - - if(this.fromApiJsonHelper.parameterExists("staffId", element)) { + + if (this.fromApiJsonHelper.parameterExists("staffId", element)) { final Long staffId = this.fromApiJsonHelper.extractLongNamed("staffId", element); - baseDataValidator.reset().parameter("staffId").value(staffId).integerGreaterThanZero(); + baseDataValidator.reset().parameter("staffId").value(staffId).ignoreIfNull().integerGreaterThanZero(); } if (this.fromApiJsonHelper.parameterExists("username", element)) {