Exemple #1
0
        async Task <MultiTenantParamJson> ProcessMultiTenantParams(ExpandoObject prms)
        {
            var permssionModel = await _dbContext.LoadModelAsync(_host.TenantDataSource, "a2security_tenant.[Permission.LoadMenu]", prms);

            if (permssionModel == null)
            {
                return(null);
            }
            var root = permssionModel.Root;

            if (root == null)
            {
                return(null);
            }

            // current company id
            Int64 currentCompanyId = root.Eval <Int64>("CurrentCompany.Id");

            if (currentCompanyId != 0)
            {
                _userStateManager.SetUserCompanyId(currentCompanyId);
            }

            // get keys and features
            StringBuilder strKeys     = new StringBuilder();
            StringBuilder strFeatures = new StringBuilder();
            var           modules     = root.Eval <List <ExpandoObject> >("Modules");
            var           features    = root.Eval <List <ExpandoObject> >("Features");

            if (modules != null)
            {
                modules.ForEach(m =>
                {
                    var key = m.Eval <String>("Module");
                    if (key != null)
                    {
                        strKeys.Append(key).Append(',');
                    }
                });
                if (strKeys.Length > 0)
                {
                    prms.Set("Keys", strKeys.RemoveTailComma().ToString());
                }
                else
                {
                    prms.Set("Keys", "none");                     // disable all
                }
            }
            if (features != null)
            {
                features.ForEach(f =>
                {
                    var feature = f.Eval <String>("Feature");
                    if (feature != null)
                    {
                        strFeatures.Append(feature).Append(",");
                    }
                });
                if (strFeatures.Length > 0)
                {
                    prms.Set("Features", strFeatures.RemoveTailComma().ToString());
                }
                else
                {
                    prms.Set("Features", "____");                     // all features disabled
                }
            }

            // avaliable companies & xtra links
            var companies = root.Eval <List <ExpandoObject> >("Companies");
            var links     = root.Eval <List <ExpandoObject> >("CompaniesLinks");
            var period    = root.Eval <Object>("Period");

            if (companies != null || period != null)
            {
                String jsonCompanies = JsonConvert.SerializeObject(new { menu = companies, links },
                                                                   JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));
                String jsonPeriod = JsonConvert.SerializeObject(period, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));
                return(new MultiTenantParamJson()
                {
                    Companies = jsonCompanies,
                    Period = jsonPeriod
                });
            }
            return(null);
        }
Exemple #2
0
        public async Task ShellScript2(Action <ExpandoObject> setParams, IUserInfo userInfo, TextWriter writer)
        {
            ExpandoObject loadPrms = new ExpandoObject();

            setParams?.Invoke(loadPrms);

            var     macros        = new ExpandoObject();
            Boolean isUserIsAdmin = userInfo.IsAdmin && _host.IsAdminAppPresent;

            macros.Append(new Dictionary <String, Object>
            {
                { "AppVersion", _host.AppVersion },
                { "Admin", isUserIsAdmin ? "true" : "false" },
                { "TenantAdmin", userInfo.IsTenantAdmin ? "true" : "false" },
                { "ExternalUser", userInfo.IsExternalUser ? "true": "false" },
                { "Debug", IsDebugConfiguration ? "true" : "false" },
                { "AppData", GetAppData() },
                { "Companies", "null" },
                { "Period", "null" },
            });

            if (_host.Mobile)
            {
                loadPrms.Set("Mobile", true);
            }

            IDataModel dm = await _dbContext.LoadModelAsync(_host.TenantDataSource, _host.CustomUserMenu, loadPrms);

            // SetUserStateFromData(dm);
            // ALWAYS from CATALOG
            IDataModel stateModel = await _dbContext.LoadModelAsync(_host.CatalogDataSource, "[a2security].[UserStateInfo.Load]", loadPrms);

            SetUserStateFromData(stateModel);
            // and SetUserState to MENU
            dm.Root.Set("UserState", stateModel.Root.Get <ExpandoObject>("UserState"));


            ExpandoObject menuRoot = dm.Root.RemoveEmptyArrays();

            var companies = menuRoot.Eval <List <ExpandoObject> >("Companies");
            var links     = menuRoot.Eval <List <ExpandoObject> >("CompaniesLinks");
            var period    = menuRoot.Eval <Object>("Period");

            if (companies != null)
            {
                String jsonCompanies = JsonConvert.SerializeObject(new { menu = companies, links },
                                                                   JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));
                var currComp = companies?.Find(c => c.Get <Boolean>("Current"));
                if (currComp == null)
                {
                    throw new InvalidDataException("There is no current company");
                }
                _userStateManager.SetUserCompanyId(currComp.Get <Int64>("Id"));
                macros.Set("Companies", jsonCompanies);
            }
            if (period != null)
            {
                String jsonPeriod = JsonConvert.SerializeObject(period, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));
                macros.Set("Period", jsonPeriod);
            }
            var perm = menuRoot.Eval <List <ExpandoObject> >("Permissions");

            SetUserStatePermission(perm, userInfo.IsAdmin);

            menuRoot.RemoveKeys("Companies,CompaniesLinks,Period");
            String jsonMenu = JsonConvert.SerializeObject(menuRoot, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));

            macros.Set("Menu", jsonMenu);

            writer.Write(Resources.shell.ResolveMacros(macros));
        }
