public async Task <IActionResult> Get() { var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last(); string[] roles = { "User", "Admin", "SchoolAdmin" }; if (RoleService.CheckRoles(token, roles, _userManager)) { var candidacyRepo = new CandidacyRepository(); var detailsRepo = new UserDetailsRepository(); var handler = new JwtSecurityTokenHandler(); var sub = handler.ReadJwtToken(token).Payload.Sub; var details = detailsRepo.GetByUserId(sub); var candidacy = candidacyRepo.GetAll().Last(x => x.OwnerId.Value == details.Id); var credentials = GoogleCredential.FromFile( PathHelper.GetCredentialsPath()); var storage = StorageClient.CreateAsync(credentials); var url = SignedUrlHelper.GenerateV4SignedGetUrl("deep-castle-261418-user-photo-bucket", candidacy.PhotoPath); return(Ok(url)); } return(Unauthorized()); }
public void Add_Normal_Conditionals() { var repo = new CandidacyRepository(); var user = new UserDetailsRepository().GetAll().FirstOrDefault(); var candidacy = new Candidacy("John", "empty description", "empty", user); repo.Add(candidacy); Assert.True(repo.GetAll().Last().Owner != null); }
public void Delete_Normal_Conditions() { var repo = new CandidacyRepository(); var user = new UserDetails(); var userRepo = new UserDetailsRepository(); userRepo.Add(user); var candidacy = new Candidacy("John", "empty description", "empty", user); repo.Add(candidacy); repo.Delete(repo.GetAll().FirstOrDefault()); }
public void Edit_Normal_Conditions() { var repo = new CandidacyRepository(); var user = new UserDetails(); var userRepo = new UserDetailsRepository(); userRepo.Add(user); var candidacy = new Candidacy("John", "empty description", "empty", user); repo.Add(candidacy); var temp = repo.GetAll().FirstOrDefault(); temp.PhotoPath = "foo"; temp.Description = "bar"; repo.Edit(temp); }
public void Edit_PhotoPath_Null() { var repo = new CandidacyRepository(); var user = new UserDetails(); var userRepo = new UserDetailsRepository(); userRepo.Add(user); var candidacy = new Candidacy("John", "empty description", "empty", user); repo.Add(candidacy); var temp = repo.GetAll().FirstOrDefault(); Assert.Throws <ArgumentNullException>(() => { temp.PhotoPath = null; temp.Description = "bar"; repo.Edit(temp); }); }
public async Task <IActionResult> Get() { var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last(); string[] roles = { "User", "Admin", "SchoolAdmin" }; var repo = new CandidacyRepository(); var detailsRepo = new UserDetailsRepository(); //Gets UserId(sub) fro token; var handler = new JwtSecurityTokenHandler(); var sub = handler.ReadJwtToken(token).Payload.Sub; var schoolId = detailsRepo.GetAll().First(x => x.UserId == sub).SchoolId; if (RoleService.CheckRoles(token, roles, _userManager)) { var result = repo.GetAll().Where(x => detailsRepo.GetById(x.OwnerId.GetValueOrDefault()).SchoolId == schoolId).ToList(); return(Ok(result)); } return(Unauthorized()); }
public async Task <IActionResult> Add() { var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last(); string[] roles = { "User" }; var handler = new JwtSecurityTokenHandler(); if (RoleService.CheckRoles(token, roles, _userManager)) { var httpRequest = HttpContext.Request; var file = httpRequest.Body; //checks the size of file var imageHandler = new ImageSecurityHandler(); if (!imageHandler.CheckFileSize(httpRequest.ContentLength.Value)) { _logger.LogInformation($"size is {httpRequest.ContentLength}"); return(BadRequest("Photo must be between 5KB and 5MB")); } //checks the format of file if (!imageHandler.CheckFileFormat(httpRequest.ContentType)) { _logger.LogInformation($"file format is {httpRequest.ContentType}"); return(BadRequest("Wrong file format")); } var sub = handler.ReadJwtToken(token).Payload.Sub; var credentials = GoogleCredential.FromFile("../Infrastructure/Images/GCStorage/Rosta-a2299c0ab851.json"); var storage = StorageClient.CreateAsync(credentials); var lastId = 0; if (storage.Result .ListObjects("deep-castle-261418-user-photo-bucket") .Select(x => x.Name) .Count(x => x.Contains(sub)) > 0) { lastId = int.Parse(storage.Result .ListObjects("deep-castle-261418-user-photo-bucket") .Select(x => x.Name).Last(x => x.Contains(sub)) .Split("-").Last()); } var detailsRepository = new UserDetailsRepository(); var details = detailsRepository.GetByUserId(sub); var candidacyRepository = new CandidacyRepository(); var candidacy = candidacyRepository.GetAll().Last(x => x.OwnerId == details.Id); //Checks if User have candidacy if (candidacyRepository.GetAll().Count(x => x.OwnerId == details.Id) == 0) { return(BadRequest("User didnt submited candidacy.")); } //Uploading Photo to Google Cloud and updating indecies. var photoName = $"{sub}-profilePhoto-{lastId + 1}"; storage.Result.UploadObject("deep-castle-261418-user-photo-bucket", photoName, MediaTypeNames.Image.Jpeg, file, null); candidacy.PhotoPath = photoName; candidacyRepository.Edit(candidacy); return(Ok()); } return(Unauthorized()); }