经过几小时的代码奋战,终于将FleaPHP的Rdbc功能搞定,非常开心,下面贴出成功权限控制的代码:
1、index.php
define('APP_DIR',dirname(__FILE__) .'/APP');
define('DEBUG_MODE',true);
require('FLEA/FLEA.php');
FLEA::setAppInf('RBACSessionKey','user');
set_app_inf('dispatcher', 'FLEA_Dispatcher_Auth');
FLEA::loadAppInf(APP_DIR .'/Config/DSN.php');
FLEA::import(APP_DIR);
FLEA::loadAppInf(dirname(__FILE__).'/include/function.php'); //加载一个应用程序
FLEA::loadAppInf(dirname(__FILE__) .'/include/Benchmark/Timer.php');
FLEA::setAppInf('dispatcherFailedCallback','displayFailedOtherCallback'); //自定义 无操作函数
FLEA::runMVC();
2、登陆函数
load_class('FLEA_Com_RBAC_RolesManager');
class MyRolesManager extends FLEA_Com_RBAC_RolesManager
{
var $tableName = 'roles';
var $primaryKey = 'role_id';
}
load_class('FLEA_Com_RBAC_UsersManager');
class MyUsersManager extends FLEA_Com_RBAC_UsersManager
{
var $tableName = 'users';
var $primaryKey = 'user_id';
var $manyToMany = array(
'tableClass' => 'MyRolesManager',
'mappingName' => 'roles',
'joinTable' => 'roles_users',
);
}
/**
* 处理用户登录
*/
function login($username, $password) {
$usersManager =& get_singleton('MyUsersManager');
/* @var $usersManager MyUsersManager */
// 验证用户名和密码是否正确
$user = $usersManager->findByUsername($username);
if (!$user || !$usersManager->checkPassword($password, $user[$usersManager->passwordField])) {
error_message("用户名或密码错!");
}
// 获取用户角色信息
$roles = $usersManager->fetchRoles($user);
// 获得 FLEA_Com_RBAC 组件实例
$rbac =& get_singleton('FLEA_Com_RBAC');
/* @var $rbac FLEA_Com_RBAC */
//dump($user);
// 为了降低服务器负担,我们只在 session 中存储用户ID和用户名
$sessionUser = array(
'USERID' => $user[$usersManager->primaryKey],
'USERNAME' => $user[$usersManager->usernameField],
'REALNAME' => $user['realname'],
'DEPOT' =>$user['depot'],
);
// 将用户ID、用户名和角色信息保存到 session
$rbac->setUser($sessionUser, $roles);
// 登录成功
//error_message("用户".$user['realname'] ."在" .date(Ymd) ."成功登陆" .$user['depot']);
//eturn $roles;
return $_SESSION;
}
3、Default.act.php
return array(
'allow' => RBAC_EVERYONE,
'actions' => array(
'index' => array(
'allow' =>RBAC_EVERYONE,
),
'certify' => array(
'allow' =>RBAC_EVERYONE,
),
),
);
4、赋予角色权限
require('FLEA/FLEA.php');
// 假定数据库连接信息保存在 APP/Config/DSN.php 文件中
register_app_inf('APP/Config/DSN.php');
// 由于没有调用 run() 来启动 MVC 模式,所以需要自行初始化 FleaPHP
__FLEA_PREPARE();
load_class('FLEA_Com_RBAC_RolesManager');
class MyRolesManager extends FLEA_Com_RBAC_RolesManager
{
var $tableName = 'roles';
var $primaryKey = 'role_id';
}
load_class('FLEA_Com_RBAC_UsersManager');
class MyUsersManager extends FLEA_Com_RBAC_UsersManager
{
var $tableName = 'users';
var $primaryKey = 'user_id';
var $manyToMany = array(
'tableClass' => 'MyRolesManager',
'mappingName' => 'roles',
'joinTable' => 'roles_users',
);
}
$usersManager =& get_singleton('MyUsersManager');
/* @var $usersManager MyUsersManager */
// 取出用户
$user = $usersManager->findByUsername('LCR');
// 清空现有的角色信息
$user[$usersManager->rolesField] = array();
// 取出 POWER_USER 角色
$rolesManager =& get_singleton('MyRolesManager');
/* @var rolesManager MyRolesManager */
$role = $rolesManager->find(array('rolename' => 'POWER_USER'));
// 指定给用户
$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];
// 取出 MANAGER 角色
$role = $rolesManager->find(array('rolename' => 'MANAGER'));
// 指定给用户
$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];
// 保存修改后的用户信息
$usersManager->update($user);
// 重新从数据库读取用户信息,确定为用户指定的角色信息已经保存到数据库
$user = $usersManager->findByUsername('LCR');
dump($user);
在这之上加上一个后台控制就OK了。
阅读(3050) | 评论(3) | 转发(0) |