Esempio n. 1
0
        /// <summary>
        /// OnPostAsync method is invoked after clicking on Submit button.
        /// </summary>
        /// <returns>Redirect to Get page or the same page with validation messages</returns>
        public async Task <IActionResult> OnPostAsync()
        {
            // Authentication
            var token = AccessHelper.GetTokenFromPageModel(this);

            if (token == null)
            {
                return(RedirectToPage("/Index"));
            }

            // Application descriptor
            ApplicationDescriptor = await AccessHelper.GetApplicationDescriptor(cache, accountService, token);

            if (ApplicationDescriptor == null)
            {
                Logger.LogToConsole($"Application descriptor for user with token {token.Value} not found.");
                return(RedirectToPage("/Error"));
            }
            // Active dataset descriptor
            var rights = await AccessHelper.GetUserRights(cache, accountService, token);

            if (rights == null)
            {
                Logger.LogToConsole($"Rights not found for user with token {token.Value}.");
                return(RedirectToPage("/Error"));
            }
            ActiveDatasetDescriptor = AccessHelper.GetActiveDatasetDescriptor(ApplicationDescriptor, rights, DatasetName);
            if (ActiveDatasetDescriptor == null)
            {
                Logger.LogToConsole($"Active dataset descriptor for dataset {DatasetName} and user with token {token.Value} not found.");
                return(RedirectToPage("/Error"));
            }

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(rights, ActiveDatasetDescriptor.Id, RightsEnum.CRU))
            {
                TempData["Messages"] = JsonConvert.SerializeObject(
                    new List <Message>()
                {
                    new Message(MessageTypeEnum.Error,
                                2010,
                                new List <string>()
                    {
                        DatasetName
                    })
                });
                return(RedirectToPage("/Data/Get"));
            }

            // Prepare new data model
            var validationHelper = new ValidationHelper();

            validationHelper.ValidateDataDictionary(DataDictionary, ActiveDatasetDescriptor.Attributes);
            var dataModelToPut = new DataModel()
            {
                Id            = DataId,
                ApplicationId = token.ApplicationId,
                DatasetId     = ActiveDatasetDescriptor.Id,
                Data          = JsonConvert.SerializeObject(DataDictionary)
            };

            // Put request to the server via rightsService
            var response = await dataService.Put(dataModelToPut, token);

            var messages = new List <Message>();

            try
            {
                // If response status code if successfull, parse messages and redirect to get page
                if (response.IsSuccessStatusCode)
                {
                    // Set messages to cookie
                    TempData["Messages"] = await response.Content.ReadAsStringAsync();

                    return(RedirectToPage("/Data/Get"));
                }
                // If user is not authenticated, redirect to login page
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    return(RedirectToPage("/Index"));
                }
                // If user is not authorized, add message
                else if (response.StatusCode == HttpStatusCode.Forbidden)
                {
                    messages.Add(new Message(MessageTypeEnum.Error,
                                             2009,
                                             new List <string>()
                    {
                        DatasetName
                    }));
                }
                // Otherwise try parse error messages and display them at the edit page
                else
                {
                    messages = JsonConvert.DeserializeObject <List <Message> >(await response.Content.ReadAsStringAsync()) ?? throw new JsonSerializationException();
                }
            }
            catch (JsonSerializationException e)
            {
                // In case of JSON parsing error, create server error message
                messages.Add(MessageHepler.Create1007());
                Logger.LogExceptionToConsole(e);
            }

            // Menu data
            MenuData = AccessHelper.GetMenuData(ApplicationDescriptor, rights);
            // Read authorized datasets
            // ReadAuthorizedDatasets = AccessHelper.GetReadAuthorizedDatasets(ApplicationDescriptor, rights);
            // SelectData
            HTMLSelectHelper dlh = new HTMLSelectHelper();

            SelectData = await dlh.FillSelectData(ApplicationDescriptor,
                                                  ActiveDatasetDescriptor.Attributes,
                                                  userService,
                                                  dataService,
                                                  token);

            // Messages
            Messages = messages;

            return(Page());
        }
