Skip to content

Commit

Permalink
Merge pull request #58 from Informatik-Projekt-Kurs/IPK-256-Fix-compa…
Browse files Browse the repository at this point in the history
…ny-methods-using-token-as-email

Ipk 256 fix company methods using token as email
  • Loading branch information
Gugi-Games authored Sep 18, 2024
2 parents 85ce1b1 + 16ec6e8 commit ce8d739
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
1 change: 0 additions & 1 deletion 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 Down
11 changes: 6 additions & 5 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,6 +26,7 @@ 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 {
return companyRepository.findCompanyById(id)
Expand All @@ -50,7 +52,7 @@ public void createCompany(String companyName, String ownerEmail, String ownerNam

@Transactional
public void editCompany(String token, String companyName, String description, String businessType) {
String ownerEmail = getCompanyWithOwnerEmail(token).getOwnerEmail();
String ownerEmail = getCompanyWithToken(token).getOwnerEmail();

Query query = new Query(Criteria.where("ownerEmail").is(ownerEmail));
Update update = new Update();
Expand All @@ -62,7 +64,7 @@ public void editCompany(String token, String companyName, String description, St

@Transactional
public void deleteCompany(String token) {
Company company = getCompanyWithOwnerEmail(token);
Company company = getCompanyWithToken(token);
try {
userController.deleteUser(token);
} catch (Throwable t) {
Expand All @@ -71,9 +73,8 @@ public void deleteCompany(String token) {
companyRepository.delete(company);
}

private Company getCompanyWithOwnerEmail(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

0 comments on commit ce8d739

Please sign in to comment.