Search in sources :

Example 1 with BusinessException

use of com.wso2telco.core.dbutils.exception.BusinessException in project core-util by WSO2Telco.

the class WSO2PermissionBuilder method build.

/**
 * This will build the permision tree using given users name
 */
public Map<String, Object> build(final String userName) throws BusinessException {
    Map<String, Object> permisionTree = Collections.emptyMap();
    RetunEntitiy retunItem = new RetunEntitiy();
    try {
        UserRoleProsser userRoleRetriever = new UserRoleProsser();
        UIPermissionNode uiPermissionTree = null;
        List<String> currentUserRoleList = userRoleRetriever.getRolesByUserName(userName);
        /**
         * None of the roles are assign for the user
         */
        if (currentUserRoleList.isEmpty()) {
            throw new BusinessException("No roles assigned for user :" + userName);
        }
        for (Iterator<String> iterator = currentUserRoleList.iterator(); iterator.hasNext(); ) {
            String roleName = iterator.next();
            UIPermissionNode rolePermissions = userAdminStub.getRolePermissions(roleName);
            /**
             * if the permission node is empty
             */
            if (rolePermissions == null || rolePermissions.getNodeList() == null) {
                continue;
            }
            /**
             * filter out ui permission only
             */
            Optional<UIPermissionNode> optNode = Arrays.stream(rolePermissions.getNodeList()).filter(rowItem -> rowItem.getDisplayName().equalsIgnoreCase(UserRolePermissionType.UI_PERMISSION.getTObject())).findFirst();
            /**
             * check for existence of node
             */
            if (optNode.isPresent()) {
                uiPermissionTree = optNode.get();
                if (uiPermissionTree.getNodeList() != null && uiPermissionTree.getNodeList().length > 0) {
                    retunItem = popUserRolePermissions(uiPermissionTree.getNodeList());
                    if (retunItem.atLeastOneSelected) {
                        break;
                    }
                } else {
                    /**
                     * if the current role does not contain Ui permission then continue
                     */
                    continue;
                }
            }
        }
        if (retunItem.returnMap.isEmpty()) {
            throw new BusinessException(UserRolePermissionType.UI_PERMISSION.getTObject() + " not assigned for the user :" + userName + " , assigned roles :[ " + StringUtils.join(currentUserRoleList, ",") + "]");
        }
    } catch (RemoteException | UserAdminUserAdminException e) {
        log.error("UIPermission.build", e);
        throw new BusinessException(GenaralError.INTERNAL_SERVER_ERROR);
    }
    if (retunItem.returnMap.isEmpty()) {
        log.warn(" No ui permission tree found for " + userName);
        return Collections.emptyMap();
    } else {
        return retunItem.returnMap;
    }
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) UserRolePermissionType(com.wso2telco.core.userprofile.util.UserRolePermissionType) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) UserAdminStub(org.wso2.carbon.user.mgt.stub.UserAdminStub) HashMap(java.util.HashMap) UserRoleProsser(com.wso2telco.core.userprofile.prosser.UserRoleProsser) APIConstants(org.wso2.carbon.apimgt.impl.APIConstants) HashSet(java.util.HashSet) CarbonUtils(org.wso2.carbon.utils.CarbonUtils) UIPermissionNode(org.wso2.carbon.user.mgt.stub.types.carbon.UIPermissionNode) Map(java.util.Map) UserAdminUserAdminException(org.wso2.carbon.user.mgt.stub.UserAdminUserAdminException) AdminServicePath(com.wso2telco.core.userprofile.util.AdminServicePath) Iterator(java.util.Iterator) Set(java.util.Set) GenaralError(com.wso2telco.core.dbutils.exception.GenaralError) HTTPConstants(org.apache.axis2.transport.http.HTTPConstants) RemoteException(java.rmi.RemoteException) List(java.util.List) HostObjectComponent(org.wso2.carbon.apimgt.hostobjects.internal.HostObjectComponent) BusinessException(com.wso2telco.core.dbutils.exception.BusinessException) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) AxisFault(org.apache.axis2.AxisFault) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) UIPermissionNode(org.wso2.carbon.user.mgt.stub.types.carbon.UIPermissionNode) BusinessException(com.wso2telco.core.dbutils.exception.BusinessException) UserAdminUserAdminException(org.wso2.carbon.user.mgt.stub.UserAdminUserAdminException) UserRoleProsser(com.wso2telco.core.userprofile.prosser.UserRoleProsser) RemoteException(java.rmi.RemoteException)

Example 2 with BusinessException

use of com.wso2telco.core.dbutils.exception.BusinessException in project core-util by WSO2Telco.

the class DbUtils method getConnection.

/**
 * Gets the db connection.
 *
 * @return the db connection
 * @throws SQLException the SQL exception
 */
