himvins / flow-data-across-js-files

This repo allows you to pass any data from one js file to another.
MIT License
0 stars 0 forks source link

Logic #232

Closed himvins closed 1 day ago

himvins commented 2 months ago

You can implement the function in C# to check whether a given request needs to be processed further or not using the provided JSON configuration. Here's a sample implementation:

using System;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

public class RequestAuthorizationChecker
{
    private JObject configuration;

    public RequestAuthorizationChecker(string jsonConfiguration)
    {
        configuration = JObject.Parse(jsonConfiguration);
    }

    public bool IsRequestAuthorized(string requestPayload)
    {
        JObject request = JObject.Parse(requestPayload);

        // Check if the appId_s matches
        string appId = (string)request["appId_s"];
        if (appId != (string)configuration["appId_s"])
        {
            return false;
        }

        // Check requestType_o
        if (!IsRequestTypeAuthorized((JObject)request["requestType_o"], (JObject)configuration["requestType_o"]))
        {
            return false;
        }

        // Check user_o
        if (!IsUserAuthorized((JObject)request["user_o"], (JObject)configuration["user_o"]))
        {
            return false;
        }

        // Check officeFa_o
        if (!IsOfficeFaAuthorized((JObject)request["officeFa_o"], (JObject)configuration["officeFa_o"]))
        {
            return false;
        }

        // Check managerCode_o
        if (!IsManagerCodeAuthorized((JObject)request["managerCode_o"], (JObject)configuration["managerCode_o"]))
        {
            return false;
        }

        // Check env_oa
        if (!IsEnvAuthorized((JObject)request["env_oa"], (JObject)configuration["env_oa"]))
        {
            return false;
        }

        return true;
    }

    private bool IsRequestTypeAuthorized(JObject requestType, JObject configRequestType)
    {
        // Check if requestType_o is active
        if (!(bool)configRequestType["isConfigActive"])
        {
            return false;
        }

        // Check allowed_sa
        JArray allowed = (JArray)configRequestType["allowed_sa"];
        foreach (var request in requestType)
        {
            if (!allowed.Contains(request.Value.ToString()))
            {
                return false;
            }
        }

        // Check denied_sa
        JArray denied = (JArray)configRequestType["denied_sa"];
        foreach (var request in requestType)
        {
            if (denied.Contains(request.Value.ToString()))
            {
                return false;
            }
        }

        return true;
    }

    private bool IsUserAuthorized(JObject user, JObject configUser)
    {
        // Similar logic as IsRequestTypeAuthorized
        // Implement based on your specific requirements
        return true;
    }

    private bool IsOfficeFaAuthorized(JObject officeFa, JObject configOfficeFa)
    {
        // Similar logic as IsRequestTypeAuthorized
        // Implement based on your specific requirements
        return true;
    }

    private bool IsManagerCodeAuthorized(JObject managerCode, JObject configManagerCode)
    {
        // Similar logic as IsRequestTypeAuthorized
        // Implement based on your specific requirements
        return true;
    }

    private bool IsEnvAuthorized(JObject env, JObject configEnv)
    {
        // Similar logic as IsRequestTypeAuthorized
        // Implement based on your specific requirements
        return true;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string jsonConfiguration = "{ \"appId_s\": \"LYDIA\", \"requestType_o\": { \"allowed_sa\": [ \"CreateTrade\", \"ManageTrade\", \"SubmitTrade\", \"CreateSATPackage\", \"ExecuteTrade\" ], \"denied_sa\": [], \"isConfigActive\": true }, \"user_o\": { \"allowed_sa\": [], \"denied_sa\": [], \"isConfigActive\": false }, \"officeFa_o\": { \"allowed_sa\": [ \"101-571\", \"829-011\" ], \"denied_sa\": [], \"isConfigActive\": false }, \"managerCode_o\": { \"allowed_sa\": [], \"denied_sa\": [], \"isConfigActive\": false }, \"env_oa\": { \"allowed_sa\": [ \"dev\" ], \"denied_sa\": [ \"prod\", \"qa\", \"qape\", \"qa1\", \"qa1pe\" ], \"isConfigActive\": true } }";
        string requestPayload = "{\"appId_s\": \"LYDIA\", \"requestType_o\": { \"type\": \"CreateTrade\" }, \"user_o\": {}, \"officeFa_o\": { \"officeId\": \"101-571\" }, \"managerCode_o\": {}, \"env_oa\": { \"environment\": \"dev\" }}";

        RequestAuthorizationChecker checker = new RequestAuthorizationChecker(jsonConfiguration);
        bool isAuthorized = checker.IsRequestAuthorized(requestPayload);
        Console.WriteLine("Is Request Authorized: " + isAuthorized);
    }
}

This code snippet provides a basic structure for checking whether a request is authorized based on the provided JSON configuration. You'll need to complete the IsUserAuthorized, IsOfficeFaAuthorized, IsManagerCodeAuthorized, and IsEnvAuthorized methods according to your specific authorization logic.

himvins commented 2 months ago

Yes, if you're working in a web environment like JavaScript, you can use the URLSearchParams interface to easily manipulate query parameters. Here's how you can remove a query parameter from a URL in JavaScript:

function removeQueryParam(url, paramToRemove) {
    const urlObj = new URL(url);
    urlObj.searchParams.delete(paramToRemove);
    return urlObj.toString();
}

// Example usage:
const url = "https://example.com/path?param1=value1&param2=value2&param3=value3";
const paramToRemove = "param2";
const newUrl = removeQueryParam(url, paramToRemove);
console.log(newUrl);

This will output:

https://example.com/path?param1=value1&param3=value3

This code creates a URL object from the input URL, deletes the specified query parameter, and returns the updated URL as a string.