I am trying to apply a security token using this code snipp from scott but I can't seem to work it out in symfony2, here is my code:
<?php
namespace Acme\UserManagementBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class TokenController extends Controller
{
public function randSecureAction($min, $max) {
$range = $max - $min;
if ($range < 0) return $min; // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return new Response ($min + $rnd);
}
public function getTokenAction($length=32) {
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
for($i=0;$i<$length;$i++) {
$token .= $codeAlphabet[randSecureAction(0,strlen($codeAlphabet))];
}
return new Response ($token);
}
}
I create this TokenController as a service like this so I can call it to my DefaultController, now the service can't call the other functions inside this controller, am I doing it wrong or there is a problem within my code because the function (getTokenAction) inside can't seem to use other function(randSecureAction) in the TokenController class.