Togglz - Username Activation Strategy Implementation

88 views
Skip to first unread message

Sugandha Mishra

unread,
Jul 9, 2020, 10:38:27 AM7/9/20
to togglz-users
I am trying to implement UsernameActivation Startegy in Springboot using togglz, but due to insufficient example/documentation on this, I am unable to do so. It is a simple Poc in maven. Here are my classes.



public enum Features implements Feature{
    
     @Label("just a description")
        @EnabledByDefault
        HELLO_WORLD,
        
        @Label("Hello World Feature")
        @DefaultActivationStrategy(id = UsernameActivationStrategy.ID, parameters = 
    {@ActivationParameter(name = UsernameActivationStrategy.PARAM_USERS, value = "suga")        
        })
        HELLO,

        @Label("another descrition")
        @EnabledByDefault
        REVERSE_GREETING;

        public boolean isActive() {
            return FeatureContext.getFeatureManager().isActive(this);
        }

}

@Component
public class Togglz implements TogglzConfig {
    
     public Class<? extends Feature> getFeatureClass() {
            return Features.class;
        }

        public StateRepository getStateRepository() {
            return new FileBasedStateRepository(new File("/tmp/features.properties"));
        }

        

        public UserProvider getUserProvider() {
            return new SpringSecurityUserProvider("ADMIN_ROLE");
        }
        
     

}

I want to use UsernameActivation strategy but i am not sure what more code changes I need to do in order for it to work. I do know that it is somehow related to UserProvider. Also, I am not sure how will it compare the username value and how will it capture the current user value. Any idea around this will be of great help!

Nikhil Talreja

unread,
Jul 10, 2020, 8:25:03 AM7/10/20
to togglz-users
I think you need have your own user provider for this. Something like:

public class MyUserProvider implements UserProvider {

@Override
public FeatureUser getCurrentUser() {
SimpleFeatureUser simpleFeatureUser = new SimpleFeatureUser(
"someUniqueIdentifier", true);
return simpleFeatureUser;
}
}

Using this UserProvider your users will be identified by the identifier you provide in the SimpleFeatureUser constructor. You need to make sure this is unique per user.

Then in the UsernameActivation strategy you can provide a comma seperated list of user identifiers for whom the feature will be enabled.

Regards,
Nikhil
Reply all
Reply to author
Forward
0 new messages