-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
336 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package access.teams; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Membership implements Serializable { | ||
|
||
private Person person; | ||
private Role role; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package access.teams; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Person implements Serializable { | ||
|
||
private String urn; | ||
private String name; | ||
private String email; | ||
private String schacHomeOrganization; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package access.teams; | ||
|
||
public enum Role { | ||
|
||
MEMBER, MANAGER, ADMIN, OWNER; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package access.teams; | ||
|
||
import access.model.Application; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Team implements Serializable { | ||
|
||
private String urn; | ||
private String name; | ||
private String description; | ||
private String landingPage; | ||
private List<Membership> memberships; | ||
private List<Application> applications; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package access.teams; | ||
|
||
import access.exception.NotFoundException; | ||
import access.exception.RemoteException; | ||
import access.manage.Manage; | ||
import access.model.Role; | ||
import access.model.*; | ||
import access.provision.ProvisioningService; | ||
import access.provision.scim.GroupURN; | ||
import access.provision.scim.OperationType; | ||
import access.repository.RoleRepository; | ||
import access.repository.UserRepository; | ||
import access.repository.UserRoleRepository; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.rmi.Remote; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
import static access.SwaggerOpenIdConfig.ATTRIBUTE_AGGREGATION_SCHEME_NAME; | ||
|
||
@RestController | ||
@RequestMapping(value = {"/api/teams", "/api/external/v1/teams"}, produces = MediaType.APPLICATION_JSON_VALUE) | ||
@SecurityRequirement(name = ATTRIBUTE_AGGREGATION_SCHEME_NAME) | ||
public class TeamsController { | ||
|
||
private static final int DEFAULT_EXPIRY_DAYS = 5 * 365; | ||
|
||
private final RoleRepository roleRepository; | ||
private final UserRepository userRepository; | ||
private final UserRoleRepository userRoleRepository; | ||
private final Manage manage; | ||
private final ProvisioningService provisioningService; | ||
|
||
public TeamsController(RoleRepository roleRepository, | ||
UserRepository userRepository, | ||
UserRoleRepository userRoleRepository, | ||
Manage manage, | ||
ProvisioningService provisioningService) { | ||
this.roleRepository = roleRepository; | ||
this.userRepository = userRepository; | ||
this.userRoleRepository = userRoleRepository; | ||
this.manage = manage; | ||
this.provisioningService = provisioningService; | ||
} | ||
|
||
@PostMapping("") | ||
@PreAuthorize("hasRole('TEAMS')") | ||
@Transactional | ||
public ResponseEntity<Void> migrateTeam(@RequestBody Team team) { | ||
Role role = new Role(); | ||
role.setName(team.getName()); | ||
role.setShortName(GroupURN.sanitizeRoleShortName(role.getName())); | ||
role.setDescription(team.getDescription()); | ||
role.setUrn(team.getUrn()); | ||
role.setLandingPage(team.getLandingPage()); | ||
role.setDefaultExpiryDays(DEFAULT_EXPIRY_DAYS); | ||
role.setIdentifier(UUID.randomUUID().toString()); | ||
role.setTeamsOrigin(true); | ||
//Check if the applications exist in Manage | ||
Set<Application> applications = team.getApplications().stream().filter(this::applicationExists).collect(Collectors.toSet()); | ||
if (applications.isEmpty()) { | ||
throw new NotFoundException(); | ||
} | ||
role.setApplications(applications); | ||
Role savedRole = roleRepository.save(role); | ||
|
||
provisioningService.newGroupRequest(savedRole); | ||
|
||
List<Membership> memberships = team.getMemberships(); | ||
memberships.forEach(membership -> this.provision(savedRole, membership)); | ||
|
||
return ResponseEntity.status(201).build(); | ||
} | ||
|
||
private boolean applicationExists(Application application) { | ||
try { | ||
manage.providerById(application.getManageType(), application.getManageId()); | ||
return true; | ||
} catch (RuntimeException e) { | ||
return false; | ||
} | ||
} | ||
|
||
private void provision(Role role, Membership membership) { | ||
Person person = membership.getPerson(); | ||
Optional<User> optionalUser = userRepository.findBySubIgnoreCase(person.getUrn()); | ||
User user = optionalUser.orElseGet(() -> { | ||
User newUser = new User(); | ||
newUser.setSub(person.getUrn()); | ||
newUser.setName(person.getName()); | ||
newUser.setEmail(person.getEmail()); | ||
newUser.setSchacHomeOrganization(person.getSchacHomeOrganization()); | ||
return userRepository.save(newUser); | ||
}); | ||
UserRole userRole = new UserRole(); | ||
userRole.setInviter("teams_migration"); | ||
userRole.setUser(user); | ||
userRole.setRole(role); | ||
Instant now = Instant.now(); | ||
userRole.setCreatedAt(now); | ||
userRole.setEndDate(now.plus(DEFAULT_EXPIRY_DAYS, ChronoUnit.DAYS)); | ||
userRole.setAuthority(membership.getRole().equals(access.teams.Role.MEMBER) ? Authority.GUEST : Authority.INVITER); | ||
userRole = userRoleRepository.save(userRole); | ||
|
||
provisioningService.updateGroupRequest(userRole, OperationType.Add); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
server/src/main/resources/db/mysql/migration/V14_0__eppn_nullable.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE `users` MODIFY `eduperson_principal_name` varchar(255) DEFAULT NULL; |
1 change: 1 addition & 0 deletions
1
server/src/main/resources/db/mysql/migration/V15_0__short_name_not_unique.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE `roles` DROP INDEX `roles_unique_short_name`; |
4 changes: 4 additions & 0 deletions
4
server/src/main/resources/db/mysql/migration/V16_0__roles_urn.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ALTER TABLE `roles` | ||
add `urn` varchar(255) DEFAULT NULL; | ||
ALTER TABLE `roles` | ||
add `teams_origin` bool DEFAULT 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.