|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Pierre-Henry Soria <[email protected]> |
|
4
|
|
|
* @copyright (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved. |
|
5
|
|
|
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
|
6
|
|
|
* @package PH7 / App / System / Core / Class |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace PH7; |
|
9
|
|
|
|
|
10
|
|
|
use |
|
|
|
|
|
|
11
|
|
|
PH7\Framework\Session\Session, |
|
12
|
|
|
PH7\Framework\Ip\Ip, |
|
13
|
|
|
PH7\Framework\Util\Various, |
|
14
|
|
|
PH7\Framework\Navigation\Browser, |
|
15
|
|
|
PH7\Framework\Mvc\Model\Security as SecurityModel; |
|
16
|
|
|
|
|
17
|
|
|
// Abstract Class |
|
18
|
|
|
class AdminCore extends UserCore |
|
19
|
|
|
{ |
|
20
|
|
|
const ROOT_PROILE_ID = 1; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Admins'levels. |
|
24
|
|
|
* |
|
25
|
|
|
* @return boolean |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function auth() |
|
28
|
|
|
{ |
|
29
|
|
|
$oSession = new Session; |
|
30
|
|
|
$bIsConnected = (((int)$oSession->exists('admin_id')) && $oSession->get('admin_ip') === Ip::get() && $oSession->get('admin_http_user_agent') === (new Browser)->getUserAgent()); |
|
31
|
|
|
unset($oSession); |
|
32
|
|
|
|
|
33
|
|
|
return $bIsConnected; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set an admin authentication. |
|
38
|
|
|
* |
|
39
|
|
|
* @param integer object $oAdminData User database object. |
|
40
|
|
|
* @param object \PH7\UserCoreModel $oAdminModel |
|
41
|
|
|
* @param object \PH7\Framework\Session\Session $oSession |
|
42
|
|
|
* @param object \PH7\Framework\Mvc\Model\Security $oSecurityModel |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setAuth($oAdminData, UserCoreModel $oAdminModel, Session $oSession, SecurityModel $oSecurityModel) |
|
46
|
|
|
{ |
|
47
|
|
|
// Remove the session if the admin is logged in as "user" or "affiliate". |
|
48
|
|
|
if (UserCore::auth() || AffiliateCore::auth()) |
|
49
|
|
|
$oSession->destroy(); |
|
50
|
|
|
|
|
51
|
|
|
// Regenerate the session ID to prevent session fixation attack |
|
52
|
|
|
$oSession->regenerateId(); |
|
53
|
|
|
|
|
54
|
|
|
$aSessionData = [ |
|
55
|
|
|
'admin_id' => $oAdminData->profileId, |
|
56
|
|
|
'admin_email' => $oAdminData->email, |
|
57
|
|
|
'admin_username' => $oAdminData->username, |
|
58
|
|
|
'admin_first_name' => $oAdminData->firstName, |
|
59
|
|
|
'admin_ip' => Ip::get(), |
|
60
|
|
|
'admin_http_user_agent' => (new Browser)->getUserAgent(), |
|
61
|
|
|
'admin_token' => Various::genRnd($oAdminData->email), |
|
62
|
|
|
]; |
|
63
|
|
|
$oSession->set($aSessionData); |
|
64
|
|
|
$oSecurityModel->addLoginLog($oAdminData->email, $oAdminData->username, '*****', 'Logged in!', 'Admins'); |
|
65
|
|
|
$oAdminModel->setLastActivity($oAdminData->profileId, 'Admins'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determines if the ID is from Root Admin (main admin). |
|
70
|
|
|
* |
|
71
|
|
|
* @param integer $iProfileId |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function isRootProfileId($iProfileId) |
|
75
|
|
|
{ |
|
76
|
|
|
return $iProfileId === static::ROOT_PROILE_ID; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|