public Connection getConnection(DataSourceNames dataSourceName) throws BusinessException {
    try {
        if (!dbDataSourceMap.containsKey(dataSourceName)) {
            Context ctx = new InitialContext();
            dbDataSourceMap.put(dataSourceName, (DataSource) ctx.lookup(dataSourceName.jndiName()));
        }
        DataSource dbDatasource = dbDataSourceMap.get(dataSourceName);
        if (dbDatasource != null) {
            log.info(dataSourceName.toString() + " DB Initialize successfully.");
            return dbDatasource.getConnection();
        } else {
            log.info(dataSourceName.toString() + " DB NOT Initialize successfully.");
            return null;
        }
    } catch (NamingException | SQLException e) {
        log.error("", e);
        throw new BusinessException(GenaralError.INTERNAL_SERVER_ERROR);
    }
/*
 * 
        try {} catch (Exception e) {

            log.info("Error while looking up the data source: " + dataSourceName.toString(), e);
            throw e;
        }*/
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) BusinessException(com.wso2telco.core.dbutils.exception.BusinessException) SQLException(java.sql.SQLException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource)

Example 3 with BusinessException

use of com.wso2telco.core.dbutils.exception.BusinessException in project core-util by WSO2Telco.

the class JSessionAuthenticationFilter method isAuthenticated.

@Override
public boolean isAuthenticated(ContainerRequestContext requestContext, Method method, String header) {
    boolean isExpired = false;
    try {
        UserProfileCachable cachable = CacheFactory.getInstance(CacheType.LOCAL).getService();
        String sessionId = header.replace(AuthFilterParam.JSESSION_ID.getTObject(), "");
        isExpired = cachable.isExpired(sessionId);
        if (isExpired) {
            requestContext.abortWith(accessDenied);
            return false;
        }
        UserProfileDTO userProfileDTO = cachable.get(sessionId);
        userName = userProfileDTO.getUserName();
        log.debug("username : " + userName);
    } catch (BusinessException e) {
        requestContext.abortWith(accessDenied);
        return false;
    }
    return true;
}
Also used : BusinessException(com.wso2telco.core.dbutils.exception.BusinessException) UserProfileCachable(com.wso2telco.core.userprofile.cache.UserProfileCachable) UserProfileDTO(com.wso2telco.core.userprofile.dto.UserProfileDTO)

Example 4 with BusinessException

use of com.wso2telco.core.dbutils.exception.BusinessException in project core-util by WSO2Telco.

the class UserManageHealper method getUser.

/**
 * this is for extracting the user from the basic auth string.
 * eg :Basic YWRtaW46YWRtaW4=
 *     return the admin as user
 * @param authHeader
 * @return
 * @throws BusinessException
 */
public String getUser(String authHeader) throws BusinessException {
    /**
     * validate null
     */
    if (authHeader == null) {
        log.debug("Auth header is null : " + authHeader);
        throw new BusinessException(GenaralError.AUTH_HEADER_NULL);
    }
    /**
     * validate auth Header string this need to formated as Basic encodeBase64(userName:password)
     */
    if (!(authHeader.contains("Basic") || authHeader.contains("basic"))) {
        // if Basic missing in the string
        log.debug("keyword Basic is missing in the string : " + authHeader);
        throw new BusinessException(GenaralError.INVALID_AUTH_HEADER);
    }
    if (authHeader.length() <= 5) {
        // if encodeBase64(userName:password) missing in the string
        log.debug("encodeBase64(userName:password) is missing in the string : " + authHeader);
        throw new BusinessException(GenaralError.INVALID_AUTH_HEADER);
    }
    final String credential = authHeader.substring(5, authHeader.length() - 1).trim();
    try {
        /**
         * decode the credential and convert into string
         */
        final String userPwd = new String(Base64.getDecoder().decode(credential), "utf-8");
        String[] userPwdArry = userPwd.split(":");
        /**
         * Split the username:password by :
         */
        if (userPwdArry.length == 2) {
            // get the user name from the array as first index
            return userPwdArry[0];
        } else {
            /**
             *  if length is less than two, that implies error on format
             */
            log.debug("Invalid format of userName:password  in the string : " + authHeader);
            throw new BusinessException(GenaralError.INVALID_AUTH_HEADER);
        }
    } catch (UnsupportedEncodingException e) {
        log.error("invalid Auth header format" + authHeader, e);
        throw new BusinessException(GenaralError.INVALID_AUTH_HEADER);
    }
}
Also used : BusinessException(com.wso2telco.core.dbutils.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

BusinessException (com.wso2telco.core.dbutils.exception.BusinessException)4 GenaralError (com.wso2telco.core.dbutils.exception.GenaralError)1 UserProfileCachable (com.wso2telco.core.userprofile.cache.UserProfileCachable)1 UserProfileDTO (com.wso2telco.core.userprofile.dto.UserProfileDTO)1 UserRoleProsser (com.wso2telco.core.userprofile.prosser.UserRoleProsser)1 AdminServicePath (com.wso2telco.core.userprofile.util.AdminServicePath)1 UserRolePermissionType (com.wso2telco.core.userprofile.util.UserRolePermissionType)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 RemoteException (java.rmi.RemoteException)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Context (javax.naming.Context)1