Example #1
0
 public static function getModule($model, $config = array(), $module_path = '', $module_suffix = '')
 {
     self::$model = $model;
     self::init($config);
     //完成数据库表名和字段名映射
     $table_group = self::$config['AUTH_TABLE']['group']['name'];
     //用户组表
     $table_resource = self::$config['AUTH_TABLE']['resource']['name'];
     //资源表
     $group_field_id = self::$config['AUTH_TABLE']['group']['field']['id'];
     //用户组表 id字段
     $group_field_power = self::$config['AUTH_TABLE']['group']['field']['power'];
     //用户组表 权限power字段
     $resource_field_id = self::$config['AUTH_TABLE']['resource']['field']['id'];
     //资源表 id字段
     $resource_field_pid = self::$config['AUTH_TABLE']['resource']['field']['pid'];
     //资源表 pid字段
     $resource_field_operate = self::$config['AUTH_TABLE']['resource']['field']['operate'];
     //资源表 operate字段
     //如果没有用户组和资源数据表,则自动创建
     $sql_group = "CREATE TABLE IF NOT EXISTS `" . self::$model->pre . $table_group . "` (\r\n  `{$group_field_id}` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(255) NOT NULL,\r\n  `{$group_field_power}` varchar(1000) NOT NULL,\r\n  PRIMARY KEY (`{$group_field_id}`)\r\n) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;";
     $sql_resource = "CREATE TABLE IF NOT EXISTS `" . self::$model->pre . $table_resource . "` (\r\n  `{$resource_field_id}` int(10) unsigned NOT NULL auto_increment,\r\n  `{$resource_field_pid}` int(10) unsigned NOT NULL,\r\n  `{$resource_field_operate}` varchar(255) NOT NULL,\r\n  `name` varchar(255) NOT NULL,\r\n  PRIMARY KEY  (`{$resource_field_id}`),\r\n  KEY `pid` (`{$resource_field_pid}`)\r\n) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
     self::$model->query($sql_group);
     self::$model->query($sql_resource);
     $module_path = empty($module_path) ? './module/' : $module_path;
     $module_suffix = empty($module_suffix) ? 'Mod.class.php' : $module_suffix;
     $return_data = array();
     if ($dir = opendir($module_path)) {
         while ($filename = readdir($dir)) {
             if (!is_dir($filename)) {
                 $module_suffix_array = explode('.', $module_suffix, 2);
                 $module = str_replace($module_suffix, '', $filename);
                 $class_name = $module . $module_suffix_array[0];
                 $class_methods = get_class_methods($class_name);
                 if (is_array($class_methods)) {
                     foreach ($class_methods as $action) {
                         //过滤魔术方法
                         if (substr($action, 0, 2) != '__') {
                             $return_data[$module][$action] = -1;
                         }
                     }
                 }
             }
         }
         closedir($dir);
     }
     if (!empty($return_data)) {
         $data = array();
         foreach ($return_data as $key => $value) {
             $data[$resource_field_pid] = $condition[$resource_field_pid] = 0;
             $data[$resource_field_operate] = $condition[$resource_field_operate] = $key;
             $info = self::$model->table($table_resource)->where($condition)->find();
             if (empty($info)) {
                 $pid = self::$model->table($table_resource)->data($data)->insert();
             } else {
                 $pid = $info[$resource_field_id];
             }
             if (is_array($value)) {
                 foreach ($value as $key => $vo) {
                     $data[$resource_field_pid] = $condition[$resource_field_pid] = $pid;
                     $data[$resource_field_operate] = $condition[$resource_field_operate] = $key;
                     $info = self::$model->table($table_resource)->where($condition)->find();
                     if (empty($info)) {
                         self::$model->table($table_resource)->data($data)->insert();
                     }
                 }
             }
         }
     }
     return $return_data;
 }
<?php

Auth::$model = 'offline\\User';
Auth::$username_field = 'email';
Auth::$password_field = 'password';
Auth::$active_field = 'active';
Auth::$fields = array('id', 'name', 'email', 'hierarchy', 'active');
Auth::$default_hierarchy = 'offline';
# cada aplicação desenvolvida neste framework deve possuir um índice diferente na sessão.
Auth::$session_key = sha1(Url::$application);
Auth::$encrypt = function ($password) {
    return md5($password);
};