The ch.insign.playauth.party.Party interface is used to represents essential information about people and organizations.
Play Auth has a default implementation of Party interface - DefaultParty. End users may extend this class.
You can store new parties with PartyRepository#save method:
public Party createJohnGult() {
Party johnGult = new DefaultParty();
johnGult.setName("John Gult");
johnGult.setEmail("j.gult@example.org");
johnGult.setGender(ISOGender.MALE);
return partyRepository.save(johnGult);
}
Retrieve the current party:
playAuthApi.getCurrentParty()
This method will return Optional with the party inside.
Here are some examples on how to retrieve parties using PartyManager.find* methods:
public Optional<Party> findJohnGult() {
return partyRepository.findOneByEmail("j.gult@atlasshrugged.com");
}
public Optional<Party> findById(String id) {
return partyRepository.findOneById(id);
}
public Iterable<Party> getAllParties() {
return partyRepository.findAll();
}
Removing party is as simple as invoking PartyRepository#delete method:
partyRepository.delete(party);
The party roles are intended to simply store common patterns of domain permissions, they have no other special meaning, so the application code must NOT rely on the assigned party roles.
You can create create new roles with PartyRoleManager.create factory method which also persist newly created entities:
PartyRole admin = playAuthApi.getPartyRoleManager().create("Administrator");
You can retrieve a specific role by its name or identifier, or get all roles at once:
playAuthApi.getPartyManager().findByRole()
Removing role is as simple as invoking PartyRoleManager.delete method:
playAuthApi.getPartyRoleManager().delete(admin);
// Add a role to the current party
playAuthApi.getCurrentParty().ifPresent(p -> p.addRole(someRole));
// Remove a role from the current subject
playAuthApi.getCurrentParty().ifPresent(p -> p.removeRole(someRole));
// Check if the current subject has a role assigned
playAuthApi.getCurrentParty().map(p -> p.hasRole(someRole)).orElse(false);
// Get all roles
Collection<PartyRole> myRoles = playAuthApi.getCurrentParty().map(Party::getRoles).orElse(Collections.empty());