0

Hey guys i have read and studied the kohana orm and auth modules. so i want to implement am admin section to my website. i get the error above and i have googled but can't seem to find the answer. am using Kohana 3.3.4 so a created a controller called admin:

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

class Controller_Admin extends Controller_Dev
{
    public $template = 'login_template';

    public function action_index()
    {
        if (Auth::instance()->logged_in()) {
            $this->redirect->body('admin/dashboard', 302);
        }
        $this->redirect('admin/login');
    }

    //lets login user
    public function action_login()
    {
        $view = new View('admin_login');
        $this->template->title = "Log in";
        if ($_POST) {
            $user = ORM::factory('user');
            $status = $user->login($_POST);
            if ($status) {
                $this->redirect('admin/dashboard', 302);
            }
            else {
                $errors = $_POST->errors('admin/login');
            }
        }

        // Display the login form
        $this->template->content = $view;
    }

    //lets logout user
    public function action_logout()
    {
        Auth::instance()->logout();
        $this->redirect('admin/login', 302);
    }

    //lets register new users
    public function action_register()
    {
        $view = View::factory('admin_register')
            ->set('values', $_POST)
            ->bind('errors', $errors);
        $this->template->title = "Registration Page";

    if ($_POST)
    {
        $user = ORM::factory('User');
            // The ORM::values() method is a shortcut to assign many values at once

        /* $external_values = array(
            // The unhashed password is needed for comparing to the password_confirm field
            'password' => Arr::get($_POST, 'password'),
        // Add all external values
        ) + Arr::get($_POST, '_external', array());
        $extra = Validation::factory($external_values)
            ->rule('confirm_password', 'matches', array(':validation', ':field', 'password')); */

        try
        {
            //$test = $extra; //Arr::get($_POST, 'password');
            //$view->test = $test;
            $data = $this->request->post();
            $user->register($data);
            // Redirect the user to his page
            $this->redirect('admin/login');
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors('models');
        }
    }

    $this->template->content = $view;
    }

and i created a model called user to help me validate the new user account before save it to the database:

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

class Model_User extends Model_Auth_User {
    //public $_table_name = 'users';

    protected $_has_many = array(
            'user_tokens'   => array('model' => 'user_token'),
            'roles'         => array('model' => 'role', 'through', 'roles_users'),
            // for facbook, google+, twitter and yahoo indentities
            'user_identity' => array(),
        );

    protected $_ignored_columns = array('confirm_password');

    public function rules()
    {
        return array(
            'username' => array(
                array('not_empty'),
                array('min_length', array(':value', 4)),
                array('max_length', array(':value', 32)),
                array(array($this, 'username_available')),
            ),
            'password' => array(
            'not_empty'  => NULL,
            'min_length' => array(5),
            'max_length' => array(42),
        ),
        'password_confirm' => array(
            'matches'    => array('password'),
        ),
        'email' => array(
            'not_empty'  => NULL,
            'min_length' => array(4),
            'max_length' => array(127),
            'email'      => NULL,
        ),
        );
    }

    public function filters()
    {
        return array(
            'password' => array(
                array(array($this, 'hash_password')),
            ),
        );
    }

    public function username_available($username)
    {
        // There are simpler ways to do this, but I will use ORM for the sake of the example
        //return ORM::factory('Member', array('username' => $username))->loaded();
        // Check if the username already exists in the database
        return ! DB::select(array(DB::expr('COUNT(username)'), 'total'))
            ->from('users')
            ->where('username', '=', $username)
            ->execute()
            ->get('total');
    }

    public function hash_password($password)
    {
        // Do something to hash the password
    }

    public function register($array)
    {
        $this->values($array);
        $this->save();
        // Create a new user record in the database


        // Save the new user id to a cookie
        cookie::set('user', $id);

        return $id;
    }
}

When i visit the admin registration page. it fails displaying an error which says:

ErrorException [ Warning ]: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

so please help me out because i think i might be missing something. Thanks in advance guys. Am using Kohana 3.3.4

fusion3k
  • 11,568
  • 4
  • 25
  • 47
Mbengchan
  • 166
  • 3
  • 17
  • Validation rules for Model::$rules and extra Validation they have different syntax. You mix it in Model. `Password` and `email` rules are wrong. – bato3 Mar 21 '16 at 11:48
  • ok @bato3. thanks for the review. can you show me like a better way to achieve the authentication system. – Mbengchan Mar 21 '16 at 12:14
  • I don't know, that my way is better, but i'm using native module: https://kohanaframework.org/3.2/guide/auth + ORM driver: https://kohanaframework.org/3.3/guide-api/Auth_ORM – bato3 Mar 21 '16 at 14:26
  • thanks guy. that was helpful – Mbengchan Mar 21 '16 at 16:19

1 Answers1

0

I had the same error recently. You need to change line:

 array(array($this, 'username_available')),

to line (for username):

 array(array($this, 'unique'), array('username', ':value')),

as stated in https://kohanaframework.org/3.3/guide-api/Model_Auth_User#rules

I hope this helps you.

Ksenia
  • 163
  • 2
  • 17