1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/UserModel.php');
|
---|
4 | include_once(dirname(__FILE__).'/UserList.php');
|
---|
5 | include_once(dirname(__FILE__).'/UserPage.php');
|
---|
6 |
|
---|
7 | class ModuleUser extends Module
|
---|
8 | {
|
---|
9 | public array $UserPanel;
|
---|
10 | public User $User;
|
---|
11 |
|
---|
12 | function __construct(System $System)
|
---|
13 | {
|
---|
14 | parent::__construct($System);
|
---|
15 | $this->Name = 'User';
|
---|
16 | $this->Version = '1.0';
|
---|
17 | $this->Creator = 'Chronos';
|
---|
18 | $this->License = 'GNU/GPLv3';
|
---|
19 | $this->Description = 'User management';
|
---|
20 | $this->Models = array(User::GetClassName(), UserOnline::GetClassName(), PermissionGroup::GetClassName(), PermissionOperation::GetClassName(),
|
---|
21 | PermissionGroupAssignment::GetClassName(), PermissionUserAssignment::GetClassName());
|
---|
22 |
|
---|
23 | $this->UserPanel = array();
|
---|
24 | $this->User = new User($System);
|
---|
25 | }
|
---|
26 |
|
---|
27 | function DoInsertSampleData(): void
|
---|
28 | {
|
---|
29 | $DbResult = $this->Database->select('User', 'Id', 'Login="admin"');
|
---|
30 | if ($DbResult->num_rows == 0)
|
---|
31 | {
|
---|
32 | $UserId = $this->Database->insert('User', array('Id' => 1, 'Login' => 'admin', 'Name' => 'Admin', 'Password' => 'd04b7c77a814de3207315c2f82dcd97a90ed33ca',
|
---|
33 | 'Salt' => 'd5d8eb61e1a20d98b45b415ccd2fae802cb41294', 'Email' => 'admin@localhost', 'LastIpAddress' => null, 'LastLoginTime' => null,
|
---|
34 | 'RegistrationTime' => '2021-02-17 14:56:22', 'Locked' => 0, 'InitPassword' => null));
|
---|
35 |
|
---|
36 | $PermissionGroup1 = $this->Database->insert('PermissionGroup', array('Description' => 'Ostatní'));
|
---|
37 | $PermissionGroup2 = $this->Database->insert('PermissionGroup', array('Description' => 'Registrovaní uživatelé'));
|
---|
38 | $PermissionGroup3 = $this->Database->insert('PermissionGroup', array('Description' => 'Zákazníci'));
|
---|
39 | $PermissionGroup4 = $this->Database->insert('PermissionGroup', array('Description' => 'Správci sítě'));
|
---|
40 | $PermissionGroup5 = $this->Database->insert('PermissionGroup', array('Description' => 'Systémoví správci'));
|
---|
41 |
|
---|
42 | $this->Database->insert('PermissionUserAssignment', array('User' => $UserId, 'AssignedGroup' => $PermissionGroup5));
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | function DoStart(): void
|
---|
47 | {
|
---|
48 | if (isset($_SERVER['REMOTE_ADDR'])) $this->User->Check();
|
---|
49 | Core::Cast($this->System)->RegisterPage(['userlist'], 'PageUserList');
|
---|
50 | Core::Cast($this->System)->RegisterPage(['user'], 'PageUser');
|
---|
51 | Core::Cast($this->System)->RegisterPageBarItem('Top', 'User', array($this, 'TopBarCallback'));
|
---|
52 | Core::Cast($this->System)->FormManager->RegisterClass('UserLogin', array(
|
---|
53 | 'Title' => 'Přihlášení uživatele',
|
---|
54 | 'SubmitText' => 'Přihlásit',
|
---|
55 | 'Table' => '',
|
---|
56 | 'Items' => array(
|
---|
57 | 'Username' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
58 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
|
---|
59 | 'StayLogged' => array('Type' => 'Boolean', 'Caption' => 'Zůstat přihlášen', 'Default' => '0'),
|
---|
60 | ),
|
---|
61 | ));
|
---|
62 | Core::Cast($this->System)->FormManager->RegisterClass('UserOptions', array(
|
---|
63 | 'Title' => 'Základní nastavení',
|
---|
64 | 'Table' => 'User',
|
---|
65 | 'SubmitText' => 'Uložit',
|
---|
66 | 'Items' => array(
|
---|
67 | 'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
68 | 'Salt' => array('Type' => 'RandomHash', 'Caption' => 'Sůl', 'Default' => ''),
|
---|
69 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
|
---|
70 | 'Name' => array('Type' => 'String', 'Caption' => 'Zobrazované jméno', 'Default' => ''),
|
---|
71 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
72 | ),
|
---|
73 | ));
|
---|
74 | Core::Cast($this->System)->FormManager->RegisterClass('UserRegister', array(
|
---|
75 | 'Title' => 'Registrace uživatele',
|
---|
76 | 'SubmitText' => 'Registrovat',
|
---|
77 | 'Table' => 'User',
|
---|
78 | 'Items' => array(
|
---|
79 | 'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
80 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => ''),
|
---|
81 | 'Password2' => array('Type' => 'Password', 'Caption' => 'Potvrzení hesla', 'Default' => ''),
|
---|
82 | 'Name' => array('Type' => 'String', 'Caption' => 'Zobrazované jméno', 'Default' => ''),
|
---|
83 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
84 | 'IsHuman' => array('Type' => 'Boolean', 'Caption' => 'Jsi člověk?', 'Default' => 0),
|
---|
85 | ),
|
---|
86 | ));
|
---|
87 | Core::Cast($this->System)->FormManager->RegisterClass('PasswordRecovery', array(
|
---|
88 | 'Title' => 'Obnova hesla',
|
---|
89 | 'SubmitText' => 'Obnovit',
|
---|
90 | 'Table' => '',
|
---|
91 | 'Items' => array(
|
---|
92 | 'Name' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
93 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
94 | 'IsHuman' => array('Type' => 'Boolean', 'Caption' => 'Jsi člověk?', 'Default' => 0),
|
---|
95 | ),
|
---|
96 | ));
|
---|
97 | Core::Cast($this->System)->FormManager->RegisterClass('APIToken', array(
|
---|
98 | 'Title' => 'Přístupový token',
|
---|
99 | 'Table' => 'APIToken',
|
---|
100 | 'Items' => array(
|
---|
101 | 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => ''),
|
---|
102 | 'Token' => array('Type' => 'String', 'Caption' => 'Token', 'Default' => ''),
|
---|
103 | ),
|
---|
104 | ));
|
---|
105 | Core::Cast($this->System)->FormManager->RegisterClass('User', array(
|
---|
106 | 'Title' => 'Uživatelé',
|
---|
107 | 'Table' => 'User',
|
---|
108 | 'DefaultSortColumn' => 'Name',
|
---|
109 | 'Items' => array(
|
---|
110 | 'Login' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => ''),
|
---|
111 | 'Name' => array('Type' => 'String', 'Caption' => 'Celé jméno', 'Default' => ''),
|
---|
112 | 'Salt' => array('Type' => 'RandomHash', 'Caption' => 'Sůl', 'Default' => '', 'NotInList' => true),
|
---|
113 | 'Password' => array('Type' => 'Password', 'Caption' => 'Heslo', 'Default' => '', 'Method' => 'DoubleSHA1', 'NotInList' => true),
|
---|
114 | 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''),
|
---|
115 | 'LastIpAddress' => array('Type' => 'IPv4Address', 'Caption' => 'Poslední IP adresa', 'Default' => '', 'ReadOnly' => true),
|
---|
116 | 'LastLoginTime' => array('Type' => 'DateTime', 'Caption' => 'Poslední čas přihlášení', 'Default' => '', 'ReadOnly' => true),
|
---|
117 | 'RegistrationTime' => array('Type' => 'DateTime', 'Caption' => 'Čas registrace', 'Default' => ''),
|
---|
118 | 'Locked' => array('Type' => 'Boolean', 'Caption' => 'Uzamčen', 'Default' => ''),
|
---|
119 | 'UserRel' => array('Type' => 'TUserCustomerRelListUser', 'Caption' => 'Přístup k zákazníkům', 'Default' => ''),
|
---|
120 | 'Permission' => array('Type' => 'TPermissionUserAssignmentListUser', 'Caption' => 'Oprávnění', 'Default' => ''),
|
---|
121 | 'Contatcs' => array('Type' => 'TContactListUser', 'Caption' => 'Kontakty', 'Default' => ''),
|
---|
122 | ),
|
---|
123 | ));
|
---|
124 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionUserAssignment', array(
|
---|
125 | 'Title' => 'Oprávnění uživatelů',
|
---|
126 | 'Table' => 'PermissionUserAssignment',
|
---|
127 | 'Items' => array(
|
---|
128 | 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => ''),
|
---|
129 | 'AssignedGroup' => array('Type' => 'TPermissionGroup', 'Caption' => 'Přiřazené skupiny', 'Default' => '', 'Null' => true),
|
---|
130 | 'AssignedOperation' => array('Type' => 'TPermissionOperation', 'Caption' => 'Přiřazené operace', 'Default' => '', 'Null' => true),
|
---|
131 | ),
|
---|
132 | ));
|
---|
133 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionGroup', array(
|
---|
134 | 'Title' => 'Skupiny oprávnění',
|
---|
135 | 'Table' => 'PermissionGroup',
|
---|
136 | 'Items' => array(
|
---|
137 | 'Description' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
|
---|
138 | 'AssignedGroup' => array('Type' => 'TPermissionGroupAssignmentListGroup', 'Caption' => 'Přiřazené skupiny a operace', 'Default' => '', 'Null' => true),
|
---|
139 | 'AssignedGroup2' => array('Type' => 'TPermissionGroupAssignmentListAssignedGroup', 'Caption' => 'Použito ve skupinách', 'Default' => '', 'Null' => true),
|
---|
140 | ),
|
---|
141 | ));
|
---|
142 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionGroupAssignment', array(
|
---|
143 | 'Title' => 'Přiřazení skupin oprávnění',
|
---|
144 | 'Table' => 'PermissionGroupAssignment',
|
---|
145 | 'Items' => array(
|
---|
146 | 'Group' => array('Type' => 'TPermissionGroup', 'Caption' => 'Skupina', 'Default' => ''),
|
---|
147 | 'AssignedGroup' => array('Type' => 'TPermissionGroup', 'Caption' => 'Přiřazené skupiny', 'Default' => '', 'Null' => true),
|
---|
148 | 'AssignedOperation' => array('Type' => 'TPermissionOperation', 'Caption' => 'Přiřazené operace', 'Default' => '', 'Null' => true),
|
---|
149 | ),
|
---|
150 | ));
|
---|
151 | Core::Cast($this->System)->FormManager->RegisterClass('PermissionOperation', array(
|
---|
152 | 'Title' => 'Operace oprávnění',
|
---|
153 | 'Table' => 'PermissionOperation',
|
---|
154 | 'Items' => array(
|
---|
155 | 'Module' => array('Type' => 'TModule', 'Caption' => 'Modul', 'Default' => ''),
|
---|
156 | 'Operation' => array('Type' => 'String', 'Caption' => 'Operace', 'Default' => ''),
|
---|
157 | 'Item' => array('Type' => 'String', 'Caption' => 'Položka', 'Default' => ''),
|
---|
158 | 'ItemId' => array('Type' => 'Integer', 'Caption' => 'Index položky', 'Default' => ''),
|
---|
159 | 'AssignedGroup' => array('Type' => 'TPermissionGroupAssignmentListOperation', 'Caption' => 'Použito ve skupinách', 'Default' => '', 'Null' => true),
|
---|
160 | ),
|
---|
161 | ));
|
---|
162 | Core::Cast($this->System)->FormManager->RegisterFormType('TUser', array(
|
---|
163 | 'Type' => 'Reference',
|
---|
164 | 'Table' => 'User',
|
---|
165 | 'Id' => 'Id',
|
---|
166 | 'Name' => 'Name',
|
---|
167 | 'Filter' => '1',
|
---|
168 | ));
|
---|
169 | Core::Cast($this->System)->FormManager->RegisterFormType('TPermissionGroup', array(
|
---|
170 | 'Type' => 'Reference',
|
---|
171 | 'Table' => 'PermissionGroup',
|
---|
172 | 'Id' => 'Id',
|
---|
173 | 'Name' => 'Description',
|
---|
174 | 'Filter' => '1',
|
---|
175 | ));
|
---|
176 | Core::Cast($this->System)->FormManager->RegisterFormType('TPermissionGroupAssignment', array(
|
---|
177 | 'Type' => 'Reference',
|
---|
178 | 'Table' => 'PermissionGroupAssignment',
|
---|
179 | 'Id' => 'Id',
|
---|
180 | 'Name' => 'Id',
|
---|
181 | 'Filter' => '1',
|
---|
182 | ));
|
---|
183 | Core::Cast($this->System)->FormManager->RegisterFormType('TPermissionOperation', array(
|
---|
184 | 'Type' => 'Reference',
|
---|
185 | 'Table' => 'PermissionOperation',
|
---|
186 | 'Id' => 'Id',
|
---|
187 | 'Name' => 'Id',
|
---|
188 | 'Filter' => '1',
|
---|
189 | ));
|
---|
190 | ModuleIS::Cast(Core::Cast($this->System)->GetModule('IS'))->RegisterDashboardItem('User',
|
---|
191 | array($this, 'ShowDashboardItem'));
|
---|
192 | }
|
---|
193 |
|
---|
194 | function ShowDashboardItem(): string
|
---|
195 | {
|
---|
196 | $DbResult = $this->Database->select('User', 'COUNT(*)', '1');
|
---|
197 | $DbRow = $DbResult->fetch_row();
|
---|
198 | $Output = 'Uživatelů: <a href="'.$this->System->Link('/is/?a=list&t=User&filter=1').'">'.$DbRow['0'].'</a><br/>';
|
---|
199 | return $Output;
|
---|
200 | }
|
---|
201 |
|
---|
202 | function TopBarCallback(): string
|
---|
203 | {
|
---|
204 | if ($this->User->User['Id'] == null)
|
---|
205 | {
|
---|
206 | $Output = '<a href="'.Core::Cast($this->System)->Link('/user/?Action=LoginForm').'">Přihlášení</a> '.
|
---|
207 | '<a href="'.Core::Cast($this->System)->Link('/user/?Action=UserRegister').'">Registrace</a>';
|
---|
208 | } else
|
---|
209 | {
|
---|
210 | $Output = $this->User->User['Name'].
|
---|
211 | ' <a href="'.Core::Cast($this->System)->Link('/user/?Action=UserMenu').'">Nabídka</a>'.
|
---|
212 | ' <a href="'.Core::Cast($this->System)->Link('/user/?Action=Logout').'">Odhlásit</a>';
|
---|
213 | // <a href="'.$this->System->Link('/?Action=UserOptions').'">Nastavení</a>';
|
---|
214 | }
|
---|
215 | return $Output;
|
---|
216 | }
|
---|
217 |
|
---|
218 | static function Cast(Module $Module): ModuleUser
|
---|
219 | {
|
---|
220 | if ($Module instanceof ModuleUser)
|
---|
221 | {
|
---|
222 | return $Module;
|
---|
223 | }
|
---|
224 | throw new Exception('Expected ModuleUser type but got '.gettype($Module));
|
---|
225 | }
|
---|
226 | }
|
---|