Advertisement
aadddrr

Untitled

Sep 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. package org.jleaf.common.bo.ou;
  2.  
  3. import java.math.BigInteger;
  4.  
  5. import javax.persistence.Query;
  6.  
  7. import org.jleaf.common.CommonExceptionConstants;
  8. import org.jleaf.common.dao.PolicyOUDao;
  9. import org.jleaf.common.entity.PolicyOU;
  10. import org.jleaf.common.entity.UserRole;
  11. import org.jleaf.core.AbstractBusinessFunction;
  12. import org.jleaf.core.BusinessFunction;
  13. import org.jleaf.core.CoreException;
  14. import org.jleaf.core.Dto;
  15. import org.jleaf.core.annotation.ErrorList;
  16. import org.jleaf.core.annotation.Info;
  17. import org.jleaf.core.annotation.InfoIn;
  18. import org.jleaf.core.annotation.InfoOut;
  19. import org.jleaf.core.dao.QueryBuilder;
  20. import org.jleaf.util.ValidationUtil;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23.  
  24. //@formatter:off
  25. @Service
  26. @InfoIn(value = {@Info(name = "userLoginId", description = "user login id", type = Long.class),
  27.                  @Info(name = "roleLoginId", description = "role login id", type = Long.class),
  28.                  @Info(name = "ouId", description = "ou id", type = Long.class),
  29.                  @Info(name = "varName", description = "variable name", type = String.class)})
  30. @InfoOut(value = {})
  31. @ErrorList(errorKeys={CommonExceptionConstants.POLICY_OU_AUTHORIZATION_FAILED})
  32. //@formatter:on
  33. public class ValAuthorizedPolicyOU extends AbstractBusinessFunction implements BusinessFunction {
  34.  
  35.     @Autowired
  36.     PolicyOUDao policyOUDao;
  37.    
  38.     @Override
  39.     public Dto execute(Dto inputDto) throws Exception {
  40.         ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
  41.         ValidationUtil.valDtoContainsKey(inputDto, "roleLoginId");
  42.         ValidationUtil.valDtoContainsKey(inputDto, "ouId");
  43.         ValidationUtil.valDtoContainsKey(inputDto, "varName");
  44.        
  45.         Long userLoginId = inputDto.getLong("userLoginId");
  46.         Long roleLoginId = inputDto.getLong("roleLoginId");
  47.         Long ouId = inputDto.getLong("ouId");
  48.         String varName = inputDto.getString("varName");
  49.        
  50.         QueryBuilder builder = new QueryBuilder();
  51.         builder.add("SELECT COUNT(*) FROM ")
  52.                .add(PolicyOU.TABLE_NAME)
  53.                .add(" a INNER JOIN ")
  54.                .add(UserRole.TABLE_NAME)
  55.                .add(" b ON a.policy_id = b.policy_id ")
  56.                .add("WHERE b.user_id = :userLoginId AND b.role_id = :roleLoginId ")
  57.                .add("AND a.ou_id = :ouId ");
  58.        
  59.         Query query = policyOUDao.createNativeQuery(builder.toString());
  60.         query.setParameter("userLoginId", userLoginId);
  61.         query.setParameter("roleLoginId", roleLoginId);
  62.         query.setParameter("ouId", ouId);
  63.  
  64.         BigInteger count = (BigInteger) query.getSingleResult();
  65.        
  66.         if (count.longValue() == 0) {
  67.             throw new CoreException(CommonExceptionConstants.POLICY_OU_AUTHORIZATION_FAILED, varName);
  68.         }
  69.        
  70.         return null;
  71.     }
  72.  
  73.     @Override
  74.     public String getDescription() {
  75.         return "Validation user and role is authorized for specified ou";
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement