The Party entity

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.

Creating Parties

You can create create new parties with PartyManager.create factory method which also persist newly created parties:

public Party createJohnGult() {
    Collection<Object> principals = new HashSet<Object>();
    principals.add(new EmailAddress("j.gult@atlasshrugged.com"));
    principals.add(PartyType.PERSON);
    principals.add(ISOGender.MALE);
 
    Party p = PlayAuth.getPartyManager().create("John Gult", principals, "secretword");
    return p;
}

Play Auth comes with a default implementation of PartyManager interface -  DefaultPartyManager.

By default the PartyManager.create factory method will create an instance of ch.insign.playauth.party.support.DefaultParty which can be changed by overriding the method PartyManager.getPartyClass. The PartyManager itself can be overridden by defining custom Dependency Injection bindings.

Retrieving Parties

Retrieve the current party:

PlayAuth.getCurrentParty()

 

Note: Returns null if no party is available.

Here are some examples on how to retrieve parties using PartyManager.find* methods:

public Party findJohnGult() {
    Party p = PlayAuth.getPartyManager().findOneByPrincipal("j.gult@atlasshrugged.com");
    return p;
}
 
/**
 * @param idOrEmail may be: String email or id, Long id, EmailAddress or PartyIdentifier
 */
public Party findByIdOrEmail(Object idOrEmail) {
    Party p = PlayAuth.getPartyManager().findOneByPrincipal(idOrEmail);
    return p;
}
 
public Collection<Party> getAllParties() {
    return PlayAuth.getPartyManager().findAll();
}

 

Removing Parties

Removing party is as simple as invoking PartyManager.delete method:

PlayAuth.getPartyManager().delete(party);

 

Party Role

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.

Creating Roles

You can create create new roles with PartyRoleManager.create factory method which also persist newly created entities:

PartyRole admin = PlayAuth.getPartyRoleManager().create("Administrator");

 

Retrieving Roles

You can retrieve a specific role by its name or identifier, or get all roles at once:

PartyRole admin = PlayAuth.getPartyRoleManager().findByName("Administrator");
Collection<PartyRole> roles = PlayAuth.getPartyRoleManager().findAll();

 

Finding parties that have a role

PlayAuth.getPartyManager().findByRole()

 

Removing Roles

Removing role is as simple as invoking  PartyRoleManager.delete method:

PlayAuth.getPartyRoleManager().delete(admin);

 

Role handling per party

// Add a role to the current party
PlayAuth.getCurrentParty().addRole(someRole);
 
// Remove a role from the current subject
PlayAuth.getCurrentParty().removeRole(someRole);
 
// Check if the current subject has a role assigned
PlayAuth.getCurrentParty().hasRole(someRole);
 
// Get all roles
Collection<PartyRole> myRoles = PlayAuth.getCurrentParty().getRoles();