summaryrefslogtreecommitdiffstats
path: root/lib/User.php
blob: 97b1e785bbd6afa2e354db74218f5155f0a59217 (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
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2014, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 | Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                     |
 +--------------------------------------------------------------------------+
*/

class User
{
    private $_authenticated = false;
    private $_groups        = false;
    private $auth;
    private $userid;
    private $username;
    private $password;
    private $domain;
    private $working_domain;


    public function authenticate($username, $password, $domain = null, &$attributes = null)
    {
        if (empty($domain)) {
            $this->auth = Auth::get_instance();
        }
        else {
            $this->auth = Auth::get_instance($domain);
        }

        $result = $this->auth->authenticate($username, $password, $domain, $attributes);

        if ($result) {
            $this->_authenticated = true;
            $this->username = $username;
            $this->password = $password;
            $this->userid   = $result;
            $this->domain   = empty($domain) ? $this->auth->domain : $domain;

            // set unique attribute value as userid
            if (strtolower($this->userid) != 'cn=directory manager') {
                $unique_attr = kolab_api_service::unique_attribute();

                if ($attributes && $attributes[$unique_attr]) {
                    $this->userid = $attributes[$unique_attr];
                }
                else if ($uid = $this->auth->get_entry_attribute($result, $unique_attr)) {
                    $this->userid = $uid;
                }
            }
        }

        return $result;
    }

    public function authenticated()
    {
        return $this->_authenticated;
    }

    public function get_username()
    {
        // Who's asking?
        return $this->username;
    }

    public function get_userid()
    {
        return $this->userid;
    }

    public function get_domain()
    {
        if ($this->working_domain) {
            return $this->working_domain;
        }
        else if ($this->domain) {
            return $this->domain;
        }
        else {
            throw new Exception("No domain selected to work on", 1024);
        }
    }

    public function groups()
    {
        if ($this->_groups || (is_array($this->_groups) && count($this->_groups) >= 1)) {
            return $this->_groups;
        }

        $this->_groups = array();
        $this->auth    = Auth::get_instance();

        $entry = $this->auth->user_find_by_attribute(array('mail' => $this->username));

        if ($entry) {
            foreach ($entry as $dn => $attributes) {
                if (array_key_exists('memberof', $attributes)) {
                    $this->_groups = (array)($attributes['memberof']);
                }
                else {
                    $this->_groups = $this->auth->find_user_groups($dn);
                }
            }
        }
        else {
            $this->_groups = array();
        }

        return $this->_groups;
    }

    public function reset_domain()
    {
        // Validate domain
        // Validate access to domain
        // Set $this->working_domain
        $this->working_domain = $this->domain;

        return true;
    }

    public function set_domain($domain)
    {
        // Validate domain
        // Validate access to domain
        // Set $this->working_domain
        $this->working_domain = $domain;

        return true;
    }
}