Esempio n. 2
0
        /// <summary>
        /// This method is used when there is a GET request to Data/Edit.cshtml page
        /// </summary>
        /// <returns>The page.</returns>
        public async Task <IActionResult> OnGetAsync(string datasetName, long id)
        {
            // Authentication
            var token = AccessHelper.GetTokenFromPageModel(this);

            if (token == null)
            {
                return(RedirectToPage("/Index"));
            }

            // Application descriptor
            ApplicationDescriptor = await AccessHelper.GetApplicationDescriptor(cache, accountService, token);

            if (ApplicationDescriptor == null)
            {
                Logger.LogToConsole($"Application descriptor for user with token {token.Value} not found.");
                return(RedirectToPage("/Error"));
            }
            // Active dataset descriptor
            var rights = await AccessHelper.GetUserRights(cache, accountService, token);

            if (rights == null)
            {
                Logger.LogToConsole($"Rights not found for user with token {token.Value}.");
                return(RedirectToPage("/Error"));
            }
            ActiveDatasetDescriptor = AccessHelper.GetActiveDatasetDescriptor(ApplicationDescriptor, rights, datasetName);
            if (ActiveDatasetDescriptor == null)
            {
                Logger.LogToConsole($"Active dataset descriptor for dataset {datasetName} and user with token {token.Value} not found.");
                return(RedirectToPage("/Error"));
            }

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(rights, ActiveDatasetDescriptor.Id, RightsEnum.CRU))
            {
                TempData["Messages"] = JsonConvert.SerializeObject(
                    new List <Message>()
                {
                    new Message(MessageTypeEnum.Error,
                                2010,
                                new List <string>()
                    {
                        datasetName
                    })
                });
                return(RedirectToPage("/Data/Get"));
            }

            #region PAGE DATA PREPARATION

            Messages = new List <Message>();
            MenuData = AccessHelper.GetMenuData(ApplicationDescriptor, rights);
            // ReadAuthorizedDatasets = AccessHelper.GetReadAuthorizedDatasets(ApplicationDescriptor, rights);
            DatasetName    = "";
            DataId         = 0;
            DataDictionary = new Dictionary <string, List <string> >();
            // SelectData
            HTMLSelectHelper dlh = new HTMLSelectHelper();
            SelectData = await dlh.FillSelectData(ApplicationDescriptor,
                                                  ActiveDatasetDescriptor.Attributes,
                                                  userService,
                                                  dataService,
                                                  token);

            // Data request to the server via dataService
            DataModel dataModel;
            var       response = await dataService.GetById(ActiveDatasetDescriptor.Id, id, token);

            try
            {
                // If response status code if successfull, try parse data
                if (response.IsSuccessStatusCode)
                {
                    dataModel = JsonConvert.DeserializeObject <DataModel>(await response.Content.ReadAsStringAsync());
                    // Data dictionary, id and dataset name
                    DatasetName = ActiveDatasetDescriptor.Name;
                    DataId      = dataModel.Id;
                    // Convert Dictionary<string, List<object>> from dataModel to Dictionary<string, List<string>> expected by html page
                    DataDictionary = dataModel.DataDictionary.ToDictionary(k => k.Key, k => k.Value.ConvertAll(x => Convert.ToString(x)));
                }
                // If user is not authenticated, redirect to login page
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    return(RedirectToPage("/Index"));
                }
                // If user is not authorized, add message
                else if (response.StatusCode == HttpStatusCode.Forbidden)
                {
                    Messages.Add(new Message(MessageTypeEnum.Error,
                                             4011,
                                             new List <string>()));
                }
                // Otherwise try parse error messages and display them at the get page
                else
                {
                    // Set messages to cookie
                    TempData["Messages"] = await response.Content.ReadAsStringAsync();

                    return(RedirectToPage("/Data/Get"));
                }
            }
            catch (JsonSerializationException e)
            {
                // In case of JSON parsing error, create server error message
                Messages.Add(MessageHepler.Create1007());
                Logger.LogExceptionToConsole(e);
            }

            #endregion

            return(Page());
        }