@@ -25,6 +25,7 @@ component threadsafe singleton accessors="true" {
2525 property name = " settings" inject = " coldbox:moduleSettings:cbsecurity" ;
2626 property name = " log" inject = " logbox:logger:{this}" ;
2727 property name = " wirebox" inject = " wirebox" ;
28+ property name = " async" inject = " coldbox:asyncManager" ;
2829 property name = " moduleSettings" inject = " coldbox:setting:modules" ;
2930 property name = " DBLogger" inject = " DBLogger@cbsecurity" ;
3031
@@ -435,7 +436,7 @@ component threadsafe singleton accessors="true" {
435436 * @message The error message to throw in the exception
436437 *
437438 * @throws NoUserLoggedIn
438- * @throws NotAuthorized
439+ * @throws NotAuthorized
439440 */
440441 CBSecurity function secureSameUser ( required user , message = variables .DEFAULT_ERROR_MESSAGE ){
441442 if ( ! sameUser ( arguments .user ) ) {
@@ -470,7 +471,7 @@ component threadsafe singleton accessors="true" {
470471 *
471472 * They receive the currently logged in user and the permissions that where evaluated
472473 *
473- * @permissions One, a list or an array of permissions
474+ * @permissions One, a list, an array of permissions or boolean evaluation
474475 * @success The closure/lambda/udf that executes if the context passes
475476 * @fail The closure/lambda/udf that executes if the context fails
476477 */
@@ -622,6 +623,50 @@ component threadsafe singleton accessors="true" {
622623 return headers .keyExists ( " host" ) ? headers [ " host" ] : " none" ;
623624 }
624625
626+
627+ /**
628+ * Generate a random, secure password using several options
629+ *
630+ * @length The length of the password. Defaults to 32 characters
631+ * @letters Use letters
632+ * @numbers Use numbers
633+ * @symbols Use symbols
634+ *
635+ * @return A secure random password
636+ */
637+ function createPassword (
638+ numeric length = 32 ,
639+ boolean letters = true ,
640+ boolean numbers = true ,
641+ boolean symbols = true
642+ ){
643+ var characters = [];
644+
645+ // cfformat-ignore-start
646+ _when ( arguments .letters , () = > characters .append ( [
647+ ' a' , ' b' , ' c' , ' d' , ' e' , ' f' , ' g' , ' h' , ' i' , ' j' , ' k' ,
648+ ' l' , ' m' , ' n' , ' o' , ' p' , ' q' , ' r' , ' s' , ' t' , ' u' , ' v' ,
649+ ' w' , ' x' , ' y' , ' z' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' , ' G' ,
650+ ' H' , ' I' , ' J' , ' K' , ' L' , ' M' , ' N' , ' O' , ' P' , ' Q' , ' R' ,
651+ ' S' , ' T' , ' U' , ' V' , ' W' , ' X' , ' Y' , ' Z'
652+ ], true ) )
653+ ._when ( arguments .numbers , () = > characters .append ( [
654+ ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9'
655+ ], true ) )
656+ ._when ( arguments .symbols , () = > characters .append ( [
657+ ' ~' , ' !' , ' ## ' , ' $' , ' %' , ' ^' , ' &' , ' *' , ' (' , ' )' , ' -' ,
658+ ' _' , ' .' , ' ,' , ' <' , ' >' , ' ?' , ' /' , ' \' , ' {' , ' }' , ' [' ,
659+ ' ]' , ' |' , ' :' , ' ;'
660+ ], true ) );
661+ // cfformat-ignore-end
662+
663+ return repeatString ( " 1" , arguments .length )
664+ .listToArray ( " " )
665+ .map ( () = > characters [ randRange ( 1 , characters .len () ) ] )
666+ .toList ( " " );
667+ }
668+
669+
625670 /* **************************************************************/
626671 /* Private Methods
627672 /***************************************************************/
@@ -635,4 +680,28 @@ component threadsafe singleton accessors="true" {
635680 return isArray ( arguments .items ) ? items : listToArray ( items );
636681 }
637682
683+ /**
684+ * TODO: Migrate from FlowHelpers once ColdBox 7 goes gold.
685+ * This function evaluates the target boolean expression and if `true` it will execute the `success` closure
686+ * else, if the `failure` closure is passed, it will execute it.
687+ *
688+ * @target The boolean evaluator, this can be a boolean value
689+ * @success The closure/lambda to execute if the boolean value is true
690+ * @failure The closure/lambda to execute if the boolean value is false
691+ *
692+ * @return Returns itself
693+ */
694+ private function _when (
695+ required boolean target ,
696+ required success ,
697+ failure
698+ ){
699+ if ( arguments .target ) {
700+ arguments .success ();
701+ } else if ( ! isNull ( arguments .failure ) ) {
702+ arguments .failure ();
703+ }
704+ return variables ;
705+ }
706+
638707}
0 commit comments