Exemple #3
0
        public async Task ShellScript(String dataSource, Action <ExpandoObject> setParams, IUserInfo userInfo, Boolean bAdmin, TextWriter writer)
        {
            if (!String.IsNullOrEmpty(_host.CustomLayout))
            {
                var customScript = await _host.ApplicationReader.ReadTextFileAsync("_layout", $"{_host.CustomLayout}.js");

                if (customScript == null)
                {
                    throw new RequestModelException($"File not found. [{_host.AppKey}/_layout/{_host.CustomLayout}.js]");
                }
                await writer.WriteAsync(customScript);
            }

            if (!bAdmin && _host.CustomUserMenu != null)
            {
                await ShellScript2(setParams, userInfo, writer);

                return;
            }
            String shell = bAdmin ? Resources.shellAdmin : Resources.shell;

            ExpandoObject loadPrms = new ExpandoObject();

            setParams?.Invoke(loadPrms);

            var     macros        = new ExpandoObject();
            Boolean isUserIsAdmin = userInfo.IsAdmin && _host.IsAdminAppPresent;

            macros.Append(new Dictionary <String, Object>
            {
                { "AppVersion", _host.AppVersion },
                { "Admin", isUserIsAdmin ? "true" : "false" },
                { "TenantAdmin", userInfo.IsTenantAdmin ? "true" : "false" },
                { "ExternalUser", userInfo.IsExternalUser ? "true": "false" },
                { "Debug", IsDebugConfiguration ? "true" : "false" },
                { "AppData", GetAppData() },
                { "Companies", "null" },
                { "Period", "null" },
            });

            Boolean setCompany = false;

            if (_host.IsMultiTenant || _host.IsUsePeriodAndCompanies)
            {
                // for all users (include features)
                var res = await ProcessMultiTenantParams(loadPrms);

                if (res != null)
                {
                    if (res.Companies != null)
                    {
                        macros.Set("Companies", res.Companies);
                    }
                    if (res.Period != null)
                    {
                        macros.Set("Period", res.Period);
                    }
                }
            }
            else if (_host.IsMultiCompany && !bAdmin)
            {
                setCompany = true;
            }

            if (_host.Mobile)
            {
                loadPrms.Set("Mobile", true);
            }

            if (_host.IsMultiTenant)
            {
                // TODO: Create Multi Tenant Load Menus....
            }
            String     proc = bAdmin ? "a2admin.[Menu.Admin.Load]" : "a2ui.[Menu.User.Load]";
            IDataModel dm   = await _dbContext.LoadModelAsync(dataSource, proc, loadPrms);

            ExpandoObject menuRoot = dm.Root.RemoveEmptyArrays();

            SetUserStateFromData(dm);

            String jsonMenu = JsonConvert.SerializeObject(menuRoot, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));

            macros.Set("Menu", jsonMenu);

            if (setCompany)
            {
                var comps    = dm.Root.Get <List <ExpandoObject> >("Companies");
                var currComp = comps?.Find(c => c.Get <Boolean>("Current"));

                if (currComp == null)
                {
                    throw new InvalidDataException("There is no current company");
                }

                var menuJson = JsonConvert.SerializeObject(comps, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration));
                macros.Set("Companies", $"{{menu:{menuJson}, links:null}}");

                _userStateManager.SetUserCompanyId(currComp.Get <Int64>("Id"));
            }

            writer.Write(shell.ResolveMacros(macros));
        }
