Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/authentication/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication Kohana身份验证登录_Authentication_Kohana 3.2 - Fatal编程技术网

Authentication Kohana身份验证登录

Authentication Kohana身份验证登录,authentication,kohana-3.2,Authentication,Kohana 3.2,我正在尝试使用kohana auth模块,但出现错误: '调用未定义的方法Model_User::unique_key()' 我使用代码: $auth = Auth::instance(); if ($auth->login($_POST['email'], $_POST['password'])) { echo 'hello, '.$auth->$_POST['username']; } else { echo

我正在尝试使用kohana auth模块,但出现错误: '调用未定义的方法Model_User::unique_key()'

我使用代码:

$auth = Auth::instance();
    if ($auth->login($_POST['email'], $_POST['password']))
    {
        echo 'hello, '.$auth->$_POST['username'];
    }
    else
    {
        echo 'login failed!';
    }
当电子邮件和密码正常时会发生这种情况。
您知道哪里出了问题吗?

您收到的错误似乎是因为您没有为auth设置配置文件。您应该有一个位于/application/config/auth.php的文件。以下是我的一个例子:

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

return array(

    'driver'       => 'ORM',
    'hash_method'  => 'sha256',
    'hash_key'     => 'Somebiglonghaskeyofmixedcharacters102345567709',
    'lifetime'     => 1209600,
    'session_type' => Session::$default,
    'session_key'  => 'auth_user',

    // Username/password combinations for the Auth File driver
    'users' => array(
        // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
    ),
);
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_User extends ORM {

/**
 * A user has many tokens and roles
 *
 * @var array Relationhips
 */
protected $_has_many = array(
    'user_tokens' => array('model' => 'user_token'),
    'roles'       => array('model' => 'role', 'through' => 'roles_users'),
);

/**
 * Rules for the user model. Because the password is _always_ a hash
 * when it's set,you need to run an additional not_empty rule in your controller
 * to make sure you didn't hash an empty string. The password rules
 * should be enforced outside the model or with a model helper method.
 *
 * @return array Rules
 */
public function rules()
{
    return array(
        'username' => array(
            array('not_empty'),
            array('max_length', array(':value', 32)),
            array(array($this, 'unique'), array('username', ':value')),
            array('regex', array(':value', '/^[a-z][a-z0-9]+$/i')),
        ),
        'email' => array(
            array('not_empty'),
            array('email'),
            array(array($this, 'unique'), array('email', ':value')),
        ),
    );
}

/**
 * Filters to run when data is set in this model. The password filter
 * automatically hashes the password when it's set in the model.
 *
 * @return array Filters
 */
public function filters()
{
    return array(
        'password' => array(
            array(array(Auth::instance(), 'hash'))
        )
    );
}

/**
 * Labels for fields in this model
 *
 * @return array Labels
 */
public function labels()
{
    return array();
}

/**
 * Complete the login for a user by incrementing the logins and saving login timestamp
 *
 * @return void
 */
public function complete_login()
{
    if ($this->_loaded)
    {
        // Update the number of logins
        $this->logins = new Database_Expression('logins + 1');

        // Set the last login date
        $this->last_login = time();

        // Save the user
        $this->update();
    }
}

/**
 * Tests if a unique key value exists in the database.
 *
 * @param   mixed    the value to test
 * @param   string   field name
 * @return  boolean
 */
public function unique_key_exists($value, $field = NULL)
{
    if ($field === NULL)
    {
        // Automatically determine field by looking at the value
        $field = $this->unique_key($value);
    }

    return (bool) DB::select(array('COUNT("*")', 'total_count'))
        ->from($this->_table_name)
        ->where($field, '=', $value)
        ->where($this->_primary_key, '!=', $this->pk())
        ->execute($this->_db)
        ->get('total_count');
}

/**
 * Allows a model use both email and username as unique identifiers for login
 *
 * @param   string  unique value
 * @return  string  field name
 */
public function unique_key($value)
{
    return Valid::email($value) ? 'email' : 'username';
}

/**
 * Password validation for plain passwords.
 *
 * @param array $values
 * @return Validation
 */
public static function get_password_validation($values)
{
    return Validation::factory($values)
        ->rule('password', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'not_empty')
        ->rule('password_confirm', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'matches', array(':validation', 'password', ':field'));
}

/**
 * Create a new user
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')->create_user($_POST, array(
 *  'username',
 *  'password',
 *  'email',
 * );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function create_user($values, $expected)
{
    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values)
        ->rule('password', 'not_empty');

    return $this->values($values, $expected)->create($extra_validation);
}

/**
 * Update an existing user
 *
 * [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')
 *  ->where('username', '=', 'kiall')
 *  ->find()
 *  ->update_user($_POST, array(
 *      'username',
 *      'password',
 *      'email',
 *  );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function update_user($values, $expected = NULL)
{
    if (empty($values['password']))
    {
        unset($values['password'], $values['password_confirm']);
    }

    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values);

    return $this->values($values, $expected)->update($extra_validation);
}
} // End Auth User Model
我还发现在application/classes/model/user.php中创建自己的用户模型很有用。以下是我的一个例子:

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

return array(

    'driver'       => 'ORM',
    'hash_method'  => 'sha256',
    'hash_key'     => 'Somebiglonghaskeyofmixedcharacters102345567709',
    'lifetime'     => 1209600,
    'session_type' => Session::$default,
    'session_key'  => 'auth_user',

    // Username/password combinations for the Auth File driver
    'users' => array(
        // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
    ),
);
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_User extends ORM {

/**
 * A user has many tokens and roles
 *
 * @var array Relationhips
 */
protected $_has_many = array(
    'user_tokens' => array('model' => 'user_token'),
    'roles'       => array('model' => 'role', 'through' => 'roles_users'),
);

/**
 * Rules for the user model. Because the password is _always_ a hash
 * when it's set,you need to run an additional not_empty rule in your controller
 * to make sure you didn't hash an empty string. The password rules
 * should be enforced outside the model or with a model helper method.
 *
 * @return array Rules
 */
public function rules()
{
    return array(
        'username' => array(
            array('not_empty'),
            array('max_length', array(':value', 32)),
            array(array($this, 'unique'), array('username', ':value')),
            array('regex', array(':value', '/^[a-z][a-z0-9]+$/i')),
        ),
        'email' => array(
            array('not_empty'),
            array('email'),
            array(array($this, 'unique'), array('email', ':value')),
        ),
    );
}

/**
 * Filters to run when data is set in this model. The password filter
 * automatically hashes the password when it's set in the model.
 *
 * @return array Filters
 */
public function filters()
{
    return array(
        'password' => array(
            array(array(Auth::instance(), 'hash'))
        )
    );
}

/**
 * Labels for fields in this model
 *
 * @return array Labels
 */
public function labels()
{
    return array();
}

/**
 * Complete the login for a user by incrementing the logins and saving login timestamp
 *
 * @return void
 */
public function complete_login()
{
    if ($this->_loaded)
    {
        // Update the number of logins
        $this->logins = new Database_Expression('logins + 1');

        // Set the last login date
        $this->last_login = time();

        // Save the user
        $this->update();
    }
}

/**
 * Tests if a unique key value exists in the database.
 *
 * @param   mixed    the value to test
 * @param   string   field name
 * @return  boolean
 */
public function unique_key_exists($value, $field = NULL)
{
    if ($field === NULL)
    {
        // Automatically determine field by looking at the value
        $field = $this->unique_key($value);
    }

    return (bool) DB::select(array('COUNT("*")', 'total_count'))
        ->from($this->_table_name)
        ->where($field, '=', $value)
        ->where($this->_primary_key, '!=', $this->pk())
        ->execute($this->_db)
        ->get('total_count');
}

/**
 * Allows a model use both email and username as unique identifiers for login
 *
 * @param   string  unique value
 * @return  string  field name
 */
public function unique_key($value)
{
    return Valid::email($value) ? 'email' : 'username';
}

/**
 * Password validation for plain passwords.
 *
 * @param array $values
 * @return Validation
 */
public static function get_password_validation($values)
{
    return Validation::factory($values)
        ->rule('password', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'not_empty')
        ->rule('password_confirm', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'matches', array(':validation', 'password', ':field'));
}

/**
 * Create a new user
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')->create_user($_POST, array(
 *  'username',
 *  'password',
 *  'email',
 * );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function create_user($values, $expected)
{
    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values)
        ->rule('password', 'not_empty');

    return $this->values($values, $expected)->create($extra_validation);
}

/**
 * Update an existing user
 *
 * [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')
 *  ->where('username', '=', 'kiall')
 *  ->find()
 *  ->update_user($_POST, array(
 *      'username',
 *      'password',
 *      'email',
 *  );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function update_user($values, $expected = NULL)
{
    if (empty($values['password']))
    {
        unset($values['password'], $values['password_confirm']);
    }

    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values);

    return $this->values($values, $expected)->update($extra_validation);
}
} // End Auth User Model

您收到的错误似乎是因为您没有为auth设置配置文件。您应该有一个位于/application/config/auth.php的文件。以下是我的一个例子:

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

return array(

    'driver'       => 'ORM',
    'hash_method'  => 'sha256',
    'hash_key'     => 'Somebiglonghaskeyofmixedcharacters102345567709',
    'lifetime'     => 1209600,
    'session_type' => Session::$default,
    'session_key'  => 'auth_user',

    // Username/password combinations for the Auth File driver
    'users' => array(
        // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
    ),
);
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_User extends ORM {

/**
 * A user has many tokens and roles
 *
 * @var array Relationhips
 */
protected $_has_many = array(
    'user_tokens' => array('model' => 'user_token'),
    'roles'       => array('model' => 'role', 'through' => 'roles_users'),
);

/**
 * Rules for the user model. Because the password is _always_ a hash
 * when it's set,you need to run an additional not_empty rule in your controller
 * to make sure you didn't hash an empty string. The password rules
 * should be enforced outside the model or with a model helper method.
 *
 * @return array Rules
 */
public function rules()
{
    return array(
        'username' => array(
            array('not_empty'),
            array('max_length', array(':value', 32)),
            array(array($this, 'unique'), array('username', ':value')),
            array('regex', array(':value', '/^[a-z][a-z0-9]+$/i')),
        ),
        'email' => array(
            array('not_empty'),
            array('email'),
            array(array($this, 'unique'), array('email', ':value')),
        ),
    );
}

/**
 * Filters to run when data is set in this model. The password filter
 * automatically hashes the password when it's set in the model.
 *
 * @return array Filters
 */
public function filters()
{
    return array(
        'password' => array(
            array(array(Auth::instance(), 'hash'))
        )
    );
}

/**
 * Labels for fields in this model
 *
 * @return array Labels
 */
public function labels()
{
    return array();
}

/**
 * Complete the login for a user by incrementing the logins and saving login timestamp
 *
 * @return void
 */
public function complete_login()
{
    if ($this->_loaded)
    {
        // Update the number of logins
        $this->logins = new Database_Expression('logins + 1');

        // Set the last login date
        $this->last_login = time();

        // Save the user
        $this->update();
    }
}

/**
 * Tests if a unique key value exists in the database.
 *
 * @param   mixed    the value to test
 * @param   string   field name
 * @return  boolean
 */
public function unique_key_exists($value, $field = NULL)
{
    if ($field === NULL)
    {
        // Automatically determine field by looking at the value
        $field = $this->unique_key($value);
    }

    return (bool) DB::select(array('COUNT("*")', 'total_count'))
        ->from($this->_table_name)
        ->where($field, '=', $value)
        ->where($this->_primary_key, '!=', $this->pk())
        ->execute($this->_db)
        ->get('total_count');
}

/**
 * Allows a model use both email and username as unique identifiers for login
 *
 * @param   string  unique value
 * @return  string  field name
 */
public function unique_key($value)
{
    return Valid::email($value) ? 'email' : 'username';
}

/**
 * Password validation for plain passwords.
 *
 * @param array $values
 * @return Validation
 */
public static function get_password_validation($values)
{
    return Validation::factory($values)
        ->rule('password', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'not_empty')
        ->rule('password_confirm', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'matches', array(':validation', 'password', ':field'));
}

/**
 * Create a new user
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')->create_user($_POST, array(
 *  'username',
 *  'password',
 *  'email',
 * );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function create_user($values, $expected)
{
    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values)
        ->rule('password', 'not_empty');

    return $this->values($values, $expected)->create($extra_validation);
}

/**
 * Update an existing user
 *
 * [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')
 *  ->where('username', '=', 'kiall')
 *  ->find()
 *  ->update_user($_POST, array(
 *      'username',
 *      'password',
 *      'email',
 *  );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function update_user($values, $expected = NULL)
{
    if (empty($values['password']))
    {
        unset($values['password'], $values['password_confirm']);
    }

    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values);

    return $this->values($values, $expected)->update($extra_validation);
}
} // End Auth User Model
我还发现在application/classes/model/user.php中创建自己的用户模型很有用。以下是我的一个例子:

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

return array(

    'driver'       => 'ORM',
    'hash_method'  => 'sha256',
    'hash_key'     => 'Somebiglonghaskeyofmixedcharacters102345567709',
    'lifetime'     => 1209600,
    'session_type' => Session::$default,
    'session_key'  => 'auth_user',

    // Username/password combinations for the Auth File driver
    'users' => array(
        // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
    ),
);
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_User extends ORM {

/**
 * A user has many tokens and roles
 *
 * @var array Relationhips
 */
protected $_has_many = array(
    'user_tokens' => array('model' => 'user_token'),
    'roles'       => array('model' => 'role', 'through' => 'roles_users'),
);

/**
 * Rules for the user model. Because the password is _always_ a hash
 * when it's set,you need to run an additional not_empty rule in your controller
 * to make sure you didn't hash an empty string. The password rules
 * should be enforced outside the model or with a model helper method.
 *
 * @return array Rules
 */
public function rules()
{
    return array(
        'username' => array(
            array('not_empty'),
            array('max_length', array(':value', 32)),
            array(array($this, 'unique'), array('username', ':value')),
            array('regex', array(':value', '/^[a-z][a-z0-9]+$/i')),
        ),
        'email' => array(
            array('not_empty'),
            array('email'),
            array(array($this, 'unique'), array('email', ':value')),
        ),
    );
}

/**
 * Filters to run when data is set in this model. The password filter
 * automatically hashes the password when it's set in the model.
 *
 * @return array Filters
 */
public function filters()
{
    return array(
        'password' => array(
            array(array(Auth::instance(), 'hash'))
        )
    );
}

/**
 * Labels for fields in this model
 *
 * @return array Labels
 */
public function labels()
{
    return array();
}

/**
 * Complete the login for a user by incrementing the logins and saving login timestamp
 *
 * @return void
 */
public function complete_login()
{
    if ($this->_loaded)
    {
        // Update the number of logins
        $this->logins = new Database_Expression('logins + 1');

        // Set the last login date
        $this->last_login = time();

        // Save the user
        $this->update();
    }
}

/**
 * Tests if a unique key value exists in the database.
 *
 * @param   mixed    the value to test
 * @param   string   field name
 * @return  boolean
 */
public function unique_key_exists($value, $field = NULL)
{
    if ($field === NULL)
    {
        // Automatically determine field by looking at the value
        $field = $this->unique_key($value);
    }

    return (bool) DB::select(array('COUNT("*")', 'total_count'))
        ->from($this->_table_name)
        ->where($field, '=', $value)
        ->where($this->_primary_key, '!=', $this->pk())
        ->execute($this->_db)
        ->get('total_count');
}

/**
 * Allows a model use both email and username as unique identifiers for login
 *
 * @param   string  unique value
 * @return  string  field name
 */
public function unique_key($value)
{
    return Valid::email($value) ? 'email' : 'username';
}

/**
 * Password validation for plain passwords.
 *
 * @param array $values
 * @return Validation
 */
public static function get_password_validation($values)
{
    return Validation::factory($values)
        ->rule('password', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'not_empty')
        ->rule('password_confirm', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'matches', array(':validation', 'password', ':field'));
}

/**
 * Create a new user
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')->create_user($_POST, array(
 *  'username',
 *  'password',
 *  'email',
 * );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function create_user($values, $expected)
{
    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values)
        ->rule('password', 'not_empty');

    return $this->values($values, $expected)->create($extra_validation);
}

/**
 * Update an existing user
 *
 * [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
 *
 * Example usage:
 * ~~~
 * $user = ORM::factory('user')
 *  ->where('username', '=', 'kiall')
 *  ->find()
 *  ->update_user($_POST, array(
 *      'username',
 *      'password',
 *      'email',
 *  );
 * ~~~
 *
 * @param array $values
 * @param array $expected
 * @throws ORM_Validation_Exception
 */
public function update_user($values, $expected = NULL)
{
    if (empty($values['password']))
    {
        unset($values['password'], $values['password_confirm']);
    }

    // Validation for passwords
    $extra_validation = Model_User::get_password_validation($values);

    return $this->values($values, $expected)->update($extra_validation);
}
} // End Auth User Model

我遇到了这个问题,并通过使我的模型扩展
模型验证用户
修复了它。我遇到了这个问题,并通过使我的模型扩展
模型验证用户
修复了它