forked from MDCPS/DamageAssessment_Backend
		
	user access module with latest dev branch changes
This commit is contained in:
		| @ -1,4 +1,5 @@ | ||||
| using DamageAssesment.Api.Questions.Interfaces; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace DamageAssesment.Api.Questions.Controllers | ||||
| @ -10,16 +11,13 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|  | ||||
|         public QuestionsController(IQuestionsProvider questionsProvider) | ||||
|         { | ||||
|  | ||||
|             this.questionsProvider = questionsProvider; | ||||
|  | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving questions. | ||||
|         /// </summary> | ||||
|  | ||||
|         // get all questions | ||||
|         //get all questions | ||||
|         [Authorize(Roles = "admin,survey,user,report")] | ||||
|         [Route("questions")] | ||||
|         [Route("questions/{language:alpha}")] | ||||
|         [HttpGet] | ||||
| @ -37,6 +35,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving a question by ID. | ||||
|         /// </summary> | ||||
|         [Authorize(Roles = "admin,survey,user,report")] | ||||
|         [Route("questions/{id}/{language:alpha}")] | ||||
|         [Route("questions/{id:int}")] | ||||
|         [HttpGet] | ||||
| @ -55,6 +54,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// GET request for retrieving survey questions based on a survey ID. | ||||
|         /// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages  | ||||
|         /// </summary> | ||||
|         [Authorize(Roles = "admin,survey,user,report")] | ||||
|         [Route("questions/bysurvey/{surveyId:int}")] | ||||
|         [Route("questions/bysurvey/{surveyId:int}/{language:alpha}")] | ||||
|         [HttpGet] | ||||
| @ -71,6 +71,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// PUT request for updating a question (multilingual). | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin")] | ||||
|         [HttpPut("questions")] | ||||
|         public async Task<IActionResult> UpdateQuestion(Models.Question question) | ||||
|         { | ||||
| @ -92,6 +93,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// POST request for creating a new question (multilingual). | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin")] | ||||
|         [HttpPost("questions")] | ||||
|         public async Task<IActionResult> CreateQuestion(Models.Question question) | ||||
|         { | ||||
| @ -110,6 +112,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// DELETE request for deleting a question based on ID. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin")] | ||||
|         [HttpDelete("questions/{id}")] | ||||
|         public async Task<IActionResult> DeleteQuestion(int id) | ||||
|         { | ||||
| @ -125,6 +128,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// GET request for retrieving question categories. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin,user,report")] | ||||
|         [HttpGet("questions/categories")] | ||||
|         [HttpGet("questions/categories/{language:alpha}")] | ||||
|         public async Task<IActionResult> GetQuestionCategoriesAsync(string? language) | ||||
| @ -139,7 +143,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving a question category by ID. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin,report")] | ||||
|         [HttpGet("questions/categories/{id:int}")] | ||||
|         [HttpGet("questions/categories/{id:int}/{language:alpha}")] | ||||
|         public async Task<IActionResult> GetQuestionCategoryAsync(int id,string? language) | ||||
| @ -156,7 +160,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// <summary> | ||||
|         /// PUT request for updating a question category. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin,survey,report")] | ||||
|         [HttpPut("questions/categories")] | ||||
|         public async Task<IActionResult> UpdateQuestionCategory(Models.QuestionCategory questionCategory) | ||||
|         { | ||||
| @ -178,6 +182,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// POST request for creating a new question category. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin")] | ||||
|         [HttpPost("questions/categories")] | ||||
|         public async Task<IActionResult> CreateQuestionCategory(Models.QuestionCategory questionCategory) | ||||
|         { | ||||
| @ -196,6 +201,7 @@ namespace DamageAssesment.Api.Questions.Controllers | ||||
|         /// DELETE request for deleting a question category based on ID. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Authorize(Roles = "admin")] | ||||
|         [HttpDelete("questions/categories/{id}")] | ||||
|         public async Task<IActionResult> DeleteQuestionCategory(int id) | ||||
|         { | ||||
|  | ||||
| @ -12,7 +12,7 @@ | ||||
|         public bool IsRequired { get; set; } | ||||
|         public bool Comment { get; set; } | ||||
|         public bool Key { get; set; } | ||||
|         public int? SurveyId { get; set; } | ||||
|         public int SurveyId { get; set; } | ||||
|         public int CategoryId { get; set; } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,11 +1,33 @@ | ||||
| using DamageAssesment.Api.Questions.Db; | ||||
| using DamageAssesment.Api.Questions.Interfaces; | ||||
| using DamageAssesment.Api.Questions.Providers; | ||||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
| using Microsoft.OpenApi.Models; | ||||
| using System.Reflection; | ||||
| using System.Text; | ||||
|  | ||||
| var builder = WebApplication.CreateBuilder(args); | ||||
|  | ||||
| // Add services to the container. | ||||
| var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey"); | ||||
| builder.Services.AddAuthentication(item => | ||||
| { | ||||
|     item.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | ||||
|     item.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | ||||
| }).AddJwtBearer(item => | ||||
| { | ||||
|     item.RequireHttpsMetadata = true; | ||||
|     item.SaveToken = true; | ||||
|     item.TokenValidationParameters = new TokenValidationParameters() | ||||
|     { | ||||
|         ValidateIssuerSigningKey = true, | ||||
|         IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authkey)), | ||||
|         ValidateIssuer = false, | ||||
|         ValidateAudience = false, | ||||
|         ClockSkew = TimeSpan.Zero | ||||
|     }; | ||||
| }); | ||||
| // Add services to the container. | ||||
|  | ||||
| builder.Services.AddControllers(); | ||||
| @ -17,13 +39,41 @@ builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); | ||||
|  | ||||
| builder.Services.AddEndpointsApiExplorer(); | ||||
| //builder.Services.AddSwaggerGen(); | ||||
| builder.Services.AddSwaggerGen(c => | ||||
| builder.Services.AddSwaggerGen(options => | ||||
| { | ||||
|     // Include XML comments from your assembly | ||||
|     var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||||
|     var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); | ||||
|     c.IncludeXmlComments(xmlPath); | ||||
|     options.IncludeXmlComments(xmlPath); | ||||
|  | ||||
|     OpenApiSecurityScheme securityDefinition = new OpenApiSecurityScheme() | ||||
|     { | ||||
|         Name = "Bearer", | ||||
|         BearerFormat = "JWT", | ||||
|         Scheme = "bearer", | ||||
|         Description = "Specify the authorization token.", | ||||
|         In = ParameterLocation.Header, | ||||
|         Type = SecuritySchemeType.Http, | ||||
|     }; | ||||
|  | ||||
|     options.AddSecurityDefinition("jwt_auth", securityDefinition); | ||||
|  | ||||
|     // Make sure swagger UI requires a Bearer token specified | ||||
|     OpenApiSecurityScheme securityScheme = new OpenApiSecurityScheme() | ||||
|     { | ||||
|         Reference = new OpenApiReference() | ||||
|         { | ||||
|             Id = "jwt_auth", | ||||
|             Type = ReferenceType.SecurityScheme | ||||
|         } | ||||
|     }; | ||||
|     OpenApiSecurityRequirement securityRequirements = new OpenApiSecurityRequirement() | ||||
|     { | ||||
|         {securityScheme, new string[] { }}, | ||||
|     }; | ||||
|     options.AddSecurityRequirement(securityRequirements); | ||||
| }); | ||||
|  | ||||
| builder.Services.AddDbContext<QuestionDbContext>(option => | ||||
| { | ||||
|     option.UseInMemoryDatabase("Questions"); | ||||
| @ -43,7 +93,7 @@ if (app.Environment.IsDevelopment()) | ||||
|         questionProvider.SeedData(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| app.UseAuthentication(); | ||||
| app.UseAuthorization(); | ||||
|  | ||||
| app.MapControllers(); | ||||
|  | ||||
| @ -19,7 +19,7 @@ namespace DamageAssesment.Api.Questions.Providers | ||||
|             this.questionDbContext = questionDbContext; | ||||
|             this.logger = logger; | ||||
|             this.mapper = mapper; | ||||
|            // SeedData(); | ||||
|             // SeedData(); | ||||
|         } | ||||
|  | ||||
|         public void SeedData() | ||||
| @ -31,8 +31,7 @@ namespace DamageAssesment.Api.Questions.Providers | ||||
|                 questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "TextBox" }); | ||||
|                 questionDbContext.SaveChanges(); | ||||
|             } | ||||
|  | ||||
|             if (!questionDbContext.QuestionCategories.Any()) | ||||
|             if (!questionDbContext.QuestionsTranslations.Any()) | ||||
|             { | ||||
|                 questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Flooding", IconLibrary = "https://example.com/images/img1.png" }); | ||||
|                 questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Electrical", IconLibrary = "https://example.com/images/img2.png" }); | ||||
| @ -70,7 +69,7 @@ namespace DamageAssesment.Api.Questions.Providers | ||||
|                 var question4 = new Db.Question() { QuestionTypeId = 1, SurveyId = 2, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 2 }; | ||||
|                 var question5 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = 1 }; | ||||
|                 var question6 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 2 }; | ||||
|                 var question7 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = 3}; | ||||
|                 var question7 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = 3 }; | ||||
|                 var question8 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 4 }; | ||||
|                 var question9 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 1, IsRequired = true, Comment = true, Key = true, CategoryId = 5 }; | ||||
|                 var question10 = new Db.Question() { QuestionTypeId = 1, SurveyId = 3, QuestionNumber = 2, IsRequired = false, Comment = false, Key = true, CategoryId = 1 }; | ||||
| @ -275,7 +274,7 @@ namespace DamageAssesment.Api.Questions.Providers | ||||
|                 if (question != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"{question} customer(s) found"); | ||||
|                     var result = mapper.Map<Db.Question, Models.MultiLanguage>(question);                     | ||||
|                     var result = mapper.Map<Db.Question, Models.MultiLanguage>(question); | ||||
|                     result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(id, language)); | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
| @ -314,7 +313,7 @@ namespace DamageAssesment.Api.Questions.Providers | ||||
|                             CategoryId = item.Id, | ||||
|                             IconLibrary = item.IconLibrary, | ||||
|                             IconName = item.IconName, | ||||
|                             CategoryNames= CreateCategoryMultiLanguageObject(GetCategoryTranslations(item.Id, language)), | ||||
|                             CategoryNames = CreateCategoryMultiLanguageObject(GetCategoryTranslations(item.Id, language)), | ||||
|                             QuestionsText = GetSurveyQuestion(mapper.Map<List<Db.Question>, List<Models.MultiLanguage>>(questions.Where(a => a.CategoryId == item.Id).ToList()), language) | ||||
|                         }); | ||||
|                     } | ||||
| @ -344,7 +343,7 @@ namespace DamageAssesment.Api.Questions.Providers | ||||
|                 questionDbContext.SaveChanges(); | ||||
|                 Question.Id = dbquestion.Id; | ||||
|                 var result = mapper.Map<Db.Question, Models.MultiLanguage>(dbquestion); | ||||
|                 result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id,"")); | ||||
|                 result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, "")); | ||||
|                 return (true, result, null); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user