summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/AuthUser.php
blob: a293af5ef415f68f1f9bfa57bb8b08c2ff261db9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

require_once 'Auth.php';
require_once 'SemanticScuttle/Service/User.php';

/**
 * SemanticScuttle extendet user management service utilizing
 * the PEAR Auth package to enable authentication against
 * different services, i.e. LDAP or other databases.
 *
 * Requires the Log packages for debugging purposes.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Service_AuthUser extends SemanticScuttle_Service_User
{
    /**
     * PEAR Auth instance
     *
     * @var Auth
     */
    protected $auth = null;

    /**
     * If we want to debug authentication process
     *
     * @var boolean
     */
    protected $authdebug = false;

    /**
    * Authentication type (i.e. LDAP)
    *
    * @var string
    *
    * @link http://pear.php.net/manual/en/package.authentication.auth.intro-storage.php
    */
    var $authtype = null;
    
    /**
    * Authentication options
    *
    * @var array
    *
    * @link http://pear.php.net/manual/en/package.authentication.auth.intro.php
    */
    var $authoptions = null;



    /**
     * Returns the single service instance
     *
     * @param sql_db $db Database object
     *
     * @return SemanticScuttle_Service_AuthUser
     */
    public static function getInstance($db)
    {
        static $instance;
        if (!isset($instance)) {
            $instance = new self($db);
        }
        return $instance;
    }



    /**
     * Create new instance
     *
     * @var sql_db $db Database object
     */
    protected function __construct($db)
    {
        parent::__construct($db);

        $this->authtype    = $GLOBALS['authType'];
        $this->authoptions = $GLOBALS['authOptions'];
        $this->authdebug   = $GLOBALS['authDebug'];

        //FIXME: throw error when no authtype set?
        if (!$this->authtype) {
            return;
        }
        require_once 'Auth.php';
        $this->auth = new Auth($this->authtype, $this->authoptions);
        //FIXME: check if it worked (i.e. db connection)
        if ($this->authdebug) {
            require_once 'Log.php';
            $this->auth->logger = Log::singleton(
                'display', '', '', array(), PEAR_LOG_DEBUG
            );
            $this->auth->enableLogging = true;
        }
        $this->auth->setShowLogin(false);
    }



    /**
     * Return current user id based on session or cookie
     *
     * @return mixed Integer user id or boolean false when user
     *               could not be found or is not logged on.
     */
    public function getCurrentUserId()
    {
        if (!$this->auth) {
            return parent::getCurrentUserId();
        }

        //FIXME: caching?
        $name = $this->auth->getUsername();
        if (!$name) {
            return false;
        }
        return $this->getIdFromUser($name);
    }



    /**
     * Try to authenticate and login a user with
     * username and password.
     *
     * @param string  $username Name of user
     * @param string  $password Password
     * @param boolean $remember If a long-time cookie shall be set
     *
     * @return boolean True if the user could be authenticated,
     *                 false if not.
     */
    public function login($username, $password, $remember = false)
    {
        if (!$this->auth) {
            return parent::login($username, $password, $remember);
        }

        $ok = $this->loginAuth($username, $password);
        if (!$ok) {
            return false;
        }

        //utilize real login method to get longtime cookie support etc.
        return parent::login($username, $password, $remember);
    }


    /**
    * Uses PEAR's Auth class to authenticate the user against a container.
    * This allows us to use LDAP, a different database or some other
    * external system.
    *
    * @param string $username Username to check
    * @param string $password Password to check
    *
    * @return boolean If the user has been authenticated or not
    */
    public function loginAuth($username, $password)
    {
        $this->auth->post = array(
            'username' => $username,
            'password' => $password,
        );
        $this->auth->start();

        if (!$this->auth->checkAuth()) {
            return false;
        }

        //put user in database
        if (!$this->getUserByUsername($username)) {
            $this->addUser(
                $username, $password,
                $username . $GLOBALS['authEmailSuffix']
            );
        }
        //FIXME: what if the user changed his password?
        //FIXME: what if the user does not need an email domain?

        return true;
     }




    /**
     * Logs the current user out of the system.
     *
     * @return void
     */
    public function logout()
    {
        parent::logout();

        if ($this->auth) {
            $this->auth->logout();
            $this->auth = null;
        }
    }

}
?>