Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipk 256 fix company methods using token as email #58

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/com/MeetMate/company/CompanyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InaccessibleObjectException;

Expand All @@ -28,13 +27,14 @@ public Company getCompany(@Argument long id) {

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
return null;
return new Company(-1, "error", "error");

// if (tc == EntityNotFoundException.class)
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());
//

// if (tc == IllegalArgumentException.class)
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage());
//

// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/com/MeetMate/company/CompanyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.MeetMate.company.sequence.SequenceService;
import com.MeetMate.enums.BusinessType;
import com.MeetMate.enums.UserRole;
import com.MeetMate.security.JwtService;
import com.MeetMate.user.UserController;
import com.MeetMate.user.UserRepository;
import jakarta.persistence.EntityNotFoundException;
Expand All @@ -25,15 +26,9 @@ public class CompanyService {
private final CompanyRepository companyRepository;
private final MongoTemplate mongoTemplate;
private final SequenceService sequenceService;
private final JwtService jwtService;

public Company getCompany(long id) throws IllegalArgumentException {

//Test if user is a company owner
if (userRepository.findUserById(id)
.orElseThrow(() -> new EntityNotFoundException("User not found!"))
.getRole() != UserRole.COMPANY_OWNER)
throw new IllegalArgumentException("User is not a company owner");

return companyRepository.findCompanyById(id)
.orElseThrow(() -> new EntityNotFoundException("Company not found"));
}
Expand Down Expand Up @@ -78,9 +73,8 @@ public void deleteCompany(String token) {
companyRepository.delete(company);
}

private Company getCompanyWithToken(String ownerEmail) throws IllegalArgumentException {

//Test if user is a company owner
private Company getCompanyWithToken(String token) throws IllegalArgumentException {
String ownerEmail = jwtService.extractUserEmail(token);
if (userRepository.findUserByEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("User not found!"))
.getRole() != UserRole.COMPANY_OWNER)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/MeetMate/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
.authorizeHttpRequests(
authorizeRequests ->
authorizeRequests
// .requestMatchers("/api/user/login", "/api/user/signup", "/test/test", "/graphql/**")
.requestMatchers("/**")
.requestMatchers("/api/user/login", "/api/user/signup", "/test/test", "/graphql/**")
// .requestMatchers("/**")
.permitAll() // Whitelist
.anyRequest().authenticated() // Everything else should be authenticated
)
Expand Down
Loading