-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial work for merging whitelist in master * disable postpone assignment option if one or more categories are linked to groups * #487 - remove members + remove groups * #487 - display "linked to group" message on event level only if the group is not linked directly to a category * #487 - remove unnecessary ticket->email map
- Loading branch information
Showing
35 changed files
with
1,831 additions
and
51 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
184 changes: 184 additions & 0 deletions
184
src/main/java/alfio/controller/api/admin/GroupApiController.java
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,184 @@ | ||
/** | ||
* This file is part of alf.io. | ||
* | ||
* alf.io is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* alf.io is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with alf.io. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package alfio.controller.api.admin; | ||
|
||
import alfio.manager.EventManager; | ||
import alfio.manager.GroupManager; | ||
import alfio.manager.user.UserManager; | ||
import alfio.model.group.Group; | ||
import alfio.model.group.LinkedGroup; | ||
import alfio.model.modification.GroupModification; | ||
import alfio.model.modification.LinkedGroupModification; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.security.Principal; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static alfio.util.OptionalWrapper.optionally; | ||
|
||
@RestController | ||
@RequestMapping("/admin/api/group") | ||
@RequiredArgsConstructor | ||
public class GroupApiController { | ||
|
||
private final GroupManager groupManager; | ||
private final UserManager userManager; | ||
private final EventManager eventManager; | ||
|
||
@GetMapping("/for/{organizationId}") | ||
public ResponseEntity<List<Group>> loadAllGroupsForOrganization(@PathVariable("organizationId") int organizationId, Principal principal) { | ||
if(notOwner(principal.getName(), organizationId)) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
return ResponseEntity.ok(groupManager.getAllForOrganization(organizationId)); | ||
} | ||
|
||
@GetMapping("/for/{organizationId}/detail/{listId}") | ||
public ResponseEntity<GroupModification> loadDetail(@PathVariable("organizationId") int organizationId, @PathVariable("listId") int listId, Principal principal) { | ||
if(notOwner(principal.getName(), organizationId)) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
return groupManager.loadComplete(listId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping("/for/{organizationId}/update/{groupId}") | ||
public ResponseEntity<GroupModification> updateGroup(@PathVariable("organizationId") int organizationId, | ||
@PathVariable("groupId") int listId, | ||
@RequestBody GroupModification modification, | ||
Principal principal) { | ||
if(notOwner(principal.getName(), organizationId)) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
return groupManager.update(listId, modification).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping("/for/{organizationId}/new") | ||
public ResponseEntity<Integer> createNew(@PathVariable("organizationId") int organizationId, @RequestBody GroupModification request, Principal principal) { | ||
if(notOwner(principal.getName(), organizationId)) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
if(request.getOrganizationId() != organizationId) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
return ResponseEntity.ok(groupManager.createNew(request)); | ||
} | ||
|
||
@GetMapping("/for/event/{eventName}/all") | ||
public ResponseEntity<List<LinkedGroup>> findLinked(@PathVariable("eventName") String eventName, | ||
Principal principal) { | ||
return eventManager.getOptionalByName(eventName, principal.getName()) | ||
.map(event -> ResponseEntity.ok(groupManager.getLinksForEvent(event.getId()))) | ||
.orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@GetMapping("/for/event/{eventName}") | ||
public ResponseEntity<LinkedGroup> findActiveGroup(@PathVariable("eventName") String eventName, | ||
Principal principal) { | ||
return eventManager.getOptionalByName(eventName, principal.getName()) | ||
.map(event -> { | ||
Optional<LinkedGroup> configuration = groupManager.getLinksForEvent(event.getId()).stream() | ||
.filter(c -> c.getTicketCategoryId() == null) | ||
.findFirst(); | ||
return configuration.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.noContent().build()); | ||
}) | ||
.orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@GetMapping("/for/event/{eventName}/category/{categoryId}") | ||
public ResponseEntity<LinkedGroup> findActiveGroup(@PathVariable("eventName") String eventName, | ||
@PathVariable("categoryId") int categoryId, | ||
Principal principal) { | ||
return eventManager.getOptionalByName(eventName, principal.getName()) | ||
.map(event -> { | ||
Optional<LinkedGroup> configuration = groupManager.findLinks(event.getId(), categoryId) | ||
.stream() | ||
.filter(c -> c.getTicketCategoryId() != null && c.getTicketCategoryId() == categoryId) | ||
.findFirst(); | ||
return configuration.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.noContent().build()); | ||
}) | ||
.orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping("/{groupId}/link") | ||
public ResponseEntity<Integer> linkGroup(@PathVariable("groupId") int groupId, @RequestBody LinkedGroupModification body, Principal principal) { | ||
if(body == null || groupId != body.getGroupId()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
return optionally(() -> eventManager.getSingleEventById(body.getEventId(), principal.getName())) | ||
.map(event -> { | ||
Optional<LinkedGroup> existing = groupManager.getLinksForEvent(event.getId()) | ||
.stream() | ||
.filter(c -> Objects.equals(body.getTicketCategoryId(), c.getTicketCategoryId())) | ||
.findFirst(); | ||
LinkedGroup link; | ||
if(existing.isPresent()) { | ||
link = groupManager.updateLink(existing.get().getId(), body); | ||
} else { | ||
link = groupManager.createLink(groupId, event.getId(), body); | ||
} | ||
return ResponseEntity.ok(link.getId()); | ||
}) | ||
.orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@DeleteMapping("/for/{organizationId}/link/{configurationId}") | ||
public ResponseEntity<String> unlinkGroup(@PathVariable("organizationId") int organizationId, @PathVariable("configurationId") int configurationId, Principal principal) { | ||
if(optionally(() -> userManager.findUserByUsername(principal.getName())).filter(u -> userManager.isOwnerOfOrganization(u, organizationId)).isPresent()) { | ||
groupManager.disableLink(configurationId); | ||
return ResponseEntity.ok("OK"); | ||
} | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
|
||
@DeleteMapping("/for/{organizationId}/id/{groupId}/member/{memberId}") | ||
public ResponseEntity<Boolean> deactivateMember(@PathVariable("groupId") int groupId, | ||
@PathVariable("memberId") int memberId, | ||
@PathVariable("organizationId") int organizationId, | ||
Principal principal) { | ||
if(notOwner(principal.getName(), organizationId) || !groupManager.findById(groupId, organizationId).isPresent()) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
|
||
return ResponseEntity.ok(groupManager.deactivateMembers(Collections.singletonList(memberId), groupId)); | ||
|
||
} | ||
|
||
@DeleteMapping("/for/{organizationId}/id/{groupId}") | ||
public ResponseEntity<Boolean> deactivateGroup(@PathVariable("groupId") int groupId, | ||
@PathVariable("organizationId") int organizationId, | ||
Principal principal) { | ||
if(notOwner(principal.getName(), organizationId) || !groupManager.findById(groupId, organizationId).isPresent()) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
|
||
return ResponseEntity.ok(groupManager.deactivateGroup(groupId)); | ||
} | ||
|
||
private boolean notOwner(String username, int organizationId) { | ||
return !optionally(() -> userManager.findUserByUsername(username)) | ||
.filter(user -> userManager.isOwnerOfOrganization(user, organizationId)) | ||
.isPresent(); | ||
} | ||
|
||
} |
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.