Self user role and report access

This commit is contained in:
Nazeer Hussain Shaik
2017-08-21 17:42:19 +05:30
parent 6e95f88d7b
commit df16dc3cad
6 changed files with 99 additions and 1 deletions
@@ -44,5 +44,6 @@ public class SelfServiceApiConstants {
authenticationTokenParamName)); authenticationTokenParamName));
public static final Object[] SUPPORTED_AUTHENTICATION_MODE_PARAMETERS = new Object[] {emailModeParamName, public static final Object[] SUPPORTED_AUTHENTICATION_MODE_PARAMETERS = new Object[] {emailModeParamName,
mobileModeParamName}; mobileModeParamName};
public static final String SELF_SERVICE_USER_ROLE = "Self Service User";
} }
@@ -58,7 +58,9 @@ import org.apache.fineract.useradministration.domain.AppUser;
import org.apache.fineract.useradministration.domain.PasswordValidationPolicy; import org.apache.fineract.useradministration.domain.PasswordValidationPolicy;
import org.apache.fineract.useradministration.domain.PasswordValidationPolicyRepository; import org.apache.fineract.useradministration.domain.PasswordValidationPolicyRepository;
import org.apache.fineract.useradministration.domain.Role; import org.apache.fineract.useradministration.domain.Role;
import org.apache.fineract.useradministration.domain.RoleRepository;
import org.apache.fineract.useradministration.domain.UserDomainService; import org.apache.fineract.useradministration.domain.UserDomainService;
import org.apache.fineract.useradministration.exception.RoleNotFoundException;
import org.apache.fineract.useradministration.service.AppUserReadPlatformService; import org.apache.fineract.useradministration.service.AppUserReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
@@ -85,6 +87,7 @@ public class SelfServiceRegistrationWritePlatformServiceImpl implements SelfServ
private SmsMessageScheduledJobService smsMessageScheduledJobService; private SmsMessageScheduledJobService smsMessageScheduledJobService;
private final SmsCampaignDropdownReadPlatformService smsCampaignDropdownReadPlatformService; private final SmsCampaignDropdownReadPlatformService smsCampaignDropdownReadPlatformService;
private final AppUserReadPlatformService appUserReadPlatformService; private final AppUserReadPlatformService appUserReadPlatformService;
private final RoleRepository roleRepository;
@Autowired @Autowired
public SelfServiceRegistrationWritePlatformServiceImpl(final SelfServiceRegistrationRepository selfServiceRegistrationRepository, public SelfServiceRegistrationWritePlatformServiceImpl(final SelfServiceRegistrationRepository selfServiceRegistrationRepository,
@@ -94,7 +97,7 @@ public class SelfServiceRegistrationWritePlatformServiceImpl implements SelfServ
final UserDomainService userDomainService, final GmailBackedPlatformEmailService gmailBackedPlatformEmailService, final UserDomainService userDomainService, final GmailBackedPlatformEmailService gmailBackedPlatformEmailService,
final SmsMessageRepository smsMessageRepository, SmsMessageScheduledJobService smsMessageScheduledJobService, final SmsMessageRepository smsMessageRepository, SmsMessageScheduledJobService smsMessageScheduledJobService,
final SmsCampaignDropdownReadPlatformService smsCampaignDropdownReadPlatformService, final SmsCampaignDropdownReadPlatformService smsCampaignDropdownReadPlatformService,
final AppUserReadPlatformService appUserReadPlatformService) { final AppUserReadPlatformService appUserReadPlatformService,final RoleRepository roleRepository) {
this.selfServiceRegistrationRepository = selfServiceRegistrationRepository; this.selfServiceRegistrationRepository = selfServiceRegistrationRepository;
this.fromApiJsonHelper = fromApiJsonHelper; this.fromApiJsonHelper = fromApiJsonHelper;
this.selfServiceRegistrationReadPlatformService = selfServiceRegistrationReadPlatformService; this.selfServiceRegistrationReadPlatformService = selfServiceRegistrationReadPlatformService;
@@ -106,6 +109,7 @@ public class SelfServiceRegistrationWritePlatformServiceImpl implements SelfServ
this.smsMessageScheduledJobService = smsMessageScheduledJobService; this.smsMessageScheduledJobService = smsMessageScheduledJobService;
this.smsCampaignDropdownReadPlatformService = smsCampaignDropdownReadPlatformService; this.smsCampaignDropdownReadPlatformService = smsCampaignDropdownReadPlatformService;
this.appUserReadPlatformService = appUserReadPlatformService; this.appUserReadPlatformService = appUserReadPlatformService;
this.roleRepository = roleRepository;
} }
@Override @Override
@@ -262,6 +266,12 @@ public class SelfServiceRegistrationWritePlatformServiceImpl implements SelfServ
final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>(); final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("DUMMY_ROLE_NOT_USED_OR_PERSISTED_TO_AVOID_EXCEPTION")); authorities.add(new SimpleGrantedAuthority("DUMMY_ROLE_NOT_USED_OR_PERSISTED_TO_AVOID_EXCEPTION"));
final Set<Role> allRoles = new HashSet<>(); final Set<Role> allRoles = new HashSet<>();
Role role = this.roleRepository.getRoleByName(SelfServiceApiConstants.SELF_SERVICE_USER_ROLE);
if(role != null){
allRoles.add(role);
}else{
throw new RoleNotFoundException(SelfServiceApiConstants.SELF_SERVICE_USER_ROLE);
}
List<Client> clients = new ArrayList<>(Arrays.asList(client)); List<Client> clients = new ArrayList<>(Arrays.asList(client));
User user = new User(selfServiceRegistration.getUsername(), selfServiceRegistration.getPassword(), authorities); User user = new User(selfServiceRegistration.getUsername(), selfServiceRegistration.getPassword(), authorities);
AppUser appUser = new AppUser(client.getOffice(), user, allRoles, selfServiceRegistration.getEmail(), client.getFirstname(), AppUser appUser = new AppUser(client.getOffice(), user, allRoles, selfServiceRegistration.getEmail(), client.getFirstname(),
@@ -0,0 +1,60 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.self.runreport;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
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.Response;
import javax.ws.rs.core.UriInfo;
import org.apache.fineract.infrastructure.dataqueries.api.RunreportsApiResource;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Path("/self/runreports")
@Component
@Scope("singleton")
public class SelfRunReportApiResource {
private final PlatformSecurityContext context;
private final RunreportsApiResource runreportsApiResource;
@Autowired
public SelfRunReportApiResource(final PlatformSecurityContext context, final RunreportsApiResource runreportsApiResource) {
this.context = context;
this.runreportsApiResource = runreportsApiResource;
}
@GET
@Path("{reportName}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON, "text/csv", "application/vnd.ms-excel", "application/pdf", "text/html" })
public Response runReport(@PathParam("reportName") final String reportName, @Context final UriInfo uriInfo) {
this.context.authenticatedUser();
return this.runreportsApiResource.runReport(reportName, uriInfo);
}
}
@@ -28,4 +28,7 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
@Query("SELECT COUNT(a) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId AND a.deleted = false") @Query("SELECT COUNT(a) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId AND a.deleted = false")
Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId); Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId);
@Query("SELECT role FROM Role role WHERE role.name = :name")
Role getRoleByName(@Param("name") String name);
} }
@@ -28,4 +28,8 @@ public class RoleNotFoundException extends AbstractPlatformResourceNotFoundExcep
public RoleNotFoundException(final Long id) { public RoleNotFoundException(final Long id) {
super("error.msg.role.id.invalid", "Role with identifier " + id + " does not exist", id); super("error.msg.role.id.invalid", "Role with identifier " + id + " does not exist", id);
} }
public RoleNotFoundException(final String name) {
super("error.msg.role.name.invalid", "Role with name " + name + " does not exist", name);
}
} }
@@ -0,0 +1,20 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
--
INSERT INTO `m_role` (`name`, `description`, `is_disabled`) VALUES ('Self Service User', 'self service user role', 1);