codeandpixel/userclass

此包的最新版本(dev-master)没有可用的许可信息。

Userclass旨在使一切变得简单

dev-master 2017-03-03 19:06 UTC

This package is auto-updated.

Last update: 2024-09-15 04:00:14 UTC


README

一个出色的用户管理类,使用(SimplePDO)[https://github.com/bennettstone/SimplePDO] 数据库包装器进行预查询。

为RapidPHP项目提供简单的集成和支持,并且可以很容易地适应其他用途(只需将 $this->database->[dothing] 的调用替换为您首选的方法)。

内置标准注册,以及通过(PHP Oauth API)[http://www.phpclasses.org/package/7700-PHP-Authorize-and-access-APIs-using-OAuth.html](由Manuel Lemos创建)的facebook和google身份验证,并使用PHP的密码哈希算法与password_compatibility库为旧版本的php提供支持。

使用的数据库模式在/db_schemas/users.sql文件夹中提供。

函数和用法

初始化类

该类假设存在数据库连接,因此必须使用数据库实例和任何需要的参数进行初始化...

require_once( 'classes/userClass.php' );
$users = new userClass( SimplePDO::getInstance(), $options );

设置类选项

此类具有许多选项,可以设置以控制路径、oauth密钥和用户表名...

$options = array(

    //require email conf. for normal reg?
    'confirm_users' => false,
    
    //require email conf. for social media auth?
    'confirm_social_users' => false, 
    
    //database table name to use for users
    'users_table' => 'users',  
    
    //Force header() location redirect?           
    'header_redirect' => false,
    
    //Reg with email OR username/email
    'email_only' => true,
    
    //Trailing slash IS important!
    'url' => 'http://yoururl.com/',
    
    //Use PHP 5 >= 5.5.0, PHP 7 password_hash algos
    'use_pwd_hash' => true, 
    
    //Multiplier for password hashing
    'hash_cost_factor' => 10,

    //2 weeks for secure cookie
    'cookie_runtime' => 1209600, 
    
    // important: always put a dot in front of the domain, like ".mydomain.com" see http://stackoverflow.com/q/9618217/1114320
    'cookie_domain' => '.127.0.0.1', 
    
    //Name of cookie to set
    'cookie_name' => 'remember_me', 
    
    //Outbound email "from" email
    'from_email' => 'noreply@yoursite.com', 
    
    //Outbound email "from" name
    'from_name' => 'yoursite', 
    
    //Full URI to where you plan on having the activate_account function
    'email_verification_url' => 'login/verify', 
    
    //Subject line for account activation emails
    'email_verification_subject' => 'account activation for project xy', 
    
    //Password reset URI where verify_pass_reset() will be
    'email_password_reset_url' => 'login/verifypasswordreset', 
    
    //Password reset email subject
    'email_password_reset_subject' => 'password reset for project xy', 
    
    //Logout redirect for page_protect()
    'logout_redirect' => '', 
    
    //Oauth credentials
    'oauth' => array(
        'fb' => array(
            'client_id' => '', 
            'client_secret' => '', 
            'client_redirect' => ''
        ), 
        'google' => array(
            'client_id' => '', 
            'client_secret' => '', 
            'client_redirect' => ''
        )
    ), 
    
    //Email messaging copy
    'message_copy' => array(

        //Registration without confirmation
        'registration' => '',

        //Registration WITH confirmation
        'registration_confirm' => '',

        //Password reset messages
        'password_reset' => '',

    ),
    
    //Copy => variable replacements for message copy above
    'message_replacements' => array(
    
        'email' => 'user_email',

        'password' => 'user_password',
    ),
);

$users = new userClass( SimplePDO::getInstance(), $options );

注册用户

用户注册需要一些字段,其他字段需要添加到register()函数中。

必填字段

  • user_name
  • user_email
  • user_password

对于无效提交返回数组,对于成功提交返回true(布尔值)。

$_POST = array(
    'user_name' => 'booooyahhhh',
    'user_email' => 'you@email.com',  
    'user_password' => '12345678'
);

$reg = $users->register();

//Handle any way preferred...
//Redirect on success...
if( $reg === true )
{
    header( 'Location: http://awesome.com' );
    exit;
}
else
{
    //Loop through the errors
    foreach( $reg['errors'] as $e )
    {
        echo $e;
    }
}

通过Facebook注册/登录用户

由于需要通过社交媒体授权用户的重定向路径,因此您需要做一些额外的检查,首先确保在set_options中设置了'oauth'参数,否则userClass将抛出警告。您可以在(https://developers.facebook.com/apps)创建一个应用。

if( isset( $_GET['facebook_login'] ) || isset( $_GET['code'] ) )
{
    $facebook = $users->facebook_authenticate();
    if( $facebook === true )
    {
        //Do your stuff here
        header( 'Location: http://yoursite.com/loggedinsuccess' );
        exit;
    }
    else
    {
        foreach( $facebook['errors'] as $e )
        {
            echo $e;
        }
    }
}

通过Google注册/登录用户

由于需要通过社交媒体授权用户的重定向路径,因此您需要做一些额外的检查,首先确保在set_options中设置了'oauth'参数,否则userClass将抛出警告。您可以在Google API控制台页面(http://code.google.com/apis/console)创建一个应用,并在API访问选项卡中创建一个新的客户端ID,将client_id设置为客户端ID,将client_secret设置为客户端密钥。回调URL必须是.$this->options['url'],但请确保域名有效且可以通过公共DNS解析。

if( isset( $_GET['google_login'] ) || isset( $_GET['state'] ) )
{
    $google = $users->google_authenticate();
    if( $google === true )
    {
        //Do your stuff here
        header( 'Location: http://yoursite.com/loggedinsuccess' );
        exit;
    }
    else
    {
        foreach( $google['errors'] as $e )
        {
            echo $e;
        }
    }
}

激活用户

如果将'confirm_users'或'confirm_social_users'参数设置为"true",则需要使用"activateaccount()"函数(如果确认选项设置为true,则会自动发送激活邮件)。

仅返回布尔值。

if( isset( $_GET['user'] ) && isset( $_GET['code'] ) )
{
    if( $users->activate_account() )
    {
        header( 'Location: http://yoursite.com/activationsuccess' );
        exit;
    }
    else
    {
        //show some sort of error or other redirect here
    }
}

检查用户是否已登录

使用它来保护仅对已登录用户可见的页面或条件性地显示内容。

if( !$users->logged_in() )
{
    header( 'Location: http://yoursite.com/loggedout' );
    exit;
}

登录用户

与注册类似,需要一些参数

  • user_email
  • user_password

可选字段

  • user_name(如果存在,系统将对其进行用户名的检查)
  • user_remember(设置记住cookie)
  • redirect(如果尚未发送头部,则重定向到任何地方,并且已经设置了'userClass'的'header_redirect'选项)

如果已经发送了标题,或者初始化header()重定向存在问题,并且用户成功登录,则会返回一个数组,您可以根据自己的需求处理。

array( 'success' => true, 'redirect' => $_POST['redirect'] );

否则,成功登录仅返回一个布尔值,您可以根据应用程序的需求进行处理。

$_POST = array(
    'user_email' => 'you@email.com', 
    'user_password' => '12345678', 
    'user_remember' => true, 
    'redirect' => 'http://google.com'
);
$login = $users->login();
if( $login === true )
{
    header( 'Location: http://yoursite.com/loggedin/' );
    exit;
}
else
{
    foreach( $login['errors'] as $e )
    {
        echo $e;
    }
}

注销用户

非常直观,通常有助于在条件语句中添加额外的检查,以确保用户确实已登录。

如果直接将uri传递给logout()函数,则会启动header()重定向;否则返回一个布尔值。

//Manually handle logout
if( isset( $_GET['logout'] ) && $users->logged_in() && $users->logout() )
{
    header( 'Location: '. $users->get_option( 'url' ) );
    exit;
}

//Handle logout with a specific uri
if( isset( $_GET['logout'] ) && $users->logged_in() )
{
    $users->logout( 'http://yoursite.com/loggedout/' );
}

请求密码重置

仅返回布尔值,因此可以在应用程序特定基础上进行处理。

$_POST = array(
    'user_email' => 'you@email.com'
);

$reset = $users->password_reset();
if( $reset === true )
{
    echo 'Please check your email address for a password verification message.';
}
elseif( isset( $reset['errors'] ) )
{
    foreach( $reset['errors'] as $e )
    {
        echo $e;
    }
}
else
{
    echo 'Something broke somewhere';
}

验证密码重置尝试

应位于userClass配置中定义的'email_password_reset_url'参数指定的路径。返回布尔值,并设置$_SESSION['reset_key']参数,然后可以使用该参数作为update_password()函数的验证方法。

$_GET = array(
    'code' => 'longrandomstringofstuffhere'
);
$reset = $users->verify_pass_reset();
if( $reset === true )
{
    //Direct user to password change form
}
elseif( isset( $reset['errors'] ) )
{
    foreach( $reset['errors'] as $e )
    {
        echo $e;
    }
}
else
{
    echo 'Something broke somewhere';
}

更新用户的密码

如果从有效的"verify_pass_reset()"动作设置了会话参数,或者用户当前已登录,则可以使用。如果密码不匹配,则返回布尔值或数组。

$_POST = array(
    'old_password' => 'youroldpassword', 
    'new_password' => 'yournewpassword'
);
$change = $users->update_password();
if( $change === true )
{
    //toss awesome success message
}
elseif( isset( $change['errors'] ) )
{
    foreach( $change['errors'] as $e )
    {
        echo $e;
    }
}
else
{
    echo 'Something broke somewhere';
}

强制保护页面

对于任何尚未登录的用户,启动强制重定向到新页面(如果尚未发送标题)。

在页面上的其他任何输出之前放置此函数调用,以确保只有已登录用户才能访问。

此功能仅当'secure_redirect'参数设置为实际值,或者函数调用中指定了URI时才有效。

//Send user to the default location specific in config
$users->page_protect();

//Send users to a custom location
$users->page_protect( 'http://yoursite.com/unauthorized ');