summaryrefslogtreecommitdiffstats
path: root/lib/User.php
blob: 2e0ea21c27391c5d915b7cf884ab6917191e375c (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
<?php
//     @require_once($_SERVER["DOCUMENT_ROOT"] . "../bin/lib/User/Type.php");
//     @require_once($_SERVER["DOCUMENT_ROOT"] . "../bin/lib/User/LDAP.php");
//     @require_once($_SERVER["DOCUMENT_ROOT"] . "../bin/lib/User/SQL.php");

require_once 'Auth.php';

class User
{
    private $_authenticated = FALSE;
    private $auth;

    private $username = NULL;
    private $password = NULL;

    private $_groups = FALSE;

    private $domain;
    private $working_domain;

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

    public function _get_information()
    {
        // Return an array of information about this user. For one, the auth method.
        $user['information'] = array(
            'email_address' => $this->_auth_method->_get_email_address(),
            'username' => $this->username,
            'password' => $this->password,
        );
    }

    public function authenticate($username, $password, $method = FALSE)
    {
        $this->auth = Auth::get_instance();

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

        if ($result) {
            $this->_authenticated = TRUE;
            $this->username = $username;
            $this->password = $password;
            $this->domain = $this->auth->domain;
//            $this->_groups = $this->groups();
        }

        return $this->_authenticated;
    }

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

    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()
    {
        //error_log("Called " . __FUNCTION__ . " on line " . __LINE__ . " of " . __FILE__);
        //debug_print_backtrace();

        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;
    }

}