public async Task <IActionResult> DeleteOrganisation( [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "Organisation/DeleteOrganisation")] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function(DeleteOrganisation) processed a request."); try { // Validate JWT var accessTokenResult = _tokenProvider.ValidateToken(req); if (accessTokenResult.Status != AccessTokenStatus.Valid) { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var userAccountCreateModel = JsonConvert.DeserializeObject <UserAccountCreateModel>(requestBody); // Validate Email/Password var loginManager = new LoginManager(); var loginResult = loginManager.AttemptLogin(userAccountCreateModel.EmailAddress, userAccountCreateModel.Password); if (loginResult.Status != LoginStatus.Success) { return(new BadRequestObjectResult(loginResult.FailureReason)); } Guid userAccountId = new Guid(accessTokenResult.Principal.Claims.First(c => c.Type == "UserAccount").Value); Guid organisationId = new Guid(accessTokenResult.Principal.Claims.First(c => c.Type == "Organisation").Value); // Make sure this UserAccount is the Organisation Owner var organisationMembershipRepository = new OrganisationMembershipRepository(); var organisationMembership = organisationMembershipRepository.GetOrganisationMembership(userAccountId, organisationId); if (organisationMembership.UserType == UserType.OrganisationOwner) { var organisationRepo = new OrganisationRepository(); bool deleted = organisationRepo.DeleteOrganisation(organisationId); return(new OkObjectResult(deleted)); } else { return(new UnauthorizedResult()); } } catch (Exception exception) { return(new BadRequestObjectResult(exception.Message)); } }
public async Task <IActionResult> ChangeActiveOrganisation([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Organisation/ChangeActiveOrganisation")] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function(ChangeActiveOrganisation) processed a request."); try { // Validate JWT var accessTokenResult = _tokenProvider.ValidateToken(req); if (accessTokenResult.Status != AccessTokenStatus.Valid) { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); // not sure if I should check if this organisation exists first? var targetOrganisation = JsonConvert.DeserializeObject <Organisation>(requestBody); Guid userAccountId = new Guid(accessTokenResult.Principal.Claims.First(c => c.Type == "UserAccount").Value); Guid organisationId = new Guid(accessTokenResult.Principal.Claims.First(c => c.Type == "Organisation").Value); // Make sure this user is a member of the organisation var organisationMembershipRepository = new OrganisationMembershipRepository(); var organisationMembership = organisationMembershipRepository.GetOrganisationMembership(userAccountId, organisationId); if (organisationMembership != null) { // return JWT with UserAccountId var jwt = _tokenCreator.CreateToken(userAccountId, targetOrganisation.OrganisationId); return(new OkObjectResult(jwt)); } else { return(new ConflictObjectResult("User is not a member of this Organisation. Please contact support.")); } } catch (Exception exception) { return(new BadRequestObjectResult(exception.Message)); } }