package Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . auth . model Ċ Ċ import Ġcom . google . common . collect . ImmutableList Ċ import Ġorg . springframework . data . jpa . domain . Specification Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . security . Auth Source Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . utils . Email Helper . format Ċ import Ġjavax . persistence . criteria . Criteria Builder Ċ import Ġjavax . persistence . criteria . Criteria Query Ċ import Ġjavax . persistence . criteria . Join Type Ċ import Ġjavax . persistence . criteria . Predicate Ċ import Ġjavax . persistence . criteria . Root Ċ Ċ class ĠUser Filter ( ĊĠ Ġname : ĠString ? Ġ= Ġnull , ĊĠ Ġval Ġrole Codes : ĠList < String >? Ġ= Ġnull , ĊĠ Ġval Ġgroup Codes : ĠList < String >? Ġ= Ġnull , ĊĠ Ġval Ġstatus : ĠStatus Ġ= ĠStatus . ALL , ĊĠ Ġval Ġauth Sources : ĠList < Auth Source >? Ġ= Ġnull , Ċ ) Ġ: ĠSpecification < User > Ġ{ ĊĊĠ Ġprivate Ġval Ġname : ĠString ? Ġ= Ġif Ġ( name . isNull Or Blank ()) Ġnull Ġelse Ġname . trim () ĊĊĠ Ġoverride Ġfun Ġto Predicate ( ĊĠĠĠ Ġroot : ĠRoot < User >, ĊĠĠĠ Ġquery : ĠCriteria Query <* >, ĊĠĠĠ Ġcb : ĠCriteria Builder ĊĠ Ġ): ĠPredicate Ġ{ ĊĠĠĠ Ġval Ġand Builder Ġ= ĠImmutableList . builder < Predicate >() ĊĊĠĠĠ Ġ// ĠDefaults Ġto ĠAuth Source . auth Ġwhen Ġno Ġauth Sources Ġprovided ĊĠĠĠ Ġand Builder . add ( build Sources Predicate ( root , Ġcb , Ġauth Sources )) ĊĊĠĠĠ Ġif Ġ(! role Codes . isNull OrEmpty ()) Ġ{ ĊĠĠĠĠĠ Ġval Ġrole Predicate Ġ= ĊĠĠĠĠĠĠĠ Ġroot . join < Any , ĠAny >(" author ities "). get < Any >(" role Code "). ` in `( role Codes . map Ġ{ Ġit . trim (). uppercase () Ġ}) ĊĠĠĠĠĠ Ġand Builder . add ( role Predicate ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġif Ġ(! group Codes . isNull OrEmpty ()) Ġ{ ĊĠĠĠĠĠ Ġval Ġgroup Predicate Ġ= ĊĠĠĠĠĠĠĠ Ġroot . join < Any , ĠAny >(" groups "). get < Any >(" group Code "). ` in `( group Codes . map Ġ{ Ġit . trim (). uppercase () Ġ}) ĊĠĠĠĠĠ Ġand Builder . add ( group Predicate ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġif Ġ(! name . isNull Or Blank ()) Ġ{ ĊĠĠĠĠĠ Ġand Builder . add ( build Name Predicate ( root , Ġcb )) ĊĠĠĠ Ġ} ĊĠĠĠ Ġif Ġ( status Ġ!= ĠStatus . ALL ) Ġ{ ĊĠĠĠĠĠ Ġand Builder . add ( cb . equal ( root . get < Any >(" enabled "), Ġstatus Ġ== ĠStatus . ACTIVE )) ĊĠĠĠ Ġ} ĊĠĠĠ Ġquery . distinct ( true ) ĊĠĠĠ Ġval Ġperson Join Ġ= Ġroot . join < Any , ĠAny >(" person ", ĠJoin Type . INNER ) ĊĠĠĠ Ġquery . orderBy ( cb . asc ( person Join . get < Any >(" firstName ")), Ġcb . asc ( person Join . get < Any >(" lastName "))) ĊĠĠĠ Ġreturn Ġcb . and (* and Builder . build (). to Typed Array ()) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġbuild Sources Predicate ( root : ĠRoot < User >, Ġcb : ĠCriteria Builder , Ġsources : ĠList < Auth Source >? ): ĠPredicate Ġ{ ĊĠĠĠ Ġif Ġ(! sources . isNull OrEmpty ()) Ġ{ ĊĠĠĠĠĠ Ġval Ġor Builder Ġ= ĠImmutableList . builder < Predicate >() ĊĠĠĠĠĠ Ġsources . forEach Ġ{ ĊĠĠĠĠĠĠĠ Ġor Builder . add ( cb . equal ( root . get < Any >(" source "), Ġit )) ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġreturn Ġcb . or (* or Builder . build (). to Typed Array ()) ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġcb . equal ( root . get < Any >(" source "), ĠAuth Source . auth ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġbuild Name Predicate ( root : ĠRoot < User >, Ġcb : ĠCriteria Builder ): ĠPredicate Ġ{ ĊĠĠĠ Ġval Ġor Builder Ġ= ĠImmutableList . builder < Predicate >() ĊĠĠĠ Ġval Ġpattern Ġ= Ġ"% " Ġ+ Ġname !!. replace (',', Ġ' Ġ'). replace (" Ġ[ Ġ] * ". to Regex (), Ġ"% Ġ% ") Ġ+ Ġ"% " ĊĠĠĠ Ġor Builder . add ( cb . like ( root . get (" email "), Ġformat ( pattern ))) ĊĠĠĠ Ġor Builder . add ( cb . like ( root . get (" username "), Ġpattern . uppercase ())) ĊĠĠĠ Ġval Ġperson Join Ġ= Ġroot . join < Any , ĠAny >(" person ", ĠJoin Type . INNER ) ĊĠĠĠ Ġor Builder . add ( ĊĠĠĠĠĠ Ġcb . like ( ĊĠĠĠĠĠĠĠ Ġcb . lower ( cb . concat ( cb . concat ( person Join . get (" firstName "), Ġ" Ġ"), Ġperson Join . get (" lastName ")) ), ĊĠĠĠĠĠĠĠ Ġpattern . lowercase () ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġor Builder . add ( ĊĠĠĠĠĠ Ġcb . like ( ĊĠĠĠĠĠĠĠ Ġcb . lower ( cb . concat ( cb . concat ( person Join . get (" lastName "), Ġ" Ġ"), Ġperson Join . get (" firstName ")) ), ĊĠĠĠĠĠĠĠ Ġpattern . lowercase () ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġreturn Ġcb . or (* or Builder . build (). to Typed Array ()) ĊĠ Ġ} ĊĊĠ Ġenum Ġclass ĠStatus Ġ{ ĊĠĠĠ ĠACT IVE , ĠIN ACTIVE , ĠALL ĊĠ Ġ} Ċ } Ċ