Welcome Guest, Not a member yet? Register   Sign In
How to call tank Auth hashing function inside my own controller in CodeIgniter?
#1

[eluser]term25[/eluser]
I use Tank Auth library on my site and it work great from frontend, however I want to be able to manage users from admin side of my website, so I need to be able to create a new user via admin.

I would like to use Tank auth's functions to create the password in the same format as phpass in tank auth is using.

So, after successful validation I need to hash $_POST['password'] using Tank Auth function PasswordHash.

So, now I have this code in my controller:

Code:
$this->load->helper(array('form', 'url'));
         $this->load->library('form_validation');
         $this->load->library('security');
         $this->load->library('tank_auth');
         $this->lang->load('tank_auth');
    
         $password_original = $_POST['password'];
    
         $hasher = new PasswordHash(
          $this->ci->config->item('phpass_hash_strength', 'tank_auth'),
          $this->ci->config->item('phpass_hash_portable', 'tank_auth')
          );
         $hashed_password = $hasher->HashPassword($password);


However I am getting this error message:
Message: Undefined property: Admin::$ci

I guess I have to initialize new instance with &ci; something, but I don't know how.

Thanks in advance for any help or advice how to do it differently.
#2

[eluser]term25[/eluser]
Any advice?
#3

[eluser]LuckyFella73[/eluser]
Quote:Message: Undefined property: Admin::$ci

The error is about:
Code:
$this->ci // undefined property

In your controller the CI superobject can be accessed directly - I guess you took the
code from a library where an instance of the CI superobject was assigned
to $this->ci ?
The config item can be set in your controller like "usual":
Code:
$this->config->set_item('item_name', 'item_value');
#4

[eluser]term25[/eluser]
[quote author="LuckyFella73" date="1339588717"]
Quote:Message: Undefined property: Admin::$ci

The error is about:
Code:
$this->ci // undefined property

In your controller the CI superobject can be accessed directly - I guess you took the
code from a library where an instance of the CI superobject was assigned
to $this->ci ?
The config item can be set in your controller like "usual":
Code:
$this->config->set_item('item_name', 'item_value');
[/quote]

Thanks,you are right, the code is from Tank Auth (auth.php, that comes with this library and is in controllers folder)library.

How can I correct this error?
#5

[eluser]LuckyFella73[/eluser]
Why don't you just take the prebuild interfaces you get with the
tank auth library and adjust the HTML/ CSS to your needs? Would
make it easier for you to integrate that into your admin panel.

If you just need the password create function you could extend
the tank auth library and reuse/extract the code inside the method "create_user"

Code:
// new method in tank_auth library:
public function just_create_password(){ // choose a better name of course ;)
$hasher = new PasswordHash(
     $this->ci->config->item('phpass_hash_strength', 'tank_auth'),
     $this->ci->config->item('phpass_hash_portable', 'tank_auth'));
   $hashed_password = $hasher->HashPassword($password);
return $hashed_password;
}

When loading the tank_auth lib into your controller you should get the hashed password
calling that method then.
#6

[eluser]term25[/eluser]
I have added your code into the file Tank_auth.php in my libraries folder. This library is autoloaded. But I have slightly changed it to look like this:

Code:
// new method in tank_auth library:
public function just_create_password($password){
$hasher = new PasswordHash(
      $this->ci->config->item('phpass_hash_strength', 'tank_auth'),
      $this->ci->config->item('phpass_hash_portable', 'tank_auth'));
    $hashed_password = $hasher->HashPassword($password);
return $hashed_password;
}

The only difference is the parameter in this function:
public function just_create_password($password)

In yours, there the parameter $password was missing.

But when I call the function in my controller like:

Code:
$password = $_POST['password'];

     $hashed_password = just_create_password($password);

     pre_r('$hashed_password');

     die();

So, I can see what is in $hashed_password. Pre_r function is just my function to output everything out.


It throws this error:


Fatal error: Call to undefined function just_create_password()

Any advice how should I call it?

#7

[eluser]LuckyFella73[/eluser]
Sorry I forgot the paramter ..

To call the new method - assuming you placed it in the tank_auth library -
do this:
Code:
$hashed_password = $this->tank_auth->just_create_password($password);
#8

[eluser]term25[/eluser]
Thanks, now it finally works! Smile




Theme © iAndrew 2016 - Forum software by © MyBB