1
votes

I am a noob to Kohana and was trying to implement the login functionality using Auth ORM. Following is the code that I have written:

/classes/controller/admin.php

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin extends Controller_Default
{
    public function action_index()
    {            
        // Enter a new user manually
        $user = ORM::factory('admin');
        $user->username = 'admin';
        $user->password = 'password';        
        $user->save();

        // Login with this user
        $success = Auth::instance()->login('admin','password','admin');
        if ($success){
            echo "Welcome !";
        }else{
            echo "Not welcome...";
        }
    }

}

/classes/models/admin.php

<?php defined('SYSPATH') or die('No direct access allowed.');


class Model_Admin extends ORM {

    public function save(Validation $validation = NULL)
    {
        $this->salt = uniqid();
        $this->password = Auth::instance()->hash($this->password, $this->salt);
        $this->created = date('Y-m-d');

        parent::save($validation);
    }

}

bootstrap.php

    Kohana::modules(array(
    'auth'       => MODPATH.'auth',       // Basic authentication
    // 'cache'      => MODPATH.'cache',    auth  // Caching with multiple backends
    // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
     'database'   => MODPATH.'database',   // Database access
    // 'image'      => MODPATH.'image',      // Image manipulation
     'orm'        => MODPATH.'orm',        // Object Relationship Mapping
    // 'unittest'   => MODPATH.'unittest',   // Unit testing
    // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
    ));

Cookie::$salt = 'somerandomstring';

Database table structure is as follows: id=>primary key username password salt created

I have

And I am getting a message of "Not welcome..." every time. I am not sure where I am going wrong.

1

1 Answers

0
votes

I see a couple of thinks that may help here

Auth::instance()->login('admin','password','admin');

third parameter should be a boolean, not a string, it enables auto login

http://kohanaframework.org/3.3/guide-api/Auth_ORM#login

By default Kohana Auth use an User Model as the object to authenticate, so I would use

    $user = ORM::factory('User');
    $user->username = 'admin';
    $user->password = 'password';        
    $user->save();

This assuming you are still using a classes/models/user.php instead or classes/models/admin.php, mainly for the pass encryption you are using, but even that is necessary, you can use the following

ORM::factory('user')->create_user($this->request->post(), array(
                'username',
                'password',
                'email'
            ));

http://kohanaframework.org/3.3/guide-api/Model_User#create_user

so you actually don't need create a

classes/models/user.php or

classes/models/admin.php,

unless you need so store that created,

If by some reason you need to extend the user other model , you should look at how to extend it. But I would recommend to look into how roles works, then you can keep the user and assign it an administrator role.

I hope this helps

Regards Andres