0

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!

1 Answers1

1

I had to override the getUserProvider method. Since I am using Spring for auto-configuration, and not extending ToggleConfig, I added this as a bean to load at startup.

@Bean
public UserProvider getUserProvider() {
        return new UserProvider() {

            @Override
            public FeatureUser getCurrentUser() {
                String username = <MyAppSecurityProvider>.getUserName();
                boolean isAdmin = "admin".equals(username);

                return new SimpleFeatureUser(username, isAdmin);

            }
        };
    }

Note: I had to do this as my app uses our inbuilt security mechanism. Reading the documentation, it looks like its easier if you are using standard security such as Spring or Servlet.

My config in application.yml(the same thing you have in the annotation)

togglz:
  features:
    FRIST_FEATURE:
      enabled: true
      strategy: username
      param:
        users: user1,user2
    SECOND_FEATURE:
      enabled: true
      strategy: username
      param:
        users: user2,user3
Dharman
  • 21,838
  • 18
  • 57
  • 107
JackTrades
  • 11
  • 3