0

I am validating a java class using @Pattern annotation from Deadbolt. I want to give access if either of these strings are present : 'TM_TASK' OR 'TM_CHECKLIST'. Right now i am a little confused as to how to provide these strings inside @Pattern so that it gives access if the string matches either of those 2 strings.I am also open to using regex (although i am new to them) as to how to verify if it is any of those 2 strings ('TM_TASK' OR 'TM_CHECKLIST') using Regex and not any other invalid string.

e.g.

@Api(value = "/tasks")
@Pattern(value = "^[a-zA-Z0-9_]*$", patternType = PatternType.REGEX)
public class TaskController extends BaseController {

  private static Logger.ALogger log = Logger.of(TaskController.class);
  private TaskService taskService;
  private FormFactory formFactory;
}
Sourav Bebarta
  • 51
  • 2
  • 10

1 Answers1

0

You can use a standard regex of (TM_TASK|TM_CHECKLIST) to do this.

@Api(value = "/tasks")
@Pattern(value = "^(TM_TASK|TM_CHECKLIST)$", patternType = PatternType.REGEX)
public class TaskController extends BaseController {

  private static Logger.ALogger log = Logger.of(TaskController.class);
  private TaskService taskService;
  private FormFactory formFactory;
}
Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • No the regex does not work. It could not access the controller methods(which is otherwise accessible) even if the user has "TM_CHECKLIST" permission. The same case happens for "TM_TASK" .May be the @Pattern doesn't validate such expressions. Any other solution ? – Sourav Bebarta Aug 29 '17 at 09:04
  • Take a look at [this example](https://github.com/schaloner/deadbolt-2-java-examples/blob/master/app/controllers/PatternController.java#L44). The most likely explanation is the permissions you're returning from `Subject#getPermissions()` don't match your regex. Set a breakpoint in `be.objectify.deadbolt.java.ConstraintLogic#pattern` to see exactly what's happening. – Steve Chaloner Aug 29 '17 at 09:33
  • Ya...now it's working.Actually had forgotten to add the pattern type with the pattern inside the annotation. – Sourav Bebarta Aug 29 '17 at 13:20
  • @SouravBebarta if your question is answered, please mark the reply as accepted to help other people. – Steve Chaloner Aug 30 '17 at 07:44
  • My reputation being less than 15, the post score isn't updated publicly although recorded. – Sourav Bebarta Aug 31 '17 at 10:19
  • Do you have any idea how to form the regex in Pattern annotation in case my requirement is validation of both strings (TM_TASK and TM_CHECKLIST) separately in any order.How to have the AND condition ? – Sourav Bebarta Sep 01 '17 at 09:23
  • Which version of Deadbolt are you using? – Steve Chaloner Sep 01 '17 at 09:48
  • I am using deadbolt 2.6.1 – Sourav Bebarta Sep 01 '17 at 10:27
  • You can use composite constraints to do this. Take a look at https://deadbolt-java.readme.io/docs/composite-constraints – Steve Chaloner Sep 01 '17 at 10:43