]> git.mxchange.org Git - core.git/blob - framework/main/classes/auth/class_CookieAuth.php
0652bb173a19bcc896a2210f5f1baf9a6c3ba5d7
[core.git] / framework / main / classes / auth / class_CookieAuth.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Auth;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Response\Responseable;
10
11 /**
12  * A cookie-bases authorization class
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16 <<<<<<< HEAD:framework/main/classes/auth/class_CookieAuth.php
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 =======
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
20 >>>>>>> Some updates::inc/main/classes/auth/class_CookieAuth.php
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class CookieAuth extends BaseFrameworkSystem implements Authorizeable, Registerable {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this class by the given response instance
50          *
51          * @param       $responseInstance       An instance of a Responseable class
52          * @return      $loginInstance          An instance of this login class
53          */
54         public static final function createCookieAuth (Responseable $responseInstance) {
55                 // Get a new instance
56                 $loginInstance = new CookieAuth();
57
58                 // Return the prepared instance
59                 return $loginInstance;
60         }
61
62         /**
63          * "Setter" for username auth data
64          *
65          * @param       $userName       The username from request we shall set
66          * @return      void
67          */
68         public function setUserAuth ($userName) {
69                 FrameworkBootstrap::getResponseInstance()->addCookie('username', $userName);
70         }
71
72         /**
73          * "Setter" for password hash auth data
74          *
75          * @param       $passHash       The hashed password from request we shall set
76          * @return      void
77          */
78         public function setPasswordAuth ($passHash) {
79                 FrameworkBootstrap::getResponseInstance()->addCookie('u_hash', $passHash);
80         }
81
82         /**
83          * Getter for user auth cookie
84          *
85          * @return      $userName       Username to get from cookie
86          */
87         public function getUserAuth () {
88                 // Get the username from cookie
89                 $userName = FrameworkBootstrap::getRequestInstance()->readCookie('username');
90
91                 // Return the username
92                 return $userName;
93         }
94
95         /**
96          * Getter for password hash auth cookie
97          *
98          * @return      $passHash       Password hash to get from cookie
99          */
100         public function getPasswordAuth () {
101                 // Get the username from cookie
102                 $passHash = FrameworkBootstrap::getRequestInstance()->readCookie('u_hash');
103
104                 // Return the username
105                 return $passHash;
106         }
107
108         /**
109          * Destroy the authorization data
110          *
111          * @return      void
112          */
113         public function destroyAuthData () {
114                 // Expire both cookies
115                 FrameworkBootstrap::getResponseInstance()->expireCookie('username');
116                 FrameworkBootstrap::getResponseInstance()->expireCookie('u_hash');
117         }
118
119         /**
120          * Updates the authorization data and/or sets additional tracking data
121          *
122          * @return      void
123          */
124         public function updateAuthData () {
125                 FrameworkBootstrap::getResponseInstance()->refreshCookie('username');
126                 FrameworkBootstrap::getResponseInstance()->refreshCookie('u_hash');
127         }
128
129 }