Completed
Branch master (b43d3d)
by Pierre-Henry
35:32
created

AdminCore::isAdminIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
9
namespace PH7;
10
11
use PH7\Framework\Ip\Ip;
12
use PH7\Framework\Mvc\Model\Security as SecurityModel;
13
use PH7\Framework\Navigation\Browser;
14
use PH7\Framework\Session\Session;
15
use PH7\Framework\Util\Various;
16
use stdClass;
17
18
// Abstract Class
19
class AdminCore extends UserCore
20
{
21
    const ROOT_PROILE_ID = 1;
22
23
    /**
24
     * Admins'levels.
25
     *
26
     * @return bool
27
     */
28
    public static function auth()
29
    {
30
        $oSession = new Session;
31
        $bIsConnected = ((int)$oSession->exists('admin_id')) && $oSession->get('admin_ip') === Ip::get() && $oSession->get('admin_http_user_agent') === (new Browser)->getUserAgent();
32
        unset($oSession);
33
34
        return $bIsConnected;
35
    }
36
37
    /**
38
     * Determines if the ID is from Root Admin (main admin).
39
     *
40
     * @param  integer $iProfileId
41
     *
42
     * @return bool
43
     */
44
    public static function isRootProfileId($iProfileId)
45
    {
46
        return $iProfileId == static::ROOT_PROILE_ID;
47
    }
48
49
    /**
50
     * @param AdminCoreModel $oAdminModel
51
     *
52
     * @return bool TRUE if the IP is the one the site was installed, FALSE otherwise.
53
     */
54
    public static function isAdminIp(AdminCoreModel $oAdminModel)
55
    {
56
        return $oAdminModel->getRootIp() === Ip::get();
57
    }
58
59
    /**
60
     * Set an admin authentication.
61
     *
62
     * @param stdClass $oAdminData User database object.
63
     * @param UserCoreModel $oAdminModel
64
     * @param Session $oSession
65
     * @param SecurityModel $oSecurityModel
66
     *
67
     * @return void
68
     */
69
    public function setAuth(stdClass $oAdminData, UserCoreModel $oAdminModel, Session $oSession, SecurityModel $oSecurityModel)
70
    {
71
        // Remove the session if the admin is logged in as "user" or "affiliate".
72
        if (UserCore::auth() || AffiliateCore::auth()) {
73
            $oSession->destroy();
74
        }
75
76
        // Regenerate the session ID to prevent session fixation attack
77
        $oSession->regenerateId();
78
79
        $aSessionData = [
80
            'admin_id' => $oAdminData->profileId,
81
            'admin_email' => $oAdminData->email,
82
            'admin_username' => $oAdminData->username,
83
            'admin_first_name' => $oAdminData->firstName,
84
            'admin_ip' => Ip::get(),
85
            'admin_http_user_agent' => (new Browser)->getUserAgent(),
86
            'admin_token' => Various::genRnd($oAdminData->email),
87
        ];
88
        $oSession->set($aSessionData);
89
        $oSecurityModel->addLoginLog($oAdminData->email, $oAdminData->username, '*****', 'Logged in!', 'Admins');
90
        $oAdminModel->setLastActivity($oAdminData->profileId, 'Admins');
91
    }
92
}
93