1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/User.php');
|
---|
4 | include_once(dirname(__FILE__).'/PageUserList.php');
|
---|
5 | include_once(dirname(__FILE__).'/PageUser.php');
|
---|
6 |
|
---|
7 | class ModuleUser extends Module
|
---|
8 | {
|
---|
9 | var $UserPanel;
|
---|
10 |
|
---|
11 | function __construct($System)
|
---|
12 | {
|
---|
13 | parent::__construct($System);
|
---|
14 | $this->Name = 'User';
|
---|
15 | $this->Version = '1.0';
|
---|
16 | $this->Creator = 'Chronos';
|
---|
17 | $this->License = 'GNU/GPLv3';
|
---|
18 | $this->Description = 'User management';
|
---|
19 | $this->Dependencies = array();
|
---|
20 | $this->UserPanel = array();
|
---|
21 | }
|
---|
22 |
|
---|
23 | function DoInstall(): void
|
---|
24 | {
|
---|
25 | $this->Database->query('CREATE TABLE IF NOT EXISTS `User` ('.
|
---|
26 | '`Id` int(11) NOT NULL AUTO_INCREMENT,'.
|
---|
27 | '`Login` varchar(64) NOT NULL,'.
|
---|
28 | '`Name` varchar(128) NOT NULL,'.
|
---|
29 | '`Password` varchar(255) NOT NULL,'.
|
---|
30 | '`Salt` varchar(255) NOT NULL,'.
|
---|
31 | '`Email` varchar(128) NOT NULL DEFAULT "",'.
|
---|
32 | '`BirthDate` date NULL,'.
|
---|
33 | '`LastIpAddress` varchar(45) NOT NULL DEFAULT "",'.
|
---|
34 | '`LastLoginTime` datetime NULL,'.
|
---|
35 | '`RegistrationTime` datetime NULL,'.
|
---|
36 | '`Locked` tinyint(1) NOT NULL DEFAULT "0",'.
|
---|
37 | 'PRIMARY KEY (`Id`),'.
|
---|
38 | 'UNIQUE KEY `Name` (`Login`),'.
|
---|
39 | 'UNIQUE KEY `Nick` (`Name`)'.
|
---|
40 | ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
41 | $this->Database->query('CREATE TABLE IF NOT EXISTS `UserOnline` ('.
|
---|
42 | '`Id` int(11) NOT NULL AUTO_INCREMENT,'.
|
---|
43 | '`User` int(11) DEFAULT NULL,'.
|
---|
44 | '`ActivityTime` datetime NULL,'.
|
---|
45 | '`LoginTime` datetime NULL,'.
|
---|
46 | '`SessionId` varchar(255) NOT NULL DEFAULT "",'.
|
---|
47 | '`IpAddress` varchar(45) NOT NULL DEFAULT "",'.
|
---|
48 | '`HostName` varchar(255) NOT NULL DEFAULT "",'.
|
---|
49 | '`ScriptName` varchar(255) NOT NULL,'.
|
---|
50 | '`StayLogged` int(11) DEFAULT NULL,'.
|
---|
51 | '`StayLoggedHash` varchar(255) NOT NULL DEFAULT "",'.
|
---|
52 | 'PRIMARY KEY (`Id`),'.
|
---|
53 | 'KEY `User` (`User`)'.
|
---|
54 | ') ENGINE=MEMORY DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
55 | $this->Database->query('CREATE TABLE IF NOT EXISTS `PermissionGroup` ('.
|
---|
56 | '`Id` int(11) NOT NULL AUTO_INCREMENT,'.
|
---|
57 | '`Description` varchar(255) NOT NULL DEFAULT "",'.
|
---|
58 | 'PRIMARY KEY (`Id`)'.
|
---|
59 | ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
60 | $this->Database->query('CREATE TABLE IF NOT EXISTS `PermissionGroupAssignment` ('.
|
---|
61 | '`Id` int(11) NOT NULL AUTO_INCREMENT,'.
|
---|
62 | '`Group` int(11) NOT NULL DEFAULT "0",'.
|
---|
63 | '`AssignedGroup` int(11) DEFAULT NULL,'.
|
---|
64 | '`AssignedOperation` int(11) DEFAULT NULL,'.
|
---|
65 | 'PRIMARY KEY (`Id`),'.
|
---|
66 | 'KEY `Group` (`Group`),'.
|
---|
67 | 'KEY `AssignedGroup` (`AssignedGroup`),'.
|
---|
68 | 'KEY `AssignedOperation` (`AssignedOperation`)'.
|
---|
69 | ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
70 | $this->Database->query('CREATE TABLE IF NOT EXISTS `PermissionOperation` ('.
|
---|
71 | '`Id` int(11) NOT NULL AUTO_INCREMENT,'.
|
---|
72 | '`Module` int(11) NOT NULL,'.
|
---|
73 | '`Operation` varchar(128) NOT NULL DEFAULT "",'.
|
---|
74 | '`Item` varchar(64) NOT NULL DEFAULT "",'.
|
---|
75 | '`ItemId` int(11) NOT NULL DEFAULT 0,'.
|
---|
76 | 'PRIMARY KEY (`Id`),'.
|
---|
77 | 'KEY `Module` (`Module`),'.
|
---|
78 | 'KEY `Operation` (`Operation`),'.
|
---|
79 | 'KEY `Item` (`Item`),'.
|
---|
80 | 'KEY `ItemId` (`ItemId`)'.
|
---|
81 | ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
82 | $this->Database->query('CREATE TABLE IF NOT EXISTS `PermissionUserAssignment` ('.
|
---|
83 | '`Id` int(11) NOT NULL AUTO_INCREMENT,'.
|
---|
84 | '`User` int(11) DEFAULT NULL,'.
|
---|
85 | '`AssignedGroup` int(11) DEFAULT NULL,'.
|
---|
86 | '`AssignedOperation` int(11) DEFAULT NULL,'.
|
---|
87 | 'PRIMARY KEY (`Id`),'.
|
---|
88 | 'KEY `User` (`User`),'.
|
---|
89 | 'KEY `AssignedGroup` (`AssignedGroup`),'.
|
---|
90 | 'KEY `AssignedOperation` (`AssignedOperation`)'.
|
---|
91 | ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
92 |
|
---|
93 | $this->Database->query("ALTER TABLE `PermissionGroupAssignment`
|
---|
94 | ADD CONSTRAINT `PermissionGroupAssignment_ibfk_1` FOREIGN KEY (`Group`) REFERENCES `PermissionGroup` (`Id`),
|
---|
95 | ADD CONSTRAINT `PermissionGroupAssignment_ibfk_2` FOREIGN KEY (`AssignedGroup`) REFERENCES `PermissionGroup` (`Id`),
|
---|
96 | ADD CONSTRAINT `PermissionGroupAssignment_ibfk_3` FOREIGN KEY (`AssignedOperation`) REFERENCES `PermissionOperation` (`Id`);");
|
---|
97 |
|
---|
98 | $this->Database->query("ALTER TABLE `PermissionOperation`
|
---|
99 | ADD CONSTRAINT `PermissionOperation_ibfk_1` FOREIGN KEY (`Module`) REFERENCES `Module` (`Id`);");
|
---|
100 |
|
---|
101 | $this->Database->query("ALTER TABLE `PermissionUserAssignment`
|
---|
102 | ADD CONSTRAINT `PermissionUserAssignment_ibfk_2` FOREIGN KEY (`AssignedGroup`) REFERENCES `PermissionGroup` (`Id`),
|
---|
103 | ADD CONSTRAINT `PermissionUserAssignment_ibfk_3` FOREIGN KEY (`AssignedOperation`) REFERENCES `PermissionOperation` (`Id`),
|
---|
104 | ADD CONSTRAINT `PermissionUserAssignment_ibfk_4` FOREIGN KEY (`User`) REFERENCES `User` (`Id`);");
|
---|
105 |
|
---|
106 | $this->Database->query('INSERT INTO `PermissionGroup` (`Id`, `Description`) VALUES (NULL, "Uživatelé");');
|
---|
107 | }
|
---|
108 |
|
---|
109 | function DoUninstall(): void
|
---|
110 | {
|
---|
111 | $this->Database->query('DROP TABLE `PermissionUserAssignment`');
|
---|
112 | $this->Database->query('DROP TABLE `PermissionGroupAssignment`');
|
---|
113 | $this->Database->query('DROP TABLE `PermissionGroup`');
|
---|
114 | $this->Database->query('DROP TABLE `PermissionOperation`');
|
---|
115 | $this->Database->query('DROP TABLE `UserOnline`');
|
---|
116 | $this->Database->query('DROP TABLE `User`');
|
---|
117 | }
|
---|
118 |
|
---|
119 | function DoUpgrade(): string
|
---|
120 | {
|
---|
121 | /*
|
---|
122 |
|
---|
123 | if ($this->InstalledVersion == '1.0') {
|
---|
124 | $this->System->Database->Query('SELECT * FROM User WHERE Id=1');
|
---|
125 | $this->InstalledVersion = '1.1';
|
---|
126 | }
|
---|
127 | */
|
---|
128 | return '';
|
---|
129 | }
|
---|
130 |
|
---|
131 | function DoStart(): void
|
---|
132 | {
|
---|
133 | Core::Cast($this->System)->User = new User($this->System);
|
---|
134 | if (isset($_SERVER['REMOTE_ADDR'])) Core::Cast($this->System)->User->Check();
|
---|
135 | $this->System->RegisterPage(['userlist'], 'PageUserList');
|
---|
136 | $this->System->RegisterPage(['user'], 'PageUser');
|
---|
137 | Core::Cast($this->System)->RegisterPageBarItem('Top', 'User', array($this, 'TopBarCallback'));
|
---|
138 | Core::Cast($this->System)->FormManager->RegisterClass('UserLogin', array(
|
---|
139 | 'Title' => 'Přihlášení uživatele',
|
---|
140 | 'SubmitText' => 'Přihlásit',
|
---|
141 | 'Table' => '',
|
---|
142 | 'Items' => array(
|
---|
143 | 'Username' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
144 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
|
---|
145 | 'StayLogged' => array('Type' => 'Boolean', 'Caption' => 'Zůstat přihlášen', 'Default' => '0'),
|
---|
146 | ),
|
---|
147 | ));
|
---|
148 | Core::Cast($this->System)->FormManager->RegisterClass('UserOptions', array(
|
---|
149 | 'Title' => 'Základní nastavení',
|
---|
150 | 'Table' => 'User',
|
---|
151 | 'SubmitText' => 'Uložit',
|
---|
152 | 'Items' => array(
|
---|
153 | 'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
154 | 'Salt' => array('Type' => 'RandomHash', 'Caption' => 'Sůl', 'Default' => ''),
|
---|
155 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
|
---|
156 | 'Name' => array('Type' => 'String', 'Caption' => 'Zobrazované jméno', 'Default' => ''),
|
---|
157 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
158 | ),
|
---|
159 | ));
|
---|
160 | Core::Cast($this->System)->FormManager->RegisterClass('UserRegister', array(
|
---|
161 | 'Title' => 'Registrace uživatele',
|
---|
162 | 'SubmitText' => 'Registrovat',
|
---|
163 | 'Table' => 'User',
|
---|
164 | 'Items' => array(
|
---|
165 | 'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
166 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
|
---|
167 | 'Password2' => array('Type' => 'Password', 'Caption' => 'Potvrzení hesla', 'Default' => ''),
|
---|
168 | 'Name' => array('Type' => 'String', 'Caption' => 'Zobrazované jméno', 'Default' => ''),
|
---|
169 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
170 | ),
|
---|
171 | ));
|
---|
172 | Core::Cast($this->System)->FormManager->RegisterClass('PasswordRecovery', array(
|
---|
173 | 'Title' => 'Obnova hesla',
|
---|
174 | 'SubmitText' => 'Obnovit',
|
---|
175 | 'Table' => '',
|
---|
176 | 'Items' => array(
|
---|
177 | 'Name' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
178 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
179 | ),
|
---|
180 | ));
|
---|
181 | Core::Cast($this->System)->FormManager->RegisterClass('APIToken', array(
|
---|
182 | 'Title' => 'Přístupový token',
|
---|
183 | 'Table' => 'APIToken',
|
---|
184 | 'Items' => array(
|
---|
185 | 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => ''),
|
---|
186 | 'Token' => array('Type' => 'String', 'Caption' => 'Token', 'Default' => ''),
|
---|
187 | ),
|
---|
188 | ));
|
---|
189 | Core::Cast($this->System)->FormManager->RegisterClass('User', array(
|
---|
190 | 'Title' => 'Uživatelé',
|
---|
191 | 'Table' => 'User',
|
---|
192 | 'DefaultSortColumn' => 'Name',
|
---|
193 | 'Items' => array(
|
---|
194 | 'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
195 | 'Name' => array('Type' => 'String', 'Caption' => 'Celé jméno', 'Default' => ''),
|
---|
196 | 'Salt' => array('Type' => 'RandomHash', 'Caption' => 'Sůl', 'Default' => '', 'NotInList' => true),
|
---|
197 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => '', 'Method' => 'DoubleSHA1', 'NotInList' => true),
|
---|
198 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
199 | 'LastIpAddress' => array('Type' => 'IPv4Address', 'Caption' => 'Poslední IP adresa', 'Default' => '', 'ReadOnly' => true),
|
---|
200 | 'LastLoginTime' => array('Type' => 'DateTime', 'Caption' => 'Poslední čas přihlášení', 'Default' => '', 'ReadOnly' => true),
|
---|
201 | 'RegistrationTime' => array('Type' => 'DateTime', 'Caption' => 'Čas registrace', 'Default' => ''),
|
---|
202 | 'Locked' => array('Type' => 'Boolean', 'Caption' => 'Uzamčen', 'Default' => ''),
|
---|
203 | 'UserRel' => array('Type' => 'TUserCustomerRelListUser', 'Caption' => 'Přístup k zákazníkům', 'Default' => ''),
|
---|
204 | 'Permission' => array('Type' => 'TPermissionUserAssignmentListUser', 'Caption' => 'Oprávnění', 'Default' => ''),
|
---|
205 | 'Contatcs' => array('Type' => 'TContactListUser', 'Caption' => 'Kontakty', 'Default' => ''),
|
---|
206 | ),
|
---|
207 | ));
|
---|
208 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionUserAssignment', array(
|
---|
209 | 'Title' => 'Oprávnění uživatelů',
|
---|
210 | 'Table' => 'PermissionUserAssignment',
|
---|
211 | 'Items' => array(
|
---|
212 | 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => ''),
|
---|
213 | 'AssignedGroup' => array('Type' => 'TPermissionGroup', 'Caption' => 'Přiřazené skupiny', 'Default' => '', 'Null' => true),
|
---|
214 | 'AssignedOperation' => array('Type' => 'TPermissionOperation', 'Caption' => 'Přiřazené operace', 'Default' => '', 'Null' => true),
|
---|
215 | ),
|
---|
216 | ));
|
---|
217 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionGroup', array(
|
---|
218 | 'Title' => 'Skupiny oprávnění',
|
---|
219 | 'Table' => 'PermissionGroup',
|
---|
220 | 'Items' => array(
|
---|
221 | 'Description' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
|
---|
222 | 'AssignedGroup' => array('Type' => 'TPermissionGroupAssignmentListGroup', 'Caption' => 'Přiřazené skupiny a operace', 'Default' => '', 'Null' => true),
|
---|
223 | 'AssignedGroup2' => array('Type' => 'TPermissionGroupAssignmentListAssignedGroup', 'Caption' => 'Použito ve skupinách', 'Default' => '', 'Null' => true),
|
---|
224 | ),
|
---|
225 | ));
|
---|
226 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionGroupAssignment', array(
|
---|
227 | 'Title' => 'Přiřazení skupin oprávnění',
|
---|
228 | 'Table' => 'PermissionGroupAssignment',
|
---|
229 | 'Items' => array(
|
---|
230 | 'Group' => array('Type' => 'TPermissionGroup', 'Caption' => 'Skupina', 'Default' => ''),
|
---|
231 | 'AssignedGroup' => array('Type' => 'TPermissionGroup', 'Caption' => 'Přiřazené skupiny', 'Default' => '', 'Null' => true),
|
---|
232 | 'AssignedOperation' => array('Type' => 'TPermissionOperation', 'Caption' => 'Přiřazené operace', 'Default' => '', 'Null' => true),
|
---|
233 | ),
|
---|
234 | ));
|
---|
235 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionOperation', array(
|
---|
236 | 'Title' => 'Operace oprávnění',
|
---|
237 | 'Table' => 'PermissionOperation',
|
---|
238 | 'Items' => array(
|
---|
239 | 'Module' => array('Type' => 'TModule', 'Caption' => 'Modul', 'Default' => ''),
|
---|
240 | 'Operation' => array('Type' => 'String', 'Caption' => 'Operace', 'Default' => ''),
|
---|
241 | 'Item' => array('Type' => 'String', 'Caption' => 'Položka', 'Default' => ''),
|
---|
242 | 'ItemId' => array('Type' => 'Integer', 'Caption' => 'Index položky', 'Default' => ''),
|
---|
243 | 'AssignedGroup' => array('Type' => 'TPermissionGroupAssignmentListOperation', 'Caption' => 'Použito ve skupinách', 'Default' => '', 'Null' => true),
|
---|
244 | ),
|
---|
245 | ));
|
---|
246 | Core::Cast($this->System)->FormManager->RegisterFormType('TUser', array(
|
---|
247 | 'Type' => 'Reference',
|
---|
248 | 'Table' => 'User',
|
---|
249 | 'Id' => 'Id',
|
---|
250 | 'Name' => 'Name',
|
---|
251 | 'Filter' => '1',
|
---|
252 | ));
|
---|
253 | Core::Cast($this->System)->FormManager->RegisterFormType('TPermissionGroup', array(
|
---|
254 | 'Type' => 'Reference',
|
---|
255 | 'Table' => 'PermissionGroup',
|
---|
256 | 'Id' => 'Id',
|
---|
257 | 'Name' => 'Description',
|
---|
258 | 'Filter' => '1',
|
---|
259 | ));
|
---|
260 | Core::Cast($this->System)->FormManager->RegisterFormType('TPermissionGroupAssignment', array(
|
---|
261 | 'Type' => 'Reference',
|
---|
262 | 'Table' => 'PermissionGroupAssignment',
|
---|
263 | 'Id' => 'Id',
|
---|
264 | 'Name' => 'Id',
|
---|
265 | 'Filter' => '1',
|
---|
266 | ));
|
---|
267 | Core::Cast($this->System)->FormManager->RegisterFormType('TPermissionOperation', array(
|
---|
268 | 'Type' => 'Reference',
|
---|
269 | 'Table' => 'PermissionOperation',
|
---|
270 | 'Id' => 'Id',
|
---|
271 | 'Name' => 'Id',
|
---|
272 | 'Filter' => '1',
|
---|
273 | ));
|
---|
274 | }
|
---|
275 |
|
---|
276 | function ShowDashboardItem()
|
---|
277 | {
|
---|
278 | $DbResult = $this->Database->select('User', 'COUNT(*)', '1');
|
---|
279 | $DbRow = $DbResult->fetch_row();
|
---|
280 | $Output = 'Uživatelů: '.$DbRow['0'].'<br/>';
|
---|
281 | return $Output;
|
---|
282 | }
|
---|
283 |
|
---|
284 | function DoStop(): void
|
---|
285 | {
|
---|
286 | }
|
---|
287 |
|
---|
288 | function TopBarCallback()
|
---|
289 | {
|
---|
290 | if (Core::Cast($this->System)->User->User['Id'] == null)
|
---|
291 | {
|
---|
292 | $Output = '<a href="'.$this->System->Link('/user/?Action=LoginForm').'">Přihlášení</a> '.
|
---|
293 | '<a href="'.$this->System->Link('/user/?Action=UserRegister').'">Registrace</a>';
|
---|
294 | } else
|
---|
295 | {
|
---|
296 | $Output = Core::Cast($this->System)->User->User['Name'].
|
---|
297 | ' <a href="'.$this->System->Link('/user/?Action=UserMenu').'">Nabídka</a>'.
|
---|
298 | ' <a href="'.$this->System->Link('/user/?Action=Logout').'">Odhlásit</a>';
|
---|
299 | // <a href="'.$this->System->Link('/?Action=UserOptions').'">Nastavení</a>';
|
---|
300 | }
|
---|
301 | return $Output;
|
---|
302 | }
|
---|
303 | }
|
---|