source: branches/2/user.php@ 8

Last change on this file since 8 was 8, checked in by george, 17 years ago

Přidáno: Oprávnění pro čtení a zápis k rolím a operacím.
Přidáno: Zobrazení matice oprávnění.
Přidáno: Uživatelské menu s proměnným obsahem podle role uživatele.
Upraveno: Rozdělení importu na import z mangosu a import ze staré databáze.

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1<?php
2
3define('NICK_USED', 'Přezdívka použita!');
4define('USER_REGISTRATED', 'Uživatel zaregistrován.');
5define('DATA_MISSING', 'Chybí emailová adresa, přezdívka, nebo některé z hesel!');
6define('PASSWORDS_UNMATCHED', 'Hesla si neodpovídají!');
7define('USER_NOT_LOGGED', 'Nejste přihlášen.');
8define('USER_LOGGED', 'Uživatel přihlášen.');
9define('USER_NOT_REGISTRED', 'Uživatel neregistrován.');
10define('USER_ALREADY_LOGGED', 'Uživatel již přihlášen.');
11define('USER_LOGGED_IN', 'Byl jste přihlášen.');
12define('USER_LOGGED_OUT', 'Byl jste odhlášen.');
13define('BAD_PASSWORD', 'Špatné heslo.');
14define('USER_TIMEOUT', 300); // in seconds
15
16class User extends Module
17{
18 var $Roles = array();
19 var $User = array();
20 var $DefaultRole = 2;
21
22 function Check()
23 {
24 $SID = session_id();
25
26 // Lookup user record
27 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
28 if($Query->num_rows > 0)
29 {
30 // Refresh time of last access
31 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('Time' => 'NOW()'));
32 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => 1, 'Time' => 'NOW()', 'IpAddress' => (gethostbyaddr(GetRemoteAddress()).' '.GetRemoteAddress())));
33
34 // Odeber neaktivní uživatele
35 $this->Database->delete('UserOnline', 'Time < DATE_SUB(NOW(), INTERVAL '.USER_TIMEOUT.' SECOND)');
36
37 // Zkontroluj přihlášení
38 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
39 $Row = $Query->fetch_array();
40 if($Row['User'] != 0)
41 {
42 $Query = $this->Database->select('User', '*', "Id=".$Row['User']."");
43 $this->User = $Query->fetch_array();
44 $Result = USER_LOGGED;
45 } else {
46 $Query = $this->Database->select('User', '*', "Id=1");
47 $this->User = $Query->fetch_array();
48 $Result = USER_NOT_LOGGED;
49 }
50 $this->LoadPermission($this->User['Role']);
51
52 // Role and permission
53 $this->LoadRoles();
54
55 }
56
57 function Register($Nick, $Password, $Password2, $Email, $FullName)
58 {
59 global $Options;
60 if(($Email == '') || ($Nick == '') || ($Password == '') || ($Password2 == '')) $Result = DATA_MISSING;
61 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
62 else
63 {
64 // Je uživatel registrován?
65 $Query = $this->Database->select('User', '*', 'Name = "'.$Nick.'"');
66 if($Query->num_rows() > 0) $Result = NICK_USED;
67 else
68 {
69 $this->Database->insert('User', array('Name' => addslashes($Nick), 'FullName' => addslashes($FullName), 'Password' => addslashes($Password), 'Email' => htmlspecialchars($Email), 'Role' => $this->DefaultRole));
70 $Result = USER_REGISTRATED;
71 }
72 }
73 return($Result);
74 }
75
76 function Login($Nick, $Password)
77 {
78 $SID = session_id();
79 // Je uživatel registrován?
80 $Query = $this->Database->select('User', '*', 'Name="'.$Nick.'"');
81 if($Query->num_rows > 0)
82 {
83 $Row = $Query->fetch_array();
84 if($Row['Password'] != $Password) $Result = BAD_PASSWORD;
85 else
86 {
87 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()'));
88 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
89 // načtení stavu stromu
90 $Result = USER_LOGGED_IN;
91 }
92 } else $Result = USER_NOT_REGISTRED;
93 return($Result);
94 }
95
96 function Logout()
97 {
98 $SID = session_id();
99 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => 1));
100 return(USER_LOGGED_OUT);
101 }
102
103 function LoadRoles()
104 {
105 $this->Roles = array();
106 $DbResult = $this->Database->select('UserRole', '*');
107 while($DbRow = $DbResult->fetch_array())
108 $this->Roles[] = $DbRow;
109 }
110
111 function LoadPermission($Role)
112 {
113 $this->User['Permission'] = array();
114 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
115 if($DbResult->num_rows > 0)
116 while($DbRow = $DbResult->fetch_array())
117 $this->User['Permission'][$DbRow['Operation']] = $DbRow;
118 }
119
120 function PermissionMatrix()
121 {
122 $Result = array();
123 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description`, `UserRole`.`Title` FROM `UserRolePermission` LEFT JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` LEFT JOIN `UserRole` ON `UserRole`.`Id` = `UserRolePermission`.`Role`');
124 while($DbRow = $DbResult->fetch_array())
125 {
126 $Value = '';
127 if($DbRow['Read']) $Value .= 'R';
128 if($DbRow['Write']) $Value .= 'W';
129 $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
130 }
131 return($Result);
132 }
133}
134
135?>
Note: See TracBrowser for help on using the repository browser.