taeluf/user-gui

一个基于Liaison构建的PHP用户系统

v0.3.x-dev 2024-09-06 14:08 UTC

This package is auto-updated.

Last update: 2024-09-10 21:01:22 UTC


README

用户登录

一个使用 Liaison 构建的用户登录系统,可以插入到任何现有的网站中,无论您是否使用Liaison。

升级通知: v0.3 已弃用。您应使用 v0.4

警告: 目前文档……不完整且相当糟糕。
法律: 您负责条款和条件以及法律事宜!如果您使用此软件,您必须阅读并理解它们。如果您不同意它们或不想使用它们,则必须替换它们。如果我所写的内容在法律上不足以充分,您仍然负有责任,并且我对与您使用此软件、用户隐私等相关的任何法律问题不承担任何责任。

状态:Alpha,功能未完全完成(2022年5月6日)

此库包含用户登录的基本功能:登录、密码重置、注册。它还具有必要的功能,如条款和条件以及安全功能。它有CSRF、XSS预防、蜜罐、节流和安全日志。

目前的缺点是缺乏用户管理、管理和角色/权限的GUI。不幸的是,这必须在代码中完成。尽管后端API使用起来相当简单,所以还算不错。

该库的大部分功能都有稳健的测试。然而,我的CSRF测试是最小的,蜜罐测试不存在(除了在浏览器中尝试外)。

设置有点繁琐。目前文档较差。

我计划添加一些新的cli命令以及方便在您的服务器上进行标准设置的方法。

安装

composer require taeluf/user-gui v0.3.x-dev   

或在您的 composer.json

{"require":{ "taeluf/user-gui": "v0.3.x-dev"}}  

服务器设置

这是一个简单的Liaison服务器设置。

如果您希望在非Liaison网站上使用此功能,则应调用 $liaison->getResponse() 而不是 $liaison->deliver(),从响应中获取内容及头部,并根据您的需要进行处理。

<?php  
  
// the root dir of your server  
$dir = dirname(__DIR__,2);  
  
require($dir.'/vendor/autoload.php');  
// optional if you have other setup to do  
require(__DIR__.'/bootstrap.php');  
  
// $user_lib_dir = $dir.'/vendor/taeluf/user-gui/code/';  
$user_lib_dir = $dir.'/code/';  
  
// initialize Liaison  
$lia = new \Lia();    
$main = \Lia\Package\Server::main($lia);    
  
  
  
// set the base url for the user pages  
$lia->set('user.base_url', '/user/');  
// init the user server  
$user_package = new \Lia\Package\Server($lia, 'user', $user_lib_dir);    
  
// load db settings from env file & make pdo  
$settings = json_decode(file_get_contents(dirname(__DIR__).'/db-env.json'),true);  
$pdo = new \PDO("mysql:dbname=".$settings['db'], $settings['user'], $settings['password']);  
  
// configure the user login library  
$lib = new \Tlf\User\Lib($pdo);  
$lib->config = [  
    'web_address'=>'https://example.com',  
    'email_from'=>'notauser@example.com',  
];  
  
// uncomment this line, run once, then re-comment this line  
// $lib->init_db();  
  
// log the user in  
$current_user = $lib->user_from_cookie();  
// if there was no cookie, we'll use an unregistered user  
if ($current_user===false)$current_user = new \Tlf\User($pdo);  
  
// passes the user & library to user pages (login, register, etc)  
$user_package->public_file_params = [  
    'user'=>$current_user,  
    'lib'=>$lib  
];  
  
   
  
$lia->deliver();  

数据库设置

在上面的交付脚本中取消注释 $lib->init_db() 行,然后运行交付脚本,然后重新注释 init_db

权限和角色

权限可以添加到用户和角色。然后可以将角色添加到用户中,以授予一组权限。请参阅权限源代码code/class/Permissions.php

示例

<?php  
$pdo = $this->pdo();  
$lib = new \Tlf\User\Lib($pdo);  
$user = $lib->user_from_email('reed@document.permissions');  
//user must already be registered!  
// allow a user to create blog posts  
$user->allow('blog:create');  
// allow user to edit a blog post with id = 37  
$user->allow('blog:edit-37');  
$this->is_true( // an assertion with my test lib  
    // check if user can create a blog post  
    $user->can('blog:create')  
);  
// remove permission  
$user->deny('blog:create');  
$this->is_false( // assertion to ensure the permission was removed  
    $user->can('blog:create')  
);  
/////  
// roles  
/////  
// add a role to the user  
$user->add_role('admin');  
// add a permission to the role  
$lib->role_allow('admin', 'blog:delete');  
$this->is_true(  
    $user->can('blog:delete')  
);  
// remove a specific permission from a role  
$lib->role_deny('admin', 'blog:delete');  
// delete a role alltogether  
$lib->role_delete('admin');  

数据库模式

这是文件:code/db/create.sql

CREATE TABLE IF NOT EXISTS `user` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `email` varchar(255) NOT NULL,  
  `password` varchar(255) NOT NULL,  
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  `is_active` tinyint(1) DEFAULT 0,  
  PRIMARY KEY (`id`),  
    UNIQUE(`email`)  
) ;  
  
  
CREATE TABLE IF NOT EXISTS `code` (  
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
    `user_id` int(10) unsigned NOT NULL,  
    `type` varchar(30),  
    `code` varchar(255) NOT NULL,  
    `is_active` tinyint(1) NOT NULL DEFAULT '0',  
    `activated_at` datetime NULL DEFAULT NULL,  
    `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,  
    `expires_at` datetime NOT NULL,  
    PRIMARY KEY (`id`)  
) ;  
  
CREATE TABLE IF NOT EXISTS `permissions` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `user_id` int(10) unsigned NOT NULL,  
  `action` varchar(255) NOT NULL,  
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  PRIMARY KEY (`id`),  
    INDEX(`action`)  
) ;  
  
  
CREATE TABLE IF NOT EXISTS `user_role` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `user_id` int(10) unsigned NOT NULL,  
  `role` varchar(255) NOT NULL,  
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  PRIMARY KEY (`id`),  
    INDEX(`role`)  
) ;  
  
  
CREATE TABLE IF NOT EXISTS `role_permission` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `role` varchar(255) NOT NULL,  
  `action` varchar(255) NOT NULL,  
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  PRIMARY KEY (`id`),  
    INDEX(`role`),  
    INDEX(`action`)  
) ;  
  
  
CREATE TABLE IF NOT EXISTS `throttle` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `key` varchar(255) NOT NULL,  
  `actor` varchar(255) NOT NULL,  
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  `expires_at` timestamp(6) NOT NULL,  
  PRIMARY KEY (`id`)  
) ;  
  
CREATE TABLE IF NOT EXISTS `security_log` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `email` varchar(255) NOT NULL,  
  `action` varchar(255) NOT NULL,  
  `ip` varchar(255) NOT NULL,  
  `user_agent` varchar(500) NOT NULL,  
  `created_at` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),  
  
    PRIMARY KEY(`id`)  
);