/// <summary> /// OnPostAsync method is invoked after clicking on Log in button. /// </summary> /// <returns>Redirect to Data/Get page or the same page with validation messages.</returns> public async Task <IActionResult> OnPostAsync() { // Log in request to the server via accountService var response = await accountService.Login(LoginCredentials); var messages = new List <Message>(); try { // If response status code if successfull, parse and save token and redirect to get page if (response.IsSuccessStatusCode) { var JWTToken = response.Content.ReadAsStringAsync().Result; // Save token to the session HttpContext.Session.SetString(Constants.SessionJWTKey, JWTToken); var token = AccessHelper.GetTokenFromPageModel(this); return(RedirectToPage("/Data/Get")); } // Otherwise try parse error messages and display them at the create 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); } Messages = messages; return(Page()); }
/// <summary> /// OnPostAsync method is invoked after clicking on Submit button. /// </summary> /// <returns>Redirect to Index page or the same page with validation messages.</returns> public async Task<IActionResult> OnPostAsync() { var messages = new List<Message>(); // Check if file was posted if (FileUpload == null) { messages.Add(new Message(MessageTypeEnum.Error, 0001, new List<string>())); } // Create request to the server via appInitService else { var response = await appInitService.InitializeApplication(Email, FileUpload); try { // If response status code if successfull, parse messages and redirect to login page if (response.IsSuccessStatusCode) { // Set messages to cookie TempData["Messages"] = await response.Content.ReadAsStringAsync(); return RedirectToPage("/Index"); } // Otherwise try parse error messages 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); } } Messages = messages; return Page(); }
/// <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()); }
/// <summary> /// OnPostAsync method is invoked after clicking on Change button. /// </summary> /// <returns>Redirect to the same page with validation messages</returns> public async Task <IActionResult> OnPostAsync(string returnUrl = null) { // 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")); } // Rights 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")); } #region PAGE DATA PREPARATION and INPUT VALIDATION Messages = new List <Message>(); MenuData = AccessHelper.GetMenuData(ApplicationDescriptor, rights); // All passwords must not be null or empty strings if (String.IsNullOrEmpty(PasswordChangeStructure.OldPassword) || String.IsNullOrEmpty(PasswordChangeStructure.NewPassword) || String.IsNullOrEmpty(PasswordChangeStructure.NewPasswordCopy)) { Messages.Add(new Message(MessageTypeEnum.Error, 5001, new List <string>())); return(Page()); } // Both new passwords must be equal if (PasswordChangeStructure.NewPassword != PasswordChangeStructure.NewPasswordCopy) { Messages.Add(new Message(MessageTypeEnum.Error, 5002, new List <string>())); return(Page()); } var response = await accountService.ChangePassword(PasswordChangeStructure, token); try { // If user is not authenticated, redirect to login page if (response.StatusCode == HttpStatusCode.Unauthorized) { return(RedirectToPage("/Index")); } // Otherwise try parse messages and display them at the 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); } #endregion return(Page()); }
/// <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()); }