Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MySqlDatabase
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 connection
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 testConnection
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getInfo
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getCreatedTables
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 install
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace UserFrosting;
4
5// Represents the UserFrosting database.  Used for initializing connections for queries.  Set $params to the connection variables you'd like to use.
6abstract class MySqlDatabase extends UFDatabase implements DatabaseInterface {
7   
8    // Call this to get a fresh connection to the database.
9    public static function connection(){
10        $db_host = static::$app->config('db')['db_host'];
11        $db_name = static::$app->config('db')['db_name'];
12    
13        $db = new \PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8", static::$app->config('db')['db_user'], static::$app->config('db')['db_pass']);
14        $db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
15        $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);       // Let this function throw a PDO exception if it cannot connect.
16        return $db;    
17    }
18    
19    // Test whether a DB connection can be established
20    public static function testConnection(){
21        try {
22            static::connection();
23        } catch (\PDOException $e){
24            return false;
25        }
26        return true;
27    }
28    
29    // Get information about this database as key->value pairs
30    public static function getInfo(){
31        $connection = static::connection();
32        $results = [];
33        try {
34            $results['db_type'] = $connection->getAttribute(\PDO::ATTR_DRIVER_NAME);
35        } catch (Exception $e){
36            $results['db_type'] = "Unknown";
37        }
38        try {
39            $results['db_version'] = $connection->getAttribute(\PDO::ATTR_SERVER_VERSION);
40        } catch (Exception $e){
41            $results['db_type'] = "";
42        }
43        $results['db_name'] = static::$app->config('db')['db_name'];
44        $results['table_prefix'] = static::$app->config('db')['db_prefix'];
45        return $results;
46    }
47    
48    // Return a list of UF tables that actually exist
49    public static function getCreatedTables(){
50        if (!static::testConnection())
51            return [];
52        
53        $connection = static::connection();
54        $results = [];
55        
56        $test_list = [
57            static::getTable('user')->name,
58            static::getTable('group')->name,
59            static::getTable('group_user')->name,
60            static::getTable('authorize_user')->name,
61            static::getTable('authorize_group')->name
62        ];
63        
64        foreach ($test_list as $table){
65            try {
66                $stmt = $connection->prepare("SELECT 1 FROM `$table` LIMIT 1;");
67            } catch (\PDOException $e){
68                continue;
69            }
70            $results[] = $table;
71        }
72        
73        return $results;
74    }
75    
76    // Creates the tables for UserFrosting
77    public static function install(){
78        $connection = static::connection();
79        
80        $prefix = static::$app->config('db')['db_prefix'];
81        
82        $connection->query("CREATE TABLE IF NOT EXISTS `$prefix" . "authorize_group` (
83            `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
84            `group_id` int(11) NOT NULL,
85            `hook` varchar(200) NOT NULL COMMENT 'A code that references a specific action or URI that the group has access to.',
86            `conditions` text NOT NULL COMMENT 'The conditions under which members of this group have access to this hook.',
87            PRIMARY KEY (`id`)
88          ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;");
89          
90        $connection->query("CREATE TABLE IF NOT EXISTS `$prefix" . "authorize_user` (
91            `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
92            `user_id` int(11) NOT NULL,
93            `hook` varchar(200) NOT NULL COMMENT 'A code that references a specific action or URI that the user has access to.',
94            `conditions` text NOT NULL COMMENT 'The conditions under which the user has access to this action.',
95            PRIMARY KEY (`id`)
96          ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;");
97              
98        $connection->query("CREATE TABLE IF NOT EXISTS `$prefix" . "group` (
99            `id` int(11) NOT NULL AUTO_INCREMENT,
100            `name` varchar(150) NOT NULL,
101            `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Specifies whether this permission is a default setting for new accounts.',
102            `can_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Specifies whether this permission can be deleted from the control panel.',
103            `theme` varchar(100) NOT NULL DEFAULT 'default' COMMENT 'The theme assigned to primary users in this group.',
104            `landing_page` varchar(200) NOT NULL DEFAULT 'account' COMMENT 'The page to take primary members to when they first log in.',
105            `new_user_title` varchar(200) NOT NULL DEFAULT 'New User' COMMENT 'The default title to assign to new primary users.',
106            `icon` varchar(100) NOT NULL DEFAULT 'fa fa-user' COMMENT 'The icon representing primary users in this group.',
107            PRIMARY KEY (`id`)
108          ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
109          
110          
111        $connection->query("CREATE TABLE IF NOT EXISTS `$prefix" . "group_user` (
112            `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
113            `user_id` int(10) unsigned NOT NULL,
114            `group_id` int(10) unsigned NOT NULL,
115            PRIMARY KEY (`id`)
116          ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='Maps users to their group(s)' AUTO_INCREMENT=1 ;");
117          
118        $connection->query("CREATE TABLE IF NOT EXISTS `$prefix" . "user` (
119            `id` int(11) NOT NULL AUTO_INCREMENT,
120            `user_name` varchar(50) NOT NULL,
121            `display_name` varchar(50) NOT NULL,
122            `password` varchar(255) NOT NULL,
123            `email` varchar(150) NOT NULL,
124            `activation_token` varchar(225) NOT NULL,
125            `last_activation_request` datetime NOT NULL,
126            `lost_password_request` tinyint(1) NOT NULL DEFAULT '0',
127            `lost_password_timestamp` datetime DEFAULT NULL,
128            `active` tinyint(1) NOT NULL DEFAULT '1',
129            `title` varchar(150) NOT NULL,
130            `sign_up_stamp` datetime NOT NULL,
131            `last_sign_in_stamp` datetime DEFAULT NULL,
132            `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Specifies if the account is enabled.  Disabled accounts cannot be logged in to, but they retain all of their data and settings.',
133            `primary_group_id` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Specifies the primary group for the user.',
134            `locale` varchar(10) NOT NULL DEFAULT 'en_US' COMMENT 'The language and locale to use for this user.',
135            PRIMARY KEY (`id`)
136          ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
137
138        // Setup default configuration settings
139        static::$app->site->install_status = "pending";
140        static::$app->site->root_account_config_token = md5(uniqid(mt_rand(), false));
141        static::$app->site->store();        
142        
143        // Setup default groups.  TODO: finish Group API so they can be created through objects
144        $connection->query("INSERT INTO `$prefix" . "group` (`name`, `is_default`, `can_delete`, `theme`, `landing_page`, `new_user_title`, `icon`) VALUES
145          ('User', " . GROUP_DEFAULT_PRIMARY . ", 0, 'default', 'dashboard', 'New User', 'fa fa-user'),
146          ('Administrator', " . GROUP_NOT_DEFAULT . ", 0, 'nyx', 'dashboard', 'Brood Spawn', 'fa fa-flag'),
147          ('Zerglings', " . GROUP_NOT_DEFAULT . ", 1, 'nyx', 'dashboard', 'Tank Fodder', 'sc sc-zergling');");        
148    
149        // Setup default authorizations
150        $connection->query("INSERT INTO `$prefix" . "authorize_group` (`group_id`, `hook`, `conditions`) VALUES
151          (1, 'uri_dashboard', 'always()'),
152          (2, 'uri_dashboard', 'always()'),
153          (2, 'uri_users', 'always()'),
154          (1, 'uri_account_settings', 'always()'),
155          (1, 'update_account_setting', 'equals(self.id, user.id)&&in(property,[\"email\",\"locale\",\"password\"])'),
156          (2, 'update_account_setting', 'in(property,[\"email\",\"display_name\",\"title\",\"locale\",\"enabled\"])'),
157          (2, 'view_account_setting', 'in(property,[\"user_name\",\"email\",\"display_name\",\"title\",\"locale\",\"enabled\",\"groups\",\"primary_group_id\"])'),
158          (2, 'delete_account', '!in_group(user.id,2)'),
159          (2, 'create_account', 'always()');");    
160    }
161    
162}