root/trunk/wifidog-auth/wifidog/classes/Authenticator.php @ 733

Revision 733, 6.9 KB (checked in by aprilp, 8 years ago)

isset, and issues preventing users from logging out.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3/********************************************************************\
4 * This program is free software; you can redistribute it and/or    *
5 * modify it under the terms of the GNU General Public License as   *
6 * published by the Free Software Foundation; either version 2 of   *
7 * the License, or (at your option) any later version.              *
8 *                                                                  *
9 * This program is distributed in the hope that it will be useful,  *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
12 * GNU General Public License for more details.                     *
13 *                                                                  *
14 * You should have received a copy of the GNU General Public License*
15 * along with this program; if not, contact:                        *
16 *                                                                  *
17 * Free Software Foundation           Voice:  +1-617-542-5942       *
18 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
19 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
20 *                                                                  *
21 \********************************************************************/
22/**@file Authenticator.php
23 * @author Copyright (C) 2005 Benoit Grégoire <bock@step.polymtl.ca>,
24 * Technologies Coeus inc.
25 */
26
27/** Abstract class to represent an authentication source */
28abstract class Authenticator
29{
30        private $mAccountOrigin;
31
32        function __construct($account_orgin)
33        {
34                $this->mAccountOrigin = $account_orgin;
35        }
36
37        public function getAccountOrigin()
38        {
39                return $this->mAccountOrigin;
40        }
41
42        /** Attempts to login a user against the authentication source.  If successfull, returns a User object */
43        function login()
44        {
45        }
46
47        /** Logs out the user
48         * $conn_id:  The connection id for the connection to work on.  Optionnal.
49         *  If  it is not present, the behaviour depends if the network supports
50         * multiple logins.  If it does not, all connections associated with the
51         * current user will be destroyed.  If it does, only the connections
52         * tied to the current node will be destroyed */
53        function logout($conn_id = null)
54        {
55                global $db;
56                $conn_id = $db->escapeString($conn_id);
57                if (!empty ($conn_id))
58                {
59                        $db->ExecSqlUniqueRes("SELECT NOW(), *, CASE WHEN ((NOW() - reg_date) > networks.validation_grace_time) THEN true ELSE false END AS validation_grace_time_expired FROM connections JOIN users ON (users.user_id=connections.user_id) JOIN networks ON (users.account_origin = networks.network_id) WHERE connections.conn_id='$conn_id'", $info, false);
60
61                        $user = User :: getObject($info['user_id']);
62                        $network = $user->getNetwork();
63                        $splash_user_id = $network->getSplashOnlyUser()->getId();
64                        $this->acctStop($conn_id);
65                }
66                else
67                {
68                        $user = User :: getCurrentUser();
69                        $network = $user->getNetwork();
70                        $splash_user_id = $network->getSplashOnlyUser()->getId();
71                        if ($splash_user_id != $user->getId() && $node = Node :: getCurrentNode())
72                        {
73                                //Try to destroy all connections tied to the current node
74                                $sql = "SELECT conn_id FROM connections WHERE user_id = '{$user->getId()}' AND node_id='{$node->getId()}' AND token_status='".TOKEN_INUSE."';\n";
75                        $conn_rows = null;
76                                $db->ExecSql($sql, $conn_rows, false);
77                        if($conn_rows)
78                        {
79                                foreach ($conn_rows as $conn_row)
80                                {
81                                        $this->acctStop($conn_row['conn_id']);
82                                }
83                        }
84                        }
85                }
86
87                if ($splash_user_id != $user->getId() && $network->getMultipleLoginAllowed() == false)
88                {
89                        /* The user isn't the splash_only user and the network config does not allow multiple logins. 
90                         * Logging in with a new token implies that all other active tokens should expire */
91                        $sql = "SELECT conn_id FROM connections WHERE user_id = '{$user->getId()}' AND token_status='".TOKEN_INUSE."';\n";
92                        $conn_rows = null;
93                        $db->ExecSql($sql, $conn_rows, false);
94                        if($conn_rows)
95                        {
96                                foreach ($conn_rows as $conn_row)
97                        {
98                                $this->acctStop($conn_row['conn_id']);
99                        }
100                        }
101                }
102                global $session;
103                $session->destroy();
104
105        }
106
107        /** Start accounting traffic for the user
108         * $conn_id:  The connection id for the connection to work on */
109        function acctStart($conn_id)
110        {
111                //$info['conn_id']
112                global $db;
113                $conn_id = $db->escapeString($conn_id);
114                $db->ExecSqlUniqueRes("SELECT NOW(), *, CASE WHEN ((NOW() - reg_date) > networks.validation_grace_time) THEN true ELSE false END AS validation_grace_time_expired FROM connections JOIN users ON (users.user_id=connections.user_id) JOIN networks ON (users.account_origin = networks.network_id) WHERE connections.conn_id='$conn_id'", $info, false);
115                $network = Network :: getObject($info['network_id']);
116                $splash_user_id = $network->getSplashOnlyUser()->getId();
117                $auth_response = $info['account_status'];
118                /* Login the user */
119                $mac = $db->EscapeString($_REQUEST['mac']);
120                $ip = $db->EscapeString($_REQUEST['ip']);
121                $sql = "UPDATE connections SET "."token_status='".TOKEN_INUSE."',"."user_mac='$mac',"."user_ip='$ip',"."last_updated=NOW()"."WHERE conn_id='{$conn_id}';\n";
122                $db->ExecSqlUpdate($sql, false);
123                if ($splash_user_id != $info['user_id'] && $network->getMultipleLoginAllowed() == false)
124                {
125                        /* The user isn't the splash_only user and the network config does not allow multiple logins. 
126                         * Logging in with a new token implies that all other active tokens should expire */
127                        $token = $db->EscapeString($_REQUEST['token']);
128                        $sql = "SELECT * FROM connections WHERE user_id = '{$info['user_id']}' AND token_status='".TOKEN_INUSE."' AND token!='$token';\n";
129                        $conn_rows = array ();
130                        $db->ExecSql($sql, $conn_rows, true);
131                        if (isset($conn_rows)) {
132                foreach ($conn_rows as $conn_row)
133                            {
134                                    $this->acctStop($conn_row['conn_id']);
135                            }
136            }
137                }
138
139                /* Delete all unused tokens for this user, so we don't fill the database with them */
140                $sql = "DELETE FROM connections "."WHERE token_status='".TOKEN_UNUSED."' AND user_id = '{$info['user_id']}';\n";
141                $db->ExecSqlUpdate($sql, false);
142        }
143
144        /** Update traffic counters
145         * $conn_id: The connection id for the connection to work on */
146        function acctUpdate($conn_id, $incoming, $outgoing)
147        {
148                // Write traffic counters to database
149                global $db;
150                $conn_id = $db->escapeString($conn_id);
151                $db->ExecSqlUpdate("UPDATE connections SET "."incoming='$incoming',"."outgoing='$outgoing',"."last_updated=NOW() "."WHERE conn_id='{$conn_id}'");
152        }
153
154        /** Final update and stop accounting
155         * $conn_id:  The connection id (the token id) for the connection to work on
156         * */
157        function acctStop($conn_id)
158        {
159                // Stop traffic counters update
160                global $db;
161                $conn_id = $db->escapeString($conn_id);
162                $db->ExecSqlUpdate("UPDATE connections SET "."timestamp_out=NOW(),"."token_status='".TOKEN_USED."' "."WHERE conn_id='{$conn_id}';\n", false);
163        }
164
165        /**
166         * Property method that tells if the class allows registration
167         */
168        function isRegistrationPermitted()
169        {
170                return false;
171        }
172
173} // End class
174?>
Note: See TracBrowser for help on using the browser.