Discussions

Ask a Question
Back to All

Subject requires we have a List<? extends Role> but Role is an interface, so that's not possible?

Hi

I've recently started implementing Deadbolt 2 into my Play Framework app. I've implemented nearly everything, including the handler / cache and etc.

It works very well if I manually add a role to a user in the database. However, I cannot edit the roles via code, because I cannot implement any kind of setter as my IDE warns me that the list of roles expects a different kind of object, so I cannot put my custom UserRole object into that list of roles on the Subject.

My User object which extends the Subject has the following fields for the Roles and Permissions:

@Embedded
private List roles;

@Embedded
private List permissions;

These cannot be List<? extends Role> and List<? extends Permission> because of a MongoDB limitation. It needs to know how to map the embedded objects correctly, so I need to be specific.

The getters are things I've overriden from the interface and they are as the interface expects them. Example:

@Override
public List<? extends Role> getRoles() {
return roles;
}

They seem to work fine, since as I mentioned above, adding a role to the database manually works fine, so clearly the getters must be working properly. They are also causing no warnings. The only problem is I cannot add a UserRole or UserPermission via code. Example:

user.getRoles().add(new UserRole("Admin"));

OR

user.setRoles(rolelist);

Both can't be compiled as the IDE complains with the following
add(capture<? extends be.objectify.deadbolt.java.models.Role>) in List cannot be applied to (models.security.permissions.UserRole)

How do I solve this, please?

Kind Regards,

Sam

Admin

You just need to add specific methods to your subject to define the roles, and you can use the actual object type in the method definition. For example, public void addRole(final UserRole role) {roles.add(role)}.

ο»Ώ