|
6 | 6 |
|
7 | 7 | class UserDAO extends \TgDatabase\DAO { |
8 | 8 |
|
9 | | - public function __construct($database, $modelClass = NULL) { |
10 | | - parent::__construct($database, '#__users', $modelClass != NULL ? $modelClass : 'WebApp\\DataModel\\User'); |
11 | | - $this->initialize(); |
| 9 | + public function __construct($database, $modelClass = NULL, $checkTable = FALSE) { |
| 10 | + parent::__construct($database, '#__users', $modelClass != NULL ? $modelClass : 'WebApp\\DataModel\\User', 'uid', $checkTable); |
12 | 11 | } |
13 | 12 |
|
14 | | - protected function initialize() { |
15 | | - // Check the table existence |
16 | | - $res = $this->database->query('SELECT * FROM '.$this->tableName); |
| 13 | + public function createTable() { |
| 14 | + // Try to create |
| 15 | + $sql = |
| 16 | + 'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('. |
| 17 | + '`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'. |
| 18 | + '`created_on` DATETIME NOT NULL,'. |
| 19 | + '`email` varchar(250) COLLATE utf8mb4_bin NOT NULL,'. |
| 20 | + '`password` varchar(150) COLLATE utf8mb4_bin NOT NULL,'. |
| 21 | + '`name` varchar(50) COLLATE utf8mb4_bin NOT NULL,'. |
| 22 | + '`roles` varchar(250) COLLATE utf8mb4_bin NOT NULL,'. |
| 23 | + '`status` varchar(20) DEFAULT \'active\' NOT NULL,'. |
| 24 | + '`data` text COLLATE utf8mb4_bin NOT NULL,'. |
| 25 | + 'PRIMARY KEY (`uid`) '. |
| 26 | + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin'; |
| 27 | + $res = $this->database->query($sql); |
17 | 28 | if ($res === FALSE) { |
18 | | - // Try to create |
19 | | - $sql = |
20 | | - 'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('. |
21 | | - '`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'. |
22 | | - '`created_on` DATETIME NOT NULL,'. |
23 | | - '`email` varchar(250) COLLATE utf8mb4_bin NOT NULL,'. |
24 | | - '`password` varchar(150) COLLATE utf8mb4_bin NOT NULL,'. |
25 | | - '`name` varchar(50) COLLATE utf8mb4_bin NOT NULL,'. |
26 | | - '`roles` varchar(250) COLLATE utf8mb4_bin NOT NULL,'. |
27 | | - '`status` varchar(20) DEFAULT \'active\' NOT NULL,'. |
28 | | - '`data` text COLLATE utf8mb4_bin NOT NULL,'. |
29 | | - 'PRIMARY KEY (`uid`) '. |
30 | | - ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin'; |
31 | | - $res = $this->database->query($sql); |
32 | | - if ($res === FALSE) { |
33 | | - throw new WebAppException('Cannot create user table: '.$this->database->error()); |
34 | | - } else { |
35 | | - // Create initial superadmin |
36 | | - $now = new \TgUtils\Date(time(), WFW_TIMEZONE); |
37 | | - $password = \TgUtils\Utils::generateRandomString(); |
38 | | - $sql = 'INSERT INTO '.$this->database->quoteName($this->tableName).' (`created_on`, `email`, `password`, `name`, `roles`, `status`, `data`) VALUES '. |
39 | | - '('.$this->database->quote($now->toMysql()).', \'superadmin@example.com\', '.$this->database->quote($password).', \'Application Superadmin\', \'superadmin\', \'active\', \'{}\')'; |
40 | | - $res = $this->database->query($sql); |
41 | | - if ($res === FALSE) { |
42 | | - throw new WebAppException('Cannot create superadmin: '.$this->database->error()); |
43 | | - } else { |
44 | | - error_log('===========> Superadmin Email: superadmin@example.com'); |
45 | | - error_log('===========> Superadmin Password: '.$password); |
46 | | - error_log('===========> YOU MUST CHANGE THIS IMMEDIATELY!'); |
47 | | - } |
48 | | - } |
| 29 | + throw new WebAppException('Cannot create user table: '.$this->database->error()); |
49 | 30 | } |
| 31 | + |
| 32 | + // Create initial superadmin |
| 33 | + $now = new \TgUtils\Date(time(), WFW_TIMEZONE); |
| 34 | + $password = \TgUtils\Utils::generateRandomString(); |
| 35 | + $sql = 'INSERT INTO '.$this->database->quoteName($this->tableName).' (`created_on`, `email`, `password`, `name`, `roles`, `status`, `data`) VALUES '. |
| 36 | + '('.$this->database->quote($now->toMysql()).', \'superadmin@example.com\', '.$this->database->quote($password).', \'Application Superadmin\', \'superadmin\', \'active\', \'{}\')'; |
| 37 | + $res = $this->database->query($sql); |
| 38 | + if ($res === FALSE) { |
| 39 | + throw new WebAppException('Cannot create superadmin: '.$this->database->error()); |
| 40 | + } |
| 41 | + |
| 42 | + error_log('===========> Superadmin Email: superadmin@example.com'); |
| 43 | + error_log('===========> Superadmin Password: '.$password); |
| 44 | + error_log('===========> YOU MUST CHANGE THIS IMMEDIATELY!'); |
| 45 | + return TRUE; |
50 | 46 | } |
51 | 47 |
|
52 | 48 | public function getByEmail($email) { |
|
0 commit comments