Exemple #4
0
        public async Task SaveFiles(String pathInfo, HttpFileCollectionBase files, Action <ExpandoObject> setParams, TextWriter writer)
        {
            var rm = await RequestModel.CreateFromBaseUrl(_host, pathInfo);

            ExpandoObject prms = new ExpandoObject();
            var           ru   = rm.GetFile();


            ExpandoObject savePrms = new ExpandoObject();

            setParams?.Invoke(savePrms);
            savePrms.AppendIfNotExists(ru.parameters);

            ru.CheckPermissions(_userStateManager?.GetUserPermissions(), _host.IsDebugConfiguration);

            switch (ru.type)
            {
            case RequestFileType.clr:
            {
                if (String.IsNullOrEmpty(ru.clrType))
                {
                    throw new RequestModelException($"'clrType' is required for '{rm.ModelFile}' file");
                }
                savePrms.Set("Id", ru.Id);
                savePrms.Set("Stream", files[0].InputStream);
                savePrms.Set("FileName", files[0].FileName);
                var result = await DoUploadClr(ru, savePrms);

                writer.Write(JsonConvert.SerializeObject(result, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration)));
            }
            break;

            case RequestFileType.parse:
            {
                savePrms.Set("Id", ru.Id);
                ExpandoObject dm       = null;
                var           fileName = Path.GetFileName(files[0].FileName);
                switch (ru.parse)
                {
                case RequestFileParseType.auto:
                    var ext = Path.GetExtension(files[0].FileName).ToLowerInvariant();
                    switch (ext)
                    {
                    case ".xlsx":
                        dm = await SaveExcel(ru, files[0].InputStream, savePrms, fileName);

                        break;

                    case ".csv":
                        dm = await SaveFlat("csv", ru, files[0].InputStream, savePrms);

                        break;

                    case ".dbf":
                        dm = await SaveFlat("dbf", ru, files[0].InputStream, savePrms);

                        break;

                    case ".xml":
                        dm = await SaveFlat("xml", ru, files[0].InputStream, savePrms);

                        break;

                    case ".json":
                        dm = await SaveJson(ru, files[0].InputStream, savePrms);

                        break;

                    default:
                        throw new RequestModelException($"'{ext}' file not yet supported");
                    }
                    break;

                case RequestFileParseType.excel:
                case RequestFileParseType.xlsx:
                    dm = await SaveExcel(ru, files[0].InputStream, savePrms, fileName);

                    break;

                case RequestFileParseType.csv:
                    dm = await SaveFlat("csv", ru, files[0].InputStream, savePrms);

                    break;

                case RequestFileParseType.dbf:
                    dm = await SaveFlat("dbf", ru, files[0].InputStream, savePrms);

                    break;

                case RequestFileParseType.xml:
                    dm = await SaveFlat("xml", ru, files[0].InputStream, savePrms);

                    break;

                case RequestFileParseType.json:
                    dm = await SaveJson(ru, files[0].InputStream, savePrms);

                    break;
                }
                if (dm != null)
                {
                    WriteExpandoObject(dm, writer);
                }
            }
            break;

            case RequestFileType.sql:
            {
                savePrms.Set("Id", ru.Id);
                var result = await SaveFilesSql(ru, savePrms, files);

                writer.Write(JsonConvert.SerializeObject(result, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration)));
            }
            break;

            case RequestFileType.azureBlob:
            {
                savePrms.Set("Id", ru.Id);
                var result = await SaveFilesAzureStorage(ru, savePrms, files);

                writer.Write(JsonConvert.SerializeObject(result, JsonHelpers.ConfigSerializerSettings(_host.IsDebugConfiguration)));
            }
            break;
            